diff options
author | Amith Yamasani <yamasani@google.com> | 2011-01-04 10:47:07 -0800 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2011-01-04 10:47:07 -0800 |
commit | b2835ebdbdad14792b54865568db06e2c6517c28 (patch) | |
tree | 063798469ae8251bf974df5e562ee861fd8815e4 /src | |
parent | 8a926aa499aeb5d6db30fb13a2b6aa8239cfa8c5 (diff) | |
parent | a80cf2cbf549be3d6f788a7bf391e3c7522e24ce (diff) | |
download | packages_apps_settings-b2835ebdbdad14792b54865568db06e2c6517c28.zip packages_apps_settings-b2835ebdbdad14792b54865568db06e2c6517c28.tar.gz packages_apps_settings-b2835ebdbdad14792b54865568db06e2c6517c28.tar.bz2 |
Merge "Save and restore brightness on orientation changes."
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/settings/BrightnessPreference.java | 108 |
1 files changed, 97 insertions, 11 deletions
diff --git a/src/com/android/settings/BrightnessPreference.java b/src/com/android/settings/BrightnessPreference.java index 7494e2f..00f5deb 100644 --- a/src/com/android/settings/BrightnessPreference.java +++ b/src/com/android/settings/BrightnessPreference.java @@ -17,10 +17,13 @@ package com.android.settings; import android.content.Context; +import android.os.Parcel; +import android.os.Parcelable; import android.os.RemoteException; import android.os.IPowerManager; import android.os.ServiceManager; import android.preference.SeekBarPreference; +import android.preference.Preference.BaseSavedState; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; import android.util.AttributeSet; @@ -37,12 +40,14 @@ public class BrightnessPreference extends SeekBarPreference implements private SeekBar mSeekBar; private CheckBox mCheckBox; - + private int mOldBrightness; private int mOldAutomatic; private boolean mAutomaticAvailable; - + + private boolean mRestoredOldState; + // Backlight range is from 0 - 255. Need to make sure that user // doesn't set the backlight to 0 and get stuck private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10; @@ -112,21 +117,28 @@ public class BrightnessPreference extends SeekBarPreference implements @Override protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); - + if (positiveResult) { Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, mSeekBar.getProgress() + MINIMUM_BACKLIGHT); } else { - if (mAutomaticAvailable) { - setMode(mOldAutomatic); - } - if (!mAutomaticAvailable || mOldAutomatic == 0) { - setBrightness(mOldBrightness); - } + restoreOldState(); + } + } + + private void restoreOldState() { + if (mRestoredOldState) return; + + if (mAutomaticAvailable) { + setMode(mOldAutomatic); + } + if (!mAutomaticAvailable || mOldAutomatic == 0) { + setBrightness(mOldBrightness); } + mRestoredOldState = true; } - + private void setBrightness(int brightness) { try { IPowerManager power = IPowerManager.Stub.asInterface( @@ -136,7 +148,7 @@ public class BrightnessPreference extends SeekBarPreference implements } } catch (RemoteException doe) { - } + } } private void setMode(int mode) { @@ -148,5 +160,79 @@ public class BrightnessPreference extends SeekBarPreference implements Settings.System.putInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, mode); } + + @Override + protected Parcelable onSaveInstanceState() { + final Parcelable superState = super.onSaveInstanceState(); + if (getDialog() == null || !getDialog().isShowing()) return superState; + + // Save the dialog state + final SavedState myState = new SavedState(superState); + myState.automatic = mCheckBox.isChecked(); + myState.progress = mSeekBar.getProgress(); + myState.oldAutomatic = mOldAutomatic == 1; + myState.oldProgress = mOldBrightness; + + // Restore the old state when the activity or dialog is being paused + restoreOldState(); + return myState; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (state == null || !state.getClass().equals(SavedState.class)) { + // Didn't save state for us in onSaveInstanceState + super.onRestoreInstanceState(state); + return; + } + + SavedState myState = (SavedState) state; + super.onRestoreInstanceState(myState.getSuperState()); + mOldBrightness = myState.oldProgress; + mOldAutomatic = myState.oldAutomatic ? 1 : 0; + setMode(myState.automatic ? 1 : 0); + setBrightness(myState.progress + MINIMUM_BACKLIGHT); + } + + private static class SavedState extends BaseSavedState { + + boolean automatic; + boolean oldAutomatic; + int progress; + int oldProgress; + + public SavedState(Parcel source) { + super(source); + automatic = source.readInt() == 1; + progress = source.readInt(); + oldAutomatic = source.readInt() == 1; + oldProgress = source.readInt(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + dest.writeInt(automatic ? 1 : 0); + dest.writeInt(progress); + dest.writeInt(oldAutomatic ? 1 : 0); + dest.writeInt(oldProgress); + } + + public SavedState(Parcelable superState) { + super(superState); + } + + 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]; + } + }; + } } |