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