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.content.ContentValues; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 23 import com.android.messaging.datamodel.BugleDatabaseOperations; 24 import com.android.messaging.datamodel.DataModel; 25 import com.android.messaging.datamodel.DatabaseHelper.MessageColumns; 26 import com.android.messaging.datamodel.DatabaseWrapper; 27 import com.android.messaging.datamodel.MessagingContentProvider; 28 import com.android.messaging.datamodel.data.MessageData; 29 import com.android.messaging.util.LogUtil; 30 31 /** 32 * Action used to manually resend an outgoing message 33 */ 34 public class ResendMessageAction extends Action implements Parcelable { 35 private static final String TAG = LogUtil.BUGLE_DATAMODEL_TAG; 36 37 private static final String KEY_SUB_ID = "sub_id"; 38 39 /** 40 * Manual send of existing message (no listener) 41 */ resendMessage(final String messageId)42 public static void resendMessage(final String messageId) { 43 final ResendMessageAction action = new ResendMessageAction(messageId); 44 action.start(); 45 } 46 47 // Core parameters needed for all types of message 48 private static final String KEY_MESSAGE_ID = "message_id"; 49 50 /** 51 * Constructor used for retrying sending in the background (only message id available) 52 */ ResendMessageAction(final String messageId)53 ResendMessageAction(final String messageId) { 54 super(); 55 actionParameters.putString(KEY_MESSAGE_ID, messageId); 56 } 57 58 /** 59 * Read message from database and change status to allow sending 60 */ 61 @Override executeAction()62 protected Object executeAction() { 63 final String messageId = actionParameters.getString(KEY_MESSAGE_ID); 64 65 final DatabaseWrapper db = DataModel.get().getDatabase(); 66 67 final MessageData message = BugleDatabaseOperations.readMessage(db, messageId); 68 // Check message can be resent 69 if (message != null && message.canResendMessage()) { 70 final boolean isMms = message.getIsMms(); 71 long timestamp = System.currentTimeMillis(); 72 if (isMms) { 73 // MMS expects timestamp rounded to nearest second 74 timestamp = 1000 * ((timestamp + 500) / 1000); 75 } 76 77 LogUtil.i(TAG, "ResendMessageAction: Resending message " + messageId 78 + "; changed timestamp from " + message.getReceivedTimeStamp() + " to " 79 + timestamp); 80 81 final ContentValues values = new ContentValues(); 82 values.put(MessageColumns.STATUS, MessageData.BUGLE_STATUS_OUTGOING_YET_TO_SEND); 83 values.put(MessageColumns.RETRY_START_TIMESTAMP, timestamp); 84 85 // Row must exist as was just loaded above (on ActionService thread) 86 BugleDatabaseOperations.updateMessageRow(db, message.getMessageId(), values); 87 88 MessagingContentProvider.notifyMessagesChanged(message.getConversationId()); 89 90 actionParameters.putInt(KEY_SUB_ID, 91 BugleDatabaseOperations.getSelfSubscriptionId(db, message.getSelfId())); 92 93 // Whether we succeeded or failed we will check and maybe schedule some more work 94 ProcessPendingMessagesAction.scheduleProcessPendingMessagesAction(false, this); 95 96 return message; 97 } else { 98 String error = "ResendMessageAction: Cannot resend message " + messageId + "; "; 99 if (message != null) { 100 error += ("status = " + MessageData.getStatusDescription(message.getStatus())); 101 } else { 102 error += "not found in database"; 103 } 104 LogUtil.e(TAG, error); 105 } 106 107 return null; 108 } 109 ResendMessageAction(final Parcel in)110 private ResendMessageAction(final Parcel in) { 111 super(in); 112 } 113 114 public static final Parcelable.Creator<ResendMessageAction> CREATOR 115 = new Parcelable.Creator<ResendMessageAction>() { 116 @Override 117 public ResendMessageAction createFromParcel(final Parcel in) { 118 return new ResendMessageAction(in); 119 } 120 121 @Override 122 public ResendMessageAction[] newArray(final int size) { 123 return new ResendMessageAction[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