1 /* 2 * Copyright (C) 2017 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.launcher3.badge; 18 19 import com.android.launcher3.notification.NotificationInfo; 20 import com.android.launcher3.notification.NotificationKeyData; 21 import com.android.launcher3.util.PackageUserKey; 22 23 import java.util.ArrayList; 24 import java.util.List; 25 26 /** 27 * Contains data to be used in an icon badge. 28 */ 29 public class BadgeInfo { 30 31 public static final int MAX_COUNT = 999; 32 33 /** Used to link this BadgeInfo to icons on the workspace and all apps */ 34 private PackageUserKey mPackageUserKey; 35 36 /** 37 * The keys of the notifications that this badge represents. These keys can later be 38 * used to retrieve {@link NotificationInfo}'s. 39 */ 40 private List<NotificationKeyData> mNotificationKeys; 41 42 /** 43 * The current sum of the counts in {@link #mNotificationKeys}, 44 * updated whenever a key is added or removed. 45 */ 46 private int mTotalCount; 47 48 public BadgeInfo(PackageUserKey packageUserKey) { 49 mPackageUserKey = packageUserKey; 50 mNotificationKeys = new ArrayList<>(); 51 } 52 53 /** 54 * Returns whether the notification was added or its count changed. 55 */ 56 public boolean addOrUpdateNotificationKey(NotificationKeyData notificationKey) { 57 int indexOfPrevKey = mNotificationKeys.indexOf(notificationKey); 58 NotificationKeyData prevKey = indexOfPrevKey == -1 ? null 59 : mNotificationKeys.get(indexOfPrevKey); 60 if (prevKey != null) { 61 if (prevKey.count == notificationKey.count) { 62 return false; 63 } 64 // Notification was updated with a new count. 65 mTotalCount -= prevKey.count; 66 mTotalCount += notificationKey.count; 67 prevKey.count = notificationKey.count; 68 return true; 69 } 70 boolean added = mNotificationKeys.add(notificationKey); 71 if (added) { 72 mTotalCount += notificationKey.count; 73 } 74 return added; 75 } 76 77 /** 78 * Returns whether the notification was removed (false if it didn't exist). 79 */ 80 public boolean removeNotificationKey(NotificationKeyData notificationKey) { 81 boolean removed = mNotificationKeys.remove(notificationKey); 82 if (removed) { 83 mTotalCount -= notificationKey.count; 84 } 85 return removed; 86 } 87 88 public List<NotificationKeyData> getNotificationKeys() { 89 return mNotificationKeys; 90 } 91 92 public int getNotificationCount() { 93 return Math.min(mTotalCount, MAX_COUNT); 94 } 95 96 /** 97 * Whether newBadge represents the same PackageUserKey as this badge, and icons with 98 * this badge should be invalidated. So, for instance, if a badge has 3 notifications 99 * and one of those notifications is updated, this method should return false because 100 * the badge still says "3" and the contents of those notifications are only retrieved 101 * upon long-click. This method always returns true when adding or removing notifications, 102 * or if the badge has a notification icon to show. 103 */ 104 public boolean shouldBeInvalidated(BadgeInfo newBadge) { 105 return mPackageUserKey.equals(newBadge.mPackageUserKey) 106 && (getNotificationCount() != newBadge.getNotificationCount()); 107 } 108 } 109