Home | History | Annotate | Download | only in phone
      1 /*
      2  * Copyright (C) 2015 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.phone;
     18 
     19 import android.service.notification.StatusBarNotification;
     20 import android.support.annotation.Nullable;
     21 
     22 import com.android.systemui.statusbar.ExpandableNotificationRow;
     23 import com.android.systemui.statusbar.NotificationData;
     24 import com.android.systemui.statusbar.StatusBarState;
     25 import com.android.systemui.statusbar.policy.HeadsUpManager;
     26 
     27 import java.io.FileDescriptor;
     28 import java.io.PrintWriter;
     29 import java.util.ArrayList;
     30 import java.util.HashMap;
     31 import java.util.HashSet;
     32 import java.util.Iterator;
     33 import java.util.Map;
     34 
     35 /**
     36  * A class to handle notifications and their corresponding groups.
     37  */
     38 public class NotificationGroupManager implements HeadsUpManager.OnHeadsUpChangedListener {
     39 
     40     private final HashMap<String, NotificationGroup> mGroupMap = new HashMap<>();
     41     private OnGroupChangeListener mListener;
     42     private int mBarState = -1;
     43     private HashMap<String, StatusBarNotification> mIsolatedEntries = new HashMap<>();
     44     private HeadsUpManager mHeadsUpManager;
     45     private boolean mIsUpdatingUnchangedGroup;
     46 
     47     public void setOnGroupChangeListener(OnGroupChangeListener listener) {
     48         mListener = listener;
     49     }
     50 
     51     public boolean isGroupExpanded(StatusBarNotification sbn) {
     52         NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
     53         if (group == null) {
     54             return false;
     55         }
     56         return group.expanded;
     57     }
     58 
     59     public void setGroupExpanded(StatusBarNotification sbn, boolean expanded) {
     60         NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
     61         if (group == null) {
     62             return;
     63         }
     64         setGroupExpanded(group, expanded);
     65     }
     66 
     67     private void setGroupExpanded(NotificationGroup group, boolean expanded) {
     68         group.expanded = expanded;
     69         if (group.summary != null) {
     70             mListener.onGroupExpansionChanged(group.summary.row, expanded);
     71         }
     72     }
     73 
     74     public void onEntryRemoved(NotificationData.Entry removed) {
     75         onEntryRemovedInternal(removed, removed.notification);
     76         mIsolatedEntries.remove(removed.key);
     77     }
     78 
     79     /**
     80      * An entry was removed.
     81      *
     82      * @param removed the removed entry
     83      * @param sbn the notification the entry has, which doesn't need to be the same as it's internal
     84      *            notification
     85      */
     86     private void onEntryRemovedInternal(NotificationData.Entry removed,
     87             final StatusBarNotification sbn) {
     88         String groupKey = getGroupKey(sbn);
     89         final NotificationGroup group = mGroupMap.get(groupKey);
     90         if (group == null) {
     91             // When an app posts 2 different notifications as summary of the same group, then a
     92             // cancellation of the first notification removes this group.
     93             // This situation is not supported and we will not allow such notifications anymore in
     94             // the close future. See b/23676310 for reference.
     95             return;
     96         }
     97         if (isGroupChild(sbn)) {
     98             group.children.remove(removed);
     99         } else {
    100             group.summary = null;
    101         }
    102         updateSuppression(group);
    103         if (group.children.isEmpty()) {
    104             if (group.summary == null) {
    105                 mGroupMap.remove(groupKey);
    106             }
    107         }
    108     }
    109 
    110     public void onEntryAdded(final NotificationData.Entry added) {
    111         final StatusBarNotification sbn = added.notification;
    112         boolean isGroupChild = isGroupChild(sbn);
    113         String groupKey = getGroupKey(sbn);
    114         NotificationGroup group = mGroupMap.get(groupKey);
    115         if (group == null) {
    116             group = new NotificationGroup();
    117             mGroupMap.put(groupKey, group);
    118         }
    119         if (isGroupChild) {
    120             group.children.add(added);
    121             updateSuppression(group);
    122         } else {
    123             group.summary = added;
    124             group.expanded = added.row.areChildrenExpanded();
    125             updateSuppression(group);
    126             if (!group.children.isEmpty()) {
    127                 HashSet<NotificationData.Entry> childrenCopy =
    128                         (HashSet<NotificationData.Entry>) group.children.clone();
    129                 for (NotificationData.Entry child : childrenCopy) {
    130                     onEntryBecomingChild(child);
    131                 }
    132                 mListener.onGroupCreatedFromChildren(group);
    133             }
    134         }
    135     }
    136 
    137     private void onEntryBecomingChild(NotificationData.Entry entry) {
    138         if (entry.row.isHeadsUp()) {
    139             onHeadsUpStateChanged(entry, true);
    140         }
    141     }
    142 
    143     private void updateSuppression(NotificationGroup group) {
    144         if (group == null) {
    145             return;
    146         }
    147         boolean prevSuppressed = group.suppressed;
    148         group.suppressed = group.summary != null && !group.expanded
    149                 && (group.children.size() == 1
    150                 || (group.children.size() == 0
    151                         && group.summary.notification.getNotification().isGroupSummary()
    152                         && hasIsolatedChildren(group)));
    153         if (prevSuppressed != group.suppressed) {
    154             if (group.suppressed) {
    155                 handleSuppressedSummaryHeadsUpped(group.summary);
    156             }
    157             if (!mIsUpdatingUnchangedGroup) {
    158                 mListener.onGroupsChanged();
    159             }
    160         }
    161     }
    162 
    163     private boolean hasIsolatedChildren(NotificationGroup group) {
    164         return getNumberOfIsolatedChildren(group.summary.notification.getGroupKey()) != 0;
    165     }
    166 
    167     private int getNumberOfIsolatedChildren(String groupKey) {
    168         int count = 0;
    169         for (StatusBarNotification sbn : mIsolatedEntries.values()) {
    170             if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
    171                 count++;
    172             }
    173         }
    174         return count;
    175     }
    176 
    177     private NotificationData.Entry getIsolatedChild(String groupKey) {
    178         for (StatusBarNotification sbn : mIsolatedEntries.values()) {
    179             if (sbn.getGroupKey().equals(groupKey) && isIsolated(sbn)) {
    180                 return mGroupMap.get(sbn.getKey()).summary;
    181             }
    182         }
    183         return null;
    184     }
    185 
    186     public void onEntryUpdated(NotificationData.Entry entry,
    187             StatusBarNotification oldNotification) {
    188         String oldKey = oldNotification.getGroupKey();
    189         String newKey = entry.notification.getGroupKey();
    190         boolean groupKeysChanged = !oldKey.equals(newKey);
    191         boolean wasGroupChild = isGroupChild(oldNotification);
    192         boolean isGroupChild = isGroupChild(entry.notification);
    193         mIsUpdatingUnchangedGroup = !groupKeysChanged && wasGroupChild == isGroupChild;
    194         if (mGroupMap.get(getGroupKey(oldNotification)) != null) {
    195             onEntryRemovedInternal(entry, oldNotification);
    196         }
    197         onEntryAdded(entry);
    198         mIsUpdatingUnchangedGroup = false;
    199         if (isIsolated(entry.notification)) {
    200             mIsolatedEntries.put(entry.key, entry.notification);
    201             if (groupKeysChanged) {
    202                 updateSuppression(mGroupMap.get(oldKey));
    203                 updateSuppression(mGroupMap.get(newKey));
    204             }
    205         } else if (!wasGroupChild && isGroupChild) {
    206             onEntryBecomingChild(entry);
    207         }
    208     }
    209 
    210     public boolean isSummaryOfSuppressedGroup(StatusBarNotification sbn) {
    211         return isGroupSuppressed(getGroupKey(sbn)) && sbn.getNotification().isGroupSummary();
    212     }
    213 
    214     private boolean isOnlyChild(StatusBarNotification sbn) {
    215         return !sbn.getNotification().isGroupSummary()
    216                 && getTotalNumberOfChildren(sbn) == 1;
    217     }
    218 
    219     public boolean isOnlyChildInGroup(StatusBarNotification sbn) {
    220         if (!isOnlyChild(sbn)) {
    221             return false;
    222         }
    223         ExpandableNotificationRow logicalGroupSummary = getLogicalGroupSummary(sbn);
    224         return logicalGroupSummary != null
    225                 && !logicalGroupSummary.getStatusBarNotification().equals(sbn);
    226     }
    227 
    228     private int getTotalNumberOfChildren(StatusBarNotification sbn) {
    229         int isolatedChildren = getNumberOfIsolatedChildren(sbn.getGroupKey());
    230         NotificationGroup group = mGroupMap.get(sbn.getGroupKey());
    231         int realChildren = group != null ? group.children.size() : 0;
    232         return isolatedChildren + realChildren;
    233     }
    234 
    235     private boolean isGroupSuppressed(String groupKey) {
    236         NotificationGroup group = mGroupMap.get(groupKey);
    237         return group != null && group.suppressed;
    238     }
    239 
    240     public void setStatusBarState(int newState) {
    241         if (mBarState == newState) {
    242             return;
    243         }
    244         mBarState = newState;
    245         if (mBarState == StatusBarState.KEYGUARD) {
    246             collapseAllGroups();
    247         }
    248     }
    249 
    250     public void collapseAllGroups() {
    251         // Because notifications can become isolated when the group becomes suppressed it can
    252         // lead to concurrent modifications while looping. We need to make a copy.
    253         ArrayList<NotificationGroup> groupCopy = new ArrayList<>(mGroupMap.values());
    254         int size = groupCopy.size();
    255         for (int i = 0; i < size; i++) {
    256             NotificationGroup group =  groupCopy.get(i);
    257             if (group.expanded) {
    258                 setGroupExpanded(group, false);
    259             }
    260             updateSuppression(group);
    261         }
    262     }
    263 
    264     /**
    265      * @return whether a given notification is a child in a group which has a summary
    266      */
    267     public boolean isChildInGroupWithSummary(StatusBarNotification sbn) {
    268         if (!isGroupChild(sbn)) {
    269             return false;
    270         }
    271         NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
    272         if (group == null || group.summary == null || group.suppressed) {
    273             return false;
    274         }
    275         if (group.children.isEmpty()) {
    276             // If the suppression of a group changes because the last child was removed, this can
    277             // still be called temporarily because the child hasn't been fully removed yet. Let's
    278             // make sure we still return false in that case.
    279             return false;
    280         }
    281         return true;
    282     }
    283 
    284     /**
    285      * @return whether a given notification is a summary in a group which has children
    286      */
    287     public boolean isSummaryOfGroup(StatusBarNotification sbn) {
    288         if (!isGroupSummary(sbn)) {
    289             return false;
    290         }
    291         NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
    292         if (group == null) {
    293             return false;
    294         }
    295         return !group.children.isEmpty();
    296     }
    297 
    298     /**
    299      * Get the summary of a specified status bar notification. For isolated notification this return
    300      * itself.
    301      */
    302     public ExpandableNotificationRow getGroupSummary(StatusBarNotification sbn) {
    303         return getGroupSummary(getGroupKey(sbn));
    304     }
    305 
    306     /**
    307      * Similar to {@link #getGroupSummary(StatusBarNotification)} but doesn't get the visual summary
    308      * but the logical summary, i.e when a child is isolated, it still returns the summary as if
    309      * it wasn't isolated.
    310      */
    311     public ExpandableNotificationRow getLogicalGroupSummary(
    312             StatusBarNotification sbn) {
    313         return getGroupSummary(sbn.getGroupKey());
    314     }
    315 
    316     @Nullable
    317     private ExpandableNotificationRow getGroupSummary(String groupKey) {
    318         NotificationGroup group = mGroupMap.get(groupKey);
    319         return group == null ? null
    320                 : group.summary == null ? null
    321                         : group.summary.row;
    322     }
    323 
    324     /** @return group expansion state after toggling. */
    325     public boolean toggleGroupExpansion(StatusBarNotification sbn) {
    326         NotificationGroup group = mGroupMap.get(getGroupKey(sbn));
    327         if (group == null) {
    328             return false;
    329         }
    330         setGroupExpanded(group, !group.expanded);
    331         return group.expanded;
    332     }
    333 
    334     private boolean isIsolated(StatusBarNotification sbn) {
    335         return mIsolatedEntries.containsKey(sbn.getKey());
    336     }
    337 
    338     private boolean isGroupSummary(StatusBarNotification sbn) {
    339         if (isIsolated(sbn)) {
    340             return true;
    341         }
    342         return sbn.getNotification().isGroupSummary();
    343     }
    344 
    345     private boolean isGroupChild(StatusBarNotification sbn) {
    346         if (isIsolated(sbn)) {
    347             return false;
    348         }
    349         return sbn.isGroup() && !sbn.getNotification().isGroupSummary();
    350     }
    351 
    352     private String getGroupKey(StatusBarNotification sbn) {
    353         if (isIsolated(sbn)) {
    354             return sbn.getKey();
    355         }
    356         return sbn.getGroupKey();
    357     }
    358 
    359     @Override
    360     public void onHeadsUpPinnedModeChanged(boolean inPinnedMode) {
    361     }
    362 
    363     @Override
    364     public void onHeadsUpPinned(ExpandableNotificationRow headsUp) {
    365     }
    366 
    367     @Override
    368     public void onHeadsUpUnPinned(ExpandableNotificationRow headsUp) {
    369     }
    370 
    371     @Override
    372     public void onHeadsUpStateChanged(NotificationData.Entry entry, boolean isHeadsUp) {
    373         final StatusBarNotification sbn = entry.notification;
    374         if (entry.row.isHeadsUp()) {
    375             if (shouldIsolate(sbn)) {
    376                 // We will be isolated now, so lets update the groups
    377                 onEntryRemovedInternal(entry, entry.notification);
    378 
    379                 mIsolatedEntries.put(sbn.getKey(), sbn);
    380 
    381                 onEntryAdded(entry);
    382                 // We also need to update the suppression of the old group, because this call comes
    383                 // even before the groupManager knows about the notification at all.
    384                 // When the notification gets added afterwards it is already isolated and therefore
    385                 // it doesn't lead to an update.
    386                 updateSuppression(mGroupMap.get(entry.notification.getGroupKey()));
    387                 mListener.onGroupsChanged();
    388             } else {
    389                 handleSuppressedSummaryHeadsUpped(entry);
    390             }
    391         } else {
    392             if (mIsolatedEntries.containsKey(sbn.getKey())) {
    393                 // not isolated anymore, we need to update the groups
    394                 onEntryRemovedInternal(entry, entry.notification);
    395                 mIsolatedEntries.remove(sbn.getKey());
    396                 onEntryAdded(entry);
    397                 mListener.onGroupsChanged();
    398             }
    399         }
    400     }
    401 
    402     private void handleSuppressedSummaryHeadsUpped(NotificationData.Entry entry) {
    403         StatusBarNotification sbn = entry.notification;
    404         if (!isGroupSuppressed(sbn.getGroupKey())
    405                 || !sbn.getNotification().isGroupSummary()
    406                 || !entry.row.isHeadsUp()) {
    407             return;
    408         }
    409         // The parent of a suppressed group got huned, lets hun the child!
    410         NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
    411         if (notificationGroup != null) {
    412             Iterator<NotificationData.Entry> iterator = notificationGroup.children.iterator();
    413             NotificationData.Entry child = iterator.hasNext() ? iterator.next() : null;
    414             if (child == null) {
    415                 child = getIsolatedChild(sbn.getGroupKey());
    416             }
    417             if (child != null) {
    418                 if (mHeadsUpManager.isHeadsUp(child.key)) {
    419                     mHeadsUpManager.updateNotification(child, true);
    420                 } else {
    421                     mHeadsUpManager.showNotification(child);
    422                 }
    423             }
    424         }
    425         mHeadsUpManager.releaseImmediately(entry.key);
    426     }
    427 
    428     private boolean shouldIsolate(StatusBarNotification sbn) {
    429         NotificationGroup notificationGroup = mGroupMap.get(sbn.getGroupKey());
    430         return (sbn.isGroup() && !sbn.getNotification().isGroupSummary())
    431                 && (sbn.getNotification().fullScreenIntent != null
    432                         || notificationGroup == null
    433                         || !notificationGroup.expanded
    434                         || isGroupNotFullyVisible(notificationGroup));
    435     }
    436 
    437     private boolean isGroupNotFullyVisible(NotificationGroup notificationGroup) {
    438         return notificationGroup.summary == null
    439                 || notificationGroup.summary.row.getClipTopAmount() > 0
    440                 || notificationGroup.summary.row.getTranslationY() < 0;
    441     }
    442 
    443     public void setHeadsUpManager(HeadsUpManager headsUpManager) {
    444         mHeadsUpManager = headsUpManager;
    445     }
    446 
    447     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    448         pw.println("GroupManager state:");
    449         pw.println("  number of groups: " +  mGroupMap.size());
    450         for (Map.Entry<String, NotificationGroup>  entry : mGroupMap.entrySet()) {
    451             pw.println("\n    key: " + entry.getKey()); pw.println(entry.getValue());
    452         }
    453         pw.println("\n    isolated entries: " +  mIsolatedEntries.size());
    454         for (Map.Entry<String, StatusBarNotification> entry : mIsolatedEntries.entrySet()) {
    455             pw.print("      "); pw.print(entry.getKey());
    456             pw.print(", "); pw.println(entry.getValue());
    457         }
    458     }
    459 
    460     public static class NotificationGroup {
    461         public final HashSet<NotificationData.Entry> children = new HashSet<>();
    462         public NotificationData.Entry summary;
    463         public boolean expanded;
    464         /**
    465          * Is this notification group suppressed, i.e its summary is hidden
    466          */
    467         public boolean suppressed;
    468 
    469         @Override
    470         public String toString() {
    471             String result = "    summary:\n      "
    472                     + (summary != null ? summary.notification : "null");
    473             result += "\n    children size: " + children.size();
    474             for (NotificationData.Entry child : children) {
    475                 result += "\n      " + child.notification;
    476             }
    477             return result;
    478         }
    479     }
    480 
    481     public interface OnGroupChangeListener {
    482         /**
    483          * The expansion of a group has changed.
    484          *
    485          * @param changedRow the row for which the expansion has changed, which is also the summary
    486          * @param expanded a boolean indicating the new expanded state
    487          */
    488         void onGroupExpansionChanged(ExpandableNotificationRow changedRow, boolean expanded);
    489 
    490         /**
    491          * A group of children just received a summary notification and should therefore become
    492          * children of it.
    493          *
    494          * @param group the group created
    495          */
    496         void onGroupCreatedFromChildren(NotificationGroup group);
    497 
    498         /**
    499          * The groups have changed. This can happen if the isolation of a child has changes or if a
    500          * group became suppressed / unsuppressed
    501          */
    502         void onGroupsChanged();
    503     }
    504 }
    505