blob: 6db2001f6eee0bd1e394782ced6690a5708cc42c [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.net.wifi.WifiManager;
24import 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;
Robert Greenwaltc4764d22010-02-12 14:21:37 -080032import android.util.Log;
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080033
Chouting Zhang71cc49e2009-08-28 14:36:35 -050034import com.android.internal.telephony.TelephonyIntents;
35import com.android.internal.telephony.TelephonyProperties;
Michael Chan0cb37432009-10-29 14:42:06 -070036import com.android.settings.bluetooth.BluetoothEnabler;
37import com.android.settings.wifi.WifiEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050038
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080039public class WirelessSettings extends PreferenceActivity {
40
41 private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
42 private static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth";
43 private static final String KEY_TOGGLE_WIFI = "toggle_wifi";
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";
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;
54 private BluetoothEnabler mBtEnabler;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050055
56 /**
57 * Invoked on each preference click in this hierarchy, overrides
58 * PreferenceActivity's implementation. Used to make sure we track the
59 * preference click events.
60 */
61 @Override
62 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +080063 if (preference == mAirplaneModePreference && Boolean.parseBoolean(
64 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
Chouting Zhang71cc49e2009-08-28 14:36:35 -050065 // In ECM mode launch ECM app dialog
66 startActivityForResult(
67 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
68 REQUEST_CODE_EXIT_ECM);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050069 return true;
70 }
Chia-chi Yeh4e142112009-12-25 14:48:46 +080071 // Let the intents be launched by the Preference manager
72 return false;
Chouting Zhang71cc49e2009-08-28 14:36:35 -050073 }
Chia-chi Yehb90452f2010-01-13 06:11:29 +080074
75 public static boolean isRadioAllowed(Context context, String type) {
76 if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
77 return true;
78 }
79 // Here we use the same logic in onCreate().
80 String toggleable = Settings.System.getString(context.getContentResolver(),
81 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
82 return toggleable != null && toggleable.contains(type);
83 }
84
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -080085 @Override
86 protected void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88
89 addPreferencesFromResource(R.xml.wireless_settings);
90
Chia-chi Yeh4e142112009-12-25 14:48:46 +080091 CheckBoxPreference airplane = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
92 CheckBoxPreference wifi = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI);
93 CheckBoxPreference bt = (CheckBoxPreference) findPreference(KEY_TOGGLE_BLUETOOTH);
94
95 mAirplaneModeEnabler = new AirplaneModeEnabler(this, airplane);
Chouting Zhang71cc49e2009-08-28 14:36:35 -050096 mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
Chia-chi Yeh4e142112009-12-25 14:48:46 +080097 mWifiEnabler = new WifiEnabler(this, wifi);
98 mBtEnabler = new BluetoothEnabler(this, bt);
99
100 String toggleable = Settings.System.getString(getContentResolver(),
101 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
102
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800103 // Manually set dependencies for Wifi when not toggleable.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800104 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
105 wifi.setDependency(KEY_TOGGLE_AIRPLANE);
106 findPreference(KEY_WIFI_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
107 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
108 }
109
110 // Manually set dependencies for Bluetooth when not toggleable.
111 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
112 bt.setDependency(KEY_TOGGLE_AIRPLANE);
113 findPreference(KEY_BT_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
114 }
115
Chia-chi Yehb90452f2010-01-13 06:11:29 +0800116 // Disable Bluetooth Settings if Bluetooth service is not available.
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800117 if (ServiceManager.getService(BluetoothAdapter.BLUETOOTH_SERVICE) == null) {
118 findPreference(KEY_BT_SETTINGS).setEnabled(false);
119 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800120
121 // Disable Tethering if it's not allowed
122 ConnectivityManager cm =
123 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
124 if (!cm.isTetheringSupported()) {
125 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
126 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800127 }
Robert Greenwaltc4764d22010-02-12 14:21:37 -0800128
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800129 @Override
130 protected void onResume() {
131 super.onResume();
132
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800133 mAirplaneModeEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800134 mWifiEnabler.resume();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800135 mBtEnabler.resume();
136 }
137
138 @Override
139 protected void onPause() {
140 super.onPause();
141
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800142 mAirplaneModeEnabler.pause();
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800143 mWifiEnabler.pause();
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800144 mBtEnabler.pause();
145 }
146
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500147 @Override
148 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Chia-chi Yeh4e142112009-12-25 14:48:46 +0800149 if (requestCode == REQUEST_CODE_EXIT_ECM) {
150 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500151 // Set Airplane mode based on the return value and checkbox state
152 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
153 mAirplaneModePreference.isChecked());
Chouting Zhang71cc49e2009-08-28 14:36:35 -0500154 }
155 }
The Android Open Source Projectafc4ab22009-03-03 19:32:34 -0800156}