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 import android.text.TextUtils; 23 24 import com.android.messaging.datamodel.BugleNotifications; 25 import com.android.messaging.datamodel.DataModel; 26 import com.android.messaging.datamodel.DatabaseHelper; 27 import com.android.messaging.datamodel.DatabaseHelper.MessageColumns; 28 import com.android.messaging.datamodel.DatabaseWrapper; 29 import com.android.messaging.datamodel.MessagingContentProvider; 30 import com.android.messaging.util.LogUtil; 31 32 /** 33 * Action used to mark all messages as seen 34 */ 35 public class MarkAsSeenAction extends Action implements Parcelable { 36 private static final String TAG = LogUtil.BUGLE_DATAMODEL_TAG; 37 private static final String KEY_CONVERSATION_ID = "conversation_id"; 38 39 /** 40 * Mark all messages as seen. 41 */ markAllAsSeen()42 public static void markAllAsSeen() { 43 final MarkAsSeenAction action = new MarkAsSeenAction((String) null/*conversationId*/); 44 action.start(); 45 } 46 47 /** 48 * Mark all messages of a given conversation as seen. 49 */ markAsSeen(final String conversationId)50 public static void markAsSeen(final String conversationId) { 51 final MarkAsSeenAction action = new MarkAsSeenAction(conversationId); 52 action.start(); 53 } 54 55 /** 56 * ctor for MarkAsSeenAction. 57 * @param conversationId the conversation id for which to mark as seen, or null to mark all 58 * messages as seen 59 */ MarkAsSeenAction(final String conversationId)60 public MarkAsSeenAction(final String conversationId) { 61 actionParameters.putString(KEY_CONVERSATION_ID, conversationId); 62 } 63 64 @Override executeAction()65 protected Object executeAction() { 66 final String conversationId = 67 actionParameters.getString(KEY_CONVERSATION_ID); 68 final boolean hasSpecificConversation = !TextUtils.isEmpty(conversationId); 69 70 // Everything in telephony should already have the seen bit set. 71 // Possible exception are messages which did not have seen set and 72 // were sync'ed into bugle. 73 74 // Now mark the messages as seen in the bugle db 75 final DatabaseWrapper db = DataModel.get().getDatabase(); 76 db.beginTransaction(); 77 78 try { 79 final ContentValues values = new ContentValues(); 80 values.put(MessageColumns.SEEN, 1); 81 82 if (hasSpecificConversation) { 83 final int count = db.update(DatabaseHelper.MESSAGES_TABLE, values, 84 MessageColumns.SEEN + " != 1 AND " + 85 MessageColumns.CONVERSATION_ID + "=?", 86 new String[] { conversationId }); 87 if (count > 0) { 88 MessagingContentProvider.notifyMessagesChanged(conversationId); 89 } 90 } else { 91 db.update(DatabaseHelper.MESSAGES_TABLE, values, 92 MessageColumns.SEEN + " != 1", null/*selectionArgs*/); 93 } 94 95 db.setTransactionSuccessful(); 96 } finally { 97 db.endTransaction(); 98 } 99 // After marking messages as seen, update the notifications. This will 100 // clear the now stale notifications. 101 BugleNotifications.update(false/*silent*/, BugleNotifications.UPDATE_ALL); 102 return null; 103 } 104 MarkAsSeenAction(final Parcel in)105 private MarkAsSeenAction(final Parcel in) { 106 super(in); 107 } 108 109 public static final Parcelable.Creator<MarkAsSeenAction> CREATOR 110 = new Parcelable.Creator<MarkAsSeenAction>() { 111 @Override 112 public MarkAsSeenAction createFromParcel(final Parcel in) { 113 return new MarkAsSeenAction(in); 114 } 115 116 @Override 117 public MarkAsSeenAction[] newArray(final int size) { 118 return new MarkAsSeenAction[size]; 119 } 120 }; 121 122 @Override writeToParcel(final Parcel parcel, final int flags)123 public void writeToParcel(final Parcel parcel, final int flags) { 124 writeActionToParcel(parcel, flags); 125 } 126 } 127