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