Fix bug #15890188 wifi toggle on-off switch missing after skipping setup wizard

- remove any reference to the Switch and use the SwitchBar API instead
- set the initial state of the SwitchBar depending on the WifiManager

Change-Id: I556bf8a007892c057edf7c6c144f71b2dcfe4f99
This commit is contained in:
Fabrice Di Meglio
2014-09-05 14:08:58 -07:00
parent 0535ed559e
commit 92239ed799

View File

@@ -27,8 +27,6 @@ import android.net.wifi.WifiManager;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.provider.Settings; import android.provider.Settings;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch; import android.widget.Switch;
import android.widget.Toast; import android.widget.Toast;
@@ -41,7 +39,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class WifiEnabler implements SwitchBar.OnSwitchChangeListener { public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
private Context mContext; private Context mContext;
private Switch mSwitch;
private SwitchBar mSwitchBar; private SwitchBar mSwitchBar;
private AtomicBoolean mConnected = new AtomicBoolean(false); private AtomicBoolean mConnected = new AtomicBoolean(false);
@@ -88,22 +85,20 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
public WifiEnabler(Context context, SwitchBar switchBar) { public WifiEnabler(Context context, SwitchBar switchBar) {
mContext = context; mContext = context;
mSwitchBar = switchBar; mSwitchBar = switchBar;
mSwitch = switchBar.getSwitch();
// This is a trick: as the Wi-Fi initial state is asynchronously coming from the
// BroadcastReceiver we cannot have the Switch visible at first otherwise you will notice
// its state change later on. So start it as VIEW.GONE and make it View.VISIBLE later
// when its state is defined.
mSwitch.setVisibility(View.GONE);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION); mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
// The order matters! We really should not depend on this. :( // The order matters! We really should not depend on this. :(
mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
setupSwitchBar(); setupSwitchBar();
} }
public void setupSwitchBar() { public void setupSwitchBar() {
final int state = mWifiManager.getWifiState();
handleWifiStateChanged(state);
mSwitchBar.addOnSwitchChangeListener(this); mSwitchBar.addOnSwitchChangeListener(this);
mSwitchBar.show(); mSwitchBar.show();
} }
@@ -126,27 +121,26 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
private void handleWifiStateChanged(int state) { private void handleWifiStateChanged(int state) {
switch (state) { switch (state) {
case WifiManager.WIFI_STATE_ENABLING: case WifiManager.WIFI_STATE_ENABLING:
mSwitch.setEnabled(false); mSwitchBar.setEnabled(false);
break; break;
case WifiManager.WIFI_STATE_ENABLED: case WifiManager.WIFI_STATE_ENABLED:
setSwitchChecked(true); setSwitchBarChecked(true);
mSwitch.setEnabled(true); mSwitchBar.setEnabled(true);
updateSearchIndex(true); updateSearchIndex(true);
break; break;
case WifiManager.WIFI_STATE_DISABLING: case WifiManager.WIFI_STATE_DISABLING:
mSwitch.setEnabled(false); mSwitchBar.setEnabled(false);
break; break;
case WifiManager.WIFI_STATE_DISABLED: case WifiManager.WIFI_STATE_DISABLED:
setSwitchChecked(false); setSwitchBarChecked(false);
mSwitch.setEnabled(true); mSwitchBar.setEnabled(true);
updateSearchIndex(false); updateSearchIndex(false);
break; break;
default: default:
setSwitchChecked(false); setSwitchBarChecked(false);
mSwitch.setEnabled(true); mSwitchBar.setEnabled(true);
updateSearchIndex(false); updateSearchIndex(false);
} }
mSwitch.setVisibility(View.VISIBLE);
} }
private void updateSearchIndex(boolean isWiFiOn) { private void updateSearchIndex(boolean isWiFiOn) {
@@ -158,13 +152,11 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
mHandler.sendMessage(msg); mHandler.sendMessage(msg);
} }
private void setSwitchChecked(boolean checked) { private void setSwitchBarChecked(boolean checked) {
if (checked != mSwitch.isChecked()) {
mStateMachineEvent = true; mStateMachineEvent = true;
mSwitch.setChecked(checked); mSwitchBar.setChecked(checked);
mStateMachineEvent = false; mStateMachineEvent = false;
} }
}
private void handleStateChanged(@SuppressWarnings("unused") NetworkInfo.DetailedState state) { private void handleStateChanged(@SuppressWarnings("unused") NetworkInfo.DetailedState state) {
// After the refactoring from a CheckBoxPreference to a Switch, this method is useless since // After the refactoring from a CheckBoxPreference to a Switch, this method is useless since
@@ -192,7 +184,7 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
if (isChecked && !WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) { if (isChecked && !WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off. No infinite check/listenenr loop. // Reset switch to off. No infinite check/listenenr loop.
switchView.setChecked(false); mSwitchBar.setChecked(false);
return; return;
} }
@@ -203,10 +195,9 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
mWifiManager.setWifiApEnabled(null, false); mWifiManager.setWifiApEnabled(null, false);
} }
mSwitch.setEnabled(false);
if (!mWifiManager.setWifiEnabled(isChecked)) { if (!mWifiManager.setWifiEnabled(isChecked)) {
// Error // Error
mSwitch.setEnabled(true); mSwitchBar.setEnabled(true);
Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
} }
} }