blob: bdf8ce8333bd3c0e554c56e93a08f2c976083b94 [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 Pellyb8be6da2010-12-13 13:44:10 -0800130 if (NfcAdapter.getDefaultAdapter(activity) == 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);
Robert Greenwalt6f3a98b2010-12-28 16:11:12 -0800138 // proxy UI disabled until we have better app support
139 getPreferenceScreen().removePreference(mGlobalProxy);
Oscar Montemayor05411892010-08-03 16:56:09 -0700140 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
141
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800142 // Disable Tethering if it's not allowed
143 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700144 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800145 if (!cm.isTetheringSupported()) {
146 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700147 } else {
148 String[] usbRegexs = cm.getTetherableUsbRegexs();
149 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700150 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
151
152 boolean usbAvailable = usbRegexs.length != 0;
153 boolean wifiAvailable = wifiRegexs.length != 0;
154 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
155
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700156 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700157 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
158 p.setTitle(R.string.tether_settings_title_all);
159 p.setSummary(R.string.tether_settings_summary_all);
160 } else if (wifiAvailable && usbAvailable) {
161 p.setTitle(R.string.tether_settings_title_all);
162 p.setSummary(R.string.tether_settings_summary_usb_wifi);
163 } else if (wifiAvailable && bluetoothAvailable) {
164 p.setTitle(R.string.tether_settings_title_all);
165 p.setSummary(R.string.tether_settings_summary_wifi_bluetooth);
166 } else if (wifiAvailable) {
167 p.setTitle(R.string.tether_settings_title_wifi);
168 p.setSummary(R.string.tether_settings_summary_wifi);
169 } else if (usbAvailable && bluetoothAvailable) {
170 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
171 p.setSummary(R.string.tether_settings_summary_usb_bluetooth);
172 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700173 p.setTitle(R.string.tether_settings_title_usb);
174 p.setSummary(R.string.tether_settings_summary_usb);
175 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700176 p.setTitle(R.string.tether_settings_title_bluetooth);
177 p.setSummary(R.string.tether_settings_summary_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700178 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800179 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800180 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800181
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800182 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700183 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800184 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700185
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800186 mAirplaneModeEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800187 mWifiEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800188 mBtEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700189 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800190 }
Danica Chang32711b62010-08-10 18:41:29 -0700191
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800192 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700193 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800194 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700195
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800196 mAirplaneModeEnabler.pause();
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800197 mWifiEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800198 mBtEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700199 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800200 }
Danica Chang32711b62010-08-10 18:41:29 -0700201
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500202 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700203 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800204 if (requestCode == REQUEST_CODE_EXIT_ECM) {
205 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500206 // Set Airplane mode based on the return value and checkbox state
207 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
208 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500209 }
210 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800211}