ColorPickerPreference: Introduce set default button when attribute is set
Set a default value using the "defaultColorValue" settings namespace attribute. The general idea is often we want set a value of say "-1" and let framework handle it. One such scenario would be if you are changing the color of a ImageView using ColorFilter. When framework receives a "-1" value, framework sets the ImageView ColorFilter to null, restoring the ImageView to original state. Screenshot: https://teameos.slack.com/files/bigrushdog/F09T5S155/screenshot_2015-08-31-04-02-54.png Change-Id: I4ad63576270de344a9e7430d9e63a248d37afd9f Signed-off-by: xyyx <xyyx@mail.ru>
This commit is contained in:
committed by
hungphan2001
parent
0c156f71ba
commit
635b911717
@@ -20,4 +20,9 @@
|
||||
<attr name="min" format="integer" />
|
||||
<attr name="units" format="string|reference" />
|
||||
</declare-styleable>
|
||||
|
||||
<!-- Value to pass to callback when restore button is pressed -->
|
||||
<declare-styleable name="ColorPreference">
|
||||
<attr name="defaultColorValue" format="integer" />
|
||||
</declare-styleable>
|
||||
</resources>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
* Copyright (C) 2011 Sergey Margaritov
|
||||
* Copyright (C) 2013 Slimroms
|
||||
* Copyright (C) 2015 The TeamEos Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,12 +29,11 @@ import android.os.Parcelable;
|
||||
import android.support.v7.preference.*;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
/**
|
||||
* A preference type that allows a user to choose a time
|
||||
*
|
||||
@@ -49,6 +49,12 @@ public class ColorPickerPreference extends Preference implements
|
||||
private float mDensity = 0;
|
||||
private boolean mAlphaSliderEnabled = false;
|
||||
|
||||
// if we return -6, button is not enabled
|
||||
static final String SETTINGS_NS = "http://schemas.android.com/apk/res/com.android.settings";
|
||||
static final int DEF_VALUE_DEFAULT = -6;
|
||||
boolean mUsesDefaultButton = false;
|
||||
int mDefValue = -1;
|
||||
|
||||
private EditText mEditText;
|
||||
|
||||
public ColorPickerPreference(Context context) {
|
||||
@@ -81,6 +87,11 @@ public class ColorPickerPreference extends Preference implements
|
||||
setOnPreferenceClickListener(this);
|
||||
if (attrs != null) {
|
||||
mAlphaSliderEnabled = attrs.getAttributeBooleanValue(null, "alphaSlider", false);
|
||||
int defVal = attrs.getAttributeIntValue(SETTINGS_NS, "defaultColorValue", DEF_VALUE_DEFAULT);
|
||||
if (defVal != DEF_VALUE_DEFAULT) {
|
||||
mUsesDefaultButton = true;
|
||||
mDefValue = defVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,9 +103,67 @@ public class ColorPickerPreference extends Preference implements
|
||||
widgetFrameView = ((LinearLayout) view
|
||||
.findViewById(android.R.id.widget_frame));
|
||||
|
||||
if (mUsesDefaultButton) {
|
||||
setDefaultButton();
|
||||
}
|
||||
|
||||
setPreviewColor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore a default value, not necessarily a color
|
||||
* For example: Set default value to -1 to remove a color filter
|
||||
*
|
||||
* @author Randall Rushing aka Bigrushdog
|
||||
*/
|
||||
private void setDefaultButton() {
|
||||
if (mView == null)
|
||||
return;
|
||||
|
||||
LinearLayout widgetFrameView = ((LinearLayout) mView
|
||||
.findViewById(android.R.id.widget_frame));
|
||||
if (widgetFrameView == null)
|
||||
return;
|
||||
|
||||
ImageView defView = new ImageView(getContext());
|
||||
widgetFrameView.setOrientation(LinearLayout.HORIZONTAL);
|
||||
|
||||
// remove already created default button
|
||||
int count = widgetFrameView.getChildCount();
|
||||
if (count > 0) {
|
||||
View oldView = widgetFrameView.findViewWithTag("default");
|
||||
View spacer = widgetFrameView.findViewWithTag("spacer");
|
||||
if (oldView != null) {
|
||||
widgetFrameView.removeView(oldView);
|
||||
}
|
||||
if (spacer != null) {
|
||||
widgetFrameView.removeView(spacer);
|
||||
}
|
||||
}
|
||||
|
||||
widgetFrameView.addView(defView);
|
||||
widgetFrameView.setMinimumWidth(0);
|
||||
defView.setBackground(getContext().getDrawable(android.R.drawable.ic_menu_revert));
|
||||
defView.setTag("default");
|
||||
defView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
try {
|
||||
getOnPreferenceChangeListener().onPreferenceChange(ColorPickerPreference.this,
|
||||
Integer.valueOf(mDefValue));
|
||||
} catch (NullPointerException e) {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// sorcery for a linear layout ugh
|
||||
View spacer = new View(getContext());
|
||||
spacer.setTag("spacer");
|
||||
spacer.setLayoutParams(new LinearLayout.LayoutParams((int) (mDensity * 16),
|
||||
LayoutParams.MATCH_PARENT));
|
||||
widgetFrameView.addView(spacer);
|
||||
}
|
||||
|
||||
private void setPreviewColor() {
|
||||
if (mView == null)
|
||||
return;
|
||||
@@ -115,12 +184,22 @@ public class ColorPickerPreference extends Preference implements
|
||||
// remove already create preview image
|
||||
int count = widgetFrameView.getChildCount();
|
||||
if (count > 0) {
|
||||
widgetFrameView.removeViews(0, count);
|
||||
View preview = widgetFrameView.findViewWithTag("preview");
|
||||
if (preview != null) {
|
||||
widgetFrameView.removeView(preview);
|
||||
}
|
||||
}
|
||||
widgetFrameView.addView(iView);
|
||||
widgetFrameView.setMinimumWidth(0);
|
||||
iView.setBackgroundDrawable(new AlphaPatternDrawable((int) (5 * mDensity)));
|
||||
iView.setImageBitmap(getPreviewBitmap());
|
||||
iView.setTag("preview");
|
||||
iView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
showDialog(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Bitmap getPreviewBitmap() {
|
||||
@@ -161,7 +240,7 @@ public class ColorPickerPreference extends Preference implements
|
||||
}
|
||||
|
||||
public boolean onPreferenceClick(Preference preference) {
|
||||
showDialog(null);
|
||||
//showDialog(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user