- refactor all the code that was using the Switch to control it thru the SwitchBar
- start the Switch as View.GONE and make it View.VISIBLE when it is set
as "enabled" or "checked" so that you dont see the Switch transition (it shows
only with it final state)
Change-Id: I382076bf3c819c530b5b2c06ca2429dfb2cdc6bf
private WifiManager mWifiManager;
private SwitchBar mSwitchBar;
- private Switch mEnabledSwitch;
private boolean mLastEnabledState;
private boolean mHaveDebugSettings;
private boolean mDontPokeProperties;
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
- mEnabledSwitch = mSwitchBar.getSwitch();
if (mUnavailable) {
- mEnabledSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
return;
}
}
final ContentResolver cr = getActivity().getContentResolver();
mLastEnabledState = Settings.Global.getInt(cr,
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
- mEnabledSwitch.setChecked(mLastEnabledState);
+ mSwitchBar.setSwitchChecked(mLastEnabledState);
setPrefsEnabledState(mLastEnabledState);
if (mHaveDebugSettings && !mLastEnabledState) {
Settings.Global.putInt(getActivity().getContentResolver(),
Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
mLastEnabledState = true;
- mEnabledSwitch.setChecked(mLastEnabledState);
+ mSwitchBar.setSwitchChecked(mLastEnabledState);
setPrefsEnabledState(mLastEnabledState);
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
- if (switchView != mEnabledSwitch) {
- return;
- }
if (isChecked != mLastEnabledState) {
if (isChecked) {
mDialogClicked = false;
setPrefsEnabledState(mLastEnabledState);
} else {
// Reset the toggle
- mEnabledSwitch.setChecked(false);
+ mSwitchBar.setSwitchChecked(false);
}
}
}
mAdbDialog = null;
} else if (dialog == mEnableDialog) {
if (!mDialogClicked) {
- mEnabledSwitch.setChecked(false);
+ mSwitchBar.setSwitchChecked(false);
}
mEnableDialog = null;
}
private DreamBackend mBackend;
private DreamInfoAdapter mAdapter;
private SwitchBar mSwitchBar;
- private Switch mSwitch;
private MenuItem[] mMenuItemsWhenEnabled;
private boolean mRefreshing;
final SettingsActivity sa = (SettingsActivity) getActivity();
mSwitchBar = sa.getSwitchBar();
- mSwitch = mSwitchBar.getSwitch();
}
@Override
logd("refreshFromBackend()");
mRefreshing = true;
boolean dreamsEnabled = mBackend.isEnabled();
- if (mSwitch.isChecked() != dreamsEnabled)
- mSwitch.setChecked(dreamsEnabled);
+ if (mSwitchBar.isSwitchChecked() != dreamsEnabled)
+ mSwitchBar.setSwitchChecked(dreamsEnabled);
mAdapter.clear();
if (dreamsEnabled) {
private SubtitleView mPreviewText;
private View mPreviewWindow;
private SwitchBar mSwitchBar;
- private ToggleSwitch mToggleSwitch;
// Standard options.
private LocalePreference mLocale;
SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
- mToggleSwitch = mSwitchBar.getSwitch();
- mToggleSwitch.setCheckedInternal(enabled);
+ mSwitchBar.setSwitchChecked(enabled);
mPreviewWindow = view.findViewById(R.id.preview_window);
}
protected void onInstallSwitchBarToggleSwitch() {
- mToggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
+ mSwitchBar.setSwitchOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
@Override
public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
- toggleSwitch.setCheckedInternal(checked);
+ mSwitchBar.setSwitchChecked(checked);
Settings.Secure.putInt(getActivity().getContentResolver(),
Settings.Secure.ACCESSIBILITY_CAPTIONING_ENABLED, checked ? 1 : 0);
getPreferenceScreen().setEnabled(checked);
private void removeSwitchBarToggleSwitch() {
mSwitchBar.hide();
- mToggleSwitch.setOnBeforeCheckedChangeListener(null);
+ mSwitchBar.setSwitchOnBeforeCheckedChangeListener(null);
}
private void initializeAllPreferences() {
String settingValue = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
final boolean enabled = settingValue.contains(mComponentName.flattenToString());
- mToggleSwitch.setCheckedInternal(enabled);
+ mSwitchBar.setSwitchChecked(enabled);
}
};
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checked = (mShownDialogId == DIALOG_ID_ENABLE_WARNING);
- mToggleSwitch.setCheckedInternal(checked);
+ mSwitchBar.setSwitchChecked(checked);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, checked);
onPreferenceToggled(mPreferenceKey, checked);
break;
case DialogInterface.BUTTON_NEGATIVE:
checked = (mShownDialogId == DIALOG_ID_DISABLE_WARNING);
- mToggleSwitch.setCheckedInternal(checked);
+ mSwitchBar.setSwitchChecked(checked);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, checked);
onPreferenceToggled(mPreferenceKey, checked);
break;
@Override
protected void onInstallSwitchBarToggleSwitch() {
super.onInstallSwitchBarToggleSwitch();
- mToggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
+ mSwitchBar.setSwitchOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
@Override
public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
if (checked) {
- toggleSwitch.setCheckedInternal(false);
+ mSwitchBar.setSwitchChecked(false);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, false);
showDialog(DIALOG_ID_ENABLE_WARNING);
} else {
- toggleSwitch.setCheckedInternal(true);
+ mSwitchBar.setSwitchChecked(true);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, true);
showDialog(DIALOG_ID_DISABLE_WARNING);
}
protected void onInstallSwitchBarToggleSwitch() {
super.onInstallSwitchBarToggleSwitch();
- mToggleSwitch.setCheckedInternal(
+ mSwitchBar.setSwitchChecked(
Settings.Secure.getInt(getContentResolver(), ENABLED, 0) == 1);
mSwitchBar.addOnSwitchChangeListener(this);
}
extends SettingsPreferenceFragment {
protected SwitchBar mSwitchBar;
- protected ToggleSwitch mToggleSwitch;
protected String mPreferenceKey;
protected Preference mSummaryPreference;
SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
- mToggleSwitch = mSwitchBar.getSwitch();
onProcessArguments(getArguments());
}
}
private void removeActionBarToggleSwitch() {
- mToggleSwitch.setOnBeforeCheckedChangeListener(null);
+ mSwitchBar.setSwitchOnBeforeCheckedChangeListener(null);
onRemoveSwitchBarToggleSwitch();
mSwitchBar.hide();
}
// Enabled.
if (arguments.containsKey(AccessibilitySettings.EXTRA_CHECKED)) {
final boolean enabled = arguments.getBoolean(AccessibilitySettings.EXTRA_CHECKED);
- mToggleSwitch.setCheckedInternal(enabled);
+ mSwitchBar.setSwitchChecked(enabled);
}
// Title.
@Override
protected void onInstallSwitchBarToggleSwitch() {
super.onInstallSwitchBarToggleSwitch();
- mToggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
+ mSwitchBar.setSwitchOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
@Override
public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
- toggleSwitch.setCheckedInternal(checked);
+ mSwitchBar.setSwitchChecked(checked);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, checked);
onPreferenceToggled(mPreferenceKey, checked);
return false;
protected void onInstallSwitchBarToggleSwitch() {
super.onInstallSwitchBarToggleSwitch();
- mToggleSwitch.setCheckedInternal(
+ mSwitchBar.setSwitchChecked(
Settings.Secure.getInt(getContentResolver(), ENABLED, 0) == 1);
mSwitchBar.addOnSwitchChangeListener(this);
}
@Override
protected void onInstallSwitchBarToggleSwitch() {
super.onInstallSwitchBarToggleSwitch();
- mToggleSwitch.setOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
+ mSwitchBar.setSwitchOnBeforeCheckedChangeListener(new OnBeforeCheckedChangeListener() {
@Override
public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
- toggleSwitch.setCheckedInternal(checked);
+ mSwitchBar.setSwitchChecked(checked);
getArguments().putBoolean(AccessibilitySettings.EXTRA_CHECKED, checked);
onPreferenceToggled(mPreferenceKey, checked);
return false;
*/
public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener {
private Context mContext;
- private Switch mSwitch;
private SwitchBar mSwitchBar;
private boolean mValidListener;
private final LocalBluetoothAdapter mLocalAdapter;
public BluetoothEnabler(Context context, SwitchBar switchBar) {
mContext = context;
mSwitchBar = switchBar;
- mSwitch = switchBar.getSwitch();
mValidListener = false;
LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context);
if (manager == null) {
// Bluetooth is not supported
mLocalAdapter = null;
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
} else {
mLocalAdapter = manager.getBluetoothAdapter();
}
public void resume(Context context) {
if (mLocalAdapter == null) {
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
return;
}
void handleStateChanged(int state) {
switch (state) {
case BluetoothAdapter.STATE_TURNING_ON:
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
break;
case BluetoothAdapter.STATE_ON:
setChecked(true);
- mSwitch.setEnabled(true);
+ mSwitchBar.setSwitchEnabled(true);
updateSearchIndex(true);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
break;
case BluetoothAdapter.STATE_OFF:
setChecked(false);
- mSwitch.setEnabled(true);
+ mSwitchBar.setSwitchEnabled(true);
updateSearchIndex(false);
break;
default:
setChecked(false);
- mSwitch.setEnabled(true);
+ mSwitchBar.setSwitchEnabled(true);
updateSearchIndex(false);
}
}
private void setChecked(boolean isChecked) {
- if (isChecked != mSwitch.isChecked()) {
+ if (isChecked != mSwitchBar.isSwitchChecked()) {
// set listener to null, so onCheckedChanged won't be called
// if the checked status on Switch isn't changed by user click
if (mValidListener) {
mSwitchBar.removeOnSwitchChangeListener(this);
}
- mSwitch.setChecked(isChecked);
+ mSwitchBar.setSwitchChecked(isChecked);
if (mValidListener) {
mSwitchBar.addOnSwitchChangeListener(this);
}
if (mLocalAdapter != null) {
mLocalAdapter.setBluetoothEnabled(isChecked);
}
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
}
}
private static final String KEY_LOCATION_SERVICES = "location_services";
private SwitchBar mSwitchBar;
- private Switch mSwitch;
private boolean mValidListener;
private Preference mLocationMode;
private PreferenceCategory mCategoryRecentLocationRequests;
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
- mSwitch = mSwitchBar.getSwitch();
}
@Override
// corner cases, the location might still be enabled. In such case the master switch should
// be disabled but checked.
boolean enabled = (mode != android.provider.Settings.Secure.LOCATION_MODE_OFF);
- mSwitch.setEnabled(!restricted);
+ mSwitchBar.setSwitchEnabled(!restricted);
mLocationMode.setEnabled(enabled && !restricted);
mCategoryRecentLocationRequests.setEnabled(enabled);
- if (enabled != mSwitch.isChecked()) {
+ if (enabled != mSwitchBar.isSwitchChecked()) {
// set listener to null so that that code below doesn't trigger onCheckedChanged()
if (mValidListener) {
mSwitchBar.removeOnSwitchChangeListener(this);
}
- mSwitch.setChecked(enabled);
+ mSwitchBar.setSwitchChecked(enabled);
if (mValidListener) {
mSwitchBar.addOnSwitchChangeListener(this);
}
private View mView;
private NfcAdapter mNfcAdapter;
private SwitchBar mSwitchBar;
- private Switch mSwitch;
private CharSequence mOldActivityTitle;
@Override
SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
- mSwitch = mSwitchBar.getSwitch();
- mSwitch.setChecked(mNfcAdapter.isNdefPushEnabled());
+ mSwitchBar.setSwitchChecked(mNfcAdapter.isNdefPushEnabled());
}
@Override
@Override
public void onSwitchChanged(Switch switchView, boolean desiredState) {
boolean success = false;
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
if (desiredState) {
success = mNfcAdapter.enableNdefPush();
} else {
success = mNfcAdapter.disableNdefPush();
}
if (success) {
- mSwitch.setChecked(desiredState);
+ mSwitchBar.setSwitchChecked(desiredState);
}
- mSwitch.setEnabled(true);
+ mSwitchBar.setSwitchEnabled(true);
}
}
private final SettingsObserver mSettingsObserver = new SettingsObserver();
private SwitchBar mSwitchBar;
- private Switch mSwitch;
private Context mContext;
private PackageManager mPM;
private ZenModeConfig mConfig;
if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig);
mSwitchBar = ((SettingsActivity) mContext).getSwitchBar();
- mSwitch = mSwitchBar.getSwitch();
final PreferenceCategory general = (PreferenceCategory) root.findPreference(KEY_GENERAL);
private void updateZenMode() {
final boolean zenMode = Global.getInt(getContentResolver(),
Global.ZEN_MODE, Global.ZEN_MODE_OFF) != Global.ZEN_MODE_OFF;
- if (mSwitch.isChecked() != zenMode) {
- mSwitch.setChecked(zenMode);
+ if (mSwitchBar.isSwitchChecked() != zenMode) {
+ mSwitchBar.setSwitchChecked(zenMode);
mIgnoreNext = true;
}
}
};
private SwitchBar mSwitchBar;
- private ToggleSwitch mToggleSwitch;
private String mPreferenceKey;
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checked = true;
- mToggleSwitch.setCheckedInternal(checked);
+ mSwitchBar.setSwitchChecked(checked);
getArguments().putBoolean(PrintSettingsFragment.EXTRA_CHECKED, checked);
onPreferenceToggled(mPreferenceKey, checked);
break;
case DialogInterface.BUTTON_NEGATIVE:
checked = false;
- mToggleSwitch.setCheckedInternal(checked);
+ mSwitchBar.setSwitchChecked(checked);
getArguments().putBoolean(PrintSettingsFragment.EXTRA_CHECKED, checked);
onPreferenceToggled(mPreferenceKey, checked);
break;
ListView listView = getListView();
ViewGroup contentRoot = (ViewGroup) listView.getParent();
View emptyView = listView.getEmptyView();
- if (!mToggleSwitch.isChecked()) {
+ if (!mSwitchBar.isSwitchChecked()) {
if (emptyView != null && emptyView.getId() != R.id.empty_print_state) {
contentRoot.removeView(emptyView);
emptyView = null;
List<ComponentName> services = PrintSettingsUtils.readEnabledPrintServices(getActivity());
mServiceEnabled = services.contains(mComponentName);
if (mServiceEnabled) {
- mToggleSwitch.setCheckedInternal(true);
+ mSwitchBar.setSwitchChecked(true);
mPrintersAdapter.enable();
} else {
- mToggleSwitch.setCheckedInternal(false);
+ mSwitchBar.setSwitchChecked(false);
mPrintersAdapter.disable();
}
getActivity().invalidateOptionsMenu();
final SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar();
-
- mToggleSwitch = mSwitchBar.getSwitch();
- mToggleSwitch.setOnBeforeCheckedChangeListener(new ToggleSwitch.OnBeforeCheckedChangeListener() {
+ mSwitchBar.setSwitchOnBeforeCheckedChangeListener(new ToggleSwitch.OnBeforeCheckedChangeListener() {
@Override
public boolean onBeforeCheckedChanged(ToggleSwitch toggleSwitch, boolean checked) {
if (checked) {
if (!TextUtils.isEmpty(mEnableWarningMessage)) {
- toggleSwitch.setCheckedInternal(false);
+ mSwitchBar.setSwitchChecked(false);
getArguments().putBoolean(PrintSettingsFragment.EXTRA_CHECKED, false);
showDialog(DIALOG_ID_ENABLE_WARNING);
return true;
// Enabled.
final boolean enabled = arguments.getBoolean(PrintSettingsFragment.EXTRA_CHECKED);
- mToggleSwitch.setCheckedInternal(enabled);
+ mSwitchBar.setSwitchChecked(enabled);
// Settings title and intent.
String settingsTitle = arguments.getString(PrintSettingsFragment.EXTRA_SETTINGS_TITLE);
// Default is hide
setVisibility(View.GONE);
- }
-
- public ToggleSwitch getSwitch() {
- return mSwitch;
+ mSwitch.setVisibility(View.GONE);
}
public void show() {
}
mSwitchChangeListeners.remove(listener);
}
+
+ public void setSwitchOnBeforeCheckedChangeListener(
+ ToggleSwitch.OnBeforeCheckedChangeListener listener) {
+ mSwitch.setOnBeforeCheckedChangeListener(listener);
+ }
+
+ public void setSwitchChecked(boolean checked) {
+ setSwitchChecked(checked, false);
+ }
+
+ public void setSwitchChecked(boolean checked, boolean checkBefore) {
+ if (checkBefore) {
+ ToggleSwitch.OnBeforeCheckedChangeListener listener =
+ mSwitch.getOnBeforeCheckedChangeListener();
+ if (listener != null && listener.onBeforeCheckedChanged(mSwitch, checked)) {
+ return;
+ }
+
+ }
+ mSwitch.setCheckedInternal(checked);
+ if (mSwitch.getVisibility() == View.GONE) {
+ mSwitch.setVisibility(View.VISIBLE);
+ }
+ }
+
+ public void setSwitchEnabled(boolean enabled) {
+ mSwitch.setEnabled(enabled);
+ if (mSwitch.getVisibility() == View.GONE) {
+ mSwitch.setVisibility(View.VISIBLE);
+ }
+ }
+
+ public boolean isSwitchChecked() {
+ return mSwitch.isChecked();
+ }
}
mOnBeforeListener = listener;
}
+ public OnBeforeCheckedChangeListener getOnBeforeCheckedChangeListener() {
+ return mOnBeforeListener;
+ }
+
@Override
public void setChecked(boolean checked) {
if (mOnBeforeListener != null
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
-import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
public class WifiEnabler implements SwitchBar.OnSwitchChangeListener {
private Context mContext;
- private Switch mSwitch;
private SwitchBar mSwitchBar;
private AtomicBoolean mConnected = new AtomicBoolean(false);
public WifiEnabler(Context context, SwitchBar switchBar) {
mContext = context;
mSwitchBar = switchBar;
- mSwitch = switchBar.getSwitch();
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mIntentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
private void handleWifiStateChanged(int state) {
switch (state) {
case WifiManager.WIFI_STATE_ENABLING:
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
break;
case WifiManager.WIFI_STATE_ENABLED:
setSwitchChecked(true);
- mSwitch.setEnabled(true);
+ mSwitchBar.setSwitchEnabled(true);
updateSearchIndex(true);
break;
case WifiManager.WIFI_STATE_DISABLING:
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
break;
case WifiManager.WIFI_STATE_DISABLED:
setSwitchChecked(false);
- mSwitch.setEnabled(true);
+ mSwitchBar.setSwitchEnabled(true);
updateSearchIndex(false);
break;
default:
setSwitchChecked(false);
- mSwitch.setEnabled(true);
+ mSwitchBar.setSwitchEnabled(true);
updateSearchIndex(false);
}
}
}
private void setSwitchChecked(boolean checked) {
- if (checked != mSwitch.isChecked()) {
+ if (checked != mSwitchBar.isSwitchChecked()) {
mStateMachineEvent = true;
- mSwitch.setChecked(checked);
+ mSwitchBar.setSwitchChecked(checked);
mStateMachineEvent = false;
}
}
mWifiManager.setWifiApEnabled(null, false);
}
- mSwitch.setEnabled(false);
+ mSwitchBar.setSwitchEnabled(false);
if (!mWifiManager.setWifiEnabled(isChecked)) {
// Error
- mSwitch.setEnabled(true);
+ mSwitchBar.setSwitchEnabled(true);
Toast.makeText(mContext, R.string.wifi_error, Toast.LENGTH_SHORT).show();
}
}