diff options
author | Fabrice Di Meglio <fdimeglio@google.com> | 2014-06-11 20:14:15 -0700 |
---|---|---|
committer | Fabrice Di Meglio <fdimeglio@google.com> | 2014-06-12 11:13:23 -0700 |
commit | 138ff8c0457c6e1345015973668d652fa17c7c3b (patch) | |
tree | 4a4e80986fc33e2a297a359b07253fde5b27333c /src/com | |
parent | 601aad2022311f88c290e8fc9d426ce76f1bd1b3 (diff) | |
download | packages_apps_Settings-138ff8c0457c6e1345015973668d652fa17c7c3b.zip packages_apps_Settings-138ff8c0457c6e1345015973668d652fa17c7c3b.tar.gz packages_apps_Settings-138ff8c0457c6e1345015973668d652fa17c7c3b.tar.bz2 |
Make the SwitchBar appear immediately
- change the way the SwitchBar is shown and hidden
- save its state
- remove the delay transition code
Change-Id: I07260430e6709b42517ca011f6d3c3446a626731
Diffstat (limited to 'src/com')
13 files changed, 195 insertions, 84 deletions
diff --git a/src/com/android/settings/DevelopmentSettings.java b/src/com/android/settings/DevelopmentSettings.java index ce44907..5e5113b 100644 --- a/src/com/android/settings/DevelopmentSettings.java +++ b/src/com/android/settings/DevelopmentSettings.java @@ -167,7 +167,6 @@ public class DevelopmentSettings extends SettingsPreferenceFragment private WifiManager mWifiManager; private SwitchBar mSwitchBar; - private Switch mEnabledSwitch; private boolean mLastEnabledState; private boolean mHaveDebugSettings; private boolean mDontPokeProperties; @@ -386,11 +385,12 @@ public class DevelopmentSettings extends SettingsPreferenceFragment final SettingsActivity activity = (SettingsActivity) getActivity(); mSwitchBar = activity.getSwitchBar(); - mEnabledSwitch = mSwitchBar.getSwitch(); - if (mUnavailable) { - mEnabledSwitch.setEnabled(false); + if (mUnavailable) { + mSwitchBar.setEnabled(false); return; } + + mSwitchBar.addOnSwitchChangeListener(this); } private boolean removePreferenceForProduction(Preference preference) { @@ -441,7 +441,7 @@ public class DevelopmentSettings extends SettingsPreferenceFragment final ContentResolver cr = getActivity().getContentResolver(); mLastEnabledState = Settings.Global.getInt(cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; - mEnabledSwitch.setChecked(mLastEnabledState); + mSwitchBar.setChecked(mLastEnabledState); setPrefsEnabledState(mLastEnabledState); if (mHaveDebugSettings && !mLastEnabledState) { @@ -452,17 +452,15 @@ public class DevelopmentSettings extends SettingsPreferenceFragment Settings.Global.putInt(getActivity().getContentResolver(), Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1); mLastEnabledState = true; - mEnabledSwitch.setChecked(mLastEnabledState); + mSwitchBar.setChecked(mLastEnabledState); setPrefsEnabledState(mLastEnabledState); } - - mSwitchBar.addOnSwitchChangeListener(this); mSwitchBar.show(); } @Override - public void onPause() { - super.onPause(); + public void onDestroyView() { + super.onDestroyView(); mSwitchBar.removeOnSwitchChangeListener(this); mSwitchBar.hide(); @@ -1210,7 +1208,7 @@ public class DevelopmentSettings extends SettingsPreferenceFragment @Override public void onSwitchChanged(Switch switchView, boolean isChecked) { - if (switchView != mEnabledSwitch) { + if (switchView != mSwitchBar.getSwitch()) { return; } if (isChecked != mLastEnabledState) { @@ -1463,7 +1461,7 @@ public class DevelopmentSettings extends SettingsPreferenceFragment setPrefsEnabledState(mLastEnabledState); } else { // Reset the toggle - mEnabledSwitch.setChecked(false); + mSwitchBar.setChecked(false); } } } @@ -1477,7 +1475,7 @@ public class DevelopmentSettings extends SettingsPreferenceFragment mAdbDialog = null; } else if (dialog == mEnableDialog) { if (!mDialogClicked) { - mEnabledSwitch.setChecked(false); + mSwitchBar.setChecked(false); } mEnableDialog = null; } diff --git a/src/com/android/settings/DreamSettings.java b/src/com/android/settings/DreamSettings.java index 1175639..38cba7a 100644 --- a/src/com/android/settings/DreamSettings.java +++ b/src/com/android/settings/DreamSettings.java @@ -61,7 +61,6 @@ public class DreamSettings extends SettingsPreferenceFragment implements private DreamBackend mBackend; private DreamInfoAdapter mAdapter; private SwitchBar mSwitchBar; - private Switch mSwitch; private MenuItem[] mMenuItemsWhenEnabled; private boolean mRefreshing; @@ -105,6 +104,9 @@ public class DreamSettings extends SettingsPreferenceFragment implements public void onDestroyView() { logd("onDestroyView()"); super.onDestroyView(); + + mSwitchBar.removeOnSwitchChangeListener(this); + mSwitchBar.hide(); } @Override @@ -124,7 +126,8 @@ public class DreamSettings extends SettingsPreferenceFragment implements final SettingsActivity sa = (SettingsActivity) getActivity(); mSwitchBar = sa.getSwitchBar(); - mSwitch = mSwitchBar.getSwitch(); + mSwitchBar.addOnSwitchChangeListener(this); + mSwitchBar.show(); } @Override @@ -212,9 +215,6 @@ public class DreamSettings extends SettingsPreferenceFragment implements super.onPause(); mContext.unregisterReceiver(mPackageReceiver); - - mSwitchBar.removeOnSwitchChangeListener(this); - mSwitchBar.hide(); } @Override @@ -231,9 +231,6 @@ public class DreamSettings extends SettingsPreferenceFragment implements filter.addAction(Intent.ACTION_PACKAGE_REPLACED); filter.addDataScheme(PACKAGE_SCHEME); mContext.registerReceiver(mPackageReceiver , filter); - - mSwitchBar.addOnSwitchChangeListener(this); - mSwitchBar.show(); } public static int getSummaryResource(Context context) { @@ -263,8 +260,8 @@ public class DreamSettings extends SettingsPreferenceFragment implements logd("refreshFromBackend()"); mRefreshing = true; boolean dreamsEnabled = mBackend.isEnabled(); - if (mSwitch.isChecked() != dreamsEnabled) - mSwitch.setChecked(dreamsEnabled); + if (mSwitchBar.isChecked() != dreamsEnabled) + mSwitchBar.setChecked(dreamsEnabled); mAdapter.clear(); if (dreamsEnabled) { diff --git a/src/com/android/settings/accessibility/CaptionPropertiesFragment.java b/src/com/android/settings/accessibility/CaptionPropertiesFragment.java index 41c80ae..53d614d 100644 --- a/src/com/android/settings/accessibility/CaptionPropertiesFragment.java +++ b/src/com/android/settings/accessibility/CaptionPropertiesFragment.java @@ -142,27 +142,30 @@ public class CaptionPropertiesFragment extends SettingsPreferenceFragment refreshPreviewText(); } }); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + final boolean enabled = mCaptioningManager.isEnabled(); SettingsActivity activity = (SettingsActivity) getActivity(); mSwitchBar = activity.getSwitchBar(); + mSwitchBar.setTextViewLabel(enabled); mToggleSwitch = mSwitchBar.getSwitch(); mToggleSwitch.setCheckedInternal(enabled); getPreferenceScreen().setEnabled(enabled); refreshPreviewText(); - } - @Override - public void onResume() { - super.onResume(); installSwitchBarToggleSwitch(); } @Override - public void onPause() { + public void onDestroyView() { + super.onDestroyView(); removeSwitchBarToggleSwitch(); - super.onPause(); } private void refreshPreviewText() { diff --git a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java index 331c0e3..bc21759 100644 --- a/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java +++ b/src/com/android/settings/accessibility/ToggleFeaturePreferenceFragment.java @@ -95,17 +95,17 @@ public abstract class ToggleFeaturePreferenceFragment } @Override - public void onResume() { - super.onResume(); + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); installActionBarToggleSwitch(); } @Override - public void onPause() { - removeActionBarToggleSwitch(); + public void onDestroyView() { + super.onDestroyView(); - super.onPause(); + removeActionBarToggleSwitch(); } protected abstract void onPreferenceToggled(String preferenceKey, boolean enabled); diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java index be03b63..45f3d06 100644 --- a/src/com/android/settings/bluetooth/BluetoothEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java @@ -89,6 +89,16 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); } + public void setupSwitchBar() { + mSwitchBar.addOnSwitchChangeListener(this); + mSwitchBar.show(); + } + + public void teardownSwitchBar() { + mSwitchBar.removeOnSwitchChangeListener(this); + mSwitchBar.hide(); + } + public void resume(Context context) { if (mLocalAdapter == null) { mSwitch.setEnabled(false); @@ -103,8 +113,6 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener handleStateChanged(mLocalAdapter.getBluetoothState()); mContext.registerReceiver(mReceiver, mIntentFilter); - mSwitchBar.addOnSwitchChangeListener(this); - mSwitchBar.show(); mValidListener = true; } @@ -114,8 +122,6 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener } mContext.unregisterReceiver(mReceiver); - mSwitchBar.removeOnSwitchChangeListener(this); - mSwitchBar.hide(); mValidListener = false; } diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java index dd4edff..eb318cd 100755 --- a/src/com/android/settings/bluetooth/BluetoothSettings.java +++ b/src/com/android/settings/bluetooth/BluetoothSettings.java @@ -119,6 +119,14 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment implem mSwitchBar = activity.getSwitchBar(); mBluetoothEnabler = new BluetoothEnabler(activity, mSwitchBar); + mBluetoothEnabler.setupSwitchBar(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + + mBluetoothEnabler.teardownSwitchBar(); } @Override diff --git a/src/com/android/settings/location/LocationSettings.java b/src/com/android/settings/location/LocationSettings.java index 8794a8d..b10d4b6 100644 --- a/src/com/android/settings/location/LocationSettings.java +++ b/src/com/android/settings/location/LocationSettings.java @@ -73,6 +73,16 @@ public class LocationSettings extends LocationSettingsBase mSwitchBar = activity.getSwitchBar(); mSwitch = mSwitchBar.getSwitch(); + mSwitchBar.addOnSwitchChangeListener(this); + mSwitchBar.show(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); + + mSwitchBar.removeOnSwitchChangeListener(this); + mSwitchBar.hide(); } @Override @@ -84,8 +94,6 @@ public class LocationSettings extends LocationSettingsBase public void onResume() { super.onResume(); createPreferenceHierarchy(); - mSwitchBar.addOnSwitchChangeListener(this); - mSwitchBar.show(); mValidListener = true; } @@ -97,8 +105,6 @@ public class LocationSettings extends LocationSettingsBase // Ignore exceptions caused by race condition } super.onPause(); - mSwitchBar.removeOnSwitchChangeListener(this); - mSwitchBar.hide(); mValidListener = false; } diff --git a/src/com/android/settings/nfc/AndroidBeam.java b/src/com/android/settings/nfc/AndroidBeam.java index eb3ff9b..add0fa7 100644 --- a/src/com/android/settings/nfc/AndroidBeam.java +++ b/src/com/android/settings/nfc/AndroidBeam.java @@ -33,7 +33,6 @@ public class AndroidBeam extends Fragment private View mView; private NfcAdapter mNfcAdapter; private SwitchBar mSwitchBar; - private Switch mSwitch; private CharSequence mOldActivityTitle; @Override @@ -63,46 +62,33 @@ public class AndroidBeam extends Fragment SettingsActivity activity = (SettingsActivity) getActivity(); mSwitchBar = activity.getSwitchBar(); - mSwitch = mSwitchBar.getSwitch(); - mSwitch.setChecked(mNfcAdapter.isNdefPushEnabled()); - } - - @Override - public void onResume() { - super.onResume(); - + mSwitchBar.setChecked(mNfcAdapter.isNdefPushEnabled()); mSwitchBar.addOnSwitchChangeListener(this); mSwitchBar.show(); } @Override - public void onPause() { - super.onPause(); - - mSwitchBar.removeOnSwitchChangeListener(this); - mSwitchBar.hide(); - } - - @Override public void onDestroyView() { super.onDestroyView(); if (mOldActivityTitle != null) { getActivity().getActionBar().setTitle(mOldActivityTitle); } + mSwitchBar.removeOnSwitchChangeListener(this); + mSwitchBar.hide(); } @Override public void onSwitchChanged(Switch switchView, boolean desiredState) { boolean success = false; - mSwitch.setEnabled(false); + mSwitchBar.setEnabled(false); if (desiredState) { success = mNfcAdapter.enableNdefPush(); } else { success = mNfcAdapter.disableNdefPush(); } if (success) { - mSwitch.setChecked(desiredState); + mSwitchBar.setChecked(desiredState); } - mSwitch.setEnabled(true); + mSwitchBar.setEnabled(true); } } diff --git a/src/com/android/settings/notification/ZenModeSettings.java b/src/com/android/settings/notification/ZenModeSettings.java index e9323d8..40e2c55 100644 --- a/src/com/android/settings/notification/ZenModeSettings.java +++ b/src/com/android/settings/notification/ZenModeSettings.java @@ -107,7 +107,6 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index private final SettingsObserver mSettingsObserver = new SettingsObserver(); private SwitchBar mSwitchBar; - private Switch mSwitch; private Context mContext; private PackageManager mPM; private ZenModeConfig mConfig; @@ -159,7 +158,8 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index if (DEBUG) Log.d(TAG, "Loaded mConfig=" + mConfig); mSwitchBar = ((SettingsActivity) mContext).getSwitchBar(); - mSwitch = mSwitchBar.getSwitch(); + mSwitchBar.addOnSwitchChangeListener(this); + mSwitchBar.show(); final PreferenceCategory general = (PreferenceCategory) root.findPreference(KEY_GENERAL); @@ -400,14 +400,17 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index updateControls(); updateZenMode(); mSettingsObserver.register(); - mSwitchBar.addOnSwitchChangeListener(this); - mSwitchBar.show(); } @Override public void onPause() { super.onPause(); mSettingsObserver.unregister(); + } + + @Override + public void onDestroyView() { + super.onDestroyView(); mSwitchBar.removeOnSwitchChangeListener(this); mSwitchBar.hide(); } @@ -415,8 +418,8 @@ public class ZenModeSettings extends SettingsPreferenceFragment implements Index 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.isChecked() != zenMode) { + mSwitchBar.setChecked(zenMode); mIgnoreNext = true; } } diff --git a/src/com/android/settings/print/PrintServiceSettingsFragment.java b/src/com/android/settings/print/PrintServiceSettingsFragment.java index 872f7b8..c68f131 100644 --- a/src/com/android/settings/print/PrintServiceSettingsFragment.java +++ b/src/com/android/settings/print/PrintServiceSettingsFragment.java @@ -143,16 +143,12 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment mSettingsContentObserver.register(getContentResolver()); updateEmptyView(); updateUiForServiceState(); - mSwitchBar.addOnSwitchChangeListener(this); - mSwitchBar.show(); } @Override public void onPause() { mSettingsContentObserver.unregister(getContentResolver()); super.onPause(); - mSwitchBar.removeOnSwitchChangeListener(this); - mSwitchBar.hide(); } @Override @@ -168,6 +164,8 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment getActivity().getActionBar().setTitle(mOldActivityTitle); } super.onDestroyView(); + mSwitchBar.removeOnSwitchChangeListener(this); + mSwitchBar.hide(); } private void onPreferenceToggled(String preferenceKey, boolean enabled) { @@ -292,6 +290,8 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment final SettingsActivity activity = (SettingsActivity) getActivity(); mSwitchBar = activity.getSwitchBar(); + mSwitchBar.addOnSwitchChangeListener(this); + mSwitchBar.show(); mToggleSwitch = mSwitchBar.getSwitch(); mToggleSwitch.setOnBeforeCheckedChangeListener(new ToggleSwitch.OnBeforeCheckedChangeListener() { diff --git a/src/com/android/settings/widget/SwitchBar.java b/src/com/android/settings/widget/SwitchBar.java index bc06d87..b503cf6 100644 --- a/src/com/android/settings/widget/SwitchBar.java +++ b/src/com/android/settings/widget/SwitchBar.java @@ -17,10 +17,11 @@ package com.android.settings.widget; import android.content.Context; +import android.os.Parcel; +import android.os.Parcelable; import android.transition.TransitionManager; import android.util.AttributeSet; import android.view.LayoutInflater; -import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.CompoundButton; @@ -72,12 +73,11 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC mTextView.setText(R.string.switch_off_text); mSwitch = (ToggleSwitch) findViewById(R.id.switch_widget); - mSwitch.setOnCheckedChangeListener(this); addOnSwitchChangeListener(new OnSwitchChangeListener() { @Override public void onSwitchChanged(Switch switchView, boolean isChecked) { - mTextView.setText(isChecked ? R.string.switch_on_text : R.string.switch_off_text); + setTextViewLabel(isChecked); } }); @@ -90,18 +90,41 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC setVisibility(View.GONE); } - public ToggleSwitch getSwitch() { + public void setTextViewLabel(boolean isChecked) { + mTextView.setText(isChecked ? R.string.switch_on_text : R.string.switch_off_text); + } + + public void setChecked(boolean checked) { + setTextViewLabel(checked); + mSwitch.setChecked(checked); + } + + public boolean isChecked() { + return mSwitch.isChecked(); + } + + public void setEnabled(boolean enabled) { + super.setEnabled(enabled); + mTextView.setEnabled(enabled); + mSwitch.setEnabled(false); + } + + public final ToggleSwitch getSwitch() { return mSwitch; } public void show() { - TransitionManager.beginDelayedTransition((ViewGroup) getParent()); - setVisibility(View.VISIBLE); + if (!isShowing()) { + setVisibility(View.VISIBLE); + mSwitch.setOnCheckedChangeListener(this); + } } public void hide() { - TransitionManager.beginDelayedTransition((ViewGroup) getParent()); - setVisibility(View.GONE); + if (isShowing()) { + setVisibility(View.GONE); + mSwitch.setOnCheckedChangeListener(null); + } } public boolean isShowing() { @@ -139,4 +162,69 @@ public class SwitchBar extends LinearLayout implements CompoundButton.OnCheckedC } mSwitchChangeListeners.remove(listener); } + + static class SavedState extends BaseSavedState { + boolean checked; + boolean visible; + + SavedState(Parcelable superState) { + super(superState); + } + + /** + * Constructor called from {@link #CREATOR} + */ + private SavedState(Parcel in) { + super(in); + checked = (Boolean)in.readValue(null); + visible = (Boolean)in.readValue(null); + } + + @Override + public void writeToParcel(Parcel out, int flags) { + super.writeToParcel(out, flags); + out.writeValue(checked); + out.writeValue(visible); + } + + @Override + public String toString() { + return "SwitchBar.SavedState{" + + Integer.toHexString(System.identityHashCode(this)) + + " checked=" + checked + + " visible=" + visible + "}"; + } + + public static final Parcelable.Creator<SavedState> CREATOR + = new Parcelable.Creator<SavedState>() { + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + } + + @Override + public Parcelable onSaveInstanceState() { + Parcelable superState = super.onSaveInstanceState(); + + SavedState ss = new SavedState(superState); + ss.checked = mSwitch.isChecked(); + ss.visible = isShowing(); + return ss; + } + + @Override + public void onRestoreInstanceState(Parcelable state) { + SavedState ss = (SavedState) state; + + super.onRestoreInstanceState(ss.getSuperState()); + mSwitch.setChecked(ss.checked); + setTextViewLabel(ss.checked); + setVisibility(ss.visible ? View.VISIBLE : View.GONE); + requestLayout(); + } } diff --git a/src/com/android/settings/wifi/WifiEnabler.java b/src/com/android/settings/wifi/WifiEnabler.java index 9f0b6fa..a48598e 100644 --- a/src/com/android/settings/wifi/WifiEnabler.java +++ b/src/com/android/settings/wifi/WifiEnabler.java @@ -96,18 +96,24 @@ public class WifiEnabler implements SwitchBar.OnSwitchChangeListener { mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); } + public void setupSwitchBar() { + mSwitchBar.addOnSwitchChangeListener(this); + mSwitchBar.show(); + } + + public void teardownSwitchBar() { + mSwitchBar.removeOnSwitchChangeListener(this); + mSwitchBar.hide(); + } + public void resume(Context context) { mContext = context; // Wi-Fi state is sticky, so just let the receiver update UI mContext.registerReceiver(mReceiver, mIntentFilter); - mSwitchBar.addOnSwitchChangeListener(this); - mSwitchBar.show(); } public void pause() { mContext.unregisterReceiver(mReceiver); - mSwitchBar.removeOnSwitchChangeListener(this); - mSwitchBar.hide(); } private void handleWifiStateChanged(int state) { diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java index 604275a..b374fb7 100644 --- a/src/com/android/settings/wifi/WifiSettings.java +++ b/src/com/android/settings/wifi/WifiSettings.java @@ -405,6 +405,15 @@ public class WifiSettings extends RestrictedSettingsFragment } @Override + public void onDestroyView() { + super.onDestroyView(); + + if (mWifiEnabler != null) { + mWifiEnabler.teardownSwitchBar(); + } + } + + @Override public void onStart() { super.onStart(); @@ -414,6 +423,7 @@ public class WifiSettings extends RestrictedSettingsFragment mSwitchBar = activity.getSwitchBar(); mWifiEnabler = new WifiEnabler(activity, mSwitchBar); + mWifiEnabler.setupSwitchBar(); } } |