Skip to content

Commit

Permalink
Merge pull request attenzione#17 from jayschwa/master
Browse files Browse the repository at this point in the history
Fix a bug that stops initial value from being set (and supporting commits)
  • Loading branch information
attenzione committed Oct 31, 2012
2 parents 860823c + 71057c1 commit 42c5fd9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ You can see some tests inside
android:key="color1"
android:title="@string/color1_title"
android:summary="@string/color1_summary"
android:defaultValue="@integer/COLOR_BLACK" <!-- HEX value also accepted (v1.1) -->
android:defaultValue="@color/pumpkin_orange" <!-- integer resources are also accepted -->
alphaSlider="true" <!-- enable alpha slider via XML -->
/>

Expand Down
2 changes: 1 addition & 1 deletion project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@

android.library=true
# Project target.
target=android-15
target=android-16
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package net.margaritov.preference.colorpicker;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
Expand All @@ -25,7 +26,6 @@
import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
Expand All @@ -43,13 +43,10 @@ public class ColorPickerPreference

View mView;
ColorPickerDialog mDialog;
int mDefaultValue = Color.BLACK;
private int mValue = Color.BLACK;
private float mDensity = 0;
private boolean mAlphaSliderEnabled = false;

private static final String androidns = "http://schemas.android.com/apk/res/android";

public ColorPickerPreference(Context context) {
super(context);
init(context, null);
Expand All @@ -64,33 +61,23 @@ public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle)
super(context, attrs, defStyle);
init(context, attrs);
}


@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return a.getColor(index, Color.BLACK);
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
onColorChanged(restoreValue ? getValue() : (Integer) defaultValue);
onColorChanged(restoreValue ? getPersistedInt(mValue) : (Integer) defaultValue);
}

private void init(Context context, AttributeSet attrs) {
mDensity = getContext().getResources().getDisplayMetrics().density;
setOnPreferenceClickListener(this);
if (attrs != null) {
String defaultValue = attrs.getAttributeValue(androidns, "defaultValue");
if (defaultValue.startsWith("#")) {
try {
mDefaultValue = convertToColorInt(defaultValue);
} catch (NumberFormatException e) {
Log.e("ColorPickerPreference", "Wrong color: " + defaultValue);
mDefaultValue = convertToColorInt("#FF000000");
}
} else {
int resourceId = attrs.getAttributeResourceValue(androidns, "defaultValue", 0);
if (resourceId != 0) {
mDefaultValue = context.getResources().getInteger(resourceId);
}
}
mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false);
}
mValue = mDefaultValue;
}

@Override
Expand Down Expand Up @@ -125,7 +112,7 @@ private void setPreviewColor() {

private Bitmap getPreviewBitmap() {
int d = (int) (mDensity * 31); //30dip
int color = getValue();
int color = mValue;
Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
int w = bm.getWidth();
int h = bm.getHeight();
Expand All @@ -143,18 +130,6 @@ private Bitmap getPreviewBitmap() {
return bm;
}

public int getValue() {
try {
if (isPersistent()) {
mValue = getPersistedInt(mDefaultValue);
}
} catch (ClassCastException e) {
mValue = mDefaultValue;
}

return mValue;
}

@Override
public void onColorChanged(int color) {
if (isPersistent()) {
Expand All @@ -175,7 +150,7 @@ public boolean onPreferenceClick(Preference preference) {
}

protected void showDialog(Bundle state) {
mDialog = new ColorPickerDialog(getContext(), getValue());
mDialog = new ColorPickerDialog(getContext(), mValue);
mDialog.setOnColorChangedListener(this);
if (mAlphaSliderEnabled) {
mDialog.setAlphaSliderVisible(true);
Expand Down

0 comments on commit 42c5fd9

Please sign in to comment.