blob: 7c52413c7b8801c9053798b84eb2fce00337052b [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;
Irfan Sheriff167d7182011-09-07 14:51:11 -070023import android.content.pm.PackageManager;
Amith Yamasanid7993472010-08-18 13:59:28 -070024import android.net.ConnectivityManager;
repo syncb98463f2011-06-30 10:58:43 -070025import android.net.wifi.p2p.WifiP2pManager;
Nick Pellya57eace2010-10-15 01:19:43 -070026import android.nfc.NfcAdapter;
Amith Yamasanid7993472010-08-18 13:59:28 -070027import android.os.Bundle;
Amith Yamasanid7993472010-08-18 13:59:28 -070028import android.os.SystemProperties;
29import android.preference.CheckBoxPreference;
30import android.preference.Preference;
31import android.preference.PreferenceScreen;
32import android.provider.Settings;
repo syncb98463f2011-06-30 10:58:43 -070033import android.view.LayoutInflater;
34import android.view.View;
35import android.widget.Switch;
Amith Yamasanid7993472010-08-18 13:59:28 -070036
Gilles Debunnee78c1872011-06-20 15:00:07 -070037import com.android.internal.telephony.TelephonyIntents;
38import com.android.internal.telephony.TelephonyProperties;
39import com.android.settings.nfc.NfcEnabler;
Irfan Sheriff1610a742011-08-31 11:51:17 -070040import com.android.settings.wifi.p2p.WifiP2pEnabler;
Gilles Debunnee78c1872011-06-20 15:00:07 -070041
Amith Yamasanid7993472010-08-18 13:59:28 -070042public class WirelessSettings extends SettingsPreferenceFragment {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080043
44 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
Nick Pellyad50ba02010-09-22 10:55:13 -070045 private static final String KEY_TOGGLE_NFC = "toggle_nfc";
Mike Lockwood83bcc982009-07-29 23:25:10 -070046 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Irfan Sheriff1610a742011-08-31 11:51:17 -070047 private static final String KEY_TOGGLE_WIFI_P2P = "toggle_wifi_p2p";
repo syncb98463f2011-06-30 10:58:43 -070048 private static final String KEY_WIFI_P2P_SETTINGS = "wifi_p2p_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080049 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Oscar Montemayor05411892010-08-03 16:56:09 -070050 private static final String KEY_PROXY_SETTINGS = "proxy_settings";
Amith Yamasani0f85c482011-02-23 17:19:11 -080051 private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings";
Nick Pellyad50ba02010-09-22 10:55:13 -070052
Irfan Sheriffe964d572011-09-01 09:54:32 -070053 private static final int WIFI_P2P_DEBUG = SystemProperties.getInt("ro.debuggable", 0);
repo sync05e01be2011-08-16 21:39:55 -070054
Chouting Zhang71cc49e2009-08-28 14:36:35 -050055 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
56 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080057
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080058 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050059 private CheckBoxPreference mAirplaneModePreference;
Nick Pellyad50ba02010-09-22 10:55:13 -070060 private NfcEnabler mNfcEnabler;
Martijn Coenenbb4bdc22011-07-27 17:31:41 -050061 private NfcAdapter mNfcAdapter;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050062
Irfan Sheriff1610a742011-08-31 11:51:17 -070063 private WifiP2pEnabler mWifiP2pEnabler;
64
Chouting Zhang71cc49e2009-08-28 14:36:35 -050065 /**
66 * Invoked on each preference click in this hierarchy, overrides
67 * PreferenceActivity's implementation. Used to make sure we track the
68 * preference click events.
69 */
70 @Override
71 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080072 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
73 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050074 // In ECM mode launch ECM app dialog
75 startActivityForResult(
76 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
77 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050078 return true;
79 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080080 // Let the intents be launched by the Preference manager
Amith Yamasanid7993472010-08-18 13:59:28 -070081 return super.onPreferenceTreeClick(preferenceScreen, preference);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050082 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080083
84 public static boolean isRadioAllowed(Context context, String type) {
85 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
86 return true;
87 }
88 // Here we use the same logic in onCreate().
89 String toggleable = Settings.System.getString(context.getContentResolver(),
90 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
91 return toggleable != null && toggleable.contains(type);
92 }
93
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080094 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -070095 public void onCreate(Bundle savedInstanceState) {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080096 super.onCreate(savedInstanceState);
97
98 addPreferencesFromResource(R.xml.wireless_settings);
99
Amith Yamasanid7993472010-08-18 13:59:28 -0700100 final Activity activity = getActivity();
Gilles Debunnee78c1872011-06-20 15:00:07 -0700101 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Nick Pellyad50ba02010-09-22 10:55:13 -0700102 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800103
Irfan Sheriff1610a742011-08-31 11:51:17 -0700104 CheckBoxPreference wifiP2p = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI_P2P);
105
Gilles Debunnee78c1872011-06-20 15:00:07 -0700106 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
Martijn Coenen943af0b2011-09-14 11:47:59 +0200107 mNfcEnabler = new NfcEnabler(activity, nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800108
Amith Yamasanid7993472010-08-18 13:59:28 -0700109 String toggleable = Settings.System.getString(activity.getContentResolver(),
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800110 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
111
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800112 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800113 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800114 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
115 }
116
117 // Manually set dependencies for Bluetooth when not toggleable.
118 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
Gilles Debunnee78c1872011-06-20 15:00:07 -0700119 // No bluetooth-dependent items in the list. Code kept in case one is added later.
Nick Pellyad50ba02010-09-22 10:55:13 -0700120 }
121
Nick Pelly9894d4a2011-08-09 07:10:16 -0700122 // Manually set dependencies for NFC when not toggleable.
123 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_NFC)) {
124 findPreference(KEY_TOGGLE_NFC).setDependency(KEY_TOGGLE_AIRPLANE);
Nick Pelly9894d4a2011-08-09 07:10:16 -0700125 }
126
Nick Pellyad50ba02010-09-22 10:55:13 -0700127 // Remove NFC if its not available
Martijn Coenenbb4bdc22011-07-27 17:31:41 -0500128 mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
129 if (mNfcAdapter == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700130 getPreferenceScreen().removePreference(nfc);
Nick Pelly9894d4a2011-08-09 07:10:16 -0700131 mNfcEnabler = null;
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800132 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800133
Amith Yamasani0f85c482011-02-23 17:19:11 -0800134 // Remove Mobile Network Settings if it's a wifi-only device.
Robert Greenwalt8af88fb2011-08-31 11:17:47 -0700135 if (Utils.isWifiOnly(getActivity())) {
Amith Yamasani0f85c482011-02-23 17:19:11 -0800136 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
137 }
138
Irfan Sheriff1610a742011-08-31 11:51:17 -0700139 WifiP2pManager p2p = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE);
140
Irfan Sheriff167d7182011-09-07 14:51:11 -0700141 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)) {
Irfan Sheriff1610a742011-08-31 11:51:17 -0700142 getPreferenceScreen().removePreference(wifiP2p);
Irfan Sheriffe964d572011-09-01 09:54:32 -0700143 getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
Irfan Sheriff1610a742011-08-31 11:51:17 -0700144 } else {
145 mWifiP2pEnabler = new WifiP2pEnabler(activity, wifiP2p);
Irfan Sheriffe964d572011-09-01 09:54:32 -0700146 //Settings is used for debug alone
147 if (WIFI_P2P_DEBUG == 0) {
148 getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
149 }
repo syncb98463f2011-06-30 10:58:43 -0700150 }
151
Oscar Montemayor05411892010-08-03 16:56:09 -0700152 // Enable Proxy selector settings if allowed.
153 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
Amith Yamasanid7993472010-08-18 13:59:28 -0700154 DevicePolicyManager mDPM = (DevicePolicyManager)
155 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
Robert Greenwalt6f3a98b2010-12-28 16:11:12 -0800156 // proxy UI disabled until we have better app support
157 getPreferenceScreen().removePreference(mGlobalProxy);
Oscar Montemayor05411892010-08-03 16:56:09 -0700158 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
159
Amith Yamasani0f85c482011-02-23 17:19:11 -0800160 // Disable Tethering if it's not allowed or if it's a wifi-only device
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800161 ConnectivityManager cm =
Amith Yamasanid7993472010-08-18 13:59:28 -0700162 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
Robert Greenwalt4ad4b4f2011-02-25 17:25:38 -0800163 if (!cm.isTetheringSupported()) {
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800164 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700165 } else {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700166 Preference p = findPreference(KEY_TETHER_SETTINGS);
Jeff Sharkeya83a24f2011-09-16 01:52:39 -0700167 p.setTitle(Utils.getTetheringLabel(cm));
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800168 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800169 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800170
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800171 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700172 public void onResume() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800173 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700174
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800175 mAirplaneModeEnabler.resume();
Nick Pelly9894d4a2011-08-09 07:10:16 -0700176 if (mNfcEnabler != null) {
177 mNfcEnabler.resume();
Martijn Coenenbb4bdc22011-07-27 17:31:41 -0500178 }
Irfan Sheriff1610a742011-08-31 11:51:17 -0700179
180 if (mWifiP2pEnabler != null) {
181 mWifiP2pEnabler.resume();
182 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800183 }
Danica Chang32711b62010-08-10 18:41:29 -0700184
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800185 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700186 public void onPause() {
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800187 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700188
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800189 mAirplaneModeEnabler.pause();
Nick Pelly9894d4a2011-08-09 07:10:16 -0700190 if (mNfcEnabler != null) {
191 mNfcEnabler.pause();
192 }
Irfan Sheriff1610a742011-08-31 11:51:17 -0700193
194 if (mWifiP2pEnabler != null) {
195 mWifiP2pEnabler.pause();
196 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800197 }
Danica Chang32711b62010-08-10 18:41:29 -0700198
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500199 @Override
Amith Yamasanid7993472010-08-18 13:59:28 -0700200 public void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800201 if (requestCode == REQUEST_CODE_EXIT_ECM) {
202 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500203 // Set Airplane mode based on the return value and checkbox state
204 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
205 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500206 }
207 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800208}