1/* 2 * Copyright (C) 2010 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.wifi; 18 19import android.content.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22import android.content.IntentFilter; 23import android.net.ConnectivityManager; 24import android.net.NetworkInfo; 25import android.net.wifi.SupplicantState; 26import android.net.wifi.WifiInfo; 27import android.net.wifi.WifiManager; 28import android.os.UserHandle; 29import android.os.UserManager; 30import android.provider.Settings; 31import android.support.annotation.VisibleForTesting; 32import android.widget.Toast; 33 34import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 35import com.android.settings.R; 36import com.android.settings.widget.SwitchWidgetController; 37import com.android.settingslib.RestrictedLockUtils; 38import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 39import com.android.settingslib.WirelessUtils; 40import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 41 42import java.util.concurrent.atomic.AtomicBoolean; 43 44public class WifiEnabler implements SwitchWidgetController.OnSwitchChangeListener { 45 46 private final SwitchWidgetController mSwitchWidget; 47 private final WifiManager mWifiManager; 48 private final ConnectivityManager mConnectivityManager; 49 private final MetricsFeatureProvider mMetricsFeatureProvider; 50 51 private Context mContext; 52 private boolean mListeningToOnSwitchChange = false; 53 private AtomicBoolean mConnected = new AtomicBoolean(false); 54 55 56 private boolean mStateMachineEvent; 57 private final IntentFilter mIntentFilter; 58 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 59 @Override 60 public void onReceive(Context context, Intent intent) { 61 String action = intent.getAction(); 62 if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { 63 handleWifiStateChanged(mWifiManager.getWifiState()); 64 } else if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) { 65 if (!mConnected.get()) { 66 handleStateChanged(WifiInfo.getDetailedStateOf((SupplicantState) 67 intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE))); 68 } 69 } else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) { 70 NetworkInfo info = (NetworkInfo) intent.getParcelableExtra( 71 WifiManager.EXTRA_NETWORK_INFO); 72 mConnected.set(info.isConnected()); 73 handleStateChanged(info.getDetailedState()); 74 } 75 } 76 }; 77 78 private static final String EVENT_DATA_IS_WIFI_ON = "is_wifi_on"; 79 private static final int EVENT_UPDATE_INDEX = 0; 80 81 public WifiEnabler(Context context, SwitchWidgetController switchWidget, 82 MetricsFeatureProvider metricsFeatureProvider) { 83 this(context, switchWidget, metricsFeatureProvider, 84 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); 85 } 86 87 @VisibleForTesting 88 WifiEnabler(Context context, SwitchWidgetController switchWidget, 89 MetricsFeatureProvider metricsFeatureProvider, 90 ConnectivityManager connectivityManager) { 91 mContext = context; 92 mSwitchWidget = switchWidget; 93 mSwitchWidget.setListener(this); 94 mMetricsFeatureProvider = metricsFeatureProvider; 95 mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 96 mConnectivityManager = connectivityManager; 97 98 mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION); 99 // The order matters! We really should not depend on this. :( 100 mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 101 mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 102 103 setupSwitchController(); 104 } 105 106 public void setupSwitchController() { 107 final int state = mWifiManager.getWifiState(); 108 handleWifiStateChanged(state); 109 if (!mListeningToOnSwitchChange) { 110 mSwitchWidget.startListening(); 111 mListeningToOnSwitchChange = true; 112 } 113 mSwitchWidget.setupView(); 114 } 115 116 public void teardownSwitchController() { 117 if (mListeningToOnSwitchChange) { 118 mSwitchWidget.stopListening(); 119 mListeningToOnSwitchChange = false; 120 } 121 mSwitchWidget.teardownView(); 122 } 123 124 public void resume(Context context) { 125 mContext = context; 126 // Wi-Fi state is sticky, so just let the receiver update UI 127 mContext.registerReceiver(mReceiver, mIntentFilter); 128 if (!mListeningToOnSwitchChange) { 129 mSwitchWidget.startListening(); 130 mListeningToOnSwitchChange = true; 131 } 132 } 133 134 public void pause() { 135 mContext.unregisterReceiver(mReceiver); 136 if (mListeningToOnSwitchChange) { 137 mSwitchWidget.stopListening(); 138 mListeningToOnSwitchChange = false; 139 } 140 } 141 142 private void handleWifiStateChanged(int state) { 143 // Clear any previous state 144 mSwitchWidget.setDisabledByAdmin(null); 145 146 switch (state) { 147 case WifiManager.WIFI_STATE_ENABLING: 148 break; 149 case WifiManager.WIFI_STATE_ENABLED: 150 setSwitchBarChecked(true); 151 mSwitchWidget.setEnabled(true); 152 break; 153 case WifiManager.WIFI_STATE_DISABLING: 154 break; 155 case WifiManager.WIFI_STATE_DISABLED: 156 setSwitchBarChecked(false); 157 mSwitchWidget.setEnabled(true); 158 break; 159 default: 160 setSwitchBarChecked(false); 161 mSwitchWidget.setEnabled(true); 162 } 163 164 if (RestrictedLockUtils.hasBaseUserRestriction(mContext, 165 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId())) { 166 mSwitchWidget.setEnabled(false); 167 } else { 168 final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(mContext, 169 UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.myUserId()); 170 mSwitchWidget.setDisabledByAdmin(admin); 171 } 172 } 173 174 private void setSwitchBarChecked(boolean checked) { 175 mStateMachineEvent = true; 176 mSwitchWidget.setChecked(checked); 177 mStateMachineEvent = false; 178 } 179 180 private void handleStateChanged(@SuppressWarnings("unused") NetworkInfo.DetailedState state) { 181 // After the refactoring from a CheckBoxPreference to a Switch, this method is useless since 182 // there is nowhere to display a summary. 183 // This code is kept in case a future change re-introduces an associated text. 184 /* 185 // WifiInfo is valid if and only if Wi-Fi is enabled. 186 // Here we use the state of the switch as an optimization. 187 if (state != null && mSwitch.isChecked()) { 188 WifiInfo info = mWifiManager.getConnectionInfo(); 189 if (info != null) { 190 //setSummary(Summary.get(mContext, info.getSSID(), state)); 191 } 192 } 193 */ 194 } 195 196 @Override 197 public boolean onSwitchToggled(boolean isChecked) { 198 //Do nothing if called as a result of a state machine event 199 if (mStateMachineEvent) { 200 return true; 201 } 202 // Show toast message if Wi-Fi is not allowed in airplane mode 203 if (isChecked && !WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) { 204 Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show(); 205 // Reset switch to off. No infinite check/listener loop. 206 mSwitchWidget.setChecked(false); 207 return false; 208 } 209 210 if (isChecked) { 211 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_WIFI_ON); 212 } else { 213 // Log if user was connected at the time of switching off. 214 mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_WIFI_OFF, 215 mConnected.get()); 216 } 217 if (!mWifiManager.setWifiEnabled(isChecked)) { 218 // Error 219 mSwitchWidget.setEnabled(true); 220 Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show(); 221 } 222 return true; 223 } 224} 225