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