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.dot; 18 19 import androidx.annotation.NonNull; 20 21 import com.android.launcher3.notification.NotificationInfo; 22 import com.android.launcher3.notification.NotificationKeyData; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 27 /** 28 * Contains data to be used for a notification dot. 29 */ 30 public class DotInfo { 31 32 public static final int MAX_COUNT = 999; 33 34 /** 35 * The keys of the notifications that this dot represents. These keys can later be 36 * used to retrieve {@link NotificationInfo}'s. 37 */ 38 private final List<NotificationKeyData> mNotificationKeys = new ArrayList<>(); 39 40 /** 41 * The current sum of the counts in {@link #mNotificationKeys}, 42 * updated whenever a key is added or removed. 43 */ 44 private int mTotalCount; 45 46 /** 47 * Returns whether the notification was added or its count changed. 48 */ addOrUpdateNotificationKey(NotificationKeyData notificationKey)49 public boolean addOrUpdateNotificationKey(NotificationKeyData notificationKey) { 50 int indexOfPrevKey = mNotificationKeys.indexOf(notificationKey); 51 NotificationKeyData prevKey = indexOfPrevKey == -1 ? null 52 : mNotificationKeys.get(indexOfPrevKey); 53 if (prevKey != null) { 54 if (prevKey.count == notificationKey.count) { 55 return false; 56 } 57 // Notification was updated with a new count. 58 mTotalCount -= prevKey.count; 59 mTotalCount += notificationKey.count; 60 prevKey.count = notificationKey.count; 61 return true; 62 } 63 boolean added = mNotificationKeys.add(notificationKey); 64 if (added) { 65 mTotalCount += notificationKey.count; 66 } 67 return added; 68 } 69 70 /** 71 * Returns whether the notification was removed (false if it didn't exist). 72 */ removeNotificationKey(NotificationKeyData notificationKey)73 public boolean removeNotificationKey(NotificationKeyData notificationKey) { 74 boolean removed = mNotificationKeys.remove(notificationKey); 75 if (removed) { 76 mTotalCount -= notificationKey.count; 77 } 78 return removed; 79 } 80 getNotificationKeys()81 public List<NotificationKeyData> getNotificationKeys() { 82 return mNotificationKeys; 83 } 84 getNotificationCount()85 public int getNotificationCount() { 86 return Math.min(mTotalCount, MAX_COUNT); 87 } 88 89 @NonNull 90 @Override toString()91 public String toString() { 92 return Integer.toString(mTotalCount); 93 } 94 } 95