1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.statusbar.notification.collection.render;
18 
19 import static com.android.systemui.statusbar.notification.collection.GroupEntry.ROOT_ENTRY;
20 
21 import androidx.annotation.Nullable;
22 
23 import com.android.systemui.statusbar.notification.collection.GroupEntry;
24 import com.android.systemui.statusbar.notification.collection.ListEntry;
25 import com.android.systemui.statusbar.notification.collection.NotificationEntry;
26 
27 import java.util.List;
28 
29 /**
30  * ShadeListBuilder groups notifications from system server. This manager translates
31  * ShadeListBuilder's method of grouping to be used within SystemUI.
32  */
33 public class GroupMembershipManagerImpl implements GroupMembershipManager {
34     @Override
isGroupSummary(NotificationEntry entry)35     public boolean isGroupSummary(NotificationEntry entry) {
36         return getGroupSummary(entry) == entry;
37     }
38 
39     @Override
getGroupSummary(NotificationEntry entry)40     public NotificationEntry getGroupSummary(NotificationEntry entry) {
41         if (isEntryTopLevel(entry) || entry.getParent() == null) {
42             return null;
43         }
44 
45         return entry.getParent().getRepresentativeEntry();
46     }
47 
48     @Override
isChildInGroup(NotificationEntry entry)49     public boolean isChildInGroup(NotificationEntry entry) {
50         return !isEntryTopLevel(entry);
51     }
52 
53     @Override
isOnlyChildInGroup(NotificationEntry entry)54     public boolean isOnlyChildInGroup(NotificationEntry entry) {
55         if (entry.getParent() == null) {
56             return false;
57         }
58 
59         return !isGroupSummary(entry) && entry.getParent().getChildren().size() == 1;
60     }
61 
62     @Nullable
63     @Override
getChildren(ListEntry entry)64     public List<NotificationEntry> getChildren(ListEntry entry) {
65         if (entry instanceof GroupEntry) {
66             return ((GroupEntry) entry).getChildren();
67         }
68 
69         if (isGroupSummary(entry.getRepresentativeEntry())) {
70             // maybe we were actually passed the summary
71             return entry.getRepresentativeEntry().getParent().getChildren();
72         }
73 
74         return null;
75     }
76 
isEntryTopLevel(NotificationEntry entry)77     private boolean isEntryTopLevel(NotificationEntry entry) {
78         return entry.getParent() == ROOT_ENTRY;
79     }
80 }
81