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 package com.android.messaging.datamodel.action; 17 18 import android.os.Parcel; 19 import android.os.Parcelable; 20 import android.text.TextUtils; 21 22 import com.android.messaging.datamodel.BugleDatabaseOperations; 23 import com.android.messaging.datamodel.DataModel; 24 import com.android.messaging.datamodel.DatabaseWrapper; 25 import com.android.messaging.datamodel.MessagingContentProvider; 26 import com.android.messaging.util.Assert; 27 28 public class UpdateConversationArchiveStatusAction extends Action { 29 archiveConversation(final String conversationId)30 public static void archiveConversation(final String conversationId) { 31 final UpdateConversationArchiveStatusAction action = 32 new UpdateConversationArchiveStatusAction(conversationId, true /* isArchive */); 33 action.start(); 34 } 35 unarchiveConversation(final String conversationId)36 public static void unarchiveConversation(final String conversationId) { 37 final UpdateConversationArchiveStatusAction action = 38 new UpdateConversationArchiveStatusAction(conversationId, false /* isArchive */); 39 action.start(); 40 } 41 42 private static final String KEY_CONVERSATION_ID = "conversation_id"; 43 private static final String KEY_IS_ARCHIVE = "is_archive"; 44 UpdateConversationArchiveStatusAction( final String conversationId, final boolean isArchive)45 protected UpdateConversationArchiveStatusAction( 46 final String conversationId, final boolean isArchive) { 47 Assert.isTrue(!TextUtils.isEmpty(conversationId)); 48 actionParameters.putString(KEY_CONVERSATION_ID, conversationId); 49 actionParameters.putBoolean(KEY_IS_ARCHIVE, isArchive); 50 } 51 52 @Override executeAction()53 protected Object executeAction() { 54 final String conversationId = actionParameters.getString(KEY_CONVERSATION_ID); 55 final boolean isArchived = actionParameters.getBoolean(KEY_IS_ARCHIVE); 56 57 final DatabaseWrapper db = DataModel.get().getDatabase(); 58 db.beginTransaction(); 59 try { 60 BugleDatabaseOperations.updateConversationArchiveStatusInTransaction( 61 db, conversationId, isArchived); 62 db.setTransactionSuccessful(); 63 } finally { 64 db.endTransaction(); 65 } 66 67 MessagingContentProvider.notifyConversationListChanged(); 68 MessagingContentProvider.notifyConversationMetadataChanged(conversationId); 69 return null; 70 } 71 UpdateConversationArchiveStatusAction(final Parcel in)72 protected UpdateConversationArchiveStatusAction(final Parcel in) { 73 super(in); 74 } 75 76 public static final Parcelable.Creator<UpdateConversationArchiveStatusAction> CREATOR 77 = new Parcelable.Creator<UpdateConversationArchiveStatusAction>() { 78 @Override 79 public UpdateConversationArchiveStatusAction createFromParcel(final Parcel in) { 80 return new UpdateConversationArchiveStatusAction(in); 81 } 82 83 @Override 84 public UpdateConversationArchiveStatusAction[] newArray(final int size) { 85 return new UpdateConversationArchiveStatusAction[size]; 86 } 87 }; 88 89 @Override writeToParcel(final Parcel parcel, final int flags)90 public void writeToParcel(final Parcel parcel, final int flags) { 91 writeActionToParcel(parcel, flags); 92 } 93 } 94