Skip to content

Commit dae1965

Browse files
committed
Add ColorPicker Preference Dialog
1 parent b9115fb commit dae1965

8 files changed

Lines changed: 1467 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2010 Daniel Nilsson
4+
Copyright (C) 2012 The CyanogenMod Project
5+
Copyright (C) 2013 XuiMod
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
-->
19+
20+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
21+
android:id="@+id/LinearLayout1"
22+
android:layout_width="match_parent"
23+
android:layout_height="match_parent"
24+
android:orientation="vertical" >
25+
26+
<com.zst.app.multiwindowsidebar.preference.colorpicker.ColorPickerView
27+
android:id="@+id/color_picker_view"
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content" />
30+
31+
<LinearLayout
32+
android:id="@+id/color_text_box"
33+
android:layout_width="match_parent"
34+
android:layout_height="wrap_content"
35+
android:layout_margin="5dp"
36+
android:orientation="horizontal" >
37+
38+
<TextView
39+
android:id="@+id/color_hex_label"
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:layout_weight="0"
43+
android:gravity="right"
44+
android:text="@string/color_hex"
45+
android:textAppearance="?android:attr/textAppearanceMedium" />
46+
47+
<EditText
48+
android:id="@+id/current_hex_text"
49+
android:layout_width="wrap_content"
50+
android:layout_height="wrap_content"
51+
android:layout_weight="1"
52+
android:digits="0123456789ABCDEF"
53+
android:ems="10"
54+
android:inputType="textCapCharacters"
55+
android:maxLength="8" />
56+
57+
<Button
58+
android:id="@+id/color_apply"
59+
android:layout_width="wrap_content"
60+
android:layout_height="wrap_content"
61+
android:layout_weight="0"
62+
android:text="@string/apply" />
63+
64+
</LinearLayout>
65+
66+
<LinearLayout
67+
android:id="@+id/color_panel_view"
68+
android:layout_width="match_parent"
69+
android:layout_height="36dp"
70+
android:layout_margin="5dp"
71+
android:orientation="horizontal" >
72+
73+
<com.zst.app.multiwindowsidebar.preference.colorpicker.ColorPanelView
74+
android:id="@+id/old_color_panel"
75+
android:layout_width="0px"
76+
android:layout_height="match_parent"
77+
android:layout_weight="0.5" />
78+
79+
<TextView
80+
android:layout_width="wrap_content"
81+
android:layout_height="match_parent"
82+
android:layout_marginLeft="5dp"
83+
android:layout_marginRight="5dp"
84+
android:gravity="center"
85+
android:text="@string/arrow_symbol"
86+
android:textColor="#ffffff"
87+
android:textSize="30sp" />
88+
89+
<com.zst.app.multiwindowsidebar.preference.colorpicker.ColorPanelView
90+
android:id="@+id/new_color_panel"
91+
android:layout_width="0px"
92+
android:layout_height="match_parent"
93+
android:layout_weight="0.5" />
94+
</LinearLayout>
95+
96+
</LinearLayout>

res/values/strings.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<string name="add_app">Add App</string>
1212
<string name="group">Group</string>
1313
<string name="settings_default">Default Value</string>
14+
<string name="apply">Apply</string>
15+
<string name="color_hex">Color Hex</string>
1416

1517
<!-- Creating Group -->
1618
<string name="create_group">Create Group</string>
@@ -65,4 +67,8 @@
6567
<!-- Themes -->
6668
<string name="theme_default">Default (Original Note 3)\nby Samsung</string>
6769
<string name="theme_inverted">Inverted Note 3\nby foreverloco@XDA</string>
70+
71+
<!-- Do not translate -->
72+
<string name="arrow_symbol">➙</string>
73+
6874
</resources>

src/com/zst/app/multiwindowsidebar/Util.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.content.Context;
77
import android.content.Intent;
88
import android.content.pm.PackageManager;
9+
import android.graphics.Color;
910
import android.graphics.Paint;
1011
import android.graphics.drawable.Drawable;
1112
import android.graphics.drawable.LayerDrawable;
@@ -97,4 +98,20 @@ public static Drawable layerTwoIcons(Context c, Drawable icon0, Drawable icon1)
9798
layer.setLayerInset(0x1, size, size, 0, 0);
9899
return layer;
99100
}
101+
102+
public static int parseColorFromString(String str, String defColorWithoutSymbols) {
103+
str.replaceAll("\\s+", "");
104+
// Remove all spaces
105+
if (str.equals("")) {
106+
str = defColorWithoutSymbols;
107+
}
108+
if (!str.startsWith("#")) {
109+
str = "#" + str;
110+
}
111+
try {
112+
return Color.parseColor(str);
113+
} catch (Exception e) {
114+
return Color.parseColor("#" + defColorWithoutSymbols);
115+
}
116+
}
100117
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (C) 2013 XuiMod
3+
* Copyright (C) 2012 The CyanogenMod Project
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.zst.app.multiwindowsidebar.preference;
19+
20+
21+
import com.zst.app.multiwindowsidebar.R;
22+
import com.zst.app.multiwindowsidebar.Util;
23+
import com.zst.app.multiwindowsidebar.preference.colorpicker.ColorSettingsDialog;
24+
25+
import android.app.AlertDialog;
26+
import android.content.Context;
27+
import android.content.DialogInterface;
28+
import android.content.SharedPreferences;
29+
import android.content.res.Resources;
30+
import android.preference.Preference;
31+
import android.preference.Preference.OnPreferenceClickListener;
32+
import android.util.AttributeSet;
33+
import android.view.View;
34+
import android.widget.ImageView;
35+
36+
public abstract class ColorPickerDialog extends Preference implements OnPreferenceClickListener {
37+
38+
SharedPreferences mPref;
39+
ImageView mColorBox;
40+
Resources mRes;
41+
String mDefaultColor;
42+
boolean mHideAlpha;
43+
44+
public ColorPickerDialog(Context context, AttributeSet attrs) {
45+
super(context, attrs);
46+
47+
mDefaultColor = attrs.getAttributeValue(null, "defaultValue");
48+
if (attrs.getAttributeValue(null, "hideAlpha") != null) {
49+
mHideAlpha = true;
50+
}
51+
}
52+
53+
@Override
54+
protected void onBindView(View view) {
55+
super.onBindView(view);
56+
mColorBox = (ImageView) view.findViewById(android.R.id.icon);
57+
mPref = getPreferenceManager().getSharedPreferences();
58+
mRes = getContext().getResources();
59+
setOnPreferenceClickListener(this);
60+
refreshColorBox();
61+
}
62+
63+
@Override
64+
public boolean onPreferenceClick(Preference arg0) {
65+
final ColorSettingsDialog d = new ColorSettingsDialog(getContext(), getColor(), mDefaultColor);
66+
d.setAlphaSliderVisible(!mHideAlpha);
67+
final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
68+
@Override
69+
public void onClick(DialogInterface dialog, int which) {
70+
switch (which){
71+
case AlertDialog.BUTTON_POSITIVE:
72+
setColor(d.getColorString());
73+
break;
74+
case AlertDialog.BUTTON_NEUTRAL:
75+
setColor(mDefaultColor);
76+
break;
77+
}
78+
onSettingsChanged();
79+
}
80+
};
81+
d.setButton(AlertDialog.BUTTON_POSITIVE, mRes.getString(android.R.string.ok), listener);
82+
d.setButton(AlertDialog.BUTTON_NEUTRAL, mRes.getString(R.string.settings_default), listener);
83+
d.setButton(AlertDialog.BUTTON_NEGATIVE, mRes.getString(android.R.string.cancel), listener);
84+
d.show();
85+
return true;
86+
}
87+
88+
public abstract void onSettingsChanged();
89+
public String getColorString() {
90+
return Integer.toHexString(getColor());
91+
}
92+
93+
public int getColor() {
94+
String str = mPref.getString(getKey(), mDefaultColor);
95+
return Util.parseColorFromString(str, mDefaultColor);
96+
}
97+
98+
private void refreshColorBox(){
99+
if (mColorBox != null) {
100+
mColorBox.setBackgroundColor(getColor());
101+
mColorBox.setVisibility(View.VISIBLE);
102+
}
103+
}
104+
public void setColor(String clr) {
105+
mPref.edit().putString(getKey(), clr).commit();
106+
refreshColorBox();
107+
}
108+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (C) 2010 Daniel Nilsson
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.zst.app.multiwindowsidebar.preference.colorpicker;
18+
19+
20+
import android.graphics.*;
21+
import android.graphics.Bitmap.Config;
22+
import android.graphics.drawable.Drawable;
23+
24+
/**
25+
* This drawable that draws a simple white and gray chessboard pattern.
26+
* It's pattern you will often see as a background behind a
27+
* partly transparent image in many applications.
28+
* @author Daniel Nilsson
29+
*/
30+
public class AlphaPatternDrawable extends Drawable {
31+
32+
private int mRectangleSize = 10;
33+
34+
private Paint mPaint = new Paint();
35+
private Paint mPaintWhite = new Paint();
36+
private Paint mPaintGray = new Paint();
37+
38+
private int numRectanglesHorizontal;
39+
private int numRectanglesVertical;
40+
41+
/**
42+
* Bitmap in which the pattern will be cahched.
43+
*/
44+
private Bitmap mBitmap;
45+
46+
public AlphaPatternDrawable(int rectangleSize) {
47+
mRectangleSize = rectangleSize;
48+
mPaintWhite.setColor(0xffffffff);
49+
mPaintGray.setColor(0xffcbcbcb);
50+
}
51+
52+
@Override
53+
public void draw(Canvas canvas) {
54+
canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
55+
}
56+
57+
@Override
58+
public int getOpacity() {
59+
return 0;
60+
}
61+
62+
@Override
63+
public void setAlpha(int alpha) {
64+
throw new UnsupportedOperationException("Alpha is not supported by this drawwable.");
65+
}
66+
67+
@Override
68+
public void setColorFilter(ColorFilter cf) {
69+
throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable.");
70+
}
71+
72+
@Override
73+
protected void onBoundsChange(Rect bounds) {
74+
super.onBoundsChange(bounds);
75+
76+
int height = bounds.height();
77+
int width = bounds.width();
78+
79+
numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize));
80+
numRectanglesVertical = (int) Math.ceil(height / mRectangleSize);
81+
82+
generatePatternBitmap();
83+
}
84+
85+
/**
86+
* This will generate a bitmap with the pattern
87+
* as big as the rectangle we were allow to draw on.
88+
* We do this to chache the bitmap so we don't need to
89+
* recreate it each time draw() is called since it
90+
* takes a few milliseconds.
91+
*/
92+
private void generatePatternBitmap(){
93+
if(getBounds().width() <= 0 || getBounds().height() <= 0){
94+
return;
95+
}
96+
97+
mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888);
98+
Canvas canvas = new Canvas(mBitmap);
99+
100+
Rect r = new Rect();
101+
boolean verticalStartWhite = true;
102+
for (int i = 0; i <= numRectanglesVertical; i++) {
103+
104+
boolean isWhite = verticalStartWhite;
105+
for (int j = 0; j <= numRectanglesHorizontal; j++) {
106+
107+
r.top = i * mRectangleSize;
108+
r.left = j * mRectangleSize;
109+
r.bottom = r.top + mRectangleSize;
110+
r.right = r.left + mRectangleSize;
111+
112+
canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray);
113+
114+
isWhite = !isWhite;
115+
}
116+
verticalStartWhite = !verticalStartWhite;
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)