-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathMaterialAlertDialogBuilder.java
500 lines (438 loc) · 17 KB
/
MaterialAlertDialogBuilder.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* Copyright 2018 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.google.android.material.dialog;
import com.google.android.material.R;
import static com.google.android.material.theme.overlay.MaterialThemeOverlay.wrap;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnKeyListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.content.res.ColorStateList;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import androidx.appcompat.app.AlertDialog;
import android.util.TypedValue;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import androidx.annotation.ArrayRes;
import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.annotation.StringRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.view.ContextThemeWrapper;
import com.google.android.material.color.MaterialColors;
import com.google.android.material.resources.MaterialAttributes;
import com.google.android.material.shape.MaterialShapeDrawable;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
/**
* An extension of {@link AlertDialog.Builder} for use with a Material theme (e.g.,
* Theme.MaterialComponents).
*
* <p>This Builder must be used in order for AlertDialog objects to respond to color and shape
* theming provided by Material themes.
*
* <p>The type of dialog returned is still an {@link AlertDialog}; there is no specific Material
* implementation of {@link AlertDialog}.
*
* <p>For more information, see the <a
* href="https://github.com/material-components/material-components-android/blob/master/docs/components/Dialog.md">component
* developer guidance</a> and <a href="https://material.io/components/dialogs/overview">design
* guidelines</a>.
*/
public class MaterialAlertDialogBuilder extends AlertDialog.Builder {
@AttrRes private static final int DEF_STYLE_ATTR = R.attr.alertDialogStyle;
@StyleRes private static final int DEF_STYLE_RES = R.style.MaterialAlertDialog_MaterialComponents;
@AttrRes
private static final int MATERIAL_ALERT_DIALOG_THEME_OVERLAY = R.attr.materialAlertDialogTheme;
@Nullable private Drawable background;
@NonNull private final Rect backgroundInsets;
private static int getMaterialAlertDialogThemeOverlay(@NonNull Context context) {
TypedValue materialAlertDialogThemeOverlay =
MaterialAttributes.resolve(context, MATERIAL_ALERT_DIALOG_THEME_OVERLAY);
if (materialAlertDialogThemeOverlay == null) {
return 0;
}
return materialAlertDialogThemeOverlay.data;
}
private static Context createMaterialAlertDialogThemedContext(@NonNull Context context) {
int themeOverlayId = getMaterialAlertDialogThemeOverlay(context);
Context themedContext = wrap(context, null, DEF_STYLE_ATTR, DEF_STYLE_RES);
if (themeOverlayId == 0) {
return themedContext;
}
return new ContextThemeWrapper(themedContext, themeOverlayId);
}
private static int getOverridingThemeResId(@NonNull Context context, int overrideThemeResId) {
return overrideThemeResId == 0
? getMaterialAlertDialogThemeOverlay(context)
: overrideThemeResId;
}
public MaterialAlertDialogBuilder(@NonNull Context context) {
this(context, 0);
}
public MaterialAlertDialogBuilder(@NonNull Context context, int overrideThemeResId) {
// Only pass in 0 for overrideThemeResId if both overrideThemeResId and
// MATERIAL_ALERT_DIALOG_THEME_OVERLAY are 0 otherwise alertDialogTheme will override both.
super(
createMaterialAlertDialogThemedContext(context),
getOverridingThemeResId(context, overrideThemeResId));
// Ensure we are using the correctly themed context rather than the context that was passed in.
context = getContext();
Theme theme = context.getTheme();
backgroundInsets =
MaterialDialogs.getDialogBackgroundInsets(context, DEF_STYLE_ATTR, DEF_STYLE_RES);
int surfaceColor =
MaterialColors.getColor(context, R.attr.colorSurface, getClass().getCanonicalName());
TypedArray a =
context.obtainStyledAttributes(
/* set= */ null, R.styleable.MaterialAlertDialog, DEF_STYLE_ATTR, DEF_STYLE_RES);
int backgroundColor = a.getColor(R.styleable.MaterialAlertDialog_backgroundTint, surfaceColor);
a.recycle();
MaterialShapeDrawable materialShapeDrawable =
new MaterialShapeDrawable(context, null, DEF_STYLE_ATTR, DEF_STYLE_RES);
materialShapeDrawable.initializeElevationOverlay(context);
materialShapeDrawable.setFillColor(ColorStateList.valueOf(backgroundColor));
// dialogCornerRadius first appeared in Android Pie
if (Build.VERSION.SDK_INT >= VERSION_CODES.P) {
TypedValue dialogCornerRadiusValue = new TypedValue();
theme.resolveAttribute(android.R.attr.dialogCornerRadius, dialogCornerRadiusValue, true);
float dialogCornerRadius =
dialogCornerRadiusValue.getDimension(getContext().getResources().getDisplayMetrics());
if (dialogCornerRadiusValue.type == TypedValue.TYPE_DIMENSION && dialogCornerRadius >= 0) {
materialShapeDrawable.setCornerSize(dialogCornerRadius);
}
}
background = materialShapeDrawable;
}
@NonNull
@Override
public AlertDialog create() {
AlertDialog alertDialog = super.create();
Window window = alertDialog.getWindow();
/* {@link Window#getDecorView()} should be called before any changes are made to the Window
* as it locks in attributes and affects layout. */
View decorView = window.getDecorView();
if (background instanceof MaterialShapeDrawable) {
((MaterialShapeDrawable) background).setElevation(decorView.getElevation());
}
Drawable insetDrawable = MaterialDialogs.insetDrawable(background, backgroundInsets);
window.setBackgroundDrawable(insetDrawable);
decorView.setOnTouchListener(new InsetDialogOnTouchListener(alertDialog, backgroundInsets));
return alertDialog;
}
@Nullable
public Drawable getBackground() {
return background;
}
@NonNull
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setBackground(@Nullable Drawable background) {
this.background = background;
return this;
}
@NonNull
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setBackgroundInsetStart(@Px int backgroundInsetStart) {
if (getContext().getResources().getConfiguration().getLayoutDirection()
== View.LAYOUT_DIRECTION_RTL) {
backgroundInsets.right = backgroundInsetStart;
} else {
backgroundInsets.left = backgroundInsetStart;
}
return this;
}
@NonNull
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setBackgroundInsetTop(@Px int backgroundInsetTop) {
backgroundInsets.top = backgroundInsetTop;
return this;
}
@NonNull
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setBackgroundInsetEnd(@Px int backgroundInsetEnd) {
if (getContext().getResources().getConfiguration().getLayoutDirection()
== View.LAYOUT_DIRECTION_RTL) {
backgroundInsets.left = backgroundInsetEnd;
} else {
backgroundInsets.right = backgroundInsetEnd;
}
return this;
}
@NonNull
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setBackgroundInsetBottom(@Px int backgroundInsetBottom) {
backgroundInsets.bottom = backgroundInsetBottom;
return this;
}
// The following methods are all pass-through methods used to specify the return type for the
// builder chain.
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setTitle(@StringRes int titleId) {
return (MaterialAlertDialogBuilder) super.setTitle(titleId);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setTitle(@Nullable CharSequence title) {
return (MaterialAlertDialogBuilder) super.setTitle(title);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setCustomTitle(@Nullable View customTitleView) {
return (MaterialAlertDialogBuilder) super.setCustomTitle(customTitleView);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setMessage(@StringRes int messageId) {
return (MaterialAlertDialogBuilder) super.setMessage(messageId);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setMessage(@Nullable CharSequence message) {
return (MaterialAlertDialogBuilder) super.setMessage(message);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setIcon(@DrawableRes int iconId) {
return (MaterialAlertDialogBuilder) super.setIcon(iconId);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setIcon(@Nullable Drawable icon) {
return (MaterialAlertDialogBuilder) super.setIcon(icon);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setIconAttribute(@AttrRes int attrId) {
return (MaterialAlertDialogBuilder) super.setIconAttribute(attrId);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setPositiveButton(
@StringRes int textId, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setPositiveButton(textId, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setPositiveButton(
@Nullable CharSequence text, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setPositiveButton(text, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setPositiveButtonIcon(@Nullable Drawable icon) {
return (MaterialAlertDialogBuilder) super.setPositiveButtonIcon(icon);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setNegativeButton(
@StringRes int textId, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setNegativeButton(textId, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setNegativeButton(
@Nullable CharSequence text, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setNegativeButton(text, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setNegativeButtonIcon(@Nullable Drawable icon) {
return (MaterialAlertDialogBuilder) super.setNegativeButtonIcon(icon);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setNeutralButton(
@StringRes int textId, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setNeutralButton(textId, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setNeutralButton(
@Nullable CharSequence text, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setNeutralButton(text, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setNeutralButtonIcon(@Nullable Drawable icon) {
return (MaterialAlertDialogBuilder) super.setNeutralButtonIcon(icon);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setCancelable(boolean cancelable) {
return (MaterialAlertDialogBuilder) super.setCancelable(cancelable);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setOnCancelListener(
@Nullable OnCancelListener onCancelListener) {
return (MaterialAlertDialogBuilder) super.setOnCancelListener(onCancelListener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setOnDismissListener(
@Nullable OnDismissListener onDismissListener) {
return (MaterialAlertDialogBuilder) super.setOnDismissListener(onDismissListener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setOnKeyListener(@Nullable OnKeyListener onKeyListener) {
return (MaterialAlertDialogBuilder) super.setOnKeyListener(onKeyListener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setItems(
@ArrayRes int itemsId, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setItems(itemsId, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setItems(
@Nullable CharSequence[] items, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setItems(items, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setAdapter(
@Nullable final ListAdapter adapter, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setAdapter(adapter, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setCursor(
@Nullable final Cursor cursor,
@Nullable final OnClickListener listener,
@NonNull String labelColumn) {
return (MaterialAlertDialogBuilder) super.setCursor(cursor, listener, labelColumn);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setMultiChoiceItems(
@ArrayRes int itemsId,
@Nullable boolean[] checkedItems,
@Nullable final OnMultiChoiceClickListener listener) {
return (MaterialAlertDialogBuilder) super.setMultiChoiceItems(itemsId, checkedItems, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setMultiChoiceItems(
@Nullable CharSequence[] items,
@Nullable boolean[] checkedItems,
@Nullable final OnMultiChoiceClickListener listener) {
return (MaterialAlertDialogBuilder) super.setMultiChoiceItems(items, checkedItems, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setMultiChoiceItems(
@Nullable Cursor cursor,
@NonNull String isCheckedColumn,
@NonNull String labelColumn,
@Nullable final OnMultiChoiceClickListener listener) {
return (MaterialAlertDialogBuilder)
super.setMultiChoiceItems(cursor, isCheckedColumn, labelColumn, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setSingleChoiceItems(
@ArrayRes int itemsId, int checkedItem, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setSingleChoiceItems(itemsId, checkedItem, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setSingleChoiceItems(
@Nullable Cursor cursor,
int checkedItem,
@NonNull String labelColumn,
@Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder)
super.setSingleChoiceItems(cursor, checkedItem, labelColumn, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setSingleChoiceItems(
@Nullable CharSequence[] items, int checkedItem, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setSingleChoiceItems(items, checkedItem, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setSingleChoiceItems(
@Nullable ListAdapter adapter, int checkedItem, @Nullable final OnClickListener listener) {
return (MaterialAlertDialogBuilder) super.setSingleChoiceItems(adapter, checkedItem, listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setOnItemSelectedListener(
@Nullable final AdapterView.OnItemSelectedListener listener) {
return (MaterialAlertDialogBuilder) super.setOnItemSelectedListener(listener);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setView(int layoutResId) {
return (MaterialAlertDialogBuilder) super.setView(layoutResId);
}
@NonNull
@Override
@CanIgnoreReturnValue
public MaterialAlertDialogBuilder setView(@Nullable View view) {
return (MaterialAlertDialogBuilder) super.setView(view);
}
}