• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.settings;
18 
19 import android.bluetooth.BluetoothAdapter;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.ConnectivityManager;
23 import android.nfc.NfcAdapter;
24 import android.os.Bundle;
25 import android.os.ServiceManager;
26 import android.os.SystemProperties;
27 import android.preference.CheckBoxPreference;
28 import android.preference.Preference;
29 import android.preference.PreferenceActivity;
30 import android.preference.PreferenceScreen;
31 import android.provider.Settings;
32 import com.android.internal.telephony.TelephonyIntents;
33 import com.android.internal.telephony.TelephonyProperties;
34 import com.android.settings.bluetooth.BluetoothEnabler;
35 import com.android.settings.wifi.WifiEnabler;
36 import com.android.settings.nfc.NfcEnabler;
37 
38 public class WirelessSettings extends PreferenceActivity {
39 
40     private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
41     private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
42     private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
43     private static final String KEY_TOGGLE_NFC = "toggle_nfc";
44     private static final String KEY_WIFI_SETTINGS = "wifi_settings";
45     private static final String KEY_WIMAX_SETTINGS = "wimax_settings";
46     private static final String KEY_BT_SETTINGS = "bt_settings";
47     private static final String KEY_VPN_SETTINGS = "vpn_settings";
48     private static final String KEY_TETHER_SETTINGS = "tether_settings";
49 
50     public static final String EXIT_ECM_RESULT = "exit_ecm_result";
51     public static final int REQUEST_CODE_EXIT_ECM = 1;
52 
53     private AirplaneModeEnabler mAirplaneModeEnabler;
54     private CheckBoxPreference mAirplaneModePreference;
55     private WifiEnabler mWifiEnabler;
56     private NfcEnabler mNfcEnabler;
57     private BluetoothEnabler mBtEnabler;
58 
59     /**
60      * Invoked on each preference click in this hierarchy, overrides
61      * PreferenceActivity's implementation.  Used to make sure we track the
62      * preference click events.
63      */
64     @Override
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)65     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
66         if (preference == mAirplaneModePreference && Boolean.parseBoolean(
67                 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
68             // In ECM mode launch ECM app dialog
69             startActivityForResult(
70                 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
71                 REQUEST_CODE_EXIT_ECM);
72             return true;
73         }
74         // Let the intents be launched by the Preference manager
75         return false;
76     }
77 
isRadioAllowed(Context context, String type)78     public static boolean isRadioAllowed(Context context, String type) {
79         if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
80             return true;
81         }
82         // Here we use the same logic in onCreate().
83         String toggleable = Settings.System.getString(context.getContentResolver(),
84                 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
85         return toggleable != null && toggleable.contains(type);
86     }
87 
88     @Override
onCreate(Bundle savedInstanceState)89     protected void onCreate(Bundle savedInstanceState) {
90         super.onCreate(savedInstanceState);
91 
92         addPreferencesFromResource(R.xml.wireless_settings);
93 
94         CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
95         CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI);
96         CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
97         CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
98 
99         mAirplaneModeEnabler = new AirplaneModeEnabler(this, airplane);
100         mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
101         mWifiEnabler = new WifiEnabler(this, wifi);
102         mBtEnabler = new BluetoothEnabler(this, bt);
103         mNfcEnabler = new NfcEnabler(this, nfc);
104 
105         String toggleable = Settings.System.getString(getContentResolver(),
106                 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
107 
108         //enable/disable wimax depending on the value in config.xml
109         boolean isWimaxEnabled = this.getResources().getBoolean(
110                 com.android.internal.R.bool.config_wimaxEnabled);
111         if (!isWimaxEnabled) {
112             PreferenceScreen root = getPreferenceScreen();
113             Preference ps = (Preference) findPreference(KEY_WIMAX_SETTINGS);
114             if (ps != null)
115                 root.removePreference(ps);
116         } else {
117             if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIMAX )
118                     && isWimaxEnabled) {
119                 Preference ps = (Preference) findPreference(KEY_WIMAX_SETTINGS);
120                 ps.setDependency(KEY_TOGGLE_AIRPLANE);
121             }
122         }
123 
124         // Manually set dependencies for Wifi when not toggleable.
125         if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
126             wifi.setDependency(KEY_TOGGLE_AIRPLANE);
127             findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
128             findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
129         }
130 
131         // Manually set dependencies for Bluetooth when not toggleable.
132         if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
133             bt.setDependency(KEY_TOGGLE_AIRPLANE);
134             findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
135         }
136 
137         // Remove Bluetooth Settings if Bluetooth service is not available.
138         if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
139             getPreferenceScreen().removePreference(bt);
140         }
141 
142         // Remove NFC if its not available
143         if (NfcAdapter.getDefaultAdapter(this) == null) {
144             getPreferenceScreen().removePreference(nfc);
145         }
146 
147         // Disable Tethering if it's not allowed
148         ConnectivityManager cm =
149                 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
150         if (!cm.isTetheringSupported()) {
151             getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
152         } else {
153             String[] usbRegexs = cm.getTetherableUsbRegexs();
154             String[] wifiRegexs = cm.getTetherableWifiRegexs();
155             Preference p = findPreference(KEY_TETHER_SETTINGS);
156             if (wifiRegexs.length == 0) {
157                 p.setTitle(R.string.tether_settings_title_usb);
158                 p.setSummary(R.string.tether_settings_summary_usb);
159             } else {
160                 if (usbRegexs.length == 0) {
161                     p.setTitle(R.string.tether_settings_title_wifi);
162                     p.setSummary(R.string.tether_settings_summary_wifi);
163                 } else {
164                     p.setTitle(R.string.tether_settings_title_both);
165                     p.setSummary(R.string.tether_settings_summary_both);
166                 }
167             }
168         }
169     }
170 
171     @Override
onResume()172     protected void onResume() {
173         super.onResume();
174 
175         mAirplaneModeEnabler.resume();
176         mWifiEnabler.resume();
177         mBtEnabler.resume();
178         mNfcEnabler.resume();
179     }
180 
181     @Override
onPause()182     protected void onPause() {
183         super.onPause();
184 
185         mAirplaneModeEnabler.pause();
186         mWifiEnabler.pause();
187         mBtEnabler.pause();
188         mNfcEnabler.pause();
189     }
190 
191     @Override
onActivityResult(int requestCode, int resultCode, Intent data)192     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
193         if (requestCode == REQUEST_CODE_EXIT_ECM) {
194             Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
195             // Set Airplane mode based on the return value and checkbox state
196             mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
197                     mAirplaneModePreference.isChecked());
198         }
199     }
200 }
201