blob: 5560a03dab5339d7a1eddb03e38dc676247ccc35 [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;
Nick Pellya57eace2010-10-15 01:19:43 -070024import android.nfc.NfcAdapter;
Amith Yamasanid7993472010-08-18 13:59:28 -070025import android.os.Bundle;
Amith Yamasanid7993472010-08-18 13:59:28 -070026import android.os.SystemProperties;
27import android.preference.CheckBoxPreference;
28import android.preference.Preference;
29import android.preference.PreferenceScreen;
30import android.provider.Settings;
31
Gilles Debunnee78c1872011-06-20 15:00:07 -070032import com.android.internal.telephony.TelephonyIntents;
33import com.android.internal.telephony.TelephonyProperties;
34import com.android.settings.nfc.NfcEnabler;
35
Amith Yamasanid7993472010-08-18 13:59:28 -070036public class WirelessSettings extends SettingsPreferenceFragment {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080037
38 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
Nick Pellyad50ba02010-09-22 10:55:13 -070039 private static final String KEY_TOGGLE_NFC = "toggle_nfc";
Mike Lockwood83bcc982009-07-29 23:25:10 -070040 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080041 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Oscar Montemayor05411892010-08-03 16:56:09 -070042 private static final String KEY_PROXY_SETTINGS = "proxy_settings";
Amith Yamasani0f85c482011-02-23 17:19:11 -080043 private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings";
Nick Pellyad50ba02010-09-22 10:55:13 -070044
Chouting Zhang71cc49e2009-08-28 14:36:35 -050045 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
46 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080047
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080048 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050049 private CheckBoxPreference mAirplaneModePreference;
Nick Pellyad50ba02010-09-22 10:55:13 -070050 private NfcEnabler mNfcEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050051
52 /**
53 * Invoked on each preference click in this hierarchy, overrides
54 * PreferenceActivity's implementation. Used to make sure we track the
55 * preference click events.
56 */
57 @Override
58 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080059 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
60 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050061 // In ECM mode launch ECM app dialog
62 startActivityForResult(
63 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
64 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050065 return true;
66 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080067 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070068 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050069 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080070
71 public static boolean isRadioAllowed(Context context, String type) {
72 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
73 return true;
74 }
75 // Here we use the same logic in onCreate().
76 String toggleable = Settings.System.getString(context.getContentResolver(),
77 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
78 return toggleable != null && toggleable.contains(type);
79 }
80
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080081 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070082 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080083 super.onCreate(savedInstanceState);
84
85 addPreferencesFromResource(R.xml.wireless_settings);
86
Amith Yamasanid7993472010-08-18 13:59:28 -070087 final Activity activity = getActivity();
Gilles Debunnee78c1872011-06-20 15:00:07 -070088 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Nick Pellyad50ba02010-09-22 10:55:13 -070089 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080090
Gilles Debunnee78c1872011-06-20 15:00:07 -070091 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
Nick Pellyd83aaf22010-09-29 12:10:35 -070092 mNfcEnabler = new NfcEnabler(activity, nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080093
Amith Yamasanid7993472010-08-18 13:59:28 -070094 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +080095 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
96
Chia-chi Yehb90452f2010-01-13 06:11:29 +080097 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +080098 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080099 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
100 }
101
102 // Manually set dependencies for Bluetooth when not toggleable.
103 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700104 // No bluetooth-dependent items in the list. Code kept in case one is added later.
Nick Pellyad50ba02010-09-22 10:55:13 -0700105 }
106
107 // Remove NFC if its not available
Nick Pellyb8be6da2010-12-13 13:44:10 -0800108 if (NfcAdapter.getDefaultAdapter(activity) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700109 getPreferenceScreen().removePreference(nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800110 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800111
Amith Yamasani0f85c482011-02-23 17:19:11 -0800112 // Remove Mobile Network Settings if it's a wifi-only device.
113 if (Utils.isWifiOnly()) {
114 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
115 }
116
Oscar Montemayor05411892010-08-03 16:56:09 -0700117 // Enable Proxy selector settings if allowed.
118 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
Amith Yamasanid7993472010-08-18 13:59:28 -0700119 DevicePolicyManager mDPM = (DevicePolicyManager)
120 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
Robert Greenwalt6f3a98b2010-12-28 16:11:12 -0800121 // proxy UI disabled until we have better app support
122 getPreferenceScreen().removePreference(mGlobalProxy);
Oscar Montemayor05411892010-08-03 16:56:09 -0700123 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
124
Amith Yamasani0f85c482011-02-23 17:19:11 -0800125 // Disable Tethering if it's not allowed or if it's a wifi-only device
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800126 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700127 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwalt4ad4b4f2011-02-25 17:25:38 -0800128 if (!cm.isTetheringSupported()) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800129 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700130 } else {
131 String[] usbRegexs = cm.getTetherableUsbRegexs();
132 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700133 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
134
135 boolean usbAvailable = usbRegexs.length != 0;
136 boolean wifiAvailable = wifiRegexs.length != 0;
137 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
138
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700139 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700140 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
141 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700142 } else if (wifiAvailable && usbAvailable) {
143 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700144 } else if (wifiAvailable && bluetoothAvailable) {
145 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700146 } else if (wifiAvailable) {
147 p.setTitle(R.string.tether_settings_title_wifi);
Danica Chang32711b62010-08-10 18:41:29 -0700148 } else if (usbAvailable && bluetoothAvailable) {
149 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
Danica Chang32711b62010-08-10 18:41:29 -0700150 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700151 p.setTitle(R.string.tether_settings_title_usb);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700152 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700153 p.setTitle(R.string.tether_settings_title_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700154 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800155 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800156 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800157
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800158 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700159 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800160 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700161
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800162 mAirplaneModeEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700163 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800164 }
Danica Chang32711b62010-08-10 18:41:29 -0700165
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800166 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700167 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800168 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700169
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800170 mAirplaneModeEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700171 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800172 }
Danica Chang32711b62010-08-10 18:41:29 -0700173
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500174 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700175 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800176 if (requestCode == REQUEST_CODE_EXIT_ECM) {
177 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500178 // Set Airplane mode based on the return value and checkbox state
179 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
180 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500181 }
182 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800183}