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