1 /*
2  * Copyright (C) 2019 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.car.notification;
18 
19 import android.app.Notification;
20 import android.service.notification.StatusBarNotification;
21 
22 import androidx.annotation.VisibleForTesting;
23 
24 /**
25  * Wrapper class to store the state of a {@link StatusBarNotification}.
26  */
27 public class AlertEntry {
28 
29     private String mKey;
30     private long mPostTime;
31     private StatusBarNotification mStatusBarNotification;
32     private NotificationClickHandlerFactory mClickHandlerFactory;
33 
AlertEntry(StatusBarNotification statusBarNotification)34     public AlertEntry(StatusBarNotification statusBarNotification) {
35         mStatusBarNotification = statusBarNotification;
36         mKey = statusBarNotification.getKey();
37         mPostTime = calculatePostTime();
38     }
39 
40     // Empty constructor for Spy compatibility.
41     @VisibleForTesting
AlertEntry()42     protected AlertEntry() {}
43 
44     @VisibleForTesting
AlertEntry(StatusBarNotification statusBarNotification, long postTime)45     public AlertEntry(StatusBarNotification statusBarNotification, long postTime) {
46         this(statusBarNotification);
47         mPostTime = postTime;
48     }
49 
50     /**
51      * Updates the current post time for the Heads up notification.
52      */
updatePostTime()53     void updatePostTime() {
54         mPostTime = calculatePostTime();
55     }
56 
getPostTime()57     long getPostTime() {
58         return mPostTime;
59     }
60 
61     /**
62      * Calculate what the post time of a notification is at some current time.
63      *
64      * @return the post time
65      */
calculatePostTime()66     private long calculatePostTime() {
67         return System.currentTimeMillis();
68     }
69 
70     /**
71      * Returns the {@link StatusBarNotification} that this instance of AlertEntry is wrapping.
72      */
getStatusBarNotification()73     public StatusBarNotification getStatusBarNotification() {
74         return mStatusBarNotification;
75     }
76 
getClickHandlerFactory()77     NotificationClickHandlerFactory getClickHandlerFactory() {
78         return mClickHandlerFactory;
79     }
80 
setClickHandlerFactory(NotificationClickHandlerFactory clickHandlerFactory)81     void setClickHandlerFactory(NotificationClickHandlerFactory clickHandlerFactory) {
82         mClickHandlerFactory = clickHandlerFactory;
83     }
84 
85     /**
86      * Returns the key associated with the {@link StatusBarNotification} stored in this AlertEntry
87      * instance. This ensures that the same unique key is associated with a StatusBarNotification
88      * and the {@link AlertEntry} that wraps it.
89      */
getKey()90     public String getKey() {
91         return mKey;
92     }
93 
94     /**
95      * Returns the {@link Notification} that is associated with the {@link StatusBarNotification}
96      * that this AlertEntry instance wraps.
97      */
getNotification()98     public Notification getNotification() {
99         return mStatusBarNotification.getNotification();
100     }
101 
102     @Override
toString()103     public String toString() {
104         return mKey;
105     }
106 }
107