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