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