blob: e1fc88225e90429ccd4b07822546b0bf35dfe408 [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;
39
Amith Yamasanid7993472010-08-18 13:59:28 -070040public 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";
Nick Pellyad50ba02010-09-22 10:55:13 -070043 private static final String KEY_TOGGLE_NFC = "toggle_nfc";
Nick Pelly0fb33ab2011-08-23 19:35:13 -070044 private static final String KEY_NDEF_PUSH_SETTINGS = "ndef_push_settings";
Mike Lockwood83bcc982009-07-29 23:25:10 -070045 private static final String KEY_VPN_SETTINGS = "vpn_settings";
repo syncb98463f2011-06-30 10:58:43 -070046 private static final String KEY_WIFI_P2P_SETTINGS = "wifi_p2p_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080047 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Oscar Montemayor05411892010-08-03 16:56:09 -070048 private static final String KEY_PROXY_SETTINGS = "proxy_settings";
Amith Yamasani0f85c482011-02-23 17:19:11 -080049 private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings";
Nick Pellyad50ba02010-09-22 10:55:13 -070050
repo sync05e01be2011-08-16 21:39:55 -070051 private static final Boolean WIFI_P2P_DEBUG = false;
52
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;
Nick Pellyad50ba02010-09-22 10:55:13 -070058 private NfcEnabler mNfcEnabler;
Martijn Coenenbb4bdc22011-07-27 17:31:41 -050059 private NfcAdapter mNfcAdapter;
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();
Gilles Debunnee78c1872011-06-20 15:00:07 -070097 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Nick Pellyad50ba02010-09-22 10:55:13 -070098 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Nick Pelly0fb33ab2011-08-23 19:35:13 -070099 PreferenceScreen ndefPush = (PreferenceScreen) findPreference(KEY_NDEF_PUSH_SETTINGS);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800100
Gilles Debunnee78c1872011-06-20 15:00:07 -0700101 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
Nick Pelly0fb33ab2011-08-23 19:35:13 -0700102 mNfcEnabler = new NfcEnabler(activity, nfc, ndefPush);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800103
Amith Yamasanid7993472010-08-18 13:59:28 -0700104 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800105 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
106
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800107 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800108 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800109 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
110 }
111
112 // Manually set dependencies for Bluetooth when not toggleable.
113 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700114 // No bluetooth-dependent items in the list. Code kept in case one is added later.
Nick Pellyad50ba02010-09-22 10:55:13 -0700115 }
116
Nick Pelly9894d4a2011-08-09 07:10:16 -0700117 // Manually set dependencies for NFC when not toggleable.
118 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_NFC)) {
119 findPreference(KEY_TOGGLE_NFC).setDependency(KEY_TOGGLE_AIRPLANE);
Nick Pelly0fb33ab2011-08-23 19:35:13 -0700120 findPreference(KEY_NDEF_PUSH_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
Nick Pelly9894d4a2011-08-09 07:10:16 -0700121 }
122
Nick Pellyad50ba02010-09-22 10:55:13 -0700123 // Remove NFC if its not available
Martijn Coenenbb4bdc22011-07-27 17:31:41 -0500124 mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
125 if (mNfcAdapter == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700126 getPreferenceScreen().removePreference(nfc);
Nick Pelly0fb33ab2011-08-23 19:35:13 -0700127 getPreferenceScreen().removePreference(ndefPush);
Nick Pelly9894d4a2011-08-09 07:10:16 -0700128 mNfcEnabler = null;
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800129 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800130
Amith Yamasani0f85c482011-02-23 17:19:11 -0800131 // Remove Mobile Network Settings if it's a wifi-only device.
132 if (Utils.isWifiOnly()) {
133 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
134 }
135
repo sync05e01be2011-08-16 21:39:55 -0700136 if (!WIFI_P2P_DEBUG) {
repo syncb98463f2011-06-30 10:58:43 -0700137 getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_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);
Robert Greenwalt4ad4b4f2011-02-25 17:25:38 -0800151 if (!cm.isTetheringSupported()) {
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);
Danica Chang32711b62010-08-10 18:41:29 -0700165 } else if (wifiAvailable && usbAvailable) {
166 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700167 } else if (wifiAvailable && bluetoothAvailable) {
168 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700169 } else if (wifiAvailable) {
170 p.setTitle(R.string.tether_settings_title_wifi);
Danica Chang32711b62010-08-10 18:41:29 -0700171 } else if (usbAvailable && bluetoothAvailable) {
172 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
Danica Chang32711b62010-08-10 18:41:29 -0700173 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700174 p.setTitle(R.string.tether_settings_title_usb);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700175 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700176 p.setTitle(R.string.tether_settings_title_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700177 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800178 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800179 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800180
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800181 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700182 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800183 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700184
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800185 mAirplaneModeEnabler.resume();
Nick Pelly9894d4a2011-08-09 07:10:16 -0700186 if (mNfcEnabler != null) {
187 mNfcEnabler.resume();
Martijn Coenenbb4bdc22011-07-27 17:31:41 -0500188 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800189 }
Danica Chang32711b62010-08-10 18:41:29 -0700190
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800191 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700192 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800193 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700194
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800195 mAirplaneModeEnabler.pause();
Nick Pelly9894d4a2011-08-09 07:10:16 -0700196 if (mNfcEnabler != null) {
197 mNfcEnabler.pause();
198 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800199 }
Danica Chang32711b62010-08-10 18:41:29 -0700200
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500201 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700202 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800203 if (requestCode == REQUEST_CODE_EXIT_ECM) {
204 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500205 // Set Airplane mode based on the return value and checkbox state
206 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
207 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500208 }
209 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800210}