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.notification;
18 
19 import android.app.Notification;
20 import android.app.Person;
21 import android.service.notification.StatusBarNotification;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.annotation.VisibleForTesting;
26 
27 import com.android.launcher3.Utilities;
28 
29 import java.util.ArrayList;
30 
31 /**
32  * The key data associated with the notification, used to determine what to include
33  * in dots and stub popup views before they are populated.
34  */
35 public class NotificationKeyData {
36     public final String notificationKey;
37     public final String shortcutId;
38     @NonNull
39     public final String[] personKeysFromNotification;
40     public int count;
41 
42     @VisibleForTesting
NotificationKeyData(String notificationKey)43     public NotificationKeyData(String notificationKey) {
44         this(notificationKey, null, 1, new String[]{});
45     }
46 
NotificationKeyData(String notificationKey, String shortcutId, int count, String[] personKeysFromNotification)47     private NotificationKeyData(String notificationKey, String shortcutId, int count,
48             String[] personKeysFromNotification) {
49         this.notificationKey = notificationKey;
50         this.shortcutId = shortcutId;
51         this.count = Math.max(1, count);
52         this.personKeysFromNotification = personKeysFromNotification;
53     }
54 
fromNotification(StatusBarNotification sbn)55     public static NotificationKeyData fromNotification(StatusBarNotification sbn) {
56         Notification notif = sbn.getNotification();
57         return new NotificationKeyData(sbn.getKey(), notif.getShortcutId(), notif.number,
58                 extractPersonKeyOnly(notif.extras.getParcelableArrayList(
59                         Notification.EXTRA_PEOPLE_LIST)));
60     }
61 
extractPersonKeyOnly(@ullable ArrayList<Person> people)62     private static String[] extractPersonKeyOnly(@Nullable ArrayList<Person> people) {
63         if (people == null || people.isEmpty()) {
64             return Utilities.EMPTY_STRING_ARRAY;
65         }
66         return people.stream().filter(person -> person.getKey() != null)
67                 .map(Person::getKey).sorted().toArray(String[]::new);
68     }
69 
70     @Override
equals(Object obj)71     public boolean equals(Object obj) {
72         if (!(obj instanceof NotificationKeyData)) {
73             return false;
74         }
75         // Only compare the keys.
76         return ((NotificationKeyData) obj).notificationKey.equals(notificationKey);
77     }
78 }
79