OSDN Git Service

am 57c88834: (-s ours) am 9fe36302: (-s ours) am 9cb97c44: Merge "DO NOT MERGE Fix...
[android-x86/packages-apps-Settings.git] / src / com / android / settings / WirelessSettings.java
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
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
19 import android.app.Activity;
20 import android.app.admin.DevicePolicyManager;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.ConnectivityManager;
24 import android.net.wifi.p2p.WifiP2pManager;
25 import android.nfc.NfcAdapter;
26 import android.os.Bundle;
27 import android.os.SystemProperties;
28 import android.preference.CheckBoxPreference;
29 import android.preference.Preference;
30 import android.preference.PreferenceScreen;
31 import android.provider.Settings;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.widget.Switch;
35
36 import com.android.internal.telephony.TelephonyIntents;
37 import com.android.internal.telephony.TelephonyProperties;
38 import com.android.settings.nfc.NfcEnabler;
39
40 public class WirelessSettings extends SettingsPreferenceFragment {
41
42     private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
43     private static final String KEY_TOGGLE_NFC = "toggle_nfc";
44     private static final String KEY_NDEF_PUSH_SETTINGS = "ndef_push_settings";
45     private static final String KEY_VPN_SETTINGS = "vpn_settings";
46     private static final String KEY_WIFI_P2P_SETTINGS = "wifi_p2p_settings";
47     private static final String KEY_TETHER_SETTINGS = "tether_settings";
48     private static final String KEY_PROXY_SETTINGS = "proxy_settings";
49     private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_settings";
50
51     private static final Boolean WIFI_P2P_DEBUG = false;
52
53     public static final String EXIT_ECM_RESULT = "exit_ecm_result";
54     public static final int REQUEST_CODE_EXIT_ECM = 1;
55
56     private AirplaneModeEnabler mAirplaneModeEnabler;
57     private CheckBoxPreference mAirplaneModePreference;
58     private NfcEnabler mNfcEnabler;
59     private NfcAdapter mNfcAdapter;
60
61     /**
62      * Invoked on each preference click in this hierarchy, overrides
63      * PreferenceActivity's implementation.  Used to make sure we track the
64      * preference click events.
65      */
66     @Override
67     public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
68         if (preference == mAirplaneModePreference && Boolean.parseBoolean(
69                 SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
70             // In ECM mode launch ECM app dialog
71             startActivityForResult(
72                 new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
73                 REQUEST_CODE_EXIT_ECM);
74             return true;
75         }
76         // Let the intents be launched by the Preference manager
77         return super.onPreferenceTreeClick(preferenceScreen, preference);
78     }
79
80     public static boolean isRadioAllowed(Context context, String type) {
81         if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
82             return true;
83         }
84         // Here we use the same logic in onCreate().
85         String toggleable = Settings.System.getString(context.getContentResolver(),
86                 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
87         return toggleable != null && toggleable.contains(type);
88     }
89
90     @Override
91     public void onCreate(Bundle savedInstanceState) {
92         super.onCreate(savedInstanceState);
93
94         addPreferencesFromResource(R.xml.wireless_settings);
95
96         final Activity activity = getActivity();
97         mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
98         CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
99         PreferenceScreen ndefPush = (PreferenceScreen) findPreference(KEY_NDEF_PUSH_SETTINGS);
100
101         mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
102         mNfcEnabler = new NfcEnabler(activity, nfc, ndefPush);
103
104         String toggleable = Settings.System.getString(activity.getContentResolver(),
105                 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
106
107         // Manually set dependencies for Wifi when not toggleable.
108         if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
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             // No bluetooth-dependent items in the list. Code kept in case one is added later.
115         }
116
117         // Manually set dependencies for NFC when not toggleable.
118         if (toggleable == null || !toggleable.contains(Settings.System.RADIO_NFC)) {
119             findPreference(KEY_TOGGLE_NFC).setDependency(KEY_TOGGLE_AIRPLANE);
120             findPreference(KEY_NDEF_PUSH_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
121         }
122
123         // Remove NFC if its not available
124         mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
125         if (mNfcAdapter == null) {
126             getPreferenceScreen().removePreference(nfc);
127             getPreferenceScreen().removePreference(ndefPush);
128             mNfcEnabler = null;
129         }
130
131         // Remove Mobile Network Settings if it's a wifi-only device.
132         if (Utils.isWifiOnly()) {
133             getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
134         }
135
136         if (!WIFI_P2P_DEBUG) {
137             getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS));
138         }
139
140         // Enable Proxy selector settings if allowed.
141         Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
142         DevicePolicyManager mDPM = (DevicePolicyManager)
143                 activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
144         // proxy UI disabled until we have better app support
145         getPreferenceScreen().removePreference(mGlobalProxy);
146         mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
147
148         // Disable Tethering if it's not allowed or if it's a wifi-only device
149         ConnectivityManager cm =
150                 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
151         if (!cm.isTetheringSupported()) {
152             getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
153         } else {
154             String[] usbRegexs = cm.getTetherableUsbRegexs();
155             String[] wifiRegexs = cm.getTetherableWifiRegexs();
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
162             Preference p = findPreference(KEY_TETHER_SETTINGS);
163             if (wifiAvailable && usbAvailable && bluetoothAvailable) {
164                 p.setTitle(R.string.tether_settings_title_all);
165             } else if (wifiAvailable && usbAvailable) {
166                 p.setTitle(R.string.tether_settings_title_all);
167             } else if (wifiAvailable && bluetoothAvailable) {
168                 p.setTitle(R.string.tether_settings_title_all);
169             } else if (wifiAvailable) {
170                 p.setTitle(R.string.tether_settings_title_wifi);
171             } else if (usbAvailable && bluetoothAvailable) {
172                 p.setTitle(R.string.tether_settings_title_usb_bluetooth);
173             } else if (usbAvailable) {
174                 p.setTitle(R.string.tether_settings_title_usb);
175             } else {
176                 p.setTitle(R.string.tether_settings_title_bluetooth);
177             }
178         }
179     }
180
181     @Override
182     public void onResume() {
183         super.onResume();
184
185         mAirplaneModeEnabler.resume();
186         if (mNfcEnabler != null) {
187             mNfcEnabler.resume();
188         }
189     }
190
191     @Override
192     public void onPause() {
193         super.onPause();
194
195         mAirplaneModeEnabler.pause();
196         if (mNfcEnabler != null) {
197             mNfcEnabler.pause();
198         }
199     }
200
201     @Override
202     public void onActivityResult(int requestCode, int resultCode, Intent data) {
203         if (requestCode == REQUEST_CODE_EXIT_ECM) {
204             Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
205             // Set Airplane mode based on the return value and checkbox state
206             mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
207                     mAirplaneModePreference.isChecked());
208         }
209     }
210 }