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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* 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.android.settingslib.widget;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.android.settingslib.utils.BuildCompatUtils;
/**
* Banner message is a banner displaying important information (permission request, page error etc),
* and provide actions for user to address. It requires a user action to be dismissed.
*/
public class BannerMessagePreference extends Preference {
public enum AttentionLevel {
HIGH(0, R.color.banner_background_attention_high, R.color.banner_accent_attention_high),
MEDIUM(1,
R.color.banner_background_attention_medium,
R.color.banner_accent_attention_medium),
LOW(2, R.color.banner_background_attention_low, R.color.banner_accent_attention_low);
// Corresponds to the enum valye of R.attr.attentionLevel
private final int mAttrValue;
@ColorRes private final int mBackgroundColorResId;
@ColorRes private final int mAccentColorResId;
AttentionLevel(int attrValue, @ColorRes int backgroundColorResId,
@ColorRes int accentColorResId) {
mAttrValue = attrValue;
mBackgroundColorResId = backgroundColorResId;
mAccentColorResId = accentColorResId;
}
static AttentionLevel fromAttr(int attrValue) {
for (AttentionLevel level : values()) {
if (level.mAttrValue == attrValue) {
return level;
}
}
throw new IllegalArgumentException();
}
public @ColorRes int getAccentColorResId() {
return mAccentColorResId;
}
public @ColorRes int getBackgroundColorResId() {
return mBackgroundColorResId;
}
}
private static final String TAG = "BannerPreference";
private static final boolean IS_AT_LEAST_S = BuildCompatUtils.isAtLeastS();
private final BannerMessagePreference.ButtonInfo mPositiveButtonInfo =
new BannerMessagePreference.ButtonInfo();
private final BannerMessagePreference.ButtonInfo mNegativeButtonInfo =
new BannerMessagePreference.ButtonInfo();
private final BannerMessagePreference.DismissButtonInfo mDismissButtonInfo =
new BannerMessagePreference.DismissButtonInfo();
// Default attention level is High.
private AttentionLevel mAttentionLevel = AttentionLevel.HIGH;
private String mSubtitle;
public BannerMessagePreference(Context context) {
super(context);
init(context, null /* attrs */);
}
public BannerMessagePreference(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public BannerMessagePreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public BannerMessagePreference(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
setSelectable(false);
setLayoutResource(R.layout.settingslib_banner_message);
if (IS_AT_LEAST_S) {
if (attrs != null) {
// Get attention level and subtitle from layout XML
TypedArray a =
context.obtainStyledAttributes(attrs, R.styleable.BannerMessagePreference);
int mAttentionLevelValue =
a.getInt(R.styleable.BannerMessagePreference_attentionLevel, 0);
mAttentionLevel = AttentionLevel.fromAttr(mAttentionLevelValue);
mSubtitle = a.getString(R.styleable.BannerMessagePreference_subtitle);
a.recycle();
}
}
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
final Context context = getContext();
final TextView titleView = (TextView) holder.findViewById(R.id.banner_title);
CharSequence title = getTitle();
titleView.setText(title);
titleView.setVisibility(title == null ? View.GONE : View.VISIBLE);
final TextView summaryView = (TextView) holder.findViewById(R.id.banner_summary);
summaryView.setText(getSummary());
mPositiveButtonInfo.mButton = (Button) holder.findViewById(R.id.banner_positive_btn);
mNegativeButtonInfo.mButton = (Button) holder.findViewById(R.id.banner_negative_btn);
if (IS_AT_LEAST_S) {
final Resources.Theme theme = context.getTheme();
@ColorInt final int accentColor =
context.getResources().getColor(mAttentionLevel.getAccentColorResId(), theme);
@ColorInt final int backgroundColor =
context.getResources().getColor(
mAttentionLevel.getBackgroundColorResId(), theme);
holder.setDividerAllowedAbove(false);
holder.setDividerAllowedBelow(false);
holder.itemView.getBackground().setTint(backgroundColor);
mPositiveButtonInfo.mColor = accentColor;
mNegativeButtonInfo.mColor = accentColor;
mDismissButtonInfo.mButton = (ImageButton) holder.findViewById(R.id.banner_dismiss_btn);
mDismissButtonInfo.setUpButton();
final TextView subtitleView = (TextView) holder.findViewById(R.id.banner_subtitle);
subtitleView.setText(mSubtitle);
subtitleView.setVisibility(mSubtitle == null ? View.GONE : View.VISIBLE);
final ImageView iconView = (ImageView) holder.findViewById(R.id.banner_icon);
if (iconView != null) {
Drawable icon = getIcon();
iconView.setImageDrawable(
icon == null
? getContext().getDrawable(R.drawable.ic_warning)
: icon);
iconView.setColorFilter(
new PorterDuffColorFilter(accentColor, PorterDuff.Mode.SRC_IN));
}
} else {
holder.setDividerAllowedAbove(true);
holder.setDividerAllowedBelow(true);
}
mPositiveButtonInfo.setUpButton();
mNegativeButtonInfo.setUpButton();
}
/**
* Set the visibility state of positive button.
*/
public BannerMessagePreference setPositiveButtonVisible(boolean isVisible) {
if (isVisible != mPositiveButtonInfo.mIsVisible) {
mPositiveButtonInfo.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
/**
* Set the visibility state of negative button.
*/
public BannerMessagePreference setNegativeButtonVisible(boolean isVisible) {
if (isVisible != mNegativeButtonInfo.mIsVisible) {
mNegativeButtonInfo.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
/**
* Set the visibility state of dismiss button.
*/
@RequiresApi(Build.VERSION_CODES.S)
public BannerMessagePreference setDismissButtonVisible(boolean isVisible) {
if (isVisible != mDismissButtonInfo.mIsVisible) {
mDismissButtonInfo.mIsVisible = isVisible;
notifyChanged();
}
return this;
}
/**
* Register a callback to be invoked when positive button is clicked.
*/
public BannerMessagePreference setPositiveButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mPositiveButtonInfo.mListener) {
mPositiveButtonInfo.mListener = listener;
notifyChanged();
}
return this;
}
/**
* Register a callback to be invoked when negative button is clicked.
*/
public BannerMessagePreference setNegativeButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mNegativeButtonInfo.mListener) {
mNegativeButtonInfo.mListener = listener;
notifyChanged();
}
return this;
}
/**
* Register a callback to be invoked when the dismiss button is clicked.
*/
@RequiresApi(Build.VERSION_CODES.S)
public BannerMessagePreference setDismissButtonOnClickListener(
View.OnClickListener listener) {
if (listener != mDismissButtonInfo.mListener) {
mDismissButtonInfo.mListener = listener;
notifyChanged();
}
return this;
}
/**
* Sets the text to be displayed in positive button.
*/
public BannerMessagePreference setPositiveButtonText(@StringRes int textResId) {
return setPositiveButtonText(getContext().getString(textResId));
}
/**
* Sets the text to be displayed in positive button.
*/
public BannerMessagePreference setPositiveButtonText(String positiveButtonText) {
if (!TextUtils.equals(positiveButtonText, mPositiveButtonInfo.mText)) {
mPositiveButtonInfo.mText = positiveButtonText;
notifyChanged();
}
return this;
}
/**
* Sets the text to be displayed in negative button.
*/
public BannerMessagePreference setNegativeButtonText(@StringRes int textResId) {
return setNegativeButtonText(getContext().getString(textResId));
}
/**
* Sets the text to be displayed in negative button.
*/
public BannerMessagePreference setNegativeButtonText(String negativeButtonText) {
if (!TextUtils.equals(negativeButtonText, mNegativeButtonInfo.mText)) {
mNegativeButtonInfo.mText = negativeButtonText;
notifyChanged();
}
return this;
}
/**
* Sets the subtitle.
*/
@RequiresApi(Build.VERSION_CODES.S)
public BannerMessagePreference setSubtitle(@StringRes int textResId) {
return setSubtitle(getContext().getString(textResId));
}
/**
* Sets the subtitle.
*/
@RequiresApi(Build.VERSION_CODES.S)
public BannerMessagePreference setSubtitle(String subtitle) {
if (!TextUtils.equals(subtitle, mSubtitle)) {
mSubtitle = subtitle;
notifyChanged();
}
return this;
}
/**
* Sets the attention level. This will update the color theme of the preference.
*/
@RequiresApi(Build.VERSION_CODES.S)
public BannerMessagePreference setAttentionLevel(AttentionLevel attentionLevel) {
if (attentionLevel == mAttentionLevel) {
return this;
}
if (attentionLevel != null) {
mAttentionLevel = attentionLevel;
notifyChanged();
}
return this;
}
static class ButtonInfo {
private Button mButton;
private CharSequence mText;
private View.OnClickListener mListener;
private boolean mIsVisible = true;
@ColorInt private int mColor;
void setUpButton() {
mButton.setText(mText);
mButton.setOnClickListener(mListener);
if (IS_AT_LEAST_S) {
mButton.setTextColor(mColor);
}
if (shouldBeVisible()) {
mButton.setVisibility(View.VISIBLE);
} else {
mButton.setVisibility(View.GONE);
}
}
/**
* By default, two buttons are visible.
* If user didn't set a text for a button, then it should not be shown.
*/
private boolean shouldBeVisible() {
return mIsVisible && (!TextUtils.isEmpty(mText));
}
}
static class DismissButtonInfo {
private ImageButton mButton;
private View.OnClickListener mListener;
private boolean mIsVisible = true;
void setUpButton() {
mButton.setOnClickListener(mListener);
if (shouldBeVisible()) {
mButton.setVisibility(View.VISIBLE);
} else {
mButton.setVisibility(View.GONE);
}
}
/**
* By default, dismiss button is visible if it has a click listener.
*/
private boolean shouldBeVisible() {
return mIsVisible && (mListener != null);
}
}
}
|