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.wifi.p2p.WifiP2pEnabler; 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_TOGGLE_WIFI_P2P = "toggle_wifi_p2p"; 50 private static final String KEY_WIFI_P2P_SETTINGS = "wifi_p2p_settings"; 51 private static final String KEY_TETHER_SETTINGS = "tether_settings"; 52 private static final String KEY_PROXY_SETTINGS = "proxy_settings"; 53 private static final String KEY_MOBILE_NETWORK_SETTINGS = "mobile_network_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 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 androidBeam = (PreferenceScreen) findPreference(KEY_ANDROID_BEAM_SETTINGS); 104 105 CheckBoxPreference wifiP2p = (CheckBoxPreference) findPreference(KEY_TOGGLE_WIFI_P2P); 106 107 mAirplaneModeEnabler = new AirplaneModeEnabler(activity, mAirplaneModePreference); 108 mNfcEnabler = new NfcEnabler(activity, nfc, androidBeam); 109 110 String toggleable = Settings.System.getString(activity.getContentResolver(), 111 Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS); 112 113 //enable/disable wimax depending on the value in config.xml 114 boolean isWimaxEnabled = this.getResources().getBoolean( 115 com.android.internal.R.bool.config_wimaxEnabled); 116 if (!isWimaxEnabled) { 117 PreferenceScreen root = getPreferenceScreen(); 118 Preference ps = (Preference) findPreference(KEY_WIMAX_SETTINGS); 119 if (ps != null) root.removePreference(ps); 120 } else { 121 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIMAX ) 122 && isWimaxEnabled) { 123 Preference ps = (Preference) findPreference(KEY_WIMAX_SETTINGS); 124 ps.setDependency(KEY_TOGGLE_AIRPLANE); 125 } 126 } 127 // Manually set dependencies for Wifi when not toggleable. 128 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_WIFI)) { 129 findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE); 130 } 131 132 // Manually set dependencies for Bluetooth when not toggleable. 133 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_BLUETOOTH)) { 134 // No bluetooth-dependent items in the list. Code kept in case one is added later. 135 } 136 137 // Manually set dependencies for NFC when not toggleable. 138 if (toggleable == null || !toggleable.contains(Settings.System.RADIO_NFC)) { 139 findPreference(KEY_TOGGLE_NFC).setDependency(KEY_TOGGLE_AIRPLANE); 140 findPreference(KEY_ANDROID_BEAM_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE); 141 } 142 143 // Remove NFC if its not available 144 mNfcAdapter = NfcAdapter.getDefaultAdapter(activity); 145 if (mNfcAdapter == null) { 146 getPreferenceScreen().removePreference(nfc); 147 getPreferenceScreen().removePreference(androidBeam); 148 mNfcEnabler = null; 149 } 150 151 // Remove Mobile Network Settings if it's a wifi-only device. 152 if (Utils.isWifiOnly(getActivity())) { 153 getPreferenceScreen().removePreference(findPreference(KEY_MOBILE_NETWORK_SETTINGS)); 154 } 155 156 WifiP2pManager p2p = (WifiP2pManager) activity.getSystemService(Context.WIFI_P2P_SERVICE); 157 158 if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT)) { 159 getPreferenceScreen().removePreference(wifiP2p); 160 } else { 161 mWifiP2pEnabler = new WifiP2pEnabler(activity, wifiP2p); 162 } 163 getPreferenceScreen().removePreference(findPreference(KEY_WIFI_P2P_SETTINGS)); 164 165 // Enable Proxy selector settings if allowed. 166 Preference mGlobalProxy = findPreference(KEY_PROXY_SETTINGS); 167 DevicePolicyManager mDPM = (DevicePolicyManager) 168 activity.getSystemService(Context.DEVICE_POLICY_SERVICE); 169 // proxy UI disabled until we have better app support 170 getPreferenceScreen().removePreference(mGlobalProxy); 171 mGlobalProxy.setEnabled(mDPM.getGlobalProxyAdmin() == null); 172 173 // Disable Tethering if it's not allowed or if it's a wifi-only device 174 ConnectivityManager cm = 175 (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); 176 if (!cm.isTetheringSupported()) { 177 getPreferenceScreen().removePreference(findPreference(KEY_TETHER_SETTINGS)); 178 } else { 179 Preference p = findPreference(KEY_TETHER_SETTINGS); 180 p.setTitle(Utils.getTetheringLabel(cm)); 181 } 182 } 183 184 @Override 185 public void onResume() { 186 super.onResume(); 187 188 mAirplaneModeEnabler.resume(); 189 if (mNfcEnabler != null) { 190 mNfcEnabler.resume(); 191 } 192 193 if (mWifiP2pEnabler != null) { 194 mWifiP2pEnabler.resume(); 195 } 196 } 197 198 @Override 199 public void onPause() { 200 super.onPause(); 201 202 mAirplaneModeEnabler.pause(); 203 if (mNfcEnabler != null) { 204 mNfcEnabler.pause(); 205 } 206 207 if (mWifiP2pEnabler != null) { 208 mWifiP2pEnabler.pause(); 209 } 210 } 211 212 @Override 213 public void onActivityResult(int requestCode, int resultCode, Intent data) { 214 if (requestCode == REQUEST_CODE_EXIT_ECM) { 215 Boolean isChoiceYes = data.getBooleanExtra(EXIT_ECM_RESULT, false); 216 // Set Airplane mode based on the return value and checkbox state 217 mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes, 218 mAirplaneModePreference.isChecked()); 219 } 220 } 221 } 222