blob: beab491d21535423ad469bd269c98e850aacd7c9 [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;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050057
58 /**
59 * Invoked on each preference click in this hierarchy, overrides
60 * PreferenceActivity's implementation. Used to make sure we track the
61 * preference click events.
62 */
63 @Override
64 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080065 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
66 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050067 // In ECM mode launch ECM app dialog
68 startActivityForResult(
69 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
70 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050071 return true;
72 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080073 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070074 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050075 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080076
77 public static boolean isRadioAllowed(Context context, String type) {
78 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
79 return true;
80 }
81 // Here we use the same logic in onCreate().
82 String toggleable = Settings.System.getString(context.getContentResolver(),
83 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
84 return toggleable != null && toggleable.contains(type);
85 }
86
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080087 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070088 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080089 super.onCreate(savedInstanceState);
90
91 addPreferencesFromResource(R.xml.wireless_settings);
92
Amith Yamasanid7993472010-08-18 13:59:28 -070093 final Activity activity = getActivity();
Gilles Debunnee78c1872011-06-20 15:00:07 -070094 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Nick Pellyad50ba02010-09-22 10:55:13 -070095 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Martijn Coenenba534402011-07-21 09:33:40 +020096 PreferenceScreen zeroclick = (PreferenceScreen)
97 findPreference(KEY_ZEROCLICK_SETTINGS);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080098
Gilles Debunnee78c1872011-06-20 15:00:07 -070099 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
Martijn Coenenba534402011-07-21 09:33:40 +0200100 mNfcEnabler = new NfcEnabler(activity, nfc, zeroclick);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800101
Amith Yamasanid7993472010-08-18 13:59:28 -0700102 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800103 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
104
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800105 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800106 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800107 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
108 }
109
110 // Manually set dependencies for Bluetooth when not toggleable.
111 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700112 // No bluetooth-dependent items in the list. Code kept in case one is added later.
Nick Pellyad50ba02010-09-22 10:55:13 -0700113 }
114
115 // Remove NFC if its not available
Nick Pellyb8be6da2010-12-13 13:44:10 -0800116 if (NfcAdapter.getDefaultAdapter(activity) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700117 getPreferenceScreen().removePreference(nfc);
Martijn Coenenba534402011-07-21 09:33:40 +0200118 getPreferenceScreen().removePreference(zeroclick);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800119 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800120
Amith Yamasani0f85c482011-02-23 17:19:11 -0800121 // Remove Mobile Network Settings if it's a wifi-only device.
122 if (Utils.isWifiOnly()) {
123 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
124 }
125
repo syncb98463f2011-06-30 10:58:43 -0700126 WifiP2pManager wifiP2p = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
127 if (!wifiP2p.isP2pSupported()) {
128 getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
129 }
130
Oscar Montemayor05411892010-08-03 16:56:09 -0700131 // Enable Proxy selector settings if allowed.
132 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
Amith Yamasanid7993472010-08-18 13:59:28 -0700133 DevicePolicyManager mDPM = (DevicePolicyManager)
134 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
Robert Greenwalt6f3a98b2010-12-28 16:11:12 -0800135 // proxy UI disabled until we have better app support
136 getPreferenceScreen().removePreference(mGlobalProxy);
Oscar Montemayor05411892010-08-03 16:56:09 -0700137 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
138
Amith Yamasani0f85c482011-02-23 17:19:11 -0800139 // Disable Tethering if it's not allowed or if it's a wifi-only device
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800140 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700141 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwalt4ad4b4f2011-02-25 17:25:38 -0800142 if (!cm.isTetheringSupported()) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800143 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);
Danica Chang32711b62010-08-10 18:41:29 -0700156 } else if (wifiAvailable && usbAvailable) {
157 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700158 } else if (wifiAvailable && bluetoothAvailable) {
159 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700160 } else if (wifiAvailable) {
161 p.setTitle(R.string.tether_settings_title_wifi);
Danica Chang32711b62010-08-10 18:41:29 -0700162 } else if (usbAvailable && bluetoothAvailable) {
163 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
Danica Chang32711b62010-08-10 18:41:29 -0700164 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700165 p.setTitle(R.string.tether_settings_title_usb);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700166 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700167 p.setTitle(R.string.tether_settings_title_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700168 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800169 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800170 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800171
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800172 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700173 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800174 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700175
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800176 mAirplaneModeEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700177 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800178 }
Danica Chang32711b62010-08-10 18:41:29 -0700179
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800180 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700181 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800182 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700183
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800184 mAirplaneModeEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700185 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800186 }
Danica Chang32711b62010-08-10 18:41:29 -0700187
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500188 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700189 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800190 if (requestCode == REQUEST_CODE_EXIT_ECM) {
191 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500192 // Set Airplane mode based on the return value and checkbox state
193 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
194 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500195 }
196 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800197}