WirelessSettings.java revision 9627a8ea151c1f27e5e9f34592c7b34fea3101c3
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.os.UserHandle;
30import android.preference.CheckBoxPreference;
31import android.preference.Preference;
32import android.preference.PreferenceScreen;
33import android.provider.Settings;
34import android.view.LayoutInflater;
35import android.view.View;
36import android.widget.Switch;
37
38import com.android.internal.telephony.TelephonyIntents;
39import com.android.internal.telephony.TelephonyProperties;
40import com.android.settings.nfc.NfcEnabler;
41import com.android.settings.NsdEnabler;
42
43public class WirelessSettings extends SettingsPreferenceFragment {
44
45    private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
46    private static final String KEY_TOGGLE_NFC = "toggle_nfc";
47    private static final String KEY_WIMAX_SETTINGS = "wimax_settings";
48    private static final String KEY_ANDROID_BEAM_SETTINGS = "android_beam_settings";
49    private static final String KEY_VPN_SETTINGS = "vpn_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    private static final String KEY_TOGGLE_NSD = "toggle_nsd"; //network service discovery
54    private static final String KEY_CELL_BROADCAST_SETTINGS = "cell_broadcast_settings";
55
56    public static final String EXIT_ECM_RESULT = "exit_ecm_result";
57    public static final int REQUEST_CODE_EXIT_ECM = 1;
58
59    private AirplaneModeEnabler mAirplaneModeEnabler;
60    private CheckBoxPreference mAirplaneModePreference;
61    private NfcEnabler mNfcEnabler;
62    private NfcAdapter mNfcAdapter;
63    private NsdEnabler mNsdEnabler;
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.Global.getString(context.getContentResolver(),
90                Settings.Global.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 boolean isSecondaryUser = UserHandle.myUserId() != UserHandle.USER_OWNER;
101
102        final Activity activity = getActivity();
103        mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
104        CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
105        PreferenceScreen androidBeam = (PreferenceScreen) findPreference(KEY_ANDROID_BEAM_SETTINGS);
106        CheckBoxPreference nsd = (CheckBoxPreference) findPreference(KEY_TOGGLE_NSD);
107
108        mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
109        mNfcEnabler = new NfcEnabler(activity, nfc, androidBeam);
110
111        // Remove NSD checkbox by default
112        getPreferenceScreen().removePreference(nsd);
113        //mNsdEnabler = new NsdEnabler(activity, nsd);
114
115        String toggleable = Settings.Global.getString(activity.getContentResolver(),
116                Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
117
118        //enable/disable wimax depending on the value in config.xml
119        boolean isWimaxEnabled = !isSecondaryUser && this.getResources().getBoolean(
120                com.android.internal.R.bool.config_wimaxEnabled);
121        if (!isWimaxEnabled) {
122            PreferenceScreen root = getPreferenceScreen();
123            Preference ps = (Preference) findPreference(KEY_WIMAX_SETTINGS);
124            if (ps != null) root.removePreference(ps);
125        } else {
126            if (toggleable == null || !toggleable.contains(Settings.Global.RADIO_WIMAX )
127                    && isWimaxEnabled) {
128                Preference ps = (Preference) findPreference(KEY_WIMAX_SETTINGS);
129                ps.setDependency(KEY_TOGGLE_AIRPLANE);
130            }
131        }
132        // Manually set dependencies for Wifi when not toggleable.
133        if (toggleable == null || !toggleable.contains(Settings.Global.RADIO_WIFI)) {
134            findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
135        }
136        if (isSecondaryUser) { // Disable VPN
137            removePreference(KEY_VPN_SETTINGS);
138        }
139
140        // Manually set dependencies for Bluetooth when not toggleable.
141        if (toggleable == null || !toggleable.contains(Settings.Global.RADIO_BLUETOOTH)) {
142            // No bluetooth-dependent items in the list. Code kept in case one is added later.
143        }
144
145        // Manually set dependencies for NFC when not toggleable.
146        if (toggleable == null || !toggleable.contains(Settings.Global.RADIO_NFC)) {
147            findPreference(KEY_TOGGLE_NFC).setDependency(KEY_TOGGLE_AIRPLANE);
148            findPreference(KEY_ANDROID_BEAM_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
149        }
150
151        // Remove NFC if its not available
152        mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
153        if (mNfcAdapter == null) {
154            getPreferenceScreen().removePreference(nfc);
155            getPreferenceScreen().removePreference(androidBeam);
156            mNfcEnabler = null;
157        }
158
159        // Remove Mobile Network Settings if it's a wifi-only device.
160        if (isSecondaryUser || Utils.isWifiOnly(getActivity())) {
161            removePreference(KEY_MOBILE_NETWORK_SETTINGS);
162        }
163
164        // Enable Proxy selector settings if allowed.
165        Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
166        DevicePolicyManager mDPM = (DevicePolicyManager)
167                activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
168        // proxy UI disabled until we have better app support
169        getPreferenceScreen().removePreference(mGlobalProxy);
170        mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
171
172        // Disable Tethering if it's not allowed or if it's a wifi-only device
173        ConnectivityManager cm =
174                (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
175        if (isSecondaryUser || !cm.isTetheringSupported()) {
176            getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
177        } else {
178            Preference p = findPreference(KEY_TETHER_SETTINGS);
179            p.setTitle(Utils.getTetheringLabel(cm));
180        }
181
182        // Enable link to CMAS app settings depending on the value in config.xml.
183        boolean isCellBroadcastAppLinkEnabled = this.getResources().getBoolean(
184                com.android.internal.R.bool.config_cellBroadcastAppLinks);
185        try {
186            if (isCellBroadcastAppLinkEnabled) {
187                PackageManager pm = getPackageManager();
188                if (pm.getApplicationEnabledSetting("com.android.cellbroadcastreceiver")
189                        == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
190                    isCellBroadcastAppLinkEnabled = false;  // CMAS app disabled
191                }
192            }
193        } catch (IllegalArgumentException ignored) {
194            isCellBroadcastAppLinkEnabled = false;  // CMAS app not installed
195        }
196        if (isSecondaryUser || !isCellBroadcastAppLinkEnabled) {
197            PreferenceScreen root = getPreferenceScreen();
198            Preference ps = findPreference(KEY_CELL_BROADCAST_SETTINGS);
199            if (ps != null) root.removePreference(ps);
200        }
201    }
202
203    @Override
204    public void onResume() {
205        super.onResume();
206
207        mAirplaneModeEnabler.resume();
208        if (mNfcEnabler != null) {
209            mNfcEnabler.resume();
210        }
211        if (mNsdEnabler != null) {
212            mNsdEnabler.resume();
213        }
214    }
215
216    @Override
217    public void onPause() {
218        super.onPause();
219
220        mAirplaneModeEnabler.pause();
221        if (mNfcEnabler != null) {
222            mNfcEnabler.pause();
223        }
224        if (mNsdEnabler != null) {
225            mNsdEnabler.pause();
226        }
227    }
228
229    @Override
230    public void onActivityResult(int requestCode, int resultCode, Intent data) {
231        if (requestCode == REQUEST_CODE_EXIT_ECM) {
232            Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
233            // Set Airplane mode based on the return value and checkbox state
234            mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
235                    mAirplaneModePreference.isChecked());
236        }
237    }
238
239    @Override
240    protected int getHelpResource() {
241        return R.string.help_url_more_networks;
242    }
243}
244