Skip to content

Commit

Permalink
Merge pull request attenzione#16 from rovo89/for-upstream
Browse files Browse the repository at this point in the history
Fix: Restore color picker after rotating
thanx you ;)
  • Loading branch information
attenzione committed May 14, 2012
2 parents 490d5ad + 164088e commit 860823c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
17 changes: 15 additions & 2 deletions src/net/margaritov/preference/colorpicker/ColorPickerDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package net.margaritov.preference.colorpicker;

import net.margaritov.preference.colorpicker.R;

import android.app.Dialog;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -126,4 +125,18 @@ public void onClick(View v) {
dismiss();
}

@Override
public Bundle onSaveInstanceState() {
Bundle state = super.onSaveInstanceState();
state.putInt("old_color", mOldColor.getColor());
state.putInt("new_color", mNewColor.getColor());
return state;
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mOldColor.setColor(savedInstanceState.getInt("old_color"));
mColorPicker.setColor(savedInstanceState.getInt("new_color"), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
Expand All @@ -39,6 +42,7 @@ public class ColorPickerPreference
ColorPickerDialog.OnColorChangedListener {

View mView;
ColorPickerDialog mDialog;
int mDefaultValue = Color.BLACK;
private int mValue = Color.BLACK;
private float mDensity = 0;
Expand Down Expand Up @@ -166,14 +170,20 @@ public void onColorChanged(int color) {
}

public boolean onPreferenceClick(Preference preference) {
ColorPickerDialog picker = new ColorPickerDialog(getContext(), getValue());
picker.setOnColorChangedListener(this);
showDialog(null);
return false;
}

protected void showDialog(Bundle state) {
mDialog = new ColorPickerDialog(getContext(), getValue());
mDialog.setOnColorChangedListener(this);
if (mAlphaSliderEnabled) {
picker.setAlphaSliderVisible(true);
mDialog.setAlphaSliderVisible(true);
}
picker.show();

return false;
if (state != null) {
mDialog.onRestoreInstanceState(state);
}
mDialog.show();
}

/**
Expand Down Expand Up @@ -243,5 +253,60 @@ else if (argb.length() == 6) {

return Color.argb(alpha, red, green, blue);
}

@Override
protected Parcelable onSaveInstanceState() {
final Parcelable superState = super.onSaveInstanceState();
if (mDialog == null || !mDialog.isShowing()) {
return superState;
}

final SavedState myState = new SavedState(superState);
myState.dialogBundle = mDialog.onSaveInstanceState();
return myState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state == null || !(state instanceof SavedState)) {
// Didn't save state for us in onSaveInstanceState
super.onRestoreInstanceState(state);
return;
}

SavedState myState = (SavedState) state;
super.onRestoreInstanceState(myState.getSuperState());
showDialog(myState.dialogBundle);
}

private static class SavedState extends BaseSavedState {
Bundle dialogBundle;

public SavedState(Parcel source) {
super(source);
dialogBundle = source.readBundle();
}

@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeBundle(dialogBundle);
}

public SavedState(Parcelable superState) {
super(superState);
}

@SuppressWarnings("unused")
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];
}
};
}
}

0 comments on commit 860823c

Please sign in to comment.