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();
}
}
}

View File

@@ -19,13 +19,13 @@ package com.android.settings.wifi;
import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID;
import static android.os.UserManager.DISALLOW_CONFIG_WIFI;
import android.preference.PreferenceActivity;
import com.android.settings.R;
import com.android.settings.RestrictedSettingsFragment;
import com.android.settings.SettingsActivity;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.Indexable;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settings.widget.SwitchBar;
import com.android.settings.wifi.p2p.WifiP2pSettings;
import android.app.ActionBar;
@@ -179,7 +179,7 @@ public class WifiSettings extends RestrictedSettingsFragment
// the action bar uses a different set of controls for Setup Wizard
private boolean mSetupWizardMode;
private Switch mSwitch;
private SwitchBar mSwitchBar;
/* End of "used in Wifi Setup context" */
@@ -413,32 +413,10 @@ public class WifiSettings extends RestrictedSettingsFragment
// On/off switch is hidden for Setup Wizard
if (!mSetupWizardMode) {
final Activity activity = getActivity();
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitch = new Switch(activity.getActionBar().getThemedContext());
final int padding = activity.getResources().getDimensionPixelSize(
R.dimen.action_bar_switch_padding);
mSwitch.setPaddingRelative(0, 0, padding, 0);
activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM);
activity.getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_VERTICAL | Gravity.END));
mWifiEnabler = new WifiEnabler(activity, mSwitch);
}
}
@Override
public void onStop() {
super.onStop();
if (!mSetupWizardMode) {
final Activity activity = getActivity();
activity.getActionBar().setDisplayOptions(0, ActionBar.DISPLAY_SHOW_CUSTOM);
activity.getActionBar().setCustomView(null);
mSwitchBar = activity.getSwitchBar();
mWifiEnabler = new WifiEnabler(activity, mSwitchBar);
}
}