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

Signed-off-by: xyyx <xyyx@mail.ru>

Change-Id: I4ad63576270de344a9e7430d9e63a248d37afd9f
diff --git a/res/values/attrs.xml b/res/values/attrs.xml
index 91903c2..34f028c 100644
--- a/res/values/attrs.xml
+++ b/res/values/attrs.xml
@@ -40,4 +40,9 @@
         <attr name="previewBetweenText" format="string" />
     </declare-styleable>
 
+    <!-- Value to pass to callback when restore button is pressed -->
+    <declare-styleable name="ColorPreference">
+        <attr name="defaultColorValue" format="integer" />
+    </declare-styleable>
+
 </resources>
diff --git a/src/com/bliss/support/colorpicker/ColorPickerPreference.java b/src/com/bliss/support/colorpicker/ColorPickerPreference.java
index 17c3e31..01b0978 100644
--- a/src/com/bliss/support/colorpicker/ColorPickerPreference.java
+++ b/src/com/bliss/support/colorpicker/ColorPickerPreference.java
@@ -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,6 +29,7 @@
 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;
@@ -49,6 +51,12 @@
     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 +89,11 @@
         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 +105,67 @@
         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 +186,22 @@
         // 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 +242,7 @@
     }
 
     public boolean onPreferenceClick(Preference preference) {
-        showDialog(null);
+        //showDialog(null);
         return false;
     }