blob: 22218402ed98db44446203d46765721116bc9e68 [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;
31import android.os.Bundle;
32import android.os.ServiceManager;
33import android.os.SystemProperties;
34import android.preference.CheckBoxPreference;
35import android.preference.Preference;
36import android.preference.PreferenceScreen;
37import android.provider.Settings;
38
39public class WirelessSettings extends SettingsPreferenceFragment {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040
41 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
42 private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
43 private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
Nick Pellyad50ba02010-09-22 10:55:13 -070044 private static final String KEY_TOGGLE_NFC = "toggle_nfc";
Mike Lockwood83bcc982009-07-29 23:25:10 -070045 private static final String KEY_WIFI_SETTINGS = "wifi_settings";
Michael Chan0cb37432009-10-29 14:42:06 -070046 private static final String KEY_BT_SETTINGS = "bt_settings";
Mike Lockwood83bcc982009-07-29 23:25:10 -070047 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080048 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Oscar Montemayor05411892010-08-03 16:56:09 -070049 private static final String KEY_PROXY_SETTINGS = "proxy_settings";
Nick Pellyad50ba02010-09-22 10:55:13 -070050
Chouting Zhang71cc49e2009-08-28 14:36:35 -050051 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
52 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080053
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080054 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050055 private CheckBoxPreference mAirplaneModePreference;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080056 private WifiEnabler mWifiEnabler;
Nick Pellyad50ba02010-09-22 10:55:13 -070057 private NfcEnabler mNfcEnabler;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080058 private BluetoothEnabler mBtEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050059
60 /**
61 * Invoked on each preference click in this hierarchy, overrides
62 * PreferenceActivity's implementation. Used to make sure we track the
63 * preference click events.
64 */
65 @Override
66 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080067 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
68 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050069 // In ECM mode launch ECM app dialog
70 startActivityForResult(
71 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
72 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050073 return true;
74 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080075 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070076 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050077 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080078
79 public static boolean isRadioAllowed(Context context, String type) {
80 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
81 return true;
82 }
83 // Here we use the same logic in onCreate().
84 String toggleable = Settings.System.getString(context.getContentResolver(),
85 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
86 return toggleable != null && toggleable.contains(type);
87 }
88
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070090 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080091 super.onCreate(savedInstanceState);
92
93 addPreferencesFromResource(R.xml.wireless_settings);
94
Amith Yamasanid7993472010-08-18 13:59:28 -070095 final Activity activity = getActivity();
Chia-chi Yeh4e142112009-12-25 14:48:46 +080096 CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
97 CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI);
98 CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
Nick Pellyad50ba02010-09-22 10:55:13 -070099 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800100
Amith Yamasanid7993472010-08-18 13:59:28 -0700101 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, airplane);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500102 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Amith Yamasanid7993472010-08-18 13:59:28 -0700103 mWifiEnabler = new WifiEnabler(activity, wifi);
104 mBtEnabler = new BluetoothEnabler(activity, bt);
Nick Pellyd83aaf22010-09-29 12:10:35 -0700105 mNfcEnabler = new NfcEnabler(activity, nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800106
Amith Yamasanid7993472010-08-18 13:59:28 -0700107 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800108 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
109
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800110 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800111 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
112 wifi.setDependency(KEY_TOGGLE_AIRPLANE);
113 findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
114 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
115 }
116
117 // Manually set dependencies for Bluetooth when not toggleable.
118 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
119 bt.setDependency(KEY_TOGGLE_AIRPLANE);
120 findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
121 }
122
Nick Pellyad50ba02010-09-22 10:55:13 -0700123 // Remove Bluetooth Settings if Bluetooth service is not available.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800124 if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700125 getPreferenceScreen().removePreference(bt);
126 }
127
128 // Remove NFC if its not available
129 if (ServiceManager.getService(Context.NFC_SERVICE) == null) {
130 getPreferenceScreen().removePreference(nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800131 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800132
Oscar Montemayor05411892010-08-03 16:56:09 -0700133 // Enable Proxy selector settings if allowed.
134 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
Amith Yamasanid7993472010-08-18 13:59:28 -0700135 DevicePolicyManager mDPM = (DevicePolicyManager)
136 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
Oscar Montemayor05411892010-08-03 16:56:09 -0700137 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
138
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800139 // Disable Tethering if it's not allowed
140 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700141 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800142 if (!cm.isTetheringSupported()) {
143 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700144 } else {
145 String[] usbRegexs = cm.getTetherableUsbRegexs();
146 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700147 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
148
149 boolean usbAvailable = usbRegexs.length != 0;
150 boolean wifiAvailable = wifiRegexs.length != 0;
151 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
152
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700153 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700154 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
155 p.setTitle(R.string.tether_settings_title_all);
156 p.setSummary(R.string.tether_settings_summary_all);
157 } else if (wifiAvailable && usbAvailable) {
158 p.setTitle(R.string.tether_settings_title_all);
159 p.setSummary(R.string.tether_settings_summary_usb_wifi);
160 } else if (wifiAvailable && bluetoothAvailable) {
161 p.setTitle(R.string.tether_settings_title_all);
162 p.setSummary(R.string.tether_settings_summary_wifi_bluetooth);
163 } else if (wifiAvailable) {
164 p.setTitle(R.string.tether_settings_title_wifi);
165 p.setSummary(R.string.tether_settings_summary_wifi);
166 } else if (usbAvailable && bluetoothAvailable) {
167 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
168 p.setSummary(R.string.tether_settings_summary_usb_bluetooth);
169 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700170 p.setTitle(R.string.tether_settings_title_usb);
171 p.setSummary(R.string.tether_settings_summary_usb);
172 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700173 p.setTitle(R.string.tether_settings_title_bluetooth);
174 p.setSummary(R.string.tether_settings_summary_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700175 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800176 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800177 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800178
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800179 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700180 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800181 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700182
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800183 mAirplaneModeEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800184 mWifiEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800185 mBtEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700186 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800187 }
Danica Chang32711b62010-08-10 18:41:29 -0700188
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800189 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700190 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800191 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700192
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800193 mAirplaneModeEnabler.pause();
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800194 mWifiEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800195 mBtEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700196 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800197 }
Danica Chang32711b62010-08-10 18:41:29 -0700198
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500199 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700200 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800201 if (requestCode == REQUEST_CODE_EXIT_ECM) {
202 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500203 // Set Airplane mode based on the return value and checkbox state
204 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
205 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500206 }
207 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800208}