blob: 85cd54669ec5b5e73f9af75728ec6bd8bd02abaa [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
Michael Chan0cb37432009-10-29 14:42:06 -070019import android.bluetooth.BluetoothAdapter;
Chia-chi Yehb90452f2010-01-13 06:11:29 +080020import android.content.Context;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050021import android.content.Intent;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080022import android.net.ConnectivityManager;
Nick Pellya57eace2010-10-15 01:19:43 -070023import android.nfc.NfcAdapter;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080024import android.os.Bundle;
Michael Chan0cb37432009-10-29 14:42:06 -070025import android.os.ServiceManager;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050026import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080027import android.preference.CheckBoxPreference;
Mike Lockwood83bcc982009-07-29 23:25:10 -070028import android.preference.Preference;
29import android.preference.PreferenceActivity;
Michael Chan0cb37432009-10-29 14:42:06 -070030import android.preference.PreferenceScreen;
Mike Lockwood83bcc982009-07-29 23:25:10 -070031import android.provider.Settings;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050032import com.android.internal.telephony.TelephonyIntents;
33import com.android.internal.telephony.TelephonyProperties;
Michael Chan0cb37432009-10-29 14:42:06 -070034import com.android.settings.bluetooth.BluetoothEnabler;
35import com.android.settings.wifi.WifiEnabler;
Nick Pellyad50ba02010-09-22 10:55:13 -070036import com.android.settings.nfc.NfcEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050037
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080038public class WirelessSettings extends PreferenceActivity {
39
40 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
41 private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
42 private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
Nick Pellyad50ba02010-09-22 10:55:13 -070043 private static final String KEY_TOGGLE_NFC = "toggle_nfc";
Mike Lockwood83bcc982009-07-29 23:25:10 -070044 private static final String KEY_WIFI_SETTINGS = "wifi_settings";
Michael Chan0cb37432009-10-29 14:42:06 -070045 private static final String KEY_BT_SETTINGS = "bt_settings";
Mike Lockwood83bcc982009-07-29 23:25:10 -070046 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080047 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Nick Pellyad50ba02010-09-22 10:55:13 -070048
Chouting Zhang71cc49e2009-08-28 14:36:35 -050049 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
50 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080051
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080052 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050053 private CheckBoxPreference mAirplaneModePreference;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080054 private WifiEnabler mWifiEnabler;
Nick Pellyad50ba02010-09-22 10:55:13 -070055 private NfcEnabler mNfcEnabler;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080056 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);
Nick Pellyad50ba02010-09-22 10:55:13 -070096 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080097
98 mAirplaneModeEnabler = new AirplaneModeEnabler(this, airplane);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050099 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800100 mWifiEnabler = new WifiEnabler(this, wifi);
101 mBtEnabler = new BluetoothEnabler(this, bt);
Nick Pellyad50ba02010-09-22 10:55:13 -0700102 mNfcEnabler = new NfcEnabler(this, nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800103
104 String toggleable = Settings.System.getString(getContentResolver(),
105 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
106
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800107 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800108 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
109 wifi.setDependency(KEY_TOGGLE_AIRPLANE);
110 findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
111 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
112 }
113
114 // Manually set dependencies for Bluetooth when not toggleable.
115 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
116 bt.setDependency(KEY_TOGGLE_AIRPLANE);
117 findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
118 }
119
Nick Pellyad50ba02010-09-22 10:55:13 -0700120 // Remove Bluetooth Settings if Bluetooth service is not available.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800121 if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700122 getPreferenceScreen().removePreference(bt);
123 }
124
125 // Remove NFC if its not available
Nick Pellya57eace2010-10-15 01:19:43 -0700126 if (NfcAdapter.getDefaultAdapter() == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700127 getPreferenceScreen().removePreference(nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800128 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800129
130 // Disable Tethering if it's not allowed
131 ConnectivityManager cm =
132 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
133 if (!cm.isTetheringSupported()) {
134 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
Robert Greenwalte434bfb2010-05-08 15:20:24 -0700135 } else {
136 String[] usbRegexs = cm.getTetherableUsbRegexs();
137 String[] wifiRegexs = cm.getTetherableWifiRegexs();
138 Preference p = findPreference(KEY_TETHER_SETTINGS);
139 if (wifiRegexs.length == 0) {
140 p.setTitle(R.string.tether_settings_title_usb);
141 p.setSummary(R.string.tether_settings_summary_usb);
142 } else {
143 if (usbRegexs.length == 0) {
144 p.setTitle(R.string.tether_settings_title_wifi);
145 p.setSummary(R.string.tether_settings_summary_wifi);
146 } else {
147 p.setTitle(R.string.tether_settings_title_both);
148 p.setSummary(R.string.tether_settings_summary_both);
149 }
150 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800151 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800152 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800153
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800154 @Override
155 protected void onResume() {
156 super.onResume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700157
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800158 mAirplaneModeEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800159 mWifiEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800160 mBtEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700161 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800162 }
Nick Pellyad50ba02010-09-22 10:55:13 -0700163
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800164 @Override
165 protected void onPause() {
166 super.onPause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700167
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800168 mAirplaneModeEnabler.pause();
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800169 mWifiEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800170 mBtEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700171 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800172 }
Nick Pellyad50ba02010-09-22 10:55:13 -0700173
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500174 @Override
175 protected 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}