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 Dodd 17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel.action; 18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.app.PendingIntent; 20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.ContentValues; 21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context; 22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Parcel; 23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Parcelable; 24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.BugleDatabaseOperations; 26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.BugleNotifications; 27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DataModel; 28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DatabaseHelper; 29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DatabaseWrapper; 30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.MessagingContentProvider; 31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.MessageData; 32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil; 33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/** 35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Action to manually start an MMS download (after failed or manual mms download) 36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class RedownloadMmsAction extends Action implements Parcelable { 38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private static final String TAG = LogUtil.BUGLE_DATAMODEL_TAG; 39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private static final int REQUEST_CODE_PENDING_INTENT = 102; 40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** 42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Download an MMS message 43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static void redownloadMessage(final String messageId) { 45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final RedownloadMmsAction action = new RedownloadMmsAction(messageId); 46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd action.start(); 47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** 50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Get a pending intent of for downloading an MMS 51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static PendingIntent getPendingIntentForRedownloadMms( 53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Context context, final String messageId) { 54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Action action = new RedownloadMmsAction(messageId); 55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return ActionService.makeStartActionPendingIntent(context, 56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd action, REQUEST_CODE_PENDING_INTENT, false /*launchesAnActivity*/); 57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Core parameters needed for all types of message 60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private static final String KEY_MESSAGE_ID = "message_id"; 61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** 63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Constructor used for retrying sending in the background (only message id available) 64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd RedownloadMmsAction(final String messageId) { 66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super(); 67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd actionParameters.putString(KEY_MESSAGE_ID, messageId); 68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** 71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Read message from database and change status to allow downloading 72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected Object executeAction() { 75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String messageId = actionParameters.getString(KEY_MESSAGE_ID); 76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final DatabaseWrapper db = DataModel.get().getDatabase(); 78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd MessageData message = BugleDatabaseOperations.readMessage(db, messageId); 80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Check message can be redownloaded 81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (message != null && message.canRedownloadMessage()) { 82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final long timestamp = System.currentTimeMillis(); 83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final ContentValues values = new ContentValues(2); 85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd values.put(DatabaseHelper.MessageColumns.STATUS, 86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd MessageData.BUGLE_STATUS_INCOMING_RETRYING_MANUAL_DOWNLOAD); 87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd values.put(DatabaseHelper.MessageColumns.RETRY_START_TIMESTAMP, timestamp); 88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Row must exist as was just loaded above (on ActionService thread) 90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd BugleDatabaseOperations.updateMessageRow(db, message.getMessageId(), values); 91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd MessagingContentProvider.notifyMessagesChanged(message.getConversationId()); 93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Whether we succeeded or failed we will check and maybe schedule some more work 95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd ProcessPendingMessagesAction.scheduleProcessPendingMessagesAction(false, this); 96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd message = null; 98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.e(LogUtil.BUGLE_TAG, 99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd "Attempt to download a missing or un-redownloadable message"); 100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Immediately update the notifications in case we came from the download action from a 102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // heads-up notification. This will dismiss the heads-up notification. 103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd BugleNotifications.update(false/*silent*/, BugleNotifications.UPDATE_ALL); 104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return message; 105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private RedownloadMmsAction(final Parcel in) { 108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super(in); 109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static final Parcelable.Creator<RedownloadMmsAction> CREATOR 112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd = new Parcelable.Creator<RedownloadMmsAction>() { 113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public RedownloadMmsAction createFromParcel(final Parcel in) { 115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return new RedownloadMmsAction(in); 116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public RedownloadMmsAction[] newArray(final int size) { 120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return new RedownloadMmsAction[size]; 121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd }; 123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public void writeToParcel(final Parcel parcel, final int flags) { 126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd writeActionToParcel(parcel, flags); 127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd} 129