blob: 9567c0135fd6090edb20c1601ce7dc3687a92bb8 [file] [log] [blame]
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08001/*
Chia-chi Yeh4e142112009-12-25 14:48:46 +08002 * Copyright (C) 2009 The Android Open Source Project
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -08003 *
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
17package com.android.settings;
18
Chouting Zhang71cc49e2009-08-28 14:36:35 -050019import com.android.internal.telephony.TelephonyIntents;
20import com.android.internal.telephony.TelephonyProperties;
Michael Chan0cb37432009-10-29 14:42:06 -070021import com.android.settings.bluetooth.BluetoothEnabler;
22import com.android.settings.wifi.WifiEnabler;
Nick Pellyd83aaf22010-09-29 12:10:35 -070023import com.android.settings.nfc.NfcEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050024
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.app.Activity;
26import android.app.admin.DevicePolicyManager;
27import android.bluetooth.BluetoothAdapter;
28import android.content.Context;
29import android.content.Intent;
30import android.net.ConnectivityManager;
Nick Pellya57eace2010-10-15 01:19:43 -070031import android.nfc.NfcAdapter;
Amith Yamasanid7993472010-08-18 13:59:28 -070032import android.os.Bundle;
33import android.os.ServiceManager;
34import android.os.SystemProperties;
35import android.preference.CheckBoxPreference;
36import android.preference.Preference;
37import android.preference.PreferenceScreen;
38import android.provider.Settings;
39
40public class WirelessSettings extends SettingsPreferenceFragment {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080041
42 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
43 private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
44 private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
Nick Pellyad50ba02010-09-22 10:55:13 -070045 private static final String KEY_TOGGLE_NFC = "toggle_nfc";
Mike Lockwood83bcc982009-07-29 23:25:10 -070046 private static final String KEY_WIFI_SETTINGS = "wifi_settings";
Michael Chan0cb37432009-10-29 14:42:06 -070047 private static final String KEY_BT_SETTINGS = "bt_settings";
Mike Lockwood83bcc982009-07-29 23:25:10 -070048 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080049 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Oscar Montemayor05411892010-08-03 16:56:09 -070050 private static final String KEY_PROXY_SETTINGS = "proxy_settings";
Amith Yamasani0f85c482011-02-23 17:19:11 -080051 private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings";
Nick Pellyad50ba02010-09-22 10:55:13 -070052
Chouting Zhang71cc49e2009-08-28 14:36:35 -050053 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
54 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080055
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080056 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050057 private CheckBoxPreference mAirplaneModePreference;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080058 private WifiEnabler mWifiEnabler;
Nick Pellyad50ba02010-09-22 10:55:13 -070059 private NfcEnabler mNfcEnabler;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080060 private BluetoothEnabler mBtEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050061
62 /**
63 * Invoked on each preference click in this hierarchy, overrides
64 * PreferenceActivity's implementation. Used to make sure we track the
65 * preference click events.
66 */
67 @Override
68 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080069 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
70 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050071 // In ECM mode launch ECM app dialog
72 startActivityForResult(
73 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
74 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050075 return true;
76 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080077 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070078 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050079 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080080
81 public static boolean isRadioAllowed(Context context, String type) {
82 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
83 return true;
84 }
85 // Here we use the same logic in onCreate().
86 String toggleable = Settings.System.getString(context.getContentResolver(),
87 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
88 return toggleable != null && toggleable.contains(type);
89 }
90
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070092 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080093 super.onCreate(savedInstanceState);
94
95 addPreferencesFromResource(R.xml.wireless_settings);
96
Amith Yamasanid7993472010-08-18 13:59:28 -070097 final Activity activity = getActivity();
Chia-chi Yeh4e142112009-12-25 14:48:46 +080098 CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
99 CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI);
100 CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
Nick Pellyad50ba02010-09-22 10:55:13 -0700101 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800102
Amith Yamasanid7993472010-08-18 13:59:28 -0700103 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, airplane);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500104 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Amith Yamasanid7993472010-08-18 13:59:28 -0700105 mWifiEnabler = new WifiEnabler(activity, wifi);
106 mBtEnabler = new BluetoothEnabler(activity, bt);
Nick Pellyd83aaf22010-09-29 12:10:35 -0700107 mNfcEnabler = new NfcEnabler(activity, nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800108
Amith Yamasanid7993472010-08-18 13:59:28 -0700109 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800110 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
111
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800112 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800113 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
114 wifi.setDependency(KEY_TOGGLE_AIRPLANE);
115 findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
116 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
117 }
118
119 // Manually set dependencies for Bluetooth when not toggleable.
120 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
121 bt.setDependency(KEY_TOGGLE_AIRPLANE);
122 findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
123 }
124
Nick Pellyad50ba02010-09-22 10:55:13 -0700125 // Remove Bluetooth Settings if Bluetooth service is not available.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800126 if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700127 getPreferenceScreen().removePreference(bt);
128 }
129
130 // Remove NFC if its not available
Nick Pellyb8be6da2010-12-13 13:44:10 -0800131 if (NfcAdapter.getDefaultAdapter(activity) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700132 getPreferenceScreen().removePreference(nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800133 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800134
Amith Yamasani0f85c482011-02-23 17:19:11 -0800135 // Remove Mobile Network Settings if it's a wifi-only device.
136 if (Utils.isWifiOnly()) {
137 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
138 }
139
Oscar Montemayor05411892010-08-03 16:56:09 -0700140 // Enable Proxy selector settings if allowed.
141 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
Amith Yamasanid7993472010-08-18 13:59:28 -0700142 DevicePolicyManager mDPM = (DevicePolicyManager)
143 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
Robert Greenwalt6f3a98b2010-12-28 16:11:12 -0800144 // proxy UI disabled until we have better app support
145 getPreferenceScreen().removePreference(mGlobalProxy);
Oscar Montemayor05411892010-08-03 16:56:09 -0700146 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
147
Amith Yamasani0f85c482011-02-23 17:19:11 -0800148 // Disable Tethering if it's not allowed or if it's a wifi-only device
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800149 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700150 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Amith Yamasani0f85c482011-02-23 17:19:11 -0800151 if (!cm.isTetheringSupported() || Utils.isWifiOnly()) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800152 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700153 } else {
154 String[] usbRegexs = cm.getTetherableUsbRegexs();
155 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700156 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
157
158 boolean usbAvailable = usbRegexs.length != 0;
159 boolean wifiAvailable = wifiRegexs.length != 0;
160 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
161
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700162 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700163 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
164 p.setTitle(R.string.tether_settings_title_all);
165 p.setSummary(R.string.tether_settings_summary_all);
166 } else if (wifiAvailable && usbAvailable) {
167 p.setTitle(R.string.tether_settings_title_all);
168 p.setSummary(R.string.tether_settings_summary_usb_wifi);
169 } else if (wifiAvailable && bluetoothAvailable) {
170 p.setTitle(R.string.tether_settings_title_all);
171 p.setSummary(R.string.tether_settings_summary_wifi_bluetooth);
172 } else if (wifiAvailable) {
173 p.setTitle(R.string.tether_settings_title_wifi);
174 p.setSummary(R.string.tether_settings_summary_wifi);
175 } else if (usbAvailable && bluetoothAvailable) {
176 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
177 p.setSummary(R.string.tether_settings_summary_usb_bluetooth);
178 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700179 p.setTitle(R.string.tether_settings_title_usb);
180 p.setSummary(R.string.tether_settings_summary_usb);
181 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700182 p.setTitle(R.string.tether_settings_title_bluetooth);
183 p.setSummary(R.string.tether_settings_summary_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700184 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800185 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800186 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800187
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800188 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700189 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800190 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700191
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800192 mAirplaneModeEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800193 mWifiEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800194 mBtEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700195 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800196 }
Danica Chang32711b62010-08-10 18:41:29 -0700197
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800198 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700199 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800200 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700201
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800202 mAirplaneModeEnabler.pause();
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800203 mWifiEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800204 mBtEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700205 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800206 }
Danica Chang32711b62010-08-10 18:41:29 -0700207
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500208 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700209 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800210 if (requestCode == REQUEST_CODE_EXIT_ECM) {
211 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500212 // Set Airplane mode based on the return value and checkbox state
213 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
214 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500215 }
216 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800217}