-
Notifications
You must be signed in to change notification settings - Fork 60
/
ColorPreference.java
236 lines (213 loc) · 7.59 KB
/
ColorPreference.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (C) 2015 Martin Stone
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rarepebble.colorpicker;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.appcompat.app.AlertDialog;
import androidx.preference.DialogPreference;
import androidx.preference.PreferenceViewHolder;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
public class ColorPreference extends DialogPreference {
private final String selectNoneButtonText;
private Integer defaultColor;
private final String noneSelectedSummaryText;
private final boolean showAlpha;
private final boolean showHex;
private final boolean showPreview;
public ColorPreference(Context context) {
this(context, null);
}
public ColorPreference(Context context, AttributeSet attrs) {
super(context, attrs);
if (attrs != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ColorPicker, 0, 0);
selectNoneButtonText = a.getString(R.styleable.ColorPicker_colorpicker_selectNoneButtonText);
noneSelectedSummaryText = a.getString(R.styleable.ColorPicker_colorpicker_noneSelectedSummaryText);
showAlpha = a.getBoolean(R.styleable.ColorPicker_colorpicker_showAlpha, true);
showHex = a.getBoolean(R.styleable.ColorPicker_colorpicker_showHex, true);
showPreview = a.getBoolean(R.styleable.ColorPicker_colorpicker_showPreview, true);
}
else {
selectNoneButtonText = null;
noneSelectedSummaryText = null;
showAlpha = true;
showHex = true;
showPreview = true;
}
}
public ColorPreferenceFragment showDialog(@NonNull Fragment targetFragment, int requestCode) {
ColorPreferenceFragment fragment = ColorPreferenceFragment.newInstance(getKey());
fragment.setTargetFragment(targetFragment, requestCode);
FragmentManager fragmentManager = targetFragment.getFragmentManager();
if (fragmentManager != null) {
fragment.show(fragmentManager, getKey());
hideKeyboard(targetFragment);
}
return fragment;
}
@Override
public CharSequence getSummary() {
return (noneSelectedSummaryText != null && getPersistedIntDefaultOrNull() == null)
? noneSelectedSummaryText
: super.getSummary();
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
View thumbnail = addThumbnail(holder.itemView);
showColor(thumbnail, getPersistedIntDefaultOrNull());
super.onBindViewHolder(holder);
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
defaultColor = readDefaultValue(a, index);
return defaultColor;
}
private static Integer readDefaultValue(TypedArray a, int index) {
if (a.peekValue(index) != null) {
int type = a.peekValue(index).type;
if (type == TypedValue.TYPE_STRING) {
return Color.parseColor(standardiseColorDigits(a.getString(index)));
}
else if (TypedValue.TYPE_FIRST_COLOR_INT <= type && type <= TypedValue.TYPE_LAST_COLOR_INT) {
return a.getColor(index, Color.GRAY);
}
else if (TypedValue.TYPE_FIRST_INT <= type && type <= TypedValue.TYPE_LAST_INT) {
return a.getInt(index, Color.GRAY);
}
}
return null;
}
@Override
public void setDefaultValue(Object defaultValue) {
super.setDefaultValue(defaultValue);
defaultColor = parseDefaultValue(defaultValue);
}
@Override
protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
setColor(restorePersistedValue ? getColor() : parseDefaultValue(defaultValue));
}
private static int parseDefaultValue(Object defaultValue) {
return (defaultValue == null)
? Color.GRAY
: (defaultValue instanceof Integer)
? (Integer)defaultValue
: Color.parseColor(standardiseColorDigits(defaultValue.toString()));
}
private static String standardiseColorDigits(String s) {
if (s.charAt(0) == '#' && s.length() <= "#argb".length()) {
// Convert #[a]rgb to #[aa]rrggbb
String ss = "#";
for (int i = 1; i < s.length(); ++i) {
ss += s.charAt(i);
ss += s.charAt(i);
}
return ss;
}
else {
return s;
}
}
private View addThumbnail(View view) {
LinearLayout widgetFrameView = ((LinearLayout)view.findViewById(android.R.id.widget_frame));
widgetFrameView.setVisibility(View.VISIBLE);
widgetFrameView.removeAllViews();
LayoutInflater.from(getContext()).inflate(
isEnabled()
? R.layout.color_preference_thumbnail
: R.layout.color_preference_thumbnail_disabled,
widgetFrameView);
return widgetFrameView.findViewById(R.id.thumbnail);
}
private Integer getPersistedIntDefaultOrNull() {
return shouldPersist() && getSharedPreferences().contains(getKey())
? Integer.valueOf(getPersistedInt(Color.GRAY))
: defaultColor;
}
private void showColor(View thumbnail, Integer color) {
Integer thumbColor = color == null ? defaultColor : color;
if (thumbnail != null) {
thumbnail.setVisibility(thumbColor == null ? View.GONE : View.VISIBLE);
thumbnail.findViewById(R.id.colorPreview).setBackgroundColor(thumbColor == null ? 0 : thumbColor);
}
}
void prepareDialogBuilder(AlertDialog.Builder builder) {
final ColorPickerView picker = new ColorPickerView(getContext());
picker.setColor(getPersistedInt(defaultColor == null ? Color.GRAY : defaultColor));
picker.showAlpha(showAlpha);
picker.showHex(showHex);
picker.showPreview(showPreview);
builder
.setTitle(null)
.setView(picker)
.setPositiveButton(getPositiveButtonText(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final int color = picker.getColor();
if (callChangeListener(color)) {
setColor(color);
}
}
});
if (selectNoneButtonText != null) {
builder.setNeutralButton(selectNoneButtonText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (callChangeListener(null)) {
setColor(null);
}
}
});
}
}
private void hideKeyboard(Fragment targetFragment) {
// Nexus 7 needs the keyboard hiding explicitly.
// A flag on the activity in the manifest doesn't
// apply to the dialog, so needs to be in code:
Window window = targetFragment.getActivity().getWindow();
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
private void removeSetting() {
if (shouldPersist()) {
getSharedPreferences()
.edit()
.remove(getKey())
.apply();
}
}
public void setColor(Integer color) {
if (color == null) {
removeSetting();
}
else {
persistInt(color);
}
notifyChanged();
}
public Integer getColor() {
return getPersistedIntDefaultOrNull();
}
}