1010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham/*
2010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * Copyright (C) 2017 The Android Open Source Project
3010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham *
4010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * Licensed under the Apache License, Version 2.0 (the "License");
5010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * you may not use this file except in compliance with the License.
6010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * You may obtain a copy of the License at
7010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham *
8010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham *      http://www.apache.org/licenses/LICENSE-2.0
9010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham *
10010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * Unless required by applicable law or agreed to in writing, software
11010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * distributed under the License is distributed on an "AS IS" BASIS,
12010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * See the License for the specific language governing permissions and
14010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * limitations under the License.
15010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham */
16010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
17010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhampackage com.android.launcher3.popup;
18010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
19010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport android.content.ComponentName;
20010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport android.service.notification.StatusBarNotification;
212f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickhamimport android.support.annotation.NonNull;
22010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport android.util.Log;
23010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
24010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport com.android.launcher3.ItemInfo;
25010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport com.android.launcher3.Launcher;
26580edcf529b0755f2bba442e97dd02d8cdad3b82Tony Wickhamimport com.android.launcher3.Utilities;
27010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport com.android.launcher3.badge.BadgeInfo;
28a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickhamimport com.android.launcher3.notification.NotificationInfo;
292f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickhamimport com.android.launcher3.notification.NotificationKeyData;
309438ed414fdabadb4cd09da184867b1c44b91095Tony Wickhamimport com.android.launcher3.notification.NotificationListener;
31010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport com.android.launcher3.shortcuts.DeepShortcutManager;
32010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport com.android.launcher3.util.ComponentKey;
33010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport com.android.launcher3.util.MultiHashMap;
34010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport com.android.launcher3.util.PackageUserKey;
35010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
36a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickhamimport java.util.ArrayList;
37010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport java.util.Collections;
38010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport java.util.HashMap;
39a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickhamimport java.util.Iterator;
40010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport java.util.List;
41010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhamimport java.util.Map;
42a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickhamimport java.util.Set;
43010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
44010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham/**
45010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham * Provides data for the popup menu that appears after long-clicking on apps.
46010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham */
47010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickhampublic class PopupDataProvider implements NotificationListener.NotificationsChangedListener {
48010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
49010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    private static final boolean LOGD = false;
50010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    private static final String TAG = "PopupDataProvider";
51010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
5226b1746593851e239bfff65ae7a69b809ad87b99Tony Wickham    /** Note that these are in order of priority. */
53a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham    private static final SystemShortcut[] SYSTEM_SHORTCUTS = new SystemShortcut[] {
5426b1746593851e239bfff65ae7a69b809ad87b99Tony Wickham            new SystemShortcut.AppInfo(),
552f54a1625ae031ffbb2a99a00c7b25de3c677828Tony Wickham            new SystemShortcut.Widgets(),
5626b1746593851e239bfff65ae7a69b809ad87b99Tony Wickham    };
5726b1746593851e239bfff65ae7a69b809ad87b99Tony Wickham
58010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    private final Launcher mLauncher;
59010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
60010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    /** Maps launcher activity components to their list of shortcut ids. */
61010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    private MultiHashMap<ComponentKey, String> mDeepShortcutMap = new MultiHashMap<>();
62010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    /** Maps packages to their BadgeInfo's . */
63010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    private Map<PackageUserKey, BadgeInfo> mPackageUserToBadgeInfos = new HashMap<>();
64010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
65010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    public PopupDataProvider(Launcher launcher) {
66010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        mLauncher = launcher;
67010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
68010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
69010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    @Override
702f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham    public void onNotificationPosted(PackageUserKey postedPackageUserKey,
712f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham            NotificationKeyData notificationKey, boolean shouldBeFilteredOut) {
72a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        BadgeInfo badgeInfo = mPackageUserToBadgeInfos.get(postedPackageUserKey);
73362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham        boolean badgeShouldBeRefreshed;
74a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        if (badgeInfo == null) {
75988f34b7f2bb7e57d99ec19fbe9b7024da1fe52fTony Wickham            if (!shouldBeFilteredOut) {
76988f34b7f2bb7e57d99ec19fbe9b7024da1fe52fTony Wickham                BadgeInfo newBadgeInfo = new BadgeInfo(postedPackageUserKey);
77362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham                newBadgeInfo.addOrUpdateNotificationKey(notificationKey);
78988f34b7f2bb7e57d99ec19fbe9b7024da1fe52fTony Wickham                mPackageUserToBadgeInfos.put(postedPackageUserKey, newBadgeInfo);
79362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham                badgeShouldBeRefreshed = true;
80988f34b7f2bb7e57d99ec19fbe9b7024da1fe52fTony Wickham            } else {
81362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham                badgeShouldBeRefreshed = false;
82988f34b7f2bb7e57d99ec19fbe9b7024da1fe52fTony Wickham            }
83a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        } else {
84362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham            badgeShouldBeRefreshed = shouldBeFilteredOut
85988f34b7f2bb7e57d99ec19fbe9b7024da1fe52fTony Wickham                    ? badgeInfo.removeNotificationKey(notificationKey)
86362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham                    : badgeInfo.addOrUpdateNotificationKey(notificationKey);
870530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham            if (badgeInfo.getNotificationKeys().size() == 0) {
88ff7364a8837cf228b936555745dea7ba1648a214Tony                mPackageUserToBadgeInfos.remove(postedPackageUserKey);
89ff7364a8837cf228b936555745dea7ba1648a214Tony            }
90010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
91580edcf529b0755f2bba442e97dd02d8cdad3b82Tony Wickham        updateLauncherIconBadges(Utilities.singletonHashSet(postedPackageUserKey),
92362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham                badgeShouldBeRefreshed);
93010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
94010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
95010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    @Override
962f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham    public void onNotificationRemoved(PackageUserKey removedPackageUserKey,
972f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham            NotificationKeyData notificationKey) {
98010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        BadgeInfo oldBadgeInfo = mPackageUserToBadgeInfos.get(removedPackageUserKey);
99010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        if (oldBadgeInfo != null && oldBadgeInfo.removeNotificationKey(notificationKey)) {
1000530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham            if (oldBadgeInfo.getNotificationKeys().size() == 0) {
101010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham                mPackageUserToBadgeInfos.remove(removedPackageUserKey);
102010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            }
103580edcf529b0755f2bba442e97dd02d8cdad3b82Tony Wickham            updateLauncherIconBadges(Utilities.singletonHashSet(removedPackageUserKey));
1049438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham
1059438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham            PopupContainerWithArrow openContainer = PopupContainerWithArrow.getOpen(mLauncher);
1069438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham            if (openContainer != null) {
1079438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham                openContainer.trimNotifications(mPackageUserToBadgeInfos);
1089438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham            }
109010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
110010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
111010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
112010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    @Override
113010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    public void onNotificationFullRefresh(List<StatusBarNotification> activeNotifications) {
114010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        if (activeNotifications == null) return;
115010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        // This will contain the PackageUserKeys which have updated badges.
116010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        HashMap<PackageUserKey, BadgeInfo> updatedBadges = new HashMap<>(mPackageUserToBadgeInfos);
117010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        mPackageUserToBadgeInfos.clear();
118010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        for (StatusBarNotification notification : activeNotifications) {
119010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            PackageUserKey packageUserKey = PackageUserKey.fromNotification(notification);
120010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            BadgeInfo badgeInfo = mPackageUserToBadgeInfos.get(packageUserKey);
121010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            if (badgeInfo == null) {
122010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham                badgeInfo = new BadgeInfo(packageUserKey);
123010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham                mPackageUserToBadgeInfos.put(packageUserKey, badgeInfo);
124010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            }
125362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham            badgeInfo.addOrUpdateNotificationKey(NotificationKeyData
1262f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham                    .fromNotification(notification));
127010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
128010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
129010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        // Add and remove from updatedBadges so it contains the PackageUserKeys of updated badges.
130010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        for (PackageUserKey packageUserKey : mPackageUserToBadgeInfos.keySet()) {
131010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            BadgeInfo prevBadge = updatedBadges.get(packageUserKey);
132010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            BadgeInfo newBadge = mPackageUserToBadgeInfos.get(packageUserKey);
133010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            if (prevBadge == null) {
134010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham                updatedBadges.put(packageUserKey, newBadge);
135010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            } else {
136010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham                if (!prevBadge.shouldBeInvalidated(newBadge)) {
137010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham                    updatedBadges.remove(packageUserKey);
138010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham                }
139010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            }
140010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
141010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
142010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        if (!updatedBadges.isEmpty()) {
143a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham            updateLauncherIconBadges(updatedBadges.keySet());
144010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
1459438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham
1469438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham        PopupContainerWithArrow openContainer = PopupContainerWithArrow.getOpen(mLauncher);
1479438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham        if (openContainer != null) {
1489438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham            openContainer.trimNotifications(updatedBadges);
1499438ed414fdabadb4cd09da184867b1c44b91095Tony Wickham        }
150010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
151010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
152a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham    private void updateLauncherIconBadges(Set<PackageUserKey> updatedBadges) {
153a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        updateLauncherIconBadges(updatedBadges, true);
154a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham    }
155a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham
156a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham    /**
157a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     * Updates the icons on launcher (workspace, folders, all apps) to refresh their badges.
158a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     * @param updatedBadges The packages whose badges should be refreshed (either a notification was
159a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     *                      added or removed, or the badge should show the notification icon).
160362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham     * @param shouldRefresh An optional parameter that will allow us to only refresh badges that
161362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham     *                      have actually changed. If a notification updated its content but not
162362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham     *                      its count or icon, then the badge doesn't change.
163a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     */
164a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham    private void updateLauncherIconBadges(Set<PackageUserKey> updatedBadges,
165362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham            boolean shouldRefresh) {
166a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        Iterator<PackageUserKey> iterator = updatedBadges.iterator();
167a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        while (iterator.hasNext()) {
168a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham            BadgeInfo badgeInfo = mPackageUserToBadgeInfos.get(iterator.next());
169362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham            if (badgeInfo != null && !updateBadgeIcon(badgeInfo) && !shouldRefresh) {
170362106233919ff64cf462efe395f53c33a8ec4bfTony Wickham                // The notification icon isn't used, and the badge hasn't changed
171a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham                // so there is no update to be made.
172a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham                iterator.remove();
173a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham            }
174a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        }
175a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        if (!updatedBadges.isEmpty()) {
176a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham            mLauncher.updateIconBadges(updatedBadges);
177a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        }
178a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham    }
179a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham
180a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham    /**
181a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     * Determines whether the badge should show a notification icon rather than a number,
182a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     * and sets that icon on the BadgeInfo if so.
183a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     * @param badgeInfo The badge to update with an icon (null if it shouldn't show one).
184a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     * @return Whether the badge icon potentially changed (true unless it stayed null).
185a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham     */
186a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham    private boolean updateBadgeIcon(BadgeInfo badgeInfo) {
187a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        boolean hadNotificationToShow = badgeInfo.hasNotificationToShow();
188a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        NotificationInfo notificationInfo = null;
189be3c7d0c56fe35e8822c4bb5ee22ad44ed504850Tony        NotificationListener notificationListener = NotificationListener.getInstanceIfConnected();
1900530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham        if (notificationListener != null && badgeInfo.getNotificationKeys().size() >= 1) {
1910530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham            // Look for the most recent notification that has an icon that should be shown in badge.
1920530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham            for (NotificationKeyData notificationKeyData : badgeInfo.getNotificationKeys()) {
1930530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                String notificationKey = notificationKeyData.notificationKey;
1940530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                StatusBarNotification[] activeNotifications = notificationListener
1950530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                        .getActiveNotifications(new String[]{notificationKey});
1960530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                if (activeNotifications.length == 1) {
1970530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                    notificationInfo = new NotificationInfo(mLauncher, activeNotifications[0]);
1980530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                    if (notificationInfo.shouldShowIconInBadge()) {
1990530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                        // Found an appropriate icon.
2000530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                        break;
2010530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                    } else {
2020530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                        // Keep looking.
2030530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                        notificationInfo = null;
2040530e8c60809afa92b1aa907376f0553ad94d58fTony Wickham                    }
205a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham                }
206a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham            }
207a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        }
208a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        badgeInfo.setNotificationToShow(notificationInfo);
209a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham        return hadNotificationToShow || badgeInfo.hasNotificationToShow();
210a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham    }
211a799bed8038ae97ee8985bc4475756391f7f4f0cTony Wickham
212010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    public void setDeepShortcutMap(MultiHashMap<ComponentKey, String> deepShortcutMapCopy) {
213010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        mDeepShortcutMap = deepShortcutMapCopy;
214010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        if (LOGD) Log.d(TAG, "bindDeepShortcutMap: " + mDeepShortcutMap);
215010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
216010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
217010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    public List<String> getShortcutIdsForItem(ItemInfo info) {
218010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        if (!DeepShortcutManager.supportsShortcuts(info)) {
219010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            return Collections.EMPTY_LIST;
220010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
221010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        ComponentName component = info.getTargetComponent();
222010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        if (component == null) {
223010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            return Collections.EMPTY_LIST;
224010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
225010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
226010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        List<String> ids = mDeepShortcutMap.get(new ComponentKey(component, info.user));
227010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        return ids == null ? Collections.EMPTY_LIST : ids;
228010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
229010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
230010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    public BadgeInfo getBadgeInfoForItem(ItemInfo info) {
231010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        if (!DeepShortcutManager.supportsShortcuts(info)) {
232010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            return null;
233010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
234010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
235010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        return mPackageUserToBadgeInfos.get(PackageUserKey.fromItemInfo(info));
236010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
237010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
2382f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham    public @NonNull List<NotificationKeyData> getNotificationKeysForItem(ItemInfo info) {
2394d096298ce44d9b979ca0897032421e26392ae59Tony Wickham        BadgeInfo badgeInfo = getBadgeInfoForItem(info);
2402f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham        return badgeInfo == null ? Collections.EMPTY_LIST : badgeInfo.getNotificationKeys();
241010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
242010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
243010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    /** This makes a potentially expensive binder call and should be run on a background thread. */
2442f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham    public @NonNull List<StatusBarNotification> getStatusBarNotificationsForKeys(
2452f5bb169154d25bc8c164f057117fb056ad2de96Tony Wickham            List<NotificationKeyData> notificationKeys) {
246be3c7d0c56fe35e8822c4bb5ee22ad44ed504850Tony        NotificationListener notificationListener = NotificationListener.getInstanceIfConnected();
247010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        return notificationListener == null ? Collections.EMPTY_LIST
248010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham                : notificationListener.getNotificationsForKeys(notificationKeys);
249010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
250010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham
251a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham    public @NonNull List<SystemShortcut> getEnabledSystemShortcutsForItem(ItemInfo info) {
252a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham        List<SystemShortcut> systemShortcuts = new ArrayList<>();
253a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham        for (SystemShortcut systemShortcut : SYSTEM_SHORTCUTS) {
254a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham            if (systemShortcut.getOnClickListener(mLauncher, info) != null) {
255a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham                systemShortcuts.add(systemShortcut);
256a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham            }
257a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham        }
258a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham        return systemShortcuts;
259a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham    }
260a7e1c1c08854930ddb4366698d5bf079ae0536f3Tony Wickham
261010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    public void cancelNotification(String notificationKey) {
262be3c7d0c56fe35e8822c4bb5ee22ad44ed504850Tony        NotificationListener notificationListener = NotificationListener.getInstanceIfConnected();
263010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        if (notificationListener == null) {
264010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham            return;
265010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        }
266010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham        notificationListener.cancelNotification(notificationKey);
267010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham    }
268010d2550184179785e4467ff1ffb4f4f4e850ec2Tony Wickham}
269