1 /*
2  * Copyright (C) 2015 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 package com.android.messaging.datamodel;
17 
18 import android.app.PendingIntent;
19 import android.net.Uri;
20 import androidx.core.app.NotificationCompat;
21 
22 import com.android.messaging.datamodel.DatabaseHelper.MessageColumns;
23 import com.android.messaging.datamodel.data.MessageData;
24 import com.android.messaging.util.ConversationIdSet;
25 
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 
29 /**
30  * Base class for representing notifications. The main reason for this class is that in order to
31  * show pictures or avatars they might need to be loaded in the background. This class and
32  * subclasses can do the main work to get the notification ready and then wait until any images
33  * that are needed are ready before posting.
34  *
35  * The creation of a notification is split into two parts. The NotificationState ctor should
36  * setup the basic information including the mContentIntent. A Notification Builder is created in
37  * RealTimeChatNotifications and passed to the build() method of each notification where the
38  * Notification is fully specified.
39  *
40  * TODO: There is still some duplication and inconsistency in the utility functions and
41  * placement of different building blocks across notification types (e.g. summary text for accounts)
42  */
43 public abstract class NotificationState {
44     private static final int CONTENT_INTENT_REQUEST_CODE_OFFSET = 0;
45     private static final int CLEAR_INTENT_REQUEST_CODE_OFFSET = 1;
46     private static final int NUM_REQUEST_CODES_NEEDED = 2;
47 
48     public interface FailedMessageQuery {
49         static final String FAILED_MESSAGES_WHERE_CLAUSE =
50                 "((" + MessageColumns.STATUS + " = " +
51                 MessageData.BUGLE_STATUS_OUTGOING_FAILED + " OR " +
52                 MessageColumns.STATUS + " = " +
53                 MessageData.BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED + ") AND " +
54                 DatabaseHelper.MessageColumns.SEEN + " = 0)";
55 
56         static final String FAILED_ORDER_BY = DatabaseHelper.MessageColumns.CONVERSATION_ID + ", " +
57                 DatabaseHelper.MessageColumns.SENT_TIMESTAMP + " asc";
58     }
59 
60     public final ConversationIdSet mConversationIds;
61     public final HashSet<String> mPeople;
62 
63     public NotificationCompat.Style mNotificationStyle;
64     public NotificationCompat.Builder mNotificationBuilder;
65     public boolean mCanceled;
66     public int mType;
67     public int mBaseRequestCode;
68     public ArrayList<Uri> mParticipantAvatarsUris = null;
69     public ArrayList<Uri> mParticipantContactUris = null;
70 
NotificationState(final ConversationIdSet conversationIds)71     NotificationState(final ConversationIdSet conversationIds) {
72         mConversationIds = conversationIds;
73         mPeople = new HashSet<String>();
74     }
75 
76     /**
77      * The intent to be triggered when the notification is dismissed.
78      */
getClearIntent()79     public abstract PendingIntent getClearIntent();
80 
getAttachmentUri()81     protected Uri getAttachmentUri() {
82         return null;
83     }
84 
85     // Returns the mime type of the attachment (See ContentType class for definitions)
getAttachmentType()86     protected String getAttachmentType() {
87         return null;
88     }
89 
90     /**
91      * Build the notification using the given builder.
92      * @param builder
93      * @return The style of the notification.
94      */
build(NotificationCompat.Builder builder)95     protected abstract NotificationCompat.Style build(NotificationCompat.Builder builder);
96 
setAvatarUrlsForConversation(final String conversationId)97     protected void setAvatarUrlsForConversation(final String conversationId) {
98     }
99 
setPeopleForConversation(final String conversationId)100     protected void setPeopleForConversation(final String conversationId) {
101     }
102 
103     /**
104      * Reserves request codes for this notification type. By default 2 codes are reserved, one for
105      * the main intent and another for the cancel intent. Override this function to reserve more.
106      */
getNumRequestCodesNeeded()107     public int getNumRequestCodesNeeded() {
108         return NUM_REQUEST_CODES_NEEDED;
109     }
110 
getContentIntentRequestCode()111     public int getContentIntentRequestCode() {
112         return mBaseRequestCode + CONTENT_INTENT_REQUEST_CODE_OFFSET;
113     }
114 
getClearIntentRequestCode()115     public int getClearIntentRequestCode() {
116         return mBaseRequestCode + CLEAR_INTENT_REQUEST_CODE_OFFSET;
117     }
118 
119     /**
120      * Gets the appropriate icon needed for notifications.
121      */
getIcon()122     public abstract int getIcon();
123 
124     /**
125      * @return the type of notification that should be used from {@link RealTimeChatNotifications}
126      * so that the proper ringtone and vibrate settings can be used.
127      */
getLatestMessageNotificationType()128     public int getLatestMessageNotificationType() {
129         return BugleNotifications.LOCAL_SMS_NOTIFICATION;
130     }
131 
132     /**
133      * @return the notification priority level for this notification.
134      */
getPriority()135     public abstract int getPriority();
136 
137     /** @return custom ringtone URI or null if not set */
getRingtoneUri()138     public String getRingtoneUri() {
139         return null;
140     }
141 
getNotificationVibrate()142     public boolean getNotificationVibrate() {
143         return false;
144     }
145 
getLatestReceivedTimestamp()146     public long getLatestReceivedTimestamp() {
147         return Long.MIN_VALUE;
148     }
149 }
150