blob: 7efa15d30e98a9322fafce198ec5e2b523d7c70f [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";
Martijn Coenenba534402011-07-21 09:33:40 +020044 private static final String KEY_ZEROCLICK_SETTINGS = "zeroclick_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
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;
Nick Pellyad50ba02010-09-22 10:55:13 -070056 private NfcEnabler mNfcEnabler;
Martijn Coenenbb4bdc22011-07-27 17:31:41 -050057 private NfcAdapter mNfcAdapter;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050058
59 /**
60 * Invoked on each preference click in this hierarchy, overrides
61 * PreferenceActivity's implementation. Used to make sure we track the
62 * preference click events.
63 */
64 @Override
65 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080066 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
67 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050068 // In ECM mode launch ECM app dialog
69 startActivityForResult(
70 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
71 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050072 return true;
73 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080074 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070075 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050076 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080077
78 public static boolean isRadioAllowed(Context context, String type) {
79 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
80 return true;
81 }
82 // Here we use the same logic in onCreate().
83 String toggleable = Settings.System.getString(context.getContentResolver(),
84 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
85 return toggleable != null && toggleable.contains(type);
86 }
87
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080088 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070089 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080090 super.onCreate(savedInstanceState);
91
92 addPreferencesFromResource(R.xml.wireless_settings);
93
Amith Yamasanid7993472010-08-18 13:59:28 -070094 final Activity activity = getActivity();
Gilles Debunnee78c1872011-06-20 15:00:07 -070095 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Nick Pellyad50ba02010-09-22 10:55:13 -070096 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Martijn Coenenba534402011-07-21 09:33:40 +020097 PreferenceScreen zeroclick = (PreferenceScreen)
98 findPreference(KEY_ZEROCLICK_SETTINGS);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080099
Gilles Debunnee78c1872011-06-20 15:00:07 -0700100 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
Martijn Coenenba534402011-07-21 09:33:40 +0200101 mNfcEnabler = new NfcEnabler(activity, nfc, zeroclick);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800102
Amith Yamasanid7993472010-08-18 13:59:28 -0700103 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800104 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
105
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800106 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800107 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800108 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
109 }
110
111 // Manually set dependencies for Bluetooth when not toggleable.
112 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700113 // No bluetooth-dependent items in the list. Code kept in case one is added later.
Nick Pellyad50ba02010-09-22 10:55:13 -0700114 }
115
116 // Remove NFC if its not available
Martijn Coenenbb4bdc22011-07-27 17:31:41 -0500117 mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
118 if (mNfcAdapter == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700119 getPreferenceScreen().removePreference(nfc);
Martijn Coenenba534402011-07-21 09:33:40 +0200120 getPreferenceScreen().removePreference(zeroclick);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800121 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800122
Amith Yamasani0f85c482011-02-23 17:19:11 -0800123 // Remove Mobile Network Settings if it's a wifi-only device.
124 if (Utils.isWifiOnly()) {
125 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
126 }
127
repo syncb98463f2011-06-30 10:58:43 -0700128 WifiP2pManager wifiP2p = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
129 if (!wifiP2p.isP2pSupported()) {
130 getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
131 }
132
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);
Robert Greenwalt6f3a98b2010-12-28 16:11:12 -0800137 // proxy UI disabled until we have better app support
138 getPreferenceScreen().removePreference(mGlobalProxy);
Oscar Montemayor05411892010-08-03 16:56:09 -0700139 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
140
Amith Yamasani0f85c482011-02-23 17:19:11 -0800141 // Disable Tethering if it's not allowed or if it's a wifi-only device
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800142 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700143 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwalt4ad4b4f2011-02-25 17:25:38 -0800144 if (!cm.isTetheringSupported()) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800145 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700146 } else {
147 String[] usbRegexs = cm.getTetherableUsbRegexs();
148 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700149 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
150
151 boolean usbAvailable = usbRegexs.length != 0;
152 boolean wifiAvailable = wifiRegexs.length != 0;
153 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
154
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700155 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700156 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
157 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700158 } else if (wifiAvailable && usbAvailable) {
159 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700160 } else if (wifiAvailable && bluetoothAvailable) {
161 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700162 } else if (wifiAvailable) {
163 p.setTitle(R.string.tether_settings_title_wifi);
Danica Chang32711b62010-08-10 18:41:29 -0700164 } else if (usbAvailable && bluetoothAvailable) {
165 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
Danica Chang32711b62010-08-10 18:41:29 -0700166 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700167 p.setTitle(R.string.tether_settings_title_usb);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700168 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700169 p.setTitle(R.string.tether_settings_title_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700170 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800171 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800172 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800173
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800174 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700175 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800176 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700177
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800178 mAirplaneModeEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700179 mNfcEnabler.resume();
Martijn Coenenbb4bdc22011-07-27 17:31:41 -0500180
181 if (mNfcAdapter != null) {
182 // Update zero-click subtitle
183 Preference zeroClick = getPreferenceScreen().
184 findPreference(KEY_ZEROCLICK_SETTINGS);
185
186 if (mNfcAdapter.zeroClickEnabled()) {
187 zeroClick.setSummary(R.string.zeroclick_on_summary);
188 } else {
189 zeroClick.setSummary(R.string.zeroclick_off_summary);
190 }
191 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800192 }
Danica Chang32711b62010-08-10 18:41:29 -0700193
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800194 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700195 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800196 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700197
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800198 mAirplaneModeEnabler.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}