summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/datamodel/NotificationState.java
blob: 576a692e880bb4d858904dc2afaafd7dc59934ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.messaging.datamodel;

import android.app.PendingIntent;
import android.net.Uri;
import androidx.core.app.NotificationCompat;

import com.android.messaging.datamodel.DatabaseHelper.MessageColumns;
import com.android.messaging.datamodel.data.MessageData;
import com.android.messaging.util.ConversationIdSet;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * Base class for representing notifications. The main reason for this class is that in order to
 * show pictures or avatars they might need to be loaded in the background. This class and
 * subclasses can do the main work to get the notification ready and then wait until any images
 * that are needed are ready before posting.
 *
 * The creation of a notification is split into two parts. The NotificationState ctor should
 * setup the basic information including the mContentIntent. A Notification Builder is created in
 * RealTimeChatNotifications and passed to the build() method of each notification where the
 * Notification is fully specified.
 *
 * TODO: There is still some duplication and inconsistency in the utility functions and
 * placement of different building blocks across notification types (e.g. summary text for accounts)
 */
public abstract class NotificationState {
    private static final int CONTENT_INTENT_REQUEST_CODE_OFFSET = 0;
    private static final int CLEAR_INTENT_REQUEST_CODE_OFFSET = 1;
    private static final int READ_INTENT_REQUEST_CODE_OFFSET = 2;
    private static final int NUM_REQUEST_CODES_NEEDED = 3;

    public interface FailedMessageQuery {
        static final String FAILED_MESSAGES_WHERE_CLAUSE =
                "((" + MessageColumns.STATUS + " = " +
                MessageData.BUGLE_STATUS_OUTGOING_FAILED + " OR " +
                MessageColumns.STATUS + " = " +
                MessageData.BUGLE_STATUS_INCOMING_DOWNLOAD_FAILED + ") AND " +
                DatabaseHelper.MessageColumns.SEEN + " = 0)";

        static final String FAILED_ORDER_BY = DatabaseHelper.MessageColumns.CONVERSATION_ID + ", " +
                DatabaseHelper.MessageColumns.SENT_TIMESTAMP + " asc";
    }

    public final ConversationIdSet mConversationIds;
    public final HashSet<String> mPeople;

    public NotificationCompat.Style mNotificationStyle;
    public NotificationCompat.Builder mNotificationBuilder;
    public boolean mCanceled;
    public int mType;
    public int mBaseRequestCode;
    public ArrayList<Uri> mParticipantAvatarsUris = null;
    public ArrayList<Uri> mParticipantContactUris = null;

    NotificationState(final ConversationIdSet conversationIds) {
        mConversationIds = conversationIds;
        mPeople = new HashSet<String>();
    }

    /**
     * The intent to be triggered when the notification is dismissed.
     */
    public abstract PendingIntent getClearIntent();

    /**
     * The intent to be triggered when mark as read is pressed.
     */
    public abstract PendingIntent getReadIntent();

    protected Uri getAttachmentUri() {
        return null;
    }

    // Returns the mime type of the attachment (See ContentType class for definitions)
    protected String getAttachmentType() {
        return null;
    }

    /**
     * Build the notification using the given builder.
     * @param builder
     * @return The style of the notification.
     */
    protected abstract NotificationCompat.Style build(NotificationCompat.Builder builder);

    protected void setAvatarUrlsForConversation(final String conversationId) {
    }

    protected void setPeopleForConversation(final String conversationId) {
    }

    /**
     * Reserves request codes for this notification type. By default 2 codes are reserved, one for
     * the main intent and another for the cancel intent. Override this function to reserve more.
     */
    public int getNumRequestCodesNeeded() {
        return NUM_REQUEST_CODES_NEEDED;
    }

    public int getContentIntentRequestCode() {
        return mBaseRequestCode + CONTENT_INTENT_REQUEST_CODE_OFFSET;
    }

    public int getClearIntentRequestCode() {
        return mBaseRequestCode + CLEAR_INTENT_REQUEST_CODE_OFFSET;
    }

    public int getReadIntentRequestCode() {
        return mBaseRequestCode + READ_INTENT_REQUEST_CODE_OFFSET;
    }

    /**
     * Gets the appropriate icon needed for notifications.
     */
    public abstract int getIcon();

    /**
     * @return the type of notification that should be used from {@link RealTimeChatNotifications}
     * so that the proper ringtone and vibrate settings can be used.
     */
    public int getLatestMessageNotificationType() {
        return BugleNotifications.LOCAL_SMS_NOTIFICATION;
    }

    /**
     * @return the notification priority level for this notification.
     */
    public abstract int getPriority();

    /** @return custom ringtone URI or null if not set */
    public String getRingtoneUri() {
        return null;
    }

    public boolean getNotificationVibrate() {
        return false;
    }

    public long getLatestReceivedTimestamp() {
        return Long.MIN_VALUE;
    }
}