blob: 4e60dbac571b557e46634d44043ffde5684fcb43 [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;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080023import android.os.Bundle;
Michael Chan0cb37432009-10-29 14:42:06 -070024import android.os.ServiceManager;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050025import android.os.SystemProperties;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080026import android.preference.CheckBoxPreference;
Mike Lockwood83bcc982009-07-29 23:25:10 -070027import android.preference.Preference;
28import android.preference.PreferenceActivity;
Michael Chan0cb37432009-10-29 14:42:06 -070029import android.preference.PreferenceScreen;
Mike Lockwood83bcc982009-07-29 23:25:10 -070030import android.provider.Settings;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050031import com.android.internal.telephony.TelephonyIntents;
32import com.android.internal.telephony.TelephonyProperties;
Michael Chan0cb37432009-10-29 14:42:06 -070033import com.android.settings.bluetooth.BluetoothEnabler;
34import com.android.settings.wifi.WifiEnabler;
Nick Pellyad50ba02010-09-22 10:55:13 -070035import com.android.settings.nfc.NfcEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050036
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080037public class WirelessSettings extends PreferenceActivity {
38
39 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
40 private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
41 private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
Nick Pellyad50ba02010-09-22 10:55:13 -070042 private static final String KEY_TOGGLE_NFC = "toggle_nfc";
Mike Lockwood83bcc982009-07-29 23:25:10 -070043 private static final String KEY_WIFI_SETTINGS = "wifi_settings";
Michael Chan0cb37432009-10-29 14:42:06 -070044 private static final String KEY_BT_SETTINGS = "bt_settings";
Mike Lockwood83bcc982009-07-29 23:25:10 -070045 private static final String KEY_VPN_SETTINGS = "vpn_settings";
Robert Greenwaltc4764d22010-02-12 14:21:37 -080046 private static final String KEY_TETHER_SETTINGS = "tether_settings";
Nick Pellyad50ba02010-09-22 10:55:13 -070047
Chouting Zhang71cc49e2009-08-28 14:36:35 -050048 public static final String EXIT_ECM_RESULT = "exit_ecm_result";
49 public static final int REQUEST_CODE_EXIT_ECM = 1;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080050
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080051 private AirplaneModeEnabler mAirplaneModeEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050052 private CheckBoxPreference mAirplaneModePreference;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080053 private WifiEnabler mWifiEnabler;
Nick Pellyad50ba02010-09-22 10:55:13 -070054 private NfcEnabler mNfcEnabler;
Chia-chi Yeh4e142112009-12-25 14:48:46 +080055 private BluetoothEnabler mBtEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050056
57 /**
58 * Invoked on each preference click in this hierarchy, overrides
59 * PreferenceActivity's implementation. Used to make sure we track the
60 * preference click events.
61 */
62 @Override
63 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080064 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
65 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050066 // In ECM mode launch ECM app dialog
67 startActivityForResult(
68 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
69 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050070 return true;
71 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080072 // Let the intents be launched by the Preference manager
73 return false;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050074 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080075
76 public static boolean isRadioAllowed(Context context, String type) {
77 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
78 return true;
79 }
80 // Here we use the same logic in onCreate().
81 String toggleable = Settings.System.getString(context.getContentResolver(),
82 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
83 return toggleable != null && toggleable.contains(type);
84 }
85
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080086 @Override
87 protected void onCreate(Bundle savedInstanceState) {
88 super.onCreate(savedInstanceState);
89
90 addPreferencesFromResource(R.xml.wireless_settings);
91
Chia-chi Yeh4e142112009-12-25 14:48:46 +080092 CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
93 CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI);
94 CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
Nick Pellyad50ba02010-09-22 10:55:13 -070095 CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080096
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);
Nick Pellyad50ba02010-09-22 10:55:13 -0700101 mNfcEnabler = new NfcEnabler(this, nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800102
103 String toggleable = Settings.System.getString(getContentResolver(),
104 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
105
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800106 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800107 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
108 wifi.setDependency(KEY_TOGGLE_AIRPLANE);
109 findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
110 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
111 }
112
113 // Manually set dependencies for Bluetooth when not toggleable.
114 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
115 bt.setDependency(KEY_TOGGLE_AIRPLANE);
116 findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
117 }
118
Nick Pellyad50ba02010-09-22 10:55:13 -0700119 // Remove Bluetooth Settings if Bluetooth service is not available.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800120 if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
Nick Pellyad50ba02010-09-22 10:55:13 -0700121 getPreferenceScreen().removePreference(bt);
122 }
123
124 // Remove NFC if its not available
125 if (ServiceManager.getService(Context.NFC_SERVICE) == null) {
126 getPreferenceScreen().removePreference(nfc);
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800127 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800128
129 // Disable Tethering if it's not allowed
130 ConnectivityManager cm =
131 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
132 if (!cm.isTetheringSupported()) {
133 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();
137 Preference p = findPreference(KEY_TETHER_SETTINGS);
138 if (wifiRegexs.length == 0) {
139 p.setTitle(R.string.tether_settings_title_usb);
140 p.setSummary(R.string.tether_settings_summary_usb);
141 } else {
142 if (usbRegexs.length == 0) {
143 p.setTitle(R.string.tether_settings_title_wifi);
144 p.setSummary(R.string.tether_settings_summary_wifi);
145 } else {
146 p.setTitle(R.string.tether_settings_title_both);
147 p.setSummary(R.string.tether_settings_summary_both);
148 }
149 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800150 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800151 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800152
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800153 @Override
154 protected void onResume() {
155 super.onResume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700156
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800157 mAirplaneModeEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800158 mWifiEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800159 mBtEnabler.resume();
Nick Pellyad50ba02010-09-22 10:55:13 -0700160 mNfcEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800161 }
Nick Pellyad50ba02010-09-22 10:55:13 -0700162
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800163 @Override
164 protected void onPause() {
165 super.onPause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700166
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800167 mAirplaneModeEnabler.pause();
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800168 mWifiEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800169 mBtEnabler.pause();
Nick Pellyad50ba02010-09-22 10:55:13 -0700170 mNfcEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800171 }
Nick Pellyad50ba02010-09-22 10:55:13 -0700172
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500173 @Override
174 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800175 if (requestCode == REQUEST_CODE_EXIT_ECM) {
176 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500177 // Set Airplane mode based on the return value and checkbox state
178 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
179 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500180 }
181 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800182}