blob: c6f14a17400910606603e5dccbd5ed81c8c7cbe3 [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
Amith Yamasanid7993472010-08-18 13:59:28 -070019import android.app.Activity;
20import android.app.admin.DevicePolicyManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070021import android.content.Context;
22import android.content.Intent;
23import android.net.ConnectivityManager;
repo syncb98463f2011-06-30 10:58:43 -070024import android.net.wifi.p2p.WifiP2pManager;
Nick Pellya57eace2010-10-15 01:19:43 -070025import android.nfc.NfcAdapter;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.os.Bundle;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.SystemProperties;
28import android.preference.CheckBoxPreference;
29import android.preference.Preference;
30import android.preference.PreferenceScreen;
31import android.provider.Settings;
repo syncb98463f2011-06-30 10:58:43 -070032import android.view.LayoutInflater;
33import android.view.View;
34import android.widget.Switch;
Amith Yamasanid7993472010-08-18 13:59:28 -070035
Gilles Debunnee78c1872011-06-20 15:00:07 -070036import com.android.internal.telephony.TelephonyIntents;
37import com.android.internal.telephony.TelephonyProperties;
38import com.android.settings.nfc.NfcEnabler;
Irfan Sheriff1610a742011-08-31 11:51:17 -070039import com.android.settings.wifi.p2p.WifiP2pEnabler;
Gilles Debunnee78c1872011-06-20 15:00:07 -070040
Amith Yamasanid7993472010-08-18 13:59:28 -070041public class WirelessSettings extends SettingsPreferenceFragment {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080042
43 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
Nick Pellyad50ba02010-09-22 10:55:13 -070044 private static final String KEY_TOGGLE_NFC = "toggle_nfc";
Nick Pelly0fb33ab2011-08-23 19:35:13 -070045 private static final String KEY_NDEF_PUSH_SETTINGS = "ndef_push_settings";
Mike Lockwood83bcc982009-07-29 23:25:10 -070046 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Irfan Sheriff1610a742011-08-31 11:51:17 -070047 private static final String KEY_TOGGLE_WIFI_P2P = "toggle_wifi_p2p";
repo syncb98463f2011-06-30 10:58:43 -070048 private static final String KEY_WIFI_P2P_SETTINGS = "wifi_p2p_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
repo sync05e01be2011-08-16 21:39:55 -070053 private static final Boolean WIFI_P2P_DEBUG = false;
54
Chouting Zhang71cc49e2009-08-28 14:36:35 -050055 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
56 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080057
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080058 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050059 private CheckBoxPreference mAirplaneModePreference;
Nick Pellyad50ba02010-09-22 10:55:13 -070060 private NfcEnabler mNfcEnabler;
Martijn Coenenbb4bdc22011-07-27 17:31:41 -050061 private NfcAdapter mNfcAdapter;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050062
Irfan Sheriff1610a742011-08-31 11:51:17 -070063 private WifiP2pEnabler mWifiP2pEnabler;
64
Chouting Zhang71cc49e2009-08-28 14:36:35 -050065 /**
66 * Invoked on each preference click in this hierarchy, overrides
67 * PreferenceActivity's implementation. Used to make sure we track the
68 * preference click events.
69 */
70 @Override
71 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080072 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
73 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050074 // In ECM mode launch ECM app dialog
75 startActivityForResult(
76 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
77 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050078 return true;
79 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080080 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070081 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050082 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080083
84 public static boolean isRadioAllowed(Context context, String type) {
85 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
86 return true;
87 }
88 // Here we use the same logic in onCreate().
89 String toggleable = Settings.System.getString(context.getContentResolver(),
90 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
91 return toggleable != null && toggleable.contains(type);
92 }
93
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070095 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096 super.onCreate(savedInstanceState);
97
98 addPreferencesFromResource(R.xml.wireless_settings);
99
Amith Yamasanid7993472010-08-18 13:59:28 -0700100 final Activity activity = getActivity();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700101 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Nick Pellyad50ba02010-09-22 10:55:13 -0700102 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Nick Pelly0fb33ab2011-08-23 19:35:13 -0700103 PreferenceScreen ndefPush = (PreferenceScreen) findPreference(KEY_NDEF_PUSH_SETTINGS);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800104
Irfan Sheriff1610a742011-08-31 11:51:17 -0700105 CheckBoxPreference wifiP2p = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI_P2P);
106
Gilles Debunnee78c1872011-06-20 15:00:07 -0700107 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
Nick Pelly0fb33ab2011-08-23 19:35:13 -0700108 mNfcEnabler = new NfcEnabler(activity, nfc, ndefPush);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800109
Amith Yamasanid7993472010-08-18 13:59:28 -0700110 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800111 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
112
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800113 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800114 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800115 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)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700120 // No bluetooth-dependent items in the list. Code kept in case one is added later.
Nick Pellyad50ba02010-09-22 10:55:13 -0700121 }
122
Nick Pelly9894d4a2011-08-09 07:10:16 -0700123 // Manually set dependencies for NFC when not toggleable.
124 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_NFC)) {
125 findPreference(KEY_TOGGLE_NFC).setDependency(KEY_TOGGLE_AIRPLANE);
Nick Pelly0fb33ab2011-08-23 19:35:13 -0700126 findPreference(KEY_NDEF_PUSH_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
Nick Pelly9894d4a2011-08-09 07:10:16 -0700127 }
128
Nick Pellyad50ba02010-09-22 10:55:13 -0700129 // Remove NFC if its not available
Martijn Coenenbb4bdc22011-07-27 17:31:41 -0500130 mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
131 if (mNfcAdapter == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700132 getPreferenceScreen().removePreference(nfc);
Nick Pelly0fb33ab2011-08-23 19:35:13 -0700133 getPreferenceScreen().removePreference(ndefPush);
Nick Pelly9894d4a2011-08-09 07:10:16 -0700134 mNfcEnabler = null;
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800135 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800136
Amith Yamasani0f85c482011-02-23 17:19:11 -0800137 // Remove Mobile Network Settings if it's a wifi-only device.
Robert Greenwalt8af88fb2011-08-31 11:17:47 -0700138 if (Utils.isWifiOnly(getActivity())) {
Amith Yamasani0f85c482011-02-23 17:19:11 -0800139 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
140 }
141
Irfan Sheriff1610a742011-08-31 11:51:17 -0700142 WifiP2pManager p2p = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
143
144 if (!p2p.isP2pSupported()) {
145 getPreferenceScreen().removePreference(wifiP2p);
146 } else {
147 mWifiP2pEnabler = new WifiP2pEnabler(activity, wifiP2p);
148 }
149
150 //Settings is used for debug alone
repo sync05e01be2011-08-16 21:39:55 -0700151 if (!WIFI_P2P_DEBUG) {
repo syncb98463f2011-06-30 10:58:43 -0700152 getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
153 }
154
Oscar Montemayor05411892010-08-03 16:56:09 -0700155 // Enable Proxy selector settings if allowed.
156 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
Amith Yamasanid7993472010-08-18 13:59:28 -0700157 DevicePolicyManager mDPM = (DevicePolicyManager)
158 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
Robert Greenwalt6f3a98b2010-12-28 16:11:12 -0800159 // proxy UI disabled until we have better app support
160 getPreferenceScreen().removePreference(mGlobalProxy);
Oscar Montemayor05411892010-08-03 16:56:09 -0700161 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
162
Amith Yamasani0f85c482011-02-23 17:19:11 -0800163 // Disable Tethering if it's not allowed or if it's a wifi-only device
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800164 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700165 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwalt4ad4b4f2011-02-25 17:25:38 -0800166 if (!cm.isTetheringSupported()) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800167 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700168 } else {
169 String[] usbRegexs = cm.getTetherableUsbRegexs();
170 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700171 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
172
173 boolean usbAvailable = usbRegexs.length != 0;
174 boolean wifiAvailable = wifiRegexs.length != 0;
175 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
176
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700177 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700178 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
179 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700180 } else if (wifiAvailable && usbAvailable) {
181 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700182 } else if (wifiAvailable && bluetoothAvailable) {
183 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700184 } else if (wifiAvailable) {
185 p.setTitle(R.string.tether_settings_title_wifi);
Danica Chang32711b62010-08-10 18:41:29 -0700186 } else if (usbAvailable && bluetoothAvailable) {
187 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
Danica Chang32711b62010-08-10 18:41:29 -0700188 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700189 p.setTitle(R.string.tether_settings_title_usb);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700190 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700191 p.setTitle(R.string.tether_settings_title_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700192 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800193 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800194 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800195
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800196 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700197 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800198 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700199
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800200 mAirplaneModeEnabler.resume();
Nick Pelly9894d4a2011-08-09 07:10:16 -0700201 if (mNfcEnabler != null) {
202 mNfcEnabler.resume();
Martijn Coenenbb4bdc22011-07-27 17:31:41 -0500203 }
Irfan Sheriff1610a742011-08-31 11:51:17 -0700204
205 if (mWifiP2pEnabler != null) {
206 mWifiP2pEnabler.resume();
207 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800208 }
Danica Chang32711b62010-08-10 18:41:29 -0700209
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800210 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700211 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800212 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700213
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800214 mAirplaneModeEnabler.pause();
Nick Pelly9894d4a2011-08-09 07:10:16 -0700215 if (mNfcEnabler != null) {
216 mNfcEnabler.pause();
217 }
Irfan Sheriff1610a742011-08-31 11:51:17 -0700218
219 if (mWifiP2pEnabler != null) {
220 mWifiP2pEnabler.pause();
221 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800222 }
Danica Chang32711b62010-08-10 18:41:29 -0700223
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500224 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700225 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800226 if (requestCode == REQUEST_CODE_EXIT_ECM) {
227 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500228 // Set Airplane mode based on the return value and checkbox state
229 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
230 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500231 }
232 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800233}