The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 1 | /* |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 2 | * Copyright (C) 2009 The Android Open Source Project |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 19 | import com.android.internal.telephony.TelephonyIntents; |
| 20 | import com.android.internal.telephony.TelephonyProperties; |
Michael Chan | 0cb3743 | 2009-10-29 14:42:06 -0700 | [diff] [blame] | 21 | import com.android.settings.bluetooth.BluetoothEnabler; |
| 22 | import com.android.settings.wifi.WifiEnabler; |
Nick Pelly | d83aaf2 | 2010-09-29 12:10:35 -0700 | [diff] [blame] | 23 | import com.android.settings.nfc.NfcEnabler; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 24 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 25 | import android.app.Activity; |
| 26 | import android.app.admin.DevicePolicyManager; |
| 27 | import android.bluetooth.BluetoothAdapter; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.net.ConnectivityManager; |
Nick Pelly | a57eace | 2010-10-15 01:19:43 -0700 | [diff] [blame] | 31 | import android.nfc.NfcAdapter; |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
| 33 | import android.os.ServiceManager; |
| 34 | import android.os.SystemProperties; |
| 35 | import android.preference.CheckBoxPreference; |
| 36 | import android.preference.Preference; |
| 37 | import android.preference.PreferenceScreen; |
| 38 | import android.provider.Settings; |
| 39 | |
| 40 | public class WirelessSettings extends SettingsPreferenceFragment { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 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"; |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 45 | private static final String KEY_TOGGLE_NFC = "toggle_nfc"; |
Mike Lockwood | 83bcc98 | 2009-07-29 23:25:10 -0700 | [diff] [blame] | 46 | private static final String KEY_WIFI_SETTINGS = "wifi_settings"; |
Michael Chan | 0cb3743 | 2009-10-29 14:42:06 -0700 | [diff] [blame] | 47 | private static final String KEY_BT_SETTINGS = "bt_settings"; |
Mike Lockwood | 83bcc98 | 2009-07-29 23:25:10 -0700 | [diff] [blame] | 48 | private static final String KEY_VPN_SETTINGS = "vpn_settings"; |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 49 | private static final String KEY_TETHER_SETTINGS = "tether_settings"; |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 50 | private static final String KEY_PROXY_SETTINGS = "proxy_settings"; |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 51 | private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings"; |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 52 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 53 | public static final String EXIT_ECM_RESULT = "exit_ecm_result"; |
| 54 | public static final int REQUEST_CODE_EXIT_ECM = 1; |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 55 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 56 | private AirplaneModeEnabler mAirplaneModeEnabler; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 57 | private CheckBoxPreference mAirplaneModePreference; |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 58 | private WifiEnabler mWifiEnabler; |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 59 | private NfcEnabler mNfcEnabler; |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 60 | private BluetoothEnabler mBtEnabler; |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Invoked on each preference click in this hierarchy, overrides |
| 64 | * PreferenceActivity's implementation. Used to make sure we track the |
| 65 | * preference click events. |
| 66 | */ |
| 67 | @Override |
| 68 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 69 | if (preference == mAirplaneModePreference && Boolean.parseBoolean( |
| 70 | SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 71 | // In ECM mode launch ECM app dialog |
| 72 | startActivityForResult( |
| 73 | new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null), |
| 74 | REQUEST_CODE_EXIT_ECM); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 75 | return true; |
| 76 | } |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 77 | // Let the intents be launched by the Preference manager |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 78 | return super.onPreferenceTreeClick(preferenceScreen, preference); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 79 | } |
Chia-chi Yeh | b90452f | 2010-01-13 06:11:29 +0800 | [diff] [blame] | 80 | |
| 81 | public static boolean isRadioAllowed(Context context, String type) { |
| 82 | if (!AirplaneModeEnabler.isAirplaneModeOn(context)) { |
| 83 | return true; |
| 84 | } |
| 85 | // Here we use the same logic in onCreate(). |
| 86 | String toggleable = Settings.System.getString(context.getContentResolver(), |
| 87 | Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
| 88 | return toggleable != null && toggleable.contains(type); |
| 89 | } |
| 90 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 91 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 92 | public void onCreate(Bundle savedInstanceState) { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 93 | super.onCreate(savedInstanceState); |
| 94 | |
| 95 | addPreferencesFromResource(R.xml.wireless_settings); |
| 96 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 97 | final Activity activity = getActivity(); |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 98 | CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE); |
| 99 | CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI); |
| 100 | CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH); |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 101 | CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC); |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 102 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 103 | mAirplaneModeEnabler = new AirplaneModeEnabler(activity, airplane); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 104 | mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 105 | mWifiEnabler = new WifiEnabler(activity, wifi); |
| 106 | mBtEnabler = new BluetoothEnabler(activity, bt); |
Nick Pelly | d83aaf2 | 2010-09-29 12:10:35 -0700 | [diff] [blame] | 107 | mNfcEnabler = new NfcEnabler(activity, nfc); |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 108 | |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 109 | String toggleable = Settings.System.getString(activity.getContentResolver(), |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 110 | Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS); |
| 111 | |
Chia-chi Yeh | b90452f | 2010-01-13 06:11:29 +0800 | [diff] [blame] | 112 | // Manually set dependencies for Wifi when not toggleable. |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 113 | if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) { |
| 114 | wifi.setDependency(KEY_TOGGLE_AIRPLANE); |
| 115 | findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE); |
| 116 | findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE); |
| 117 | } |
| 118 | |
| 119 | // Manually set dependencies for Bluetooth when not toggleable. |
| 120 | if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) { |
| 121 | bt.setDependency(KEY_TOGGLE_AIRPLANE); |
| 122 | findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE); |
| 123 | } |
| 124 | |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 125 | // Remove Bluetooth Settings if Bluetooth service is not available. |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 126 | if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) { |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 127 | getPreferenceScreen().removePreference(bt); |
| 128 | } |
| 129 | |
| 130 | // Remove NFC if its not available |
Nick Pelly | b8be6da | 2010-12-13 13:44:10 -0800 | [diff] [blame] | 131 | if (NfcAdapter.getDefaultAdapter(activity) == null) { |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 132 | getPreferenceScreen().removePreference(nfc); |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 133 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 134 | |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 135 | // Remove Mobile Network Settings if it's a wifi-only device. |
| 136 | if (Utils.isWifiOnly()) { |
| 137 | getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS)); |
| 138 | } |
| 139 | |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 140 | // Enable Proxy selector settings if allowed. |
| 141 | Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS); |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 142 | DevicePolicyManager mDPM = (DevicePolicyManager) |
| 143 | activity.getSystemService(Context.DEVICE_POLICY_SERVICE); |
Robert Greenwalt | 6f3a98b | 2010-12-28 16:11:12 -0800 | [diff] [blame] | 144 | // proxy UI disabled until we have better app support |
| 145 | getPreferenceScreen().removePreference(mGlobalProxy); |
Oscar Montemayor | 0541189 | 2010-08-03 16:56:09 -0700 | [diff] [blame] | 146 | mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null); |
| 147 | |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 148 | // Disable Tethering if it's not allowed or if it's a wifi-only device |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 149 | ConnectivityManager cm = |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 150 | (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); |
Amith Yamasani | 0f85c48 | 2011-02-23 17:19:11 -0800 | [diff] [blame] | 151 | if (!cm.isTetheringSupported() || Utils.isWifiOnly()) { |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 152 | getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS)); |
Robert Greenwalt | e434bfb | 2010-05-08 15:20:24 -0700 | [diff] [blame] | 153 | } else { |
| 154 | String[] usbRegexs = cm.getTetherableUsbRegexs(); |
| 155 | String[] wifiRegexs = cm.getTetherableWifiRegexs(); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 156 | String[] bluetoothRegexs = cm.getTetherableBluetoothRegexs(); |
| 157 | |
| 158 | boolean usbAvailable = usbRegexs.length != 0; |
| 159 | boolean wifiAvailable = wifiRegexs.length != 0; |
| 160 | boolean bluetoothAvailable = bluetoothRegexs.length != 0; |
| 161 | |
Robert Greenwalt | e434bfb | 2010-05-08 15:20:24 -0700 | [diff] [blame] | 162 | Preference p = findPreference(KEY_TETHER_SETTINGS); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 163 | if (wifiAvailable && usbAvailable && bluetoothAvailable) { |
| 164 | p.setTitle(R.string.tether_settings_title_all); |
| 165 | p.setSummary(R.string.tether_settings_summary_all); |
| 166 | } else if (wifiAvailable && usbAvailable) { |
| 167 | p.setTitle(R.string.tether_settings_title_all); |
| 168 | p.setSummary(R.string.tether_settings_summary_usb_wifi); |
| 169 | } else if (wifiAvailable && bluetoothAvailable) { |
| 170 | p.setTitle(R.string.tether_settings_title_all); |
| 171 | p.setSummary(R.string.tether_settings_summary_wifi_bluetooth); |
| 172 | } else if (wifiAvailable) { |
| 173 | p.setTitle(R.string.tether_settings_title_wifi); |
| 174 | p.setSummary(R.string.tether_settings_summary_wifi); |
| 175 | } else if (usbAvailable && bluetoothAvailable) { |
| 176 | p.setTitle(R.string.tether_settings_title_usb_bluetooth); |
| 177 | p.setSummary(R.string.tether_settings_summary_usb_bluetooth); |
| 178 | } else if (usbAvailable) { |
Robert Greenwalt | e434bfb | 2010-05-08 15:20:24 -0700 | [diff] [blame] | 179 | p.setTitle(R.string.tether_settings_title_usb); |
| 180 | p.setSummary(R.string.tether_settings_summary_usb); |
| 181 | } else { |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 182 | p.setTitle(R.string.tether_settings_title_bluetooth); |
| 183 | p.setSummary(R.string.tether_settings_summary_bluetooth); |
Robert Greenwalt | e434bfb | 2010-05-08 15:20:24 -0700 | [diff] [blame] | 184 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 185 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 186 | } |
Robert Greenwalt | c4764d2 | 2010-02-12 14:21:37 -0800 | [diff] [blame] | 187 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 188 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 189 | public void onResume() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 190 | super.onResume(); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 191 | |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 192 | mAirplaneModeEnabler.resume(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 193 | mWifiEnabler.resume(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 194 | mBtEnabler.resume(); |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 195 | mNfcEnabler.resume(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 196 | } |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 197 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 198 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 199 | public void onPause() { |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 200 | super.onPause(); |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 201 | |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 202 | mAirplaneModeEnabler.pause(); |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 203 | mWifiEnabler.pause(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 204 | mBtEnabler.pause(); |
Nick Pelly | ad50ba0 | 2010-09-22 10:55:13 -0700 | [diff] [blame] | 205 | mNfcEnabler.pause(); |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 206 | } |
Danica Chang | 32711b6 | 2010-08-10 18:41:29 -0700 | [diff] [blame] | 207 | |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 208 | @Override |
Amith Yamasani | d799347 | 2010-08-18 13:59:28 -0700 | [diff] [blame] | 209 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
Chia-chi Yeh | 4e14211 | 2009-12-25 14:48:46 +0800 | [diff] [blame] | 210 | if (requestCode == REQUEST_CODE_EXIT_ECM) { |
| 211 | Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 212 | // Set Airplane mode based on the return value and checkbox state |
| 213 | mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes, |
| 214 | mAirplaneModePreference.isChecked()); |
Chouting Zhang | 71cc49e | 2009-08-28 14:36:35 -0500 | [diff] [blame] | 215 | } |
| 216 | } |
The Android Open Source Project | afc4ab2 | 2009-03-03 19:32:34 -0800 | [diff] [blame] | 217 | } |