mirror of
https://github.com/peterosterlund2/droidfish.git
synced 2024-11-23 11:31:33 +01:00
Make color picker handle configuration changes correctly
This commit is contained in:
parent
4b9eb9ba6a
commit
9548d56356
|
@ -21,7 +21,6 @@ import org.petero.droidfish.R;
|
|||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
|
@ -42,7 +41,7 @@ public class ColorPickerDialog
|
|||
private CharSequence additionalInfo;
|
||||
|
||||
public interface OnColorChangedListener {
|
||||
public void onColorChanged(int color);
|
||||
void onColorChanged(int color);
|
||||
}
|
||||
|
||||
public ColorPickerDialog(Context context, int initialColor,
|
||||
|
@ -56,10 +55,18 @@ public class ColorPickerDialog
|
|||
// To fight color banding.
|
||||
getWindow().setFormat(PixelFormat.RGBA_8888);
|
||||
|
||||
setUp(color);
|
||||
setUp(color, color);
|
||||
}
|
||||
|
||||
private void setUp(int color) {
|
||||
public void reInitUI() {
|
||||
int oldColor = mOldColor.getColor();
|
||||
int newColor = mNewColor.getColor();
|
||||
boolean alphaSlider = mColorPicker.getAlphaSliderVisible();
|
||||
setUp(oldColor, newColor);
|
||||
setAlphaSliderVisible(alphaSlider);
|
||||
}
|
||||
|
||||
private void setUp(int oldColor, int newColor) {
|
||||
setContentView(R.layout.dialog_color_picker);
|
||||
|
||||
setTitle(getContext().getText(R.string.prefs_colors_title) + " '"
|
||||
|
@ -79,8 +86,8 @@ public class ColorPickerDialog
|
|||
mOldColor.setOnClickListener(this);
|
||||
mNewColor.setOnClickListener(this);
|
||||
mColorPicker.setOnColorChangedListener(this);
|
||||
mOldColor.setColor(color);
|
||||
mColorPicker.setColor(color, true);
|
||||
mOldColor.setColor(oldColor);
|
||||
mColorPicker.setColor(newColor, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -107,25 +114,9 @@ public class ColorPickerDialog
|
|||
@Override
|
||||
public void onClick(View v) {
|
||||
if (v.getId() == R.id.new_color_panel) {
|
||||
if (mListener != null) {
|
||||
if (mListener != null)
|
||||
mListener.onColorChanged(mNewColor.getColor());
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,12 +17,11 @@
|
|||
package net.margaritov.preference.colorpicker;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.res.Configuration;
|
||||
import android.graphics.Bitmap;
|
||||
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;
|
||||
|
@ -30,6 +29,8 @@ import android.view.View;
|
|||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import org.petero.droidfish.activities.Preferences;
|
||||
|
||||
/**
|
||||
* @author Sergey Margaritov
|
||||
*/
|
||||
|
@ -38,7 +39,9 @@ public class ColorPickerPreference
|
|||
Preference
|
||||
implements
|
||||
Preference.OnPreferenceClickListener,
|
||||
ColorPickerDialog.OnColorChangedListener {
|
||||
ColorPickerDialog.OnColorChangedListener,
|
||||
DialogInterface.OnDismissListener,
|
||||
Preferences.ConfigChangedListener {
|
||||
|
||||
private View mView;
|
||||
private ColorPickerDialog mDialog;
|
||||
|
@ -186,22 +189,39 @@ public class ColorPickerPreference
|
|||
}
|
||||
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
showDialog(null);
|
||||
showDialog();
|
||||
return false;
|
||||
}
|
||||
|
||||
private void showDialog(Bundle state) {
|
||||
private void showDialog() {
|
||||
mDialog = new ColorPickerDialog(getContext(), getValue(), getTitle());
|
||||
mDialog.setOnColorChangedListener(this);
|
||||
if (mAlphaSliderEnabled) {
|
||||
if (mAlphaSliderEnabled)
|
||||
mDialog.setAlphaSliderVisible(true);
|
||||
}
|
||||
if (state != null) {
|
||||
mDialog.onRestoreInstanceState(state);
|
||||
}
|
||||
mDialog.setOnColorChangedListener(this);
|
||||
mDialog.setOnDismissListener(this);
|
||||
addRemoveConfigChangedListener();
|
||||
mDialog.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismiss(DialogInterface dialog) {
|
||||
mDialog = null;
|
||||
addRemoveConfigChangedListener();
|
||||
}
|
||||
|
||||
private void addRemoveConfigChangedListener() {
|
||||
Context context = getContext();
|
||||
if (context instanceof Preferences) {
|
||||
Preferences prefs = ((Preferences)context);
|
||||
prefs.addRemoveConfigChangedListener(this, mDialog != null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
if (mDialog != null)
|
||||
mDialog.reInitUI();
|
||||
}
|
||||
/**
|
||||
* Toggle Alpha Slider visibility (by default it's disabled)
|
||||
*/
|
||||
|
@ -256,59 +276,4 @@ public class ColorPickerPreference
|
|||
|
||||
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 Creator<SavedState> CREATOR = new Creator<SavedState>() {
|
||||
public SavedState createFromParcel(Parcel in) {
|
||||
return new SavedState(in);
|
||||
}
|
||||
|
||||
public SavedState[] newArray(int size) {
|
||||
return new SavedState[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -773,6 +773,10 @@ public class ColorPickerView extends View {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean getAlphaSliderVisible() {
|
||||
return mShowAlphaPanel;
|
||||
}
|
||||
|
||||
public void setSliderTrackerColor(int color) {
|
||||
mSliderTrackerColor = color;
|
||||
mHueTrackerPaint.setColor(mSliderTrackerColor);
|
||||
|
|
|
@ -39,7 +39,9 @@ import android.widget.AbsListView.OnScrollListener;
|
|||
import android.widget.ListView;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class Preferences extends PreferenceActivity {
|
||||
private static int currentItem = -1;
|
||||
|
@ -106,9 +108,26 @@ public class Preferences extends PreferenceActivity {
|
|||
editor.apply();
|
||||
}
|
||||
|
||||
|
||||
public interface ConfigChangedListener {
|
||||
void onConfigurationChanged(Configuration newConfig);
|
||||
}
|
||||
|
||||
private Set<ConfigChangedListener> configChangedListeners = new HashSet<>();
|
||||
|
||||
public void addRemoveConfigChangedListener(ConfigChangedListener listener, boolean add) {
|
||||
if (add) {
|
||||
configChangedListeners.add(listener);
|
||||
} else {
|
||||
configChangedListeners.remove(listener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
super.onConfigurationChanged(newConfig);
|
||||
for (ConfigChangedListener cl : configChangedListeners)
|
||||
cl.onConfigurationChanged(newConfig);
|
||||
}
|
||||
|
||||
public interface ActivityHandler {
|
||||
|
|
Loading…
Reference in New Issue
Block a user