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 Dodd
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.datamodel.action;
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.ContentValues;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Parcel;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.os.Parcelable;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.BugleDatabaseOperations;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.BugleNotifications;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DataModel;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DatabaseHelper;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DatabaseHelper.MessageColumns;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DatabaseWrapper;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.MessagingContentProvider;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.sms.MmsUtils;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.LogUtil;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Action used to mark all the messages in a conversation as read
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class MarkAsReadAction extends Action implements Parcelable {
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final String TAG = LogUtil.BUGLE_DATAMODEL_TAG;
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static final String KEY_CONVERSATION_ID = "conversation_id";
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /**
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     * Mark all the messages as read for a particular conversation.
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd     */
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static void markAsRead(final String conversationId) {
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MarkAsReadAction action = new MarkAsReadAction(conversationId);
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        action.start();
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private MarkAsReadAction(final String conversationId) {
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        actionParameters.putString(KEY_CONVERSATION_ID, conversationId);
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    protected Object executeAction() {
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String conversationId = actionParameters.getString(KEY_CONVERSATION_ID);
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // TODO: Consider doing this in background service to avoid delaying other actions
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final DatabaseWrapper db = DataModel.get().getDatabase();
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Mark all messages in thread as read in telephony
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final long threadId = BugleDatabaseOperations.getThreadId(db, conversationId);
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (threadId != -1) {
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            MmsUtils.updateSmsReadStatus(threadId, Long.MAX_VALUE);
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Update local db
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        db.beginTransaction();
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        try {
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final ContentValues values = new ContentValues();
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            values.put(MessageColumns.CONVERSATION_ID, conversationId);
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            values.put(MessageColumns.READ, 1);
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            values.put(MessageColumns.SEEN, 1);     // if they read it, they saw it
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int count = db.update(DatabaseHelper.MESSAGES_TABLE, values,
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    "(" + MessageColumns.READ + " !=1 OR " +
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            MessageColumns.SEEN + " !=1 ) AND " +
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                            MessageColumns.CONVERSATION_ID + "=?",
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    new String[] { conversationId });
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (count > 0) {
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                MessagingContentProvider.notifyMessagesChanged(conversationId);
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            db.setTransactionSuccessful();
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } finally {
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            db.endTransaction();
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // After marking messages as read, update the notifications. This will
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // clear the now stale notifications.
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        BugleNotifications.update(false/*silent*/, BugleNotifications.UPDATE_ALL);
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return null;
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private MarkAsReadAction(final Parcel in) {
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        super(in);
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final Parcelable.Creator<MarkAsReadAction> CREATOR
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            = new Parcelable.Creator<MarkAsReadAction>() {
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        @Override
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public MarkAsReadAction createFromParcel(final Parcel in) {
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return new MarkAsReadAction(in);
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        @Override
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        public MarkAsReadAction[] newArray(final int size) {
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return new MarkAsReadAction[size];
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    };
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    @Override
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public void writeToParcel(final Parcel parcel, final int flags) {
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        writeActionToParcel(parcel, flags);
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
114