diff options
author | Danny Baumann <dannybaumann@web.de> | 2013-04-18 14:37:09 +0200 |
---|---|---|
committer | Danny Baumann <dannybaumann@web.de> | 2013-04-18 18:17:05 +0200 |
commit | 54ffa7ac8ff99bf31ff2dbd1ce5c52140b4c7cbe (patch) | |
tree | bbd97aa9eb7f33bbbc733ba7b2db21d0fcae3cd9 | |
parent | b703712df50be38f7b96a6496af90fd3a8897e5f (diff) | |
download | packages_apps_Settings-54ffa7ac8ff99bf31ff2dbd1ce5c52140b4c7cbe.zip packages_apps_Settings-54ffa7ac8ff99bf31ff2dbd1ce5c52140b4c7cbe.tar.gz packages_apps_Settings-54ffa7ac8ff99bf31ff2dbd1ce5c52140b4c7cbe.tar.bz2 |
Correctly restore brightness customize dialog hierarchy.
This is especially important when rotating the device.
Change-Id: Iaf1ceb46943e9d0a171d6a75d3d446dbec205095
-rw-r--r-- | src/com/android/settings/BrightnessPreference.java | 39 | ||||
-rw-r--r-- | src/com/android/settings/cyanogenmod/AutoBrightnessCustomizeDialog.java | 499 |
2 files changed, 395 insertions, 143 deletions
diff --git a/src/com/android/settings/BrightnessPreference.java b/src/com/android/settings/BrightnessPreference.java index 6c2768d..42e307f 100644 --- a/src/com/android/settings/BrightnessPreference.java +++ b/src/com/android/settings/BrightnessPreference.java @@ -64,6 +64,8 @@ public class BrightnessPreference extends SeekBarDialogPreference implements private boolean mRestoredOldState; + private AutoBrightnessCustomizeDialog mCustomizeDialog; + private static final int SEEK_BAR_RANGE = 10000; private ContentObserver mBrightnessObserver = new ContentObserver(new Handler()) { @@ -119,8 +121,7 @@ public class BrightnessPreference extends SeekBarDialogPreference implements adjustButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - Dialog d = new AutoBrightnessCustomizeDialog(getContext()); - d.show(); + showCustomizeDialog(null); } }); } @@ -306,6 +307,26 @@ public class BrightnessPreference extends SeekBarDialogPreference implements Settings.System.SCREEN_BRIGHTNESS_MODE, mode); } + private void showCustomizeDialog(Bundle state) { + if (mCustomizeDialog != null && mCustomizeDialog.isShowing()) { + return; + } + + mCustomizeDialog = new AutoBrightnessCustomizeDialog(getContext()); + if (state != null) { + mCustomizeDialog.onRestoreInstanceState(state); + } + mCustomizeDialog.show(); + } + + @Override + public void onActivityDestroy() { + super.onActivityDestroy(); + if (mCustomizeDialog != null) { + mCustomizeDialog.dismiss(); + } + } + @Override protected Parcelable onSaveInstanceState() { final Parcelable superState = super.onSaveInstanceState(); @@ -318,6 +339,10 @@ public class BrightnessPreference extends SeekBarDialogPreference implements myState.oldAutomatic = mOldAutomatic == 1; myState.oldProgress = mOldBrightness; myState.curBrightness = mCurBrightness; + myState.customizeDialogShown = mCustomizeDialog != null && mCustomizeDialog.isShowing(); + if (myState.customizeDialogShown) { + myState.customizeDialogState = mCustomizeDialog.onSaveInstanceState(); + } // Restore the old state when the activity or dialog is being paused restoreOldState(); @@ -339,6 +364,10 @@ public class BrightnessPreference extends SeekBarDialogPreference implements setMode(myState.automatic ? 1 : 0); setBrightness(myState.progress, false); mCurBrightness = myState.curBrightness; + + if (myState.customizeDialogShown) { + showCustomizeDialog(myState.customizeDialogState); + } } private static class SavedState extends BaseSavedState { @@ -348,6 +377,8 @@ public class BrightnessPreference extends SeekBarDialogPreference implements int progress; int oldProgress; int curBrightness; + boolean customizeDialogShown; + Bundle customizeDialogState; public SavedState(Parcel source) { super(source); @@ -356,6 +387,8 @@ public class BrightnessPreference extends SeekBarDialogPreference implements oldAutomatic = source.readInt() == 1; oldProgress = source.readInt(); curBrightness = source.readInt(); + customizeDialogShown = source.readInt() == 1; + customizeDialogState = source.readBundle(); } @Override @@ -366,6 +399,8 @@ public class BrightnessPreference extends SeekBarDialogPreference implements dest.writeInt(oldAutomatic ? 1 : 0); dest.writeInt(oldProgress); dest.writeInt(curBrightness); + dest.writeInt(customizeDialogShown ? 1 : 0); + dest.writeBundle(customizeDialogState); } public SavedState(Parcelable superState) { diff --git a/src/com/android/settings/cyanogenmod/AutoBrightnessCustomizeDialog.java b/src/com/android/settings/cyanogenmod/AutoBrightnessCustomizeDialog.java index 4901f29..38f8235 100644 --- a/src/com/android/settings/cyanogenmod/AutoBrightnessCustomizeDialog.java +++ b/src/com/android/settings/cyanogenmod/AutoBrightnessCustomizeDialog.java @@ -56,7 +56,7 @@ import java.util.Locale; import com.android.settings.R; public class AutoBrightnessCustomizeDialog extends AlertDialog - implements DialogInterface.OnClickListener { + implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener { private static final String TAG = "AutoBrightnessCustomizeDialog"; private TextView mSensorLevel; @@ -78,6 +78,11 @@ public class AutoBrightnessCustomizeDialog extends AlertDialog private int mMinLevel; private boolean mIsDefault; + private AlertDialog mHelpDialog, mPreviewDialog; + private AlertDialog mSetupDialog, mSplitDialog; + private int mDialogPosition; + private boolean mWasRestored; + private SensorEventListener mLightSensorListener = new SensorEventListener() { @Override public void onSensorChanged(SensorEvent event) { @@ -149,7 +154,9 @@ public class AutoBrightnessCustomizeDialog extends AlertDialog @Override protected void onStart() { - updateSettings(false); + if (!mWasRestored) { + updateSettings(false); + } super.onStart(); @@ -169,6 +176,18 @@ public class AutoBrightnessCustomizeDialog extends AlertDialog protected void onStop() { super.onStop(); mSensorManager.unregisterListener(mLightSensorListener, mLightSensor); + if (mHelpDialog != null) { + mHelpDialog.dismiss(); + } + if (mPreviewDialog != null) { + mPreviewDialog.dismiss(); + } + if (mSetupDialog != null) { + mSetupDialog.dismiss(); + } + if (mSplitDialog != null) { + mSplitDialog.dismiss(); + } } @Override @@ -193,10 +212,10 @@ public class AutoBrightnessCustomizeDialog extends AlertDialog switch (item.getItemId() - Menu.FIRST) { case 0: - showSetup(position); + showSetup(position, null); return true; case 1: - showSplitDialog(position); + showSplitDialog(position, null); break; case 2: mAdapter.removeRow(position); @@ -215,6 +234,85 @@ public class AutoBrightnessCustomizeDialog extends AlertDialog } } + @Override + public void onDismiss(DialogInterface dialog) { + if (dialog == mHelpDialog) { + mHelpDialog = null; + } else if (dialog == mPreviewDialog) { + mPreviewDialog = null; + } else if (dialog == mSetupDialog) { + mSetupDialog = null; + } else if (dialog == mSplitDialog) { + mSplitDialog = null; + } + } + + private static final String DEFAULT_TAG = "AutoBrightnessCustomize:isDefault"; + private static final String LUX_TAG = "AutoBrightnessCustomize:luxValues"; + private static final String BACKLIGHT_TAG = "AutoBrightnessCustomize:backlightValues"; + private static final String HELP_TAG = "AutoBrightnessCustomize:helpShowing"; + private static final String PREVIEW_TAG = "AutoBrightnessCustomize:previewShowing"; + private static final String SETUP_TAG = "AutoBrightnessCustomize:setupState"; + private static final String SPLIT_TAG = "AutoBrightnessCustomize:splitState"; + private static final String POSITION_TAG = "AutoBrightnessCustomize:dialogPosition"; + + @Override + public Bundle onSaveInstanceState() { + Bundle state = super.onSaveInstanceState(); + + state.putBoolean(DEFAULT_TAG, mIsDefault); + state.putIntArray(LUX_TAG, mAdapter.getLuxValues()); + state.putIntArray(BACKLIGHT_TAG, mAdapter.getBacklightValues()); + state.putInt(POSITION_TAG, mDialogPosition); + state.putBoolean(HELP_TAG, mHelpDialog != null && mHelpDialog.isShowing()); + state.putBoolean(PREVIEW_TAG, mPreviewDialog != null && mPreviewDialog.isShowing()); + + Bundle setupState = null; + if (mSetupDialog != null && mSetupDialog.isShowing()) { + setupState = mSetupDialog.onSaveInstanceState(); + } + state.putBundle(SETUP_TAG, setupState); + + Bundle splitState = null; + if (mSplitDialog != null && mSplitDialog.isShowing()) { + splitState = mSplitDialog.onSaveInstanceState(); + } + state.putBundle(SPLIT_TAG, splitState); + + return state; + } + + @Override + public void onRestoreInstanceState(Bundle state) { + super.onRestoreInstanceState(state); + + mIsDefault = state.getBoolean(DEFAULT_TAG); + mDialogPosition = state.getInt(POSITION_TAG); + + int[] lux = state.getIntArray(LUX_TAG); + int[] backlight = state.getIntArray(BACKLIGHT_TAG); + mAdapter.initFromSettings(lux, backlight); + + Bundle setupState = state.getBundle(SETUP_TAG); + if (setupState != null) { + showSetup(mDialogPosition, setupState); + } + + Bundle splitState = state.getBundle(SPLIT_TAG); + if (splitState != null) { + showSplitDialog(mDialogPosition, splitState); + } + + if (state.getBoolean(HELP_TAG)) { + showHelp(); + } + if (state.getBoolean(PREVIEW_TAG)) { + showPreview(); + } + + mWasRestored = true; + } + private void updateSettings(boolean forceDefault) { int[] lux = null, values = null; @@ -241,19 +339,27 @@ public class AutoBrightnessCustomizeDialog extends AlertDialog } private void showHelp() { + if (mHelpDialog != null && mHelpDialog.isShowing()) { + return; + } + final View v = getLayoutInflater().inflate(R.layout.auto_brightness_help, null); - final AlertDialog d = new AlertDialog.Builder(getContext()) + mHelpDialog = new AlertDialog.Builder(getContext()) .setTitle(R.string.auto_brightness_help_dialog_title) .setCancelable(true) .setView(v) .setNegativeButton(R.string.auto_brightness_close_button, null) .create(); - - d.show(); + mHelpDialog.setOnDismissListener(this); + mHelpDialog.show(); } private void showPreview() { + if (mPreviewDialog != null && mPreviewDialog.isShowing()) { + return; + } + final int n = mAdapter.getCount(); float[] x = new float[n]; float[] y = new float[n]; @@ -270,151 +376,42 @@ public class AutoBrightnessCustomizeDialog extends AlertDialog (CubicSplinePreviewView) v.findViewById(R.id.brightness_preview); preview.setSpline(x, y); - final AlertDialog d = new AlertDialog.Builder(getContext()) + mPreviewDialog = new AlertDialog.Builder(getContext()) .setTitle(R.string.auto_brightness_preview_dialog_title) .setCancelable(true) .setView(v) .setNegativeButton(R.string.auto_brightness_close_button, null) .create(); - - d.show(); + mPreviewDialog.setOnDismissListener(this); + mPreviewDialog.show(); } - private void showSetup(final int position) { - final SettingRow row = mAdapter.getItem(position); - final View v = getLayoutInflater().inflate(R.layout.auto_brightness_level_setup, null); - final EditText lux = (EditText) v.findViewById(R.id.lux); - final SeekBar backlightBar = (SeekBar) v.findViewById(R.id.backlight); - final EditText backlightInput = (EditText) v.findViewById(R.id.backlight_input); - final DecimalFormat backlightInputFormat = new DecimalFormat("0.0", - DecimalFormatSymbols.getInstance(Locale.US)); - - final AlertDialog d = new AlertDialog.Builder(getContext()) - .setTitle(R.string.auto_brightness_level_dialog_title) - .setCancelable(true) - .setView(v) - .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - try { - int newLux = Integer.valueOf(lux.getText().toString()); - mAdapter.setLuxForRow(position, newLux); - } catch (NumberFormatException e) { - //ignored - } - } - }) - .setNegativeButton(R.string.cancel, null) - .create(); - - backlightBar.setMax(brightnessToProgress(PowerManager.BRIGHTNESS_ON)); - - lux.setText(String.valueOf(row.lux)); - backlightBar.setProgress(brightnessToProgress(row.backlight)); - backlightInput.setText(backlightInputFormat.format(brightnessToPercent(row.backlight))); - - backlightBar.setOnSeekBarChangeListener(new BrightnessSeekBarChangeListener() { - @Override - protected int getPosition(SeekBar seekBar) { - return position; - } - - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - super.onProgressChanged(seekBar, progress, fromUser); - - float brightness = progressToBrightness(seekBar.getProgress()); - backlightInput.setText(backlightInputFormat.format(brightnessToPercent(brightness))); - } - }); - - backlightInput.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - @Override - public void afterTextChanged(Editable s) { - boolean ok = false; - try { - float minValue = position == 0 - ? mMinLevel - : mAdapter.getItem(position - 1).backlight; - float maxValue = mAdapter.isLastItem(position) - ? PowerManager.BRIGHTNESS_ON - : mAdapter.getItem(position + 1).backlight; - float newBrightness = percentToBrightness(Float.valueOf(s.toString())); - - if (newBrightness >= minValue && newBrightness <= maxValue) { - ok = true; - backlightBar.setProgress(brightnessToProgress(newBrightness)); - } - } catch (NumberFormatException e) { - //ignored, ok is false anyway - } - - Button okButton = d.getButton(DialogInterface.BUTTON_POSITIVE); - if (okButton != null) { - okButton.setEnabled(ok); - } - } - }); + private void showSetup(int position, Bundle state) { + if (mSetupDialog != null && mSetupDialog.isShowing()) { + return; + } - d.show(); + mDialogPosition = position; + mSetupDialog = new RowSetupDialog(getContext(), position); + mSetupDialog.setOnDismissListener(this); + if (state != null) { + mSetupDialog.onRestoreInstanceState(state); + } + mSetupDialog.show(); } - private void showSplitDialog(final int position) { - final SettingRow row = mAdapter.getItem(position); - final View v = getLayoutInflater().inflate(R.layout.auto_brightness_split_dialog, null); - final TextView label = (TextView) v.findViewById(R.id.split_label); - final EditText value = (EditText) v.findViewById(R.id.split_position); - - final AlertDialog d = new AlertDialog.Builder(getContext()) - .setTitle(R.string.auto_brightness_split_dialog_title) - .setCancelable(true) - .setView(v) - .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - int splitLux = Integer.valueOf(value.getText().toString()); - mAdapter.splitRow(position, splitLux); - } - }) - .setNegativeButton(R.string.cancel, null) - .create(); - - final int minLux = row.lux + 1; - final int maxLux = mAdapter.isLastItem(position) ? 0 : mAdapter.getItem(position + 1).lux - 1; - - label.setText(getContext().getString(R.string.auto_brightness_split_lux_format, - minLux, maxLux)); - value.setText(String.valueOf(minLux)); - value.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence s, int start, int count, int after) { - } - @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - } - @Override - public void afterTextChanged(Editable s) { - boolean ok = false; - try { - int newLux = Integer.valueOf(s.toString()); - ok = newLux >= minLux && newLux <= maxLux; - } catch (NumberFormatException e) { - //ignored, ok is false anyway - } - Button okButton = d.getButton(DialogInterface.BUTTON_POSITIVE); - if (okButton != null) { - okButton.setEnabled(ok); - } - } - }); + private void showSplitDialog(final int position, Bundle state) { + if (mSplitDialog != null && mSplitDialog.isShowing()) { + return; + } - d.show(); + mDialogPosition = position; + mSplitDialog = new RowSplitDialog(getContext(), position); + mSplitDialog.setOnDismissListener(this); + if (state != null) { + mSplitDialog.onRestoreInstanceState(state); + } + mSplitDialog.show(); } private void showResetConfirmation() { @@ -485,6 +482,226 @@ public class AutoBrightnessCustomizeDialog extends AlertDialog Settings.System.putString(getContext().getContentResolver(), setting, value); } + private class RowSetupDialog extends AlertDialog implements DialogInterface.OnClickListener { + private static final String LUX_TAG = "AutoBrightnessCustomize:Edit:lux"; + private static final String BACKLIGHT_TAG = "AutoBrightnessCustomize:Edit:backlight"; + + private EditText mLuxInput; + private SeekBar mBacklightBar; + private EditText mBacklightInput; + + private int mPosition; + private DecimalFormat mBacklightInputFormat = new DecimalFormat("0.0", + DecimalFormatSymbols.getInstance(Locale.US)); + + public RowSetupDialog(Context context, int position) { + super(context); + mPosition = position; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + final View v = getLayoutInflater().inflate(R.layout.auto_brightness_level_setup, null); + final Context context = getContext(); + + mLuxInput = (EditText) v.findViewById(R.id.lux); + mBacklightBar = (SeekBar) v.findViewById(R.id.backlight); + mBacklightInput = (EditText) v.findViewById(R.id.backlight_input); + + setTitle(R.string.auto_brightness_level_dialog_title); + setCancelable(true); + setView(v); + + mBacklightBar.setMax(brightnessToProgress(PowerManager.BRIGHTNESS_ON)); + initListeners(); + initFromPosition(); + + setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.ok), this); + setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel), this); + + super.onCreate(savedInstanceState); + } + + @Override + public Bundle onSaveInstanceState() { + Bundle state = super.onSaveInstanceState(); + state.putCharSequence(LUX_TAG, mLuxInput.getText()); + state.putInt(BACKLIGHT_TAG, mBacklightBar.getProgress()); + return state; + } + + @Override + public void onRestoreInstanceState(Bundle state) { + super.onRestoreInstanceState(state); + mLuxInput.setText(state.getCharSequence(LUX_TAG)); + mBacklightBar.setProgress(state.getInt(BACKLIGHT_TAG)); + } + + @Override + public void onClick(DialogInterface dialog, int which) { + if (which == DialogInterface.BUTTON_POSITIVE) { + try { + int newLux = Integer.valueOf(mLuxInput.getText().toString()); + mAdapter.setLuxForRow(mPosition, newLux); + } catch (NumberFormatException e) { + //ignored + } + } + } + + private void initFromPosition() { + final SettingRow row = mAdapter.getItem(mPosition); + + mLuxInput.setText(String.valueOf(row.lux)); + mBacklightBar.setProgress(brightnessToProgress(row.backlight)); + mBacklightInput.setText(mBacklightInputFormat.format(brightnessToPercent(row.backlight))); + } + + private void initListeners() { + mBacklightBar.setOnSeekBarChangeListener(new BrightnessSeekBarChangeListener() { + @Override + protected int getPosition(SeekBar seekBar) { + return mPosition; + } + + @Override + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { + super.onProgressChanged(seekBar, progress, fromUser); + + float brightness = progressToBrightness(seekBar.getProgress()); + mBacklightInput.setText(mBacklightInputFormat.format(brightnessToPercent(brightness))); + } + }); + + mBacklightInput.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + @Override + public void afterTextChanged(Editable s) { + boolean ok = false; + try { + float minValue = mPosition == 0 + ? mMinLevel + : mAdapter.getItem(mPosition - 1).backlight; + float maxValue = mAdapter.isLastItem(mPosition) + ? PowerManager.BRIGHTNESS_ON + : mAdapter.getItem(mPosition + 1).backlight; + float newBrightness = percentToBrightness(Float.valueOf(s.toString())); + + if (newBrightness >= minValue && newBrightness <= maxValue) { + ok = true; + mBacklightBar.setProgress(brightnessToProgress(newBrightness)); + } + } catch (NumberFormatException e) { + //ignored, ok is false anyway + } + + Button okButton = mSetupDialog.getButton(DialogInterface.BUTTON_POSITIVE); + if (okButton != null) { + okButton.setEnabled(ok); + } + } + }); + } + } + + private class RowSplitDialog extends AlertDialog implements DialogInterface.OnClickListener { + private static final String SPLIT_LUX_TAG = "AutoBrightnessCustomize:Split:splitLux"; + + private TextView mLabel; + private EditText mValue; + + private int mPosition; + private int mMinLux, mMaxLux; + + public RowSplitDialog(Context context, int position) { + super(context); + mPosition = position; + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + final View v = getLayoutInflater().inflate(R.layout.auto_brightness_split_dialog, null); + final Context context = getContext(); + + mLabel = (TextView) v.findViewById(R.id.split_label); + mValue = (EditText) v.findViewById(R.id.split_position); + + setTitle(R.string.auto_brightness_split_dialog_title); + setCancelable(true); + setView(v); + + initValues(); + initListener(); + + setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.ok), this); + setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel), this); + + super.onCreate(savedInstanceState); + } + + @Override + public Bundle onSaveInstanceState() { + Bundle state = super.onSaveInstanceState(); + state.putCharSequence(SPLIT_LUX_TAG, mValue.getText()); + return state; + } + + @Override + public void onRestoreInstanceState(Bundle state) { + super.onRestoreInstanceState(state); + mValue.setText(state.getCharSequence(SPLIT_LUX_TAG)); + } + + @Override + public void onClick(DialogInterface dialog, int which) { + if (which == DialogInterface.BUTTON_POSITIVE) { + int splitLux = Integer.valueOf(mValue.getText().toString()); + mAdapter.splitRow(mPosition, splitLux); + } + } + + private void initValues() { + final SettingRow row = mAdapter.getItem(mPosition); + + mMinLux = row.lux + 1; + mMaxLux = mAdapter.isLastItem(mPosition) ? 0 : mAdapter.getItem(mPosition + 1).lux - 1; + + mLabel.setText(getContext().getString(R.string.auto_brightness_split_lux_format, + mMinLux, mMaxLux)); + mValue.setText(String.valueOf(mMinLux)); + } + + private void initListener() { + mValue.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + } + @Override + public void afterTextChanged(Editable s) { + boolean ok = false; + try { + int newLux = Integer.valueOf(s.toString()); + ok = newLux >= mMinLux && newLux <= mMaxLux; + } catch (NumberFormatException e) { + //ignored, ok is false anyway + } + Button okButton = getButton(DialogInterface.BUTTON_POSITIVE); + if (okButton != null) { + okButton.setEnabled(ok); + } + } + }); + } + } + private class SettingRowAdapter extends ArrayAdapter<SettingRow> { public SettingRowAdapter(Context context, ArrayList<SettingRow> rows) { super(context, 0, rows); |