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.data; 17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri; 19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Parcel; 20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Parcelable; 21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.annotation.NonNull; 22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert; 24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.ContentType; 25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil; 26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.SafeAsyncTask; 27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.UriUtil; 28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/** 30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Represents a "pending" message part that acts as a placeholder for the actual attachment being 31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * loaded. It handles the task to load and persist the attachment from a Uri to local scratch 32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * folder. This item is not persisted to the database. 33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class PendingAttachmentData extends MessagePartData { 35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** The pending state. This is the initial state where we haven't started loading yet */ 36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static final int STATE_PENDING = 0; 37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** The state for when we are currently loading the attachment to the scratch space */ 39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static final int STATE_LOADING = 1; 40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** The attachment has been successfully loaded and no longer pending */ 42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static final int STATE_LOADED = 2; 43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** The attachment failed to load */ 45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static final int STATE_FAILED = 3; 46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private static final int LOAD_MEDIA_TIME_LIMIT_MILLIS = 60 * 1000; // 60s 48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** The current state of the pending attachment. Refer to the STATE_* states above */ 50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd private int mCurrentState; 51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** 53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Create a new instance of PendingAttachmentData with an output Uri. 54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * @param sourceUri the source Uri of the attachment. The Uri maybe temporary or remote, 55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * so we need to persist it to local storage. 56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected PendingAttachmentData(final String caption, final String contentType, 58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @NonNull final Uri sourceUri, final int width, final int height, 59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final boolean onlySingleAttachment) { 60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super(caption, contentType, sourceUri, width, height, onlySingleAttachment); 61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mCurrentState = STATE_PENDING; 62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd /** 65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Creates a pending attachment data that is able to load from the given source uri and 66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * persist the media resource locally in the scratch folder. 67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */ 68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static PendingAttachmentData createPendingAttachmentData(final String contentType, 69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Uri sourceUri) { 70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return createPendingAttachmentData(null, contentType, sourceUri, UNSPECIFIED_SIZE, 71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd UNSPECIFIED_SIZE); 72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static PendingAttachmentData createPendingAttachmentData(final String caption, 75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String contentType, final Uri sourceUri, final int width, final int height) { 76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd Assert.isTrue(ContentType.isMediaType(contentType)); 77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return new PendingAttachmentData(caption, contentType, sourceUri, width, height, 78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd false /*onlySingleAttachment*/); 79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static PendingAttachmentData createPendingAttachmentData(final String caption, 82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String contentType, final Uri sourceUri, final int width, final int height, 83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final boolean onlySingleAttachment) { 84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd Assert.isTrue(ContentType.isMediaType(contentType)); 85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return new PendingAttachmentData(caption, contentType, sourceUri, width, height, 86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd onlySingleAttachment); 87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public int getCurrentState() { 90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return mCurrentState; 91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public void loadAttachmentForDraft(final DraftMessageData draftMessageData, 94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final String bindingId) { 95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (mCurrentState != STATE_PENDING) { 96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return; 97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mCurrentState = STATE_LOADING; 99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Kick off a SafeAsyncTask to load the content of the media and persist it locally. 101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Note: we need to persist the media locally even if it's not remote, because we 102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // want to be able to resend the media in case the message failed to send. 103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd new SafeAsyncTask<Void, Void, MessagePartData>(LOAD_MEDIA_TIME_LIMIT_MILLIS, 104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd true /* cancelExecutionOnTimeout */) { 105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected MessagePartData doInBackgroundTimed(final Void... params) { 107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Uri contentUri = getContentUri(); 108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd final Uri persistedUri = UriUtil.persistContentToScratchSpace(contentUri); 109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (persistedUri != null) { 110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return MessagePartData.createMediaMessagePart( 111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd getText(), 112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd getContentType(), 113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd persistedUri, 114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd getWidth(), 115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd getHeight()); 116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return null; 118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected void onCancelled() { 122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd LogUtil.w(LogUtil.BUGLE_TAG, "Timeout while retrieving media"); 123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mCurrentState = STATE_FAILED; 124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (draftMessageData.isBound(bindingId)) { 125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd draftMessageData.removePendingAttachment(PendingAttachmentData.this); 126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected void onPostExecute(final MessagePartData attachment) { 131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (attachment != null) { 132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mCurrentState = STATE_LOADED; 133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (draftMessageData.isBound(bindingId)) { 134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd draftMessageData.updatePendingAttachment(attachment, 135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd PendingAttachmentData.this); 136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // The draft message data is no longer bound, drop the loaded attachment. 138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd attachment.destroyAsync(); 139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } else { 141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // Media load failed. We already logged in doInBackground() so don't need to 142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd // do that again. 143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mCurrentState = STATE_FAILED; 144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd if (draftMessageData.isBound(bindingId)) { 145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd draftMessageData.onPendingAttachmentLoadFailed(PendingAttachmentData.this); 146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd draftMessageData.removePendingAttachment(PendingAttachmentData.this); 147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd }.executeOnThreadPool(); 151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd protected PendingAttachmentData(final Parcel in) { 154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super(in); 155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd mCurrentState = in.readInt(); 156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public void writeToParcel(final Parcel out, final int flags) { 160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd super.writeToParcel(out, flags); 161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd out.writeInt(mCurrentState); 162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 164d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public static final Parcelable.Creator<PendingAttachmentData> CREATOR 165d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd = new Parcelable.Creator<PendingAttachmentData>() { 166d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 167d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public PendingAttachmentData createFromParcel(final Parcel in) { 168d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return new PendingAttachmentData(in); 169d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 170d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd 171d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd @Override 172d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd public PendingAttachmentData[] newArray(final int size) { 173d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd return new PendingAttachmentData[size]; 174d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd } 175d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd }; 176d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd} 177