-
Notifications
You must be signed in to change notification settings - Fork 3k
/
BadgeUtils.java
302 lines (279 loc) · 11.9 KB
/
BadgeUtils.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
/*
* Copyright (C) 2019 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.badge;
import com.google.android.material.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.google.android.material.internal.ParcelableSparseArray;
import com.google.android.material.internal.ToolbarUtils;
/**
* Utility class for {@link BadgeDrawable}.
*
* <p>Warning: This class is experimental and the APIs are subject to change.
*/
@ExperimentalBadgeUtils
public class BadgeUtils {
public static final boolean USE_COMPAT_PARENT = VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR2;
private static final String LOG_TAG = "BadgeUtils";
private BadgeUtils() {
// Private constructor to prevent unwanted construction.
}
/**
* Updates a badge's bounds using its center coordinate, {@code halfWidth} and {@code halfHeight}.
*
* @param rect Holds rectangular coordinates of the badge's bounds.
* @param centerX A badge's center x coordinate.
* @param centerY A badge's center y coordinate.
* @param halfWidth Half of a badge's width.
* @param halfHeight Half of a badge's height.
*/
public static void updateBadgeBounds(
@NonNull Rect rect, float centerX, float centerY, float halfWidth, float halfHeight) {
rect.set(
(int) (centerX - halfWidth),
(int) (centerY - halfHeight),
(int) (centerX + halfWidth),
(int) (centerY + halfHeight));
}
public static void attachBadgeDrawable(
@NonNull BadgeDrawable badgeDrawable, @NonNull View anchor) {
attachBadgeDrawable(badgeDrawable, anchor, /* customBadgeParent */ null);
}
/**
* Attaches a BadgeDrawable to its associated anchor and update the BadgeDrawable's coordinates
* based on the anchor. For API 18+, the BadgeDrawable will be added as a view overlay. For
* pre-API 18, the BadgeDrawable will be set as the foreground of a FrameLayout that is an
* ancestor of the anchor.
*/
public static void attachBadgeDrawable(
@NonNull BadgeDrawable badgeDrawable,
@NonNull View anchor,
@Nullable FrameLayout customBadgeParent) {
setBadgeDrawableBounds(badgeDrawable, anchor, customBadgeParent);
if (badgeDrawable.getCustomBadgeParent() != null) {
badgeDrawable.getCustomBadgeParent().setForeground(badgeDrawable);
} else {
if (USE_COMPAT_PARENT) {
throw new IllegalArgumentException("Trying to reference null customBadgeParent");
} else {
anchor.getOverlay().add(badgeDrawable);
}
}
}
/**
* A convenience method to attach a BadgeDrawable to the specified menu item on a toolbar, update
* the BadgeDrawable's coordinates based on its anchor and adjust the BadgeDrawable's offset so it
* is not clipped off by the toolbar.
*/
public static void attachBadgeDrawable(
@NonNull BadgeDrawable badgeDrawable, @NonNull Toolbar toolbar, @IdRes int menuItemId) {
attachBadgeDrawable(badgeDrawable, toolbar, menuItemId, null /*customBadgeParent */);
}
/**
* Attaches a BadgeDrawable to its associated action menu item on a toolbar, update the
* BadgeDrawable's coordinates based on this anchor and adjust the BadgeDrawable's offset so it is
* not clipped off by the toolbar. For API 18+, the BadgeDrawable will be added as a view overlay.
* For pre-API 18, the BadgeDrawable will be set as the foreground of a FrameLayout that is an
* ancestor of the anchor.
*/
public static void attachBadgeDrawable(
@NonNull final BadgeDrawable badgeDrawable,
@NonNull final Toolbar toolbar,
@IdRes final int menuItemId,
@Nullable final FrameLayout customBadgeParent) {
toolbar.post(
new Runnable() {
@Override
public void run() {
ActionMenuItemView menuItemView =
ToolbarUtils.getActionMenuItemView(toolbar, menuItemId);
if (menuItemView != null) {
setToolbarOffset(badgeDrawable, toolbar.getResources());
BadgeUtils.attachBadgeDrawable(badgeDrawable, menuItemView, customBadgeParent);
attachBadgeContentDescription(badgeDrawable, menuItemView);
}
}
});
}
private static void attachBadgeContentDescription(
@NonNull final BadgeDrawable badgeDrawable, @NonNull View view) {
if (VERSION.SDK_INT >= VERSION_CODES.Q && ViewCompat.hasAccessibilityDelegate(view)) {
ViewCompat.setAccessibilityDelegate(
view,
new AccessibilityDelegateCompat(view.getAccessibilityDelegate()) {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(badgeDrawable.getContentDescription());
}
});
} else {
ViewCompat.setAccessibilityDelegate(
view,
new AccessibilityDelegateCompat() {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(badgeDrawable.getContentDescription());
}
});
}
}
/**
* Detaches a BadgeDrawable from its associated anchor. For API 18+, the BadgeDrawable will be
* removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable will be removed from
* the foreground of a FrameLayout that is an ancestor of the anchor.
*/
public static void detachBadgeDrawable(
@Nullable BadgeDrawable badgeDrawable, @NonNull View anchor) {
if (badgeDrawable == null) {
return;
}
if (USE_COMPAT_PARENT || badgeDrawable.getCustomBadgeParent() != null) {
badgeDrawable.getCustomBadgeParent().setForeground(null);
} else {
anchor.getOverlay().remove(badgeDrawable);
}
}
/**
* Detaches a BadgeDrawable from its associated action menu item on a toolbar, For API 18+, the
* BadgeDrawable will be removed from its anchor's ViewOverlay. For pre-API 18, the BadgeDrawable
* will be removed from the foreground of a FrameLayout that is an ancestor of the anchor.
*/
public static void detachBadgeDrawable(
@Nullable BadgeDrawable badgeDrawable, @NonNull Toolbar toolbar, @IdRes int menuItemId) {
if (badgeDrawable == null) {
return;
}
ActionMenuItemView menuItemView = ToolbarUtils.getActionMenuItemView(toolbar, menuItemId);
if (menuItemView != null) {
removeToolbarOffset(badgeDrawable);
detachBadgeDrawable(badgeDrawable, menuItemView);
detachBadgeContentDescription(menuItemView);
} else {
Log.w(LOG_TAG, "Trying to remove badge from a null menuItemView: " + menuItemId);
}
}
private static void detachBadgeContentDescription(@NonNull View view) {
if (VERSION.SDK_INT >= VERSION_CODES.Q && ViewCompat.hasAccessibilityDelegate(view)) {
ViewCompat.setAccessibilityDelegate(
view,
new AccessibilityDelegateCompat(view.getAccessibilityDelegate()) {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(null);
}
});
} else {
ViewCompat.setAccessibilityDelegate(view, null);
}
}
@VisibleForTesting
static void setToolbarOffset(BadgeDrawable badgeDrawable, Resources resources) {
badgeDrawable.setAdditionalHorizontalOffset(
resources.getDimensionPixelOffset(
R.dimen.mtrl_badge_toolbar_action_menu_item_horizontal_offset));
badgeDrawable.setAdditionalVerticalOffset(
resources.getDimensionPixelOffset(
R.dimen.mtrl_badge_toolbar_action_menu_item_vertical_offset));
}
@VisibleForTesting
static void removeToolbarOffset(BadgeDrawable badgeDrawable) {
badgeDrawable.setAdditionalHorizontalOffset(0);
badgeDrawable.setAdditionalVerticalOffset(0);
}
/**
* Sets the bounds of a BadgeDrawable to match the bounds of its anchor (for API 18+) or its
* anchor's FrameLayout ancestor (pre-API 18).
*/
public static void setBadgeDrawableBounds(
@NonNull BadgeDrawable badgeDrawable,
@NonNull View anchor,
@Nullable FrameLayout compatBadgeParent) {
Rect badgeBounds = new Rect();
anchor.getDrawingRect(badgeBounds);
badgeDrawable.setBounds(badgeBounds);
badgeDrawable.updateBadgeCoordinates(anchor, compatBadgeParent);
}
/**
* Given a map of int keys to {@code BadgeDrawable BadgeDrawables}, creates a parcelable map of
* unique int keys to {@code BadgeDrawable.SavedState SavedStates}. Useful for state restoration.
*
* @param badgeDrawables A {@link SparseArray} that contains a map of int keys (e.g. menuItemId)
* to {@code BadgeDrawable BadgeDrawables}.
* @return A parcelable {@link SparseArray} that contains a map of int keys (e.g. menuItemId) to
* {@code BadgeDrawable.SavedState SavedStates}.
*/
@NonNull
public static ParcelableSparseArray createParcelableBadgeStates(
@NonNull SparseArray<BadgeDrawable> badgeDrawables) {
ParcelableSparseArray badgeStates = new ParcelableSparseArray();
for (int i = 0; i < badgeDrawables.size(); i++) {
int key = badgeDrawables.keyAt(i);
BadgeDrawable badgeDrawable = badgeDrawables.valueAt(i);
if (badgeDrawable == null) {
throw new IllegalArgumentException("badgeDrawable cannot be null");
}
badgeStates.put(key, badgeDrawable.getSavedState());
}
return badgeStates;
}
/**
* Given a map of int keys to {@link BadgeState.State SavedStates}, creates a parcelable
* map of int keys to {@link BadgeDrawable BadgeDrawbles}. Useful for state restoration.
*
* @param context Current context
* @param badgeStates A parcelable {@link SparseArray} that contains a map of int keys (e.g.
* menuItemId) to {@link BadgeState.State states}.
* @return A {@link SparseArray} that contains a map of int keys (e.g. menuItemId) to {@code
* BadgeDrawable BadgeDrawbles}.
*/
@NonNull
public static SparseArray<BadgeDrawable> createBadgeDrawablesFromSavedStates(
Context context, @NonNull ParcelableSparseArray badgeStates) {
SparseArray<BadgeDrawable> badgeDrawables = new SparseArray<>(badgeStates.size());
for (int i = 0; i < badgeStates.size(); i++) {
int key = badgeStates.keyAt(i);
BadgeState.State savedState = (BadgeState.State) badgeStates.valueAt(i);
if (savedState == null) {
throw new IllegalArgumentException("BadgeDrawable's savedState cannot be null");
}
BadgeDrawable badgeDrawable = BadgeDrawable.createFromSavedState(context, savedState);
badgeDrawables.put(key, badgeDrawable);
}
return badgeDrawables;
}
}