blob: 5322555d62a256b05dbddf4a220fee66c59a9057 [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";
Nick Pellyad50ba02010-09-22 10:55:13 -070051
Chouting Zhang71cc49e2009-08-28 14:36:35 -050052 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
53 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080054
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080055 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050056 private CheckBoxPreference mAirplaneModePreference;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080057 private WifiEnabler mWifiEnabler;
Nick Pellyad50ba02010-09-22 10:55:13 -070058 private NfcEnabler mNfcEnabler;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080059 private BluetoothEnabler mBtEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050060
61 /**
62 * Invoked on each preference click in this hierarchy, overrides
63 * PreferenceActivity's implementation. Used to make sure we track the
64 * preference click events.
65 */
66 @Override
67 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080068 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
69 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050070 // In ECM mode launch ECM app dialog
71 startActivityForResult(
72 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
73 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050074 return true;
75 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080076 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070077 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050078 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080079
80 public static boolean isRadioAllowed(Context context, String type) {
81 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
82 return true;
83 }
84 // Here we use the same logic in onCreate().
85 String toggleable = Settings.System.getString(context.getContentResolver(),
86 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
87 return toggleable != null && toggleable.contains(type);
88 }
89
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070091 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080092 super.onCreate(savedInstanceState);
93
94 addPreferencesFromResource(R.xml.wireless_settings);
95
Amith Yamasanid7993472010-08-18 13:59:28 -070096 final Activity activity = getActivity();
Chia-chi Yeh4e142112009-12-25 14:48:46 +080097 CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
98 CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI);
99 CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
Nick Pellyad50ba02010-09-22 10:55:13 -0700100 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800101
Amith Yamasanid7993472010-08-18 13:59:28 -0700102 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, airplane);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500103 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Amith Yamasanid7993472010-08-18 13:59:28 -0700104 mWifiEnabler = new WifiEnabler(activity, wifi);
105 mBtEnabler = new BluetoothEnabler(activity, bt);
Nick Pellyd83aaf22010-09-29 12:10:35 -0700106 mNfcEnabler = new NfcEnabler(activity, nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800107
Amith Yamasanid7993472010-08-18 13:59:28 -0700108 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800109 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
110
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800111 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800112 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
113 wifi.setDependency(KEY_TOGGLE_AIRPLANE);
114 findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
115 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
116 }
117
118 // Manually set dependencies for Bluetooth when not toggleable.
119 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
120 bt.setDependency(KEY_TOGGLE_AIRPLANE);
121 findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
122 }
123
Nick Pellyad50ba02010-09-22 10:55:13 -0700124 // Remove Bluetooth Settings if Bluetooth service is not available.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800125 if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700126 getPreferenceScreen().removePreference(bt);
127 }
128
129 // Remove NFC if its not available
Nick Pelly267419f2010-12-09 19:39:58 -0800130 if (NfcAdapter.getDefaultAdapter(this) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700131 getPreferenceScreen().removePreference(nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800132 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800133
Oscar Montemayor05411892010-08-03 16:56:09 -0700134 // Enable Proxy selector settings if allowed.
135 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
Amith Yamasanid7993472010-08-18 13:59:28 -0700136 DevicePolicyManager mDPM = (DevicePolicyManager)
137 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
Oscar Montemayor05411892010-08-03 16:56:09 -0700138 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
139
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800140 // Disable Tethering if it's not allowed
141 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700142 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800143 if (!cm.isTetheringSupported()) {
144 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700145 } else {
146 String[] usbRegexs = cm.getTetherableUsbRegexs();
147 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700148 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
149
150 boolean usbAvailable = usbRegexs.length != 0;
151 boolean wifiAvailable = wifiRegexs.length != 0;
152 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
153
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700154 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700155 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
156 p.setTitle(R.string.tether_settings_title_all);
157 p.setSummary(R.string.tether_settings_summary_all);
158 } else if (wifiAvailable && usbAvailable) {
159 p.setTitle(R.string.tether_settings_title_all);
160 p.setSummary(R.string.tether_settings_summary_usb_wifi);
161 } else if (wifiAvailable && bluetoothAvailable) {
162 p.setTitle(R.string.tether_settings_title_all);
163 p.setSummary(R.string.tether_settings_summary_wifi_bluetooth);
164 } else if (wifiAvailable) {
165 p.setTitle(R.string.tether_settings_title_wifi);
166 p.setSummary(R.string.tether_settings_summary_wifi);
167 } else if (usbAvailable && bluetoothAvailable) {
168 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
169 p.setSummary(R.string.tether_settings_summary_usb_bluetooth);
170 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700171 p.setTitle(R.string.tether_settings_title_usb);
172 p.setSummary(R.string.tether_settings_summary_usb);
173 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700174 p.setTitle(R.string.tether_settings_title_bluetooth);
175 p.setSummary(R.string.tether_settings_summary_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700176 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800177 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800178 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800179
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800180 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700181 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800182 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700183
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800184 mAirplaneModeEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800185 mWifiEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800186 mBtEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700187 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800188 }
Danica Chang32711b62010-08-10 18:41:29 -0700189
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800190 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700191 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800192 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700193
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800194 mAirplaneModeEnabler.pause();
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800195 mWifiEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800196 mBtEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700197 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800198 }
Danica Chang32711b62010-08-10 18:41:29 -0700199
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500200 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700201 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800202 if (requestCode == REQUEST_CODE_EXIT_ECM) {
203 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500204 // Set Airplane mode based on the return value and checkbox state
205 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
206 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500207 }
208 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800209}