blob: c07388e1bea1e30f6e8f104702447756ca687f48 [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";
Martijn Coenenba534402011-07-21 09:33:40 +020040 private static final String KEY_ZEROCLICK_SETTINGS = "zeroclick_settings";
Mike Lockwood83bcc982009-07-29 23:25:10 -070041 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080042 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Oscar Montemayor05411892010-08-03 16:56:09 -070043 private static final String KEY_PROXY_SETTINGS = "proxy_settings";
Amith Yamasani0f85c482011-02-23 17:19:11 -080044 private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings";
Nick Pellyad50ba02010-09-22 10:55:13 -070045
Chouting Zhang71cc49e2009-08-28 14:36:35 -050046 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
47 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080048
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080049 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050050 private CheckBoxPreference mAirplaneModePreference;
Nick Pellyad50ba02010-09-22 10:55:13 -070051 private NfcEnabler mNfcEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050052
53 /**
54 * Invoked on each preference click in this hierarchy, overrides
55 * PreferenceActivity's implementation. Used to make sure we track the
56 * preference click events.
57 */
58 @Override
59 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080060 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
61 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050062 // In ECM mode launch ECM app dialog
63 startActivityForResult(
64 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
65 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050066 return true;
67 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080068 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070069 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050070 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080071
72 public static boolean isRadioAllowed(Context context, String type) {
73 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
74 return true;
75 }
76 // Here we use the same logic in onCreate().
77 String toggleable = Settings.System.getString(context.getContentResolver(),
78 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
79 return toggleable != null && toggleable.contains(type);
80 }
81
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080082 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070083 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080084 super.onCreate(savedInstanceState);
85
86 addPreferencesFromResource(R.xml.wireless_settings);
87
Amith Yamasanid7993472010-08-18 13:59:28 -070088 final Activity activity = getActivity();
Gilles Debunnee78c1872011-06-20 15:00:07 -070089 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Nick Pellyad50ba02010-09-22 10:55:13 -070090 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Martijn Coenenba534402011-07-21 09:33:40 +020091 PreferenceScreen zeroclick = (PreferenceScreen)
92 findPreference(KEY_ZEROCLICK_SETTINGS);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080093
Gilles Debunnee78c1872011-06-20 15:00:07 -070094 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
Martijn Coenenba534402011-07-21 09:33:40 +020095 mNfcEnabler = new NfcEnabler(activity, nfc, zeroclick);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080096
Amith Yamasanid7993472010-08-18 13:59:28 -070097 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +080098 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
99
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800100 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800101 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800102 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
103 }
104
105 // Manually set dependencies for Bluetooth when not toggleable.
106 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700107 // No bluetooth-dependent items in the list. Code kept in case one is added later.
Nick Pellyad50ba02010-09-22 10:55:13 -0700108 }
109
110 // Remove NFC if its not available
Nick Pellyb8be6da2010-12-13 13:44:10 -0800111 if (NfcAdapter.getDefaultAdapter(activity) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700112 getPreferenceScreen().removePreference(nfc);
Martijn Coenenba534402011-07-21 09:33:40 +0200113 getPreferenceScreen().removePreference(zeroclick);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800114 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800115
Amith Yamasani0f85c482011-02-23 17:19:11 -0800116 // Remove Mobile Network Settings if it's a wifi-only device.
117 if (Utils.isWifiOnly()) {
118 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
119 }
120
Oscar Montemayor05411892010-08-03 16:56:09 -0700121 // Enable Proxy selector settings if allowed.
122 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
Amith Yamasanid7993472010-08-18 13:59:28 -0700123 DevicePolicyManager mDPM = (DevicePolicyManager)
124 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
Robert Greenwalt6f3a98b2010-12-28 16:11:12 -0800125 // proxy UI disabled until we have better app support
126 getPreferenceScreen().removePreference(mGlobalProxy);
Oscar Montemayor05411892010-08-03 16:56:09 -0700127 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
128
Amith Yamasani0f85c482011-02-23 17:19:11 -0800129 // Disable Tethering if it's not allowed or if it's a wifi-only device
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800130 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700131 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwalt4ad4b4f2011-02-25 17:25:38 -0800132 if (!cm.isTetheringSupported()) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800133 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700134 } else {
135 String[] usbRegexs = cm.getTetherableUsbRegexs();
136 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700137 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
138
139 boolean usbAvailable = usbRegexs.length != 0;
140 boolean wifiAvailable = wifiRegexs.length != 0;
141 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
142
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700143 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700144 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
145 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700146 } else if (wifiAvailable && usbAvailable) {
147 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700148 } else if (wifiAvailable && bluetoothAvailable) {
149 p.setTitle(R.string.tether_settings_title_all);
Danica Chang32711b62010-08-10 18:41:29 -0700150 } else if (wifiAvailable) {
151 p.setTitle(R.string.tether_settings_title_wifi);
Danica Chang32711b62010-08-10 18:41:29 -0700152 } else if (usbAvailable && bluetoothAvailable) {
153 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
Danica Chang32711b62010-08-10 18:41:29 -0700154 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700155 p.setTitle(R.string.tether_settings_title_usb);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700156 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700157 p.setTitle(R.string.tether_settings_title_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700158 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800159 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800160 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800161
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800162 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700163 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800164 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700165
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800166 mAirplaneModeEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700167 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800168 }
Danica Chang32711b62010-08-10 18:41:29 -0700169
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800170 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700171 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800172 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700173
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800174 mAirplaneModeEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700175 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800176 }
Danica Chang32711b62010-08-10 18:41:29 -0700177
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500178 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700179 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800180 if (requestCode == REQUEST_CODE_EXIT_ECM) {
181 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500182 // Set Airplane mode based on the return value and checkbox state
183 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
184 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500185 }
186 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800187}