blob: 60ae20da03d29a44a4b8c480299ea05f23294f56 [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
Oscar Montemayor05411892010-08-03 16:56:09 -070019import android.app.admin.DevicePolicyManager;
Michael Chan0cb37432009-10-29 14:42:06 -070020import android.bluetooth.BluetoothAdapter;
Chia-chi Yehb90452f2010-01-13 06:11:29 +080021import android.content.Context;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050022import android.content.Intent;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080023import android.net.ConnectivityManager;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.net.wifi.WifiManager;
25import android.os.Bundle;
Michael Chan0cb37432009-10-29 14:42:06 -070026import android.os.ServiceManager;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050027import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080028import android.preference.CheckBoxPreference;
Mike Lockwood83bcc982009-07-29 23:25:10 -070029import android.preference.Preference;
30import android.preference.PreferenceActivity;
Michael Chan0cb37432009-10-29 14:42:06 -070031import android.preference.PreferenceScreen;
Mike Lockwood83bcc982009-07-29 23:25:10 -070032import android.provider.Settings;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080033import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080034
Chouting Zhang71cc49e2009-08-28 14:36:35 -050035import com.android.internal.telephony.TelephonyIntents;
36import com.android.internal.telephony.TelephonyProperties;
Michael Chan0cb37432009-10-29 14:42:06 -070037import com.android.settings.bluetooth.BluetoothEnabler;
38import com.android.settings.wifi.WifiEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050039
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080040public class WirelessSettings extends PreferenceActivity {
41
42 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
43 private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
44 private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
Mike Lockwood83bcc982009-07-29 23:25:10 -070045 private static final String KEY_WIFI_SETTINGS = "wifi_settings";
Michael Chan0cb37432009-10-29 14:42:06 -070046 private static final String KEY_BT_SETTINGS = "bt_settings";
Mike Lockwood83bcc982009-07-29 23:25:10 -070047 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080048 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Oscar Montemayor05411892010-08-03 16:56:09 -070049 private static final String KEY_PROXY_SETTINGS = "proxy_settings";
Chouting Zhang71cc49e2009-08-28 14:36:35 -050050 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
51 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080053 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050054 private CheckBoxPreference mAirplaneModePreference;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080055 private WifiEnabler mWifiEnabler;
56 private BluetoothEnabler mBtEnabler;
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
74 return false;
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
88 protected void onCreate(Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
90
91 addPreferencesFromResource(R.xml.wireless_settings);
92
Chia-chi Yeh4e142112009-12-25 14:48:46 +080093 CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
94 CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI);
95 CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
96
97 mAirplaneModeEnabler = new AirplaneModeEnabler(this, airplane);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050098 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080099 mWifiEnabler = new WifiEnabler(this, wifi);
100 mBtEnabler = new BluetoothEnabler(this, bt);
101
102 String toggleable = Settings.System.getString(getContentResolver(),
103 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)) {
107 wifi.setDependency(KEY_TOGGLE_AIRPLANE);
108 findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
109 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
110 }
111
112 // Manually set dependencies for Bluetooth when not toggleable.
113 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
114 bt.setDependency(KEY_TOGGLE_AIRPLANE);
115 findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
116 }
117
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800118 // Disable Bluetooth Settings if Bluetooth service is not available.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800119 if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
120 findPreference(KEY_BT_SETTINGS).setEnabled(false);
121 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800122
Oscar Montemayor05411892010-08-03 16:56:09 -0700123 // Enable Proxy selector settings if allowed.
124 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
125 DevicePolicyManager mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
126 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
127
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800128 // Disable Tethering if it's not allowed
129 ConnectivityManager cm =
130 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
131 if (!cm.isTetheringSupported()) {
132 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700133 } else {
134 String[] usbRegexs = cm.getTetherableUsbRegexs();
135 String[] wifiRegexs = cm.getTetherableWifiRegexs();
Danica Chang32711b62010-08-10 18:41:29 -0700136 String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs();
137
138 boolean usbAvailable = usbRegexs.length != 0;
139 boolean wifiAvailable = wifiRegexs.length != 0;
140 boolean bluetoothAvailable = bluetoothRegexs.length != 0;
141
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700142 Preference p = findPreference(KEY_TETHER_SETTINGS);
Danica Chang32711b62010-08-10 18:41:29 -0700143 if (wifiAvailable && usbAvailable && bluetoothAvailable) {
144 p.setTitle(R.string.tether_settings_title_all);
145 p.setSummary(R.string.tether_settings_summary_all);
146 } else if (wifiAvailable && usbAvailable) {
147 p.setTitle(R.string.tether_settings_title_all);
148 p.setSummary(R.string.tether_settings_summary_usb_wifi);
149 } else if (wifiAvailable && bluetoothAvailable) {
150 p.setTitle(R.string.tether_settings_title_all);
151 p.setSummary(R.string.tether_settings_summary_wifi_bluetooth);
152 } else if (wifiAvailable) {
153 p.setTitle(R.string.tether_settings_title_wifi);
154 p.setSummary(R.string.tether_settings_summary_wifi);
155 } else if (usbAvailable && bluetoothAvailable) {
156 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
157 p.setSummary(R.string.tether_settings_summary_usb_bluetooth);
158 } else if (usbAvailable) {
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700159 p.setTitle(R.string.tether_settings_title_usb);
160 p.setSummary(R.string.tether_settings_summary_usb);
161 } else {
Danica Chang32711b62010-08-10 18:41:29 -0700162 p.setTitle(R.string.tether_settings_title_bluetooth);
163 p.setSummary(R.string.tether_settings_summary_bluetooth);
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700164 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800165 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800166 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800167
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800168 @Override
169 protected void onResume() {
170 super.onResume();
Danica Chang32711b62010-08-10 18:41:29 -0700171
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800172 mAirplaneModeEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800173 mWifiEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800174 mBtEnabler.resume();
175 }
Danica Chang32711b62010-08-10 18:41:29 -0700176
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800177 @Override
178 protected void onPause() {
179 super.onPause();
Danica Chang32711b62010-08-10 18:41:29 -0700180
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800181 mAirplaneModeEnabler.pause();
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800182 mWifiEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800183 mBtEnabler.pause();
184 }
Danica Chang32711b62010-08-10 18:41:29 -0700185
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500186 @Override
187 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800188 if (requestCode == REQUEST_CODE_EXIT_ECM) {
189 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500190 // Set Airplane mode based on the return value and checkbox state
191 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
192 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500193 }
194 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800195}