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
|
/*
* 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.systemui.statusbar;
import android.app.Notification;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.NotificationHeaderView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.internal.R;
import com.android.internal.widget.CachingIconView;
import com.android.internal.widget.ConversationLayout;
import com.android.internal.widget.ImageFloatingTextView;
import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
import com.android.systemui.statusbar.notification.row.NotificationContentView;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
/**
* A utility to manage notification views when they are placed in a group by adjusting elements
* to reduce redundancies and occasionally tweak layouts to highlight the unique content.
*/
public class NotificationGroupingUtil {
private static final TextViewComparator TEXT_VIEW_COMPARATOR = new TextViewComparator();
private static final TextViewComparator APP_NAME_COMPARATOR = new AppNameComparator();
private static final ViewComparator BADGE_COMPARATOR = new BadgeComparator();
private static final VisibilityApplicator VISIBILITY_APPLICATOR = new VisibilityApplicator();
private static final VisibilityApplicator APP_NAME_APPLICATOR = new AppNameApplicator();
private static final ResultApplicator LEFT_ICON_APPLICATOR = new LeftIconApplicator();
private static final DataExtractor ICON_EXTRACTOR = new DataExtractor() {
@Override
public Object extractData(ExpandableNotificationRow row) {
return row.getEntry().getSbn().getNotification();
}
};
private static final IconComparator ICON_VISIBILITY_COMPARATOR = new IconComparator() {
public boolean compare(View parent, View child, Object parentData,
Object childData) {
return hasSameIcon(parentData, childData)
&& hasSameColor(parentData, childData);
}
};
private static final IconComparator GREY_COMPARATOR = new IconComparator() {
public boolean compare(View parent, View child, Object parentData,
Object childData) {
return !hasSameIcon(parentData, childData)
|| hasSameColor(parentData, childData);
}
};
private static final ResultApplicator GREY_APPLICATOR = new ResultApplicator() {
@Override
public void apply(View parent, View view, boolean apply, boolean reset) {
CachingIconView icon = view.findViewById(com.android.internal.R.id.icon);
if (icon != null) {
icon.setGrayedOut(apply);
}
}
};
private final ExpandableNotificationRow mRow;
private final ArrayList<Processor> mProcessors = new ArrayList<>();
private final HashSet<Integer> mDividers = new HashSet<>();
public NotificationGroupingUtil(ExpandableNotificationRow row) {
mRow = row;
// To hide the icons if they are the same and the color is the same
mProcessors.add(new Processor(mRow,
com.android.internal.R.id.icon,
ICON_EXTRACTOR,
ICON_VISIBILITY_COMPARATOR,
VISIBILITY_APPLICATOR));
// To grey them out the icons and expand button when the icons are not the same
mProcessors.add(new Processor(mRow,
com.android.internal.R.id.status_bar_latest_event_content,
ICON_EXTRACTOR,
GREY_COMPARATOR,
GREY_APPLICATOR));
mProcessors.add(new Processor(mRow,
com.android.internal.R.id.status_bar_latest_event_content,
ICON_EXTRACTOR,
ICON_VISIBILITY_COMPARATOR,
LEFT_ICON_APPLICATOR));
mProcessors.add(new Processor(mRow,
com.android.internal.R.id.profile_badge,
null /* Extractor */,
BADGE_COMPARATOR,
VISIBILITY_APPLICATOR));
mProcessors.add(new Processor(mRow,
com.android.internal.R.id.app_name_text,
null,
APP_NAME_COMPARATOR,
APP_NAME_APPLICATOR));
mProcessors.add(Processor.forTextView(mRow, com.android.internal.R.id.header_text));
mDividers.add(com.android.internal.R.id.header_text_divider);
mDividers.add(com.android.internal.R.id.header_text_secondary_divider);
mDividers.add(com.android.internal.R.id.time_divider);
}
/**
* Update the appearance of the children in this group to reduce redundancies.
*/
public void updateChildrenAppearance() {
List<ExpandableNotificationRow> notificationChildren = mRow.getAttachedChildren();
if (notificationChildren == null || !mRow.isSummaryWithChildren()) {
return;
}
// Initialize the processors
for (int compI = 0; compI < mProcessors.size(); compI++) {
mProcessors.get(compI).init();
}
// Compare all notification headers
for (int i = 0; i < notificationChildren.size(); i++) {
ExpandableNotificationRow row = notificationChildren.get(i);
for (int compI = 0; compI < mProcessors.size(); compI++) {
mProcessors.get(compI).compareToGroupParent(row);
}
}
// Apply the comparison to the row
for (int i = 0; i < notificationChildren.size(); i++) {
ExpandableNotificationRow row = notificationChildren.get(i);
for (int compI = 0; compI < mProcessors.size(); compI++) {
mProcessors.get(compI).apply(row);
}
// We need to sanitize the dividers since they might be off-balance now
sanitizeTopLineViews(row);
}
}
private void sanitizeTopLineViews(ExpandableNotificationRow row) {
if (row.isSummaryWithChildren()) {
sanitizeTopLine(row.getNotificationViewWrapper().getNotificationHeader(), row);
return;
}
final NotificationContentView layout = row.getPrivateLayout();
sanitizeChild(layout.getContractedChild(), row);
sanitizeChild(layout.getHeadsUpChild(), row);
sanitizeChild(layout.getExpandedChild(), row);
}
private void sanitizeChild(View child, ExpandableNotificationRow row) {
if (child != null) {
sanitizeTopLine(child.findViewById(R.id.notification_top_line), row);
}
}
private void sanitizeTopLine(ViewGroup rowHeader, ExpandableNotificationRow row) {
if (rowHeader == null) {
return;
}
final int childCount = rowHeader.getChildCount();
View time = rowHeader.findViewById(com.android.internal.R.id.time);
boolean hasVisibleText = false;
for (int i = 0; i < childCount; i++) {
View child = rowHeader.getChildAt(i);
if (child instanceof TextView
&& child.getVisibility() != View.GONE
&& !mDividers.contains(child.getId())
&& child != time) {
hasVisibleText = true;
break;
}
}
// in case no view is visible we make sure the time is visible
int timeVisibility = !hasVisibleText
|| row.getEntry().getSbn().getNotification().showsTime()
? View.VISIBLE : View.GONE;
time.setVisibility(timeVisibility);
View left = null;
View right;
for (int i = 0; i < childCount; i++) {
View child = rowHeader.getChildAt(i);
if (mDividers.contains(child.getId())) {
boolean visible = false;
// Lets find the item to the right
for (i++; i < childCount; i++) {
right = rowHeader.getChildAt(i);
if (mDividers.contains(right.getId())) {
// A divider was found, this needs to be hidden
i--;
break;
} else if (right.getVisibility() != View.GONE && right instanceof TextView) {
visible = left != null;
left = right;
break;
}
}
child.setVisibility(visible ? View.VISIBLE : View.GONE);
} else if (child.getVisibility() != View.GONE && child instanceof TextView) {
left = child;
}
}
}
/**
* Reset the modifications to this row for removing it from the group.
*/
public void restoreChildNotification(ExpandableNotificationRow row) {
for (int compI = 0; compI < mProcessors.size(); compI++) {
mProcessors.get(compI).apply(row, true /* reset */);
}
sanitizeTopLineViews(row);
}
private static class Processor {
private final int mId;
private final DataExtractor mExtractor;
private final ViewComparator mComparator;
private final ResultApplicator mApplicator;
private final ExpandableNotificationRow mParentRow;
private boolean mApply;
private View mParentView;
private Object mParentData;
public static Processor forTextView(ExpandableNotificationRow row, int id) {
return new Processor(row, id, null, TEXT_VIEW_COMPARATOR, VISIBILITY_APPLICATOR);
}
Processor(ExpandableNotificationRow row, int id, DataExtractor extractor,
ViewComparator comparator,
ResultApplicator applicator) {
mId = id;
mExtractor = extractor;
mApplicator = applicator;
mComparator = comparator;
mParentRow = row;
}
public void init() {
View header = mParentRow.getNotificationViewWrapper().getNotificationHeader();
mParentView = header == null ? null : header.findViewById(mId);
mParentData = mExtractor == null ? null : mExtractor.extractData(mParentRow);
mApply = !mComparator.isEmpty(mParentView);
}
public void compareToGroupParent(ExpandableNotificationRow row) {
if (!mApply) {
return;
}
View contractedChild = row.getPrivateLayout().getContractedChild();
if (contractedChild == null) {
return;
}
View ownView = contractedChild.findViewById(mId);
if (ownView == null) {
// No view found. We still consider this to be the same to avoid weird flickering
// when for example showing an undo notification
return;
}
Object childData = mExtractor == null ? null : mExtractor.extractData(row);
mApply = mComparator.compare(mParentView, ownView,
mParentData, childData);
}
public void apply(ExpandableNotificationRow row) {
apply(row, false /* reset */);
}
public void apply(ExpandableNotificationRow row, boolean reset) {
boolean apply = mApply && !reset;
if (row.isSummaryWithChildren()) {
applyToView(apply, reset, row.getNotificationViewWrapper().getNotificationHeader());
return;
}
applyToView(apply, reset, row.getPrivateLayout().getContractedChild());
applyToView(apply, reset, row.getPrivateLayout().getHeadsUpChild());
applyToView(apply, reset, row.getPrivateLayout().getExpandedChild());
}
private void applyToView(boolean apply, boolean reset, View parent) {
if (parent != null) {
View view = parent.findViewById(mId);
if (view != null && !mComparator.isEmpty(view)) {
mApplicator.apply(parent, view, apply, reset);
}
}
}
}
private interface ViewComparator {
/**
* @param parent the view with the given id in the group header
* @param child the view with the given id in the child notification
* @param parentData optional data for the parent
* @param childData optional data for the child
* @return whether to views are the same
*/
boolean compare(View parent, View child, Object parentData, Object childData);
boolean isEmpty(View view);
}
private interface DataExtractor {
Object extractData(ExpandableNotificationRow row);
}
private static class BadgeComparator implements ViewComparator {
@Override
public boolean compare(View parent, View child, Object parentData, Object childData) {
return parent.getVisibility() != View.GONE;
}
@Override
public boolean isEmpty(View view) {
if (view instanceof ImageView) {
return ((ImageView) view).getDrawable() == null;
}
return false;
}
}
private static class TextViewComparator implements ViewComparator {
@Override
public boolean compare(View parent, View child, Object parentData, Object childData) {
TextView parentView = (TextView) parent;
CharSequence parentText = parentView == null ? "" : parentView.getText();
TextView childView = (TextView) child;
CharSequence childText = childView == null ? "" : childView.getText();
return Objects.equals(parentText, childText);
}
@Override
public boolean isEmpty(View view) {
return view == null || TextUtils.isEmpty(((TextView) view).getText());
}
}
private abstract static class IconComparator implements ViewComparator {
@Override
public boolean compare(View parent, View child, Object parentData, Object childData) {
return false;
}
protected boolean hasSameIcon(Object parentData, Object childData) {
Icon parentIcon = ((Notification) parentData).getSmallIcon();
Icon childIcon = ((Notification) childData).getSmallIcon();
return parentIcon.sameAs(childIcon);
}
/**
* @return whether two ImageViews have the same colorFilterSet or none at all
*/
protected boolean hasSameColor(Object parentData, Object childData) {
int parentColor = ((Notification) parentData).color;
int childColor = ((Notification) childData).color;
return parentColor == childColor;
}
@Override
public boolean isEmpty(View view) {
return false;
}
}
private interface ResultApplicator {
/**
* @param parent the root view of the child notification
* @param view the view with the given id in the child notification
* @param apply whether the state should be applied or removed
* @param reset if [de]application is the result of a reset
*/
void apply(View parent, View view, boolean apply, boolean reset);
}
private static class VisibilityApplicator implements ResultApplicator {
@Override
public void apply(View parent, View view, boolean apply, boolean reset) {
if (view != null) {
view.setVisibility(apply ? View.GONE : View.VISIBLE);
}
}
}
private static class AppNameApplicator extends VisibilityApplicator {
@Override
public void apply(View parent, View view, boolean apply, boolean reset) {
if (reset && parent instanceof ConversationLayout) {
ConversationLayout layout = (ConversationLayout) parent;
apply = layout.shouldHideAppName();
}
super.apply(parent, view, apply, reset);
}
}
private static class AppNameComparator extends TextViewComparator {
@Override
public boolean compare(View parent, View child, Object parentData, Object childData) {
if (isEmpty(child)) {
// In headerless notifications the AppName view exists but is usually GONE (and not
// populated). We need to treat this case as equal to the header in order to
// deduplicate the view.
return true;
}
return super.compare(parent, child, parentData, childData);
}
}
private static class LeftIconApplicator implements ResultApplicator {
public static final int[] MARGIN_ADJUSTED_VIEWS = {
R.id.text,
R.id.big_text,
R.id.title,
R.id.notification_main_column,
R.id.notification_header};
@Override
public void apply(View parent, View child, boolean showLeftIcon, boolean reset) {
ImageView leftIcon = child.findViewById(com.android.internal.R.id.left_icon);
if (leftIcon == null) {
return;
}
ImageView rightIcon = child.findViewById(com.android.internal.R.id.right_icon);
boolean keepRightIcon = rightIcon != null && Integer.valueOf(1).equals(
rightIcon.getTag(R.id.tag_keep_when_showing_left_icon));
boolean leftIconUsesRightIconDrawable = Integer.valueOf(1).equals(
leftIcon.getTag(R.id.tag_uses_right_icon_drawable));
if (leftIconUsesRightIconDrawable) {
// Use the right drawable when showing the left, unless the right is being kept
Drawable rightDrawable = rightIcon == null ? null : rightIcon.getDrawable();
leftIcon.setImageDrawable(showLeftIcon && !keepRightIcon ? rightDrawable : null);
}
leftIcon.setVisibility(showLeftIcon ? View.VISIBLE : View.GONE);
// update the right icon as well
if (rightIcon != null) {
boolean showRightIcon = (keepRightIcon || !showLeftIcon)
&& rightIcon.getDrawable() != null;
rightIcon.setVisibility(showRightIcon ? View.VISIBLE : View.GONE);
for (int viewId : MARGIN_ADJUSTED_VIEWS) {
adjustMargins(showRightIcon, child.findViewById(viewId));
}
}
}
void adjustMargins(boolean iconVisible, View target) {
if (target == null) {
return;
}
if (target instanceof ImageFloatingTextView) {
((ImageFloatingTextView) target).setHasImage(iconVisible);
return;
}
final Integer data = (Integer) target.getTag(iconVisible
? com.android.internal.R.id.tag_margin_end_when_icon_visible
: com.android.internal.R.id.tag_margin_end_when_icon_gone);
if (data == null) {
return;
}
final DisplayMetrics metrics = target.getResources().getDisplayMetrics();
final int value = TypedValue.complexToDimensionPixelOffset(data, metrics);
if (target instanceof NotificationHeaderView) {
((NotificationHeaderView) target).setTopLineExtraMarginEnd(value);
} else {
ViewGroup.LayoutParams layoutParams = target.getLayoutParams();
if (layoutParams instanceof ViewGroup.MarginLayoutParams) {
((ViewGroup.MarginLayoutParams) layoutParams).setMarginEnd(value);
target.setLayoutParams(layoutParams);
}
}
}
}
}
|