Introduce SwitchBar widget

- SwitchBar is a LinearLayout that containts a TextView and a Switch and
is intended to replace all Switches that are put in the ActionBar as a
custom view
- use the new SwitchBar for WifiSetting only for now (a later CL will
take care of all the other Setting that are using a Switch in the
ActionBar)

Related to bug #14898161 On/Off switches must move down from Action Bar

Change-Id: I5e98dbe995bba8f440d08459e09ca3ac09d3464b
This commit is contained in:
Fabrice Di Meglio
2014-05-13 19:51:59 -07:00
parent a1ebae6f71
commit 4193776698
11 changed files with 301 additions and 80 deletions

View File

@@ -34,12 +34,14 @@ import android.widget.Toast;
import com.android.settings.R;
import com.android.settings.WirelessSettings;
import com.android.settings.search.Index;
import com.android.settings.widget.SwitchBar;
import java.util.concurrent.atomic.AtomicBoolean;
public class WifiEnabler implements CompoundButton.OnCheckedChangeListener {
public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
private Context mContext;
private Switch mSwitch;
private SwitchBar mSwitchBar;
private AtomicBoolean mConnected = new AtomicBoolean(false);
private final WifiManager mWifiManager;
@@ -82,9 +84,10 @@ public class WifiEnabler implements CompoundButton.OnCheckedChangeListener {
}
};
public WifiEnabler(Context context, Switch switch_) {
public WifiEnabler(Context context, SwitchBar switchBar) {
mContext = context;
mSwitch = switch_;
mSwitchBar = switchBar;
mSwitch = switchBar.getSwitch();
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
@@ -97,53 +100,14 @@ public class WifiEnabler implements CompoundButton.OnCheckedChangeListener {
mContext = context;
// Wi-Fi state is sticky, so just let the receiver update UI
mContext.registerReceiver(mReceiver, mIntentFilter);
mSwitch.setOnCheckedChangeListener(this);
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchBar.show();
}
public void pause() {
mContext.unregisterReceiver(mReceiver);
mSwitch.setOnCheckedChangeListener(null);
}
public void setSwitch(Switch switch_) {
if (mSwitch == switch_) return;
mSwitch.setOnCheckedChangeListener(null);
mSwitch = switch_;
mSwitch.setOnCheckedChangeListener(this);
final int wifiState = mWifiManager.getWifiState();
boolean isEnabled = wifiState == WifiManager.WIFI_STATE_ENABLED;
boolean isDisabled = wifiState == WifiManager.WIFI_STATE_DISABLED;
mSwitch.setChecked(isEnabled);
mSwitch.setEnabled(isEnabled || isDisabled);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//Do nothing if called as a result of a state machine event
if (mStateMachineEvent) {
return;
}
// Show toast message if Wi-Fi is not allowed in airplane mode
if (isChecked && !WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off. No infinite check/listenenr loop.
buttonView.setChecked(false);
return;
}
// Disable tethering if enabling Wifi
int wifiApState = mWifiManager.getWifiApState();
if (isChecked && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) ||
(wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) {
mWifiManager.setWifiApEnabled(null, false);
}
mSwitch.setEnabled(false);
if (!mWifiManager.setWifiEnabled(isChecked)) {
// Error
mSwitch.setEnabled(true);
Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
}
mSwitchBar.removeOnSwitchChangeListener(this);
mSwitchBar.hide();
}
private void handleWifiStateChanged(int state) {
@@ -203,4 +167,33 @@ public class WifiEnabler implements CompoundButton.OnCheckedChangeListener {
}
*/
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
//Do nothing if called as a result of a state machine event
if (mStateMachineEvent) {
return;
}
// Show toast message if Wi-Fi is not allowed in airplane mode
if (isChecked && !WirelessSettings.isRadioAllowed(mContext, Settings.Global.RADIO_WIFI)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off. No infinite check/listenenr loop.
switchView.setChecked(false);
return;
}
// Disable tethering if enabling Wifi
int wifiApState = mWifiManager.getWifiApState();
if (isChecked && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) ||
(wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) {
mWifiManager.setWifiApEnabled(null, false);
}
mSwitch.setEnabled(false);
if (!mWifiManager.setWifiEnabled(isChecked)) {
// Error
mSwitch.setEnabled(true);
Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
}
}
}