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