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