WirelessSettings.java revision 536c3d7651d0e37b13b5ee655c215207a39dff7d
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.NsdEnabler;
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_WIMAX_SETTINGS = "wimax_settings";
47    private static final String KEY_ANDROID_BEAM_SETTINGS = "android_beam_settings";
48    private static final String KEY_VPN_SETTINGS = "vpn_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    private static final String KEY_TOGGLE_NSD = "toggle_nsd"; //network service discovery
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    private NsdEnabler mNsdEnabler;
62
63    /**
64     * Invoked on each preference click in this hierarchy, overrides
65     * PreferenceActivity's implementation.  Used to make sure we track the
66     * preference click events.
67     */
68    @Override
69    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
70        if (preference == mAirplaneModePreference && Boolean.parseBoolean(
71                SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
72            // In ECM mode launch ECM app dialog
73            startActivityForResult(
74                new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null),
75                REQUEST_CODE_EXIT_ECM);
76            return true;
77        }
78        // Let the intents be launched by the Preference manager
79        return super.onPreferenceTreeClick(preferenceScreen, preference);
80    }
81
82    public static boolean isRadioAllowed(Context context, String type) {
83        if (!AirplaneModeEnabler.isAirplaneModeOn(context)) {
84            return true;
85        }
86        // Here we use the same logic in onCreate().
87        String toggleable = Settings.System.getString(context.getContentResolver(),
88                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
89        return toggleable != null && toggleable.contains(type);
90    }
91
92    @Override
93    public void onCreate(Bundle savedInstanceState) {
94        super.onCreate(savedInstanceState);
95
96        addPreferencesFromResource(R.xml.wireless_settings);
97
98        final Activity activity = getActivity();
99        mAirplaneModePreference = (CheckBoxPreference) findPreference(KEY_TOGGLE_AIRPLANE);
100        CheckBoxPreference nfc = (CheckBoxPreference) findPreference(KEY_TOGGLE_NFC);
101        PreferenceScreen androidBeam = (PreferenceScreen) findPreference(KEY_ANDROID_BEAM_SETTINGS);
102        CheckBoxPreference nsd = (CheckBoxPreference) findPreference(KEY_TOGGLE_NSD);
103
104        mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference);
105        mNfcEnabler = new NfcEnabler(activity, nfc, androidBeam);
106        mNsdEnabler = new NsdEnabler(activity, nsd);
107
108        String toggleable = Settings.System.getString(activity.getContentResolver(),
109                Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS);
110
111        //enable/disable wimax depending on the value in config.xml
112        boolean isWimaxEnabled = this.getResources().getBoolean(
113                com.android.internal.R.bool.config_wimaxEnabled);
114        if (!isWimaxEnabled) {
115            PreferenceScreen root = getPreferenceScreen();
116            Preference ps = (Preference) findPreference(KEY_WIMAX_SETTINGS);
117            if (ps != null) root.removePreference(ps);
118        } else {
119            if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIMAX )
120                    && isWimaxEnabled) {
121                Preference ps = (Preference) findPreference(KEY_WIMAX_SETTINGS);
122                ps.setDependency(KEY_TOGGLE_AIRPLANE);
123            }
124        }
125        // Manually set dependencies for Wifi when not toggleable.
126        if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) {
127            findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
128        }
129
130        // Manually set dependencies for Bluetooth when not toggleable.
131        if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) {
132            // No bluetooth-dependent items in the list. Code kept in case one is added later.
133        }
134
135        // Manually set dependencies for NFC when not toggleable.
136        if (toggleable == null || !toggleable.contains(Settings.System.RADIO_NFC)) {
137            findPreference(KEY_TOGGLE_NFC).setDependency(KEY_TOGGLE_AIRPLANE);
138            findPreference(KEY_ANDROID_BEAM_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
139        }
140
141        // Remove NFC if its not available
142        mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
143        if (mNfcAdapter == null) {
144            getPreferenceScreen().removePreference(nfc);
145            getPreferenceScreen().removePreference(androidBeam);
146            mNfcEnabler = null;
147        }
148
149        // Remove Mobile Network Settings if it's a wifi-only device.
150        if (Utils.isWifiOnly(getActivity())) {
151            getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS));
152        }
153
154        // Enable Proxy selector settings if allowed.
155        Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS);
156        DevicePolicyManager mDPM = (DevicePolicyManager)
157                activity.getSystemService(Context.DEVICE_POLICY_SERVICE);
158        // proxy UI disabled until we have better app support
159        getPreferenceScreen().removePreference(mGlobalProxy);
160        mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null);
161
162        // Disable Tethering if it's not allowed or if it's a wifi-only device
163        ConnectivityManager cm =
164                (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
165        if (!cm.isTetheringSupported()) {
166            getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS));
167        } else {
168            Preference p = findPreference(KEY_TETHER_SETTINGS);
169            p.setTitle(Utils.getTetheringLabel(cm));
170        }
171    }
172
173    @Override
174    public void onResume() {
175        super.onResume();
176
177        mAirplaneModeEnabler.resume();
178        if (mNfcEnabler != null) {
179            mNfcEnabler.resume();
180        }
181        mNsdEnabler.resume();
182    }
183
184    @Override
185    public void onPause() {
186        super.onPause();
187
188        mAirplaneModeEnabler.pause();
189        if (mNfcEnabler != null) {
190            mNfcEnabler.pause();
191        }
192        mNsdEnabler.pause();
193    }
194
195    @Override
196    public void onActivityResult(int requestCode, int resultCode, Intent data) {
197        if (requestCode == REQUEST_CODE_EXIT_ECM) {
198            Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false);
199            // Set Airplane mode based on the return value and checkbox state
200            mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
201                    mAirplaneModePreference.isChecked());
202        }
203    }
204}
205