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