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
17package com.android.messaging.datamodel.action;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import com.android.messaging.datamodel.BugleDatabaseOperations;
23import com.android.messaging.datamodel.DataModel;
24import com.android.messaging.datamodel.DatabaseWrapper;
25import com.android.messaging.datamodel.MessagingContentProvider;
26import com.android.messaging.datamodel.data.ConversationListItemData;
27import com.android.messaging.datamodel.data.MessageData;
28import com.android.messaging.util.LogUtil;
29
30public class WriteDraftMessageAction extends Action implements Parcelable {
31    private static final String TAG = LogUtil.BUGLE_DATAMODEL_TAG;
32
33    /**
34     * Set draft message (no listener)
35     */
36    public static void writeDraftMessage(final String conversationId, final MessageData message) {
37        final WriteDraftMessageAction action = new WriteDraftMessageAction(conversationId, message);
38        action.start();
39    }
40
41    private static final String KEY_CONVERSATION_ID = "conversationId";
42    private static final String KEY_MESSAGE = "message";
43
44    private WriteDraftMessageAction(final String conversationId, final MessageData message) {
45        actionParameters.putString(KEY_CONVERSATION_ID, conversationId);
46        actionParameters.putParcelable(KEY_MESSAGE, message);
47    }
48
49    @Override
50    protected Object executeAction() {
51        final DatabaseWrapper db = DataModel.get().getDatabase();
52        final String conversationId = actionParameters.getString(KEY_CONVERSATION_ID);
53        final MessageData message = actionParameters.getParcelable(KEY_MESSAGE);
54        if (message.getSelfId() == null || message.getParticipantId() == null) {
55            // This could happen when this occurs before the draft message is loaded
56            // In this case, we just use the conversation's current self id as draft's
57            // self id and/or participant id
58            final ConversationListItemData conversation =
59                    ConversationListItemData.getExistingConversation(db, conversationId);
60            if (conversation != null) {
61                final String senderAndSelf = conversation.getSelfId();
62                if (message.getSelfId() == null) {
63                    message.bindSelfId(senderAndSelf);
64                }
65                if (message.getParticipantId() == null) {
66                    message.bindParticipantId(senderAndSelf);
67                }
68            } else {
69                LogUtil.w(LogUtil.BUGLE_DATAMODEL_TAG, "Conversation " + conversationId +
70                        "already deleted before saving draft message " +
71                        message.getMessageId() + ". Aborting WriteDraftMessageAction.");
72                return null;
73            }
74        }
75        // Drafts are only kept in the local DB...
76        final String messageId = BugleDatabaseOperations.updateDraftMessageData(
77                db, conversationId, message, BugleDatabaseOperations.UPDATE_MODE_ADD_DRAFT);
78        MessagingContentProvider.notifyConversationListChanged();
79        MessagingContentProvider.notifyConversationMetadataChanged(conversationId);
80        return messageId;
81    }
82
83    private WriteDraftMessageAction(final Parcel in) {
84        super(in);
85    }
86
87    public static final Parcelable.Creator<WriteDraftMessageAction> CREATOR
88            = new Parcelable.Creator<WriteDraftMessageAction>() {
89        @Override
90        public WriteDraftMessageAction createFromParcel(final Parcel in) {
91            return new WriteDraftMessageAction(in);
92        }
93
94        @Override
95        public WriteDraftMessageAction[] newArray(final int size) {
96            return new WriteDraftMessageAction[size];
97        }
98    };
99
100    @Override
101    public void writeToParcel(final Parcel parcel, final int flags) {
102        writeActionToParcel(parcel, flags);
103    }
104}
105