• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 UpdateDestinationBlockedAction extends Action {
29     public interface UpdateDestinationBlockedActionListener {
30         @Assert.RunsOnMainThread
onUpdateDestinationBlockedAction(final UpdateDestinationBlockedAction action, final boolean success, final boolean block, final String destination)31         abstract void onUpdateDestinationBlockedAction(final UpdateDestinationBlockedAction action,
32                                                        final boolean success,
33                                                        final boolean block,
34                                                        final String destination);
35     }
36 
37     public static class UpdateDestinationBlockedActionMonitor extends ActionMonitor
38             implements ActionMonitor.ActionCompletedListener {
39         private final UpdateDestinationBlockedActionListener mListener;
40 
UpdateDestinationBlockedActionMonitor( Object data, UpdateDestinationBlockedActionListener mListener)41         public UpdateDestinationBlockedActionMonitor(
42                 Object data, UpdateDestinationBlockedActionListener mListener) {
43             super(STATE_CREATED, generateUniqueActionKey("UpdateDestinationBlockedAction"), data);
44             setCompletedListener(this);
45             this.mListener = mListener;
46         }
47 
onActionDone(final boolean succeeded, final ActionMonitor monitor, final Action action, final Object data, final Object result)48         private void onActionDone(final boolean succeeded,
49                                   final ActionMonitor monitor,
50                                   final Action action,
51                                   final Object data,
52                                   final Object result) {
53             mListener.onUpdateDestinationBlockedAction(
54                     (UpdateDestinationBlockedAction) action,
55                     succeeded,
56                     action.actionParameters.getBoolean(KEY_BLOCKED),
57                     action.actionParameters.getString(KEY_DESTINATION));
58         }
59 
60         @Override
onActionSucceeded(final ActionMonitor monitor, final Action action, final Object data, final Object result)61         public void onActionSucceeded(final ActionMonitor monitor,
62                                       final Action action,
63                                       final Object data,
64                                       final Object result) {
65             onActionDone(true, monitor, action, data, result);
66         }
67 
68         @Override
onActionFailed(final ActionMonitor monitor, final Action action, final Object data, final Object result)69         public void onActionFailed(final ActionMonitor monitor,
70                                    final Action action,
71                                    final Object data,
72                                    final Object result) {
73             onActionDone(false, monitor, action, data, result);
74         }
75     }
76 
77 
updateDestinationBlocked( final String destination, final boolean blocked, final String conversationId, final UpdateDestinationBlockedActionListener listener)78     public static UpdateDestinationBlockedActionMonitor updateDestinationBlocked(
79             final String destination, final boolean blocked, final String conversationId,
80             final UpdateDestinationBlockedActionListener listener) {
81         Assert.notNull(listener);
82         final UpdateDestinationBlockedActionMonitor monitor =
83                 new UpdateDestinationBlockedActionMonitor(null, listener);
84         final UpdateDestinationBlockedAction action =
85                 new UpdateDestinationBlockedAction(destination, blocked, conversationId,
86                         monitor.getActionKey());
87         action.start(monitor);
88         return monitor;
89     }
90 
91     private static final String KEY_CONVERSATION_ID = "conversation_id";
92     private static final String KEY_DESTINATION = "destination";
93     private static final String KEY_BLOCKED = "blocked";
94 
UpdateDestinationBlockedAction( final String destination, final boolean blocked, final String conversationId, final String actionKey)95     protected UpdateDestinationBlockedAction(
96             final String destination, final boolean blocked, final String conversationId,
97             final String actionKey) {
98         super(actionKey);
99         Assert.isTrue(!TextUtils.isEmpty(destination));
100         actionParameters.putString(KEY_DESTINATION, destination);
101         actionParameters.putBoolean(KEY_BLOCKED, blocked);
102         actionParameters.putString(KEY_CONVERSATION_ID, conversationId);
103     }
104 
105     @Override
executeAction()106     protected Object executeAction() {
107         final String destination = actionParameters.getString(KEY_DESTINATION);
108         final boolean isBlocked = actionParameters.getBoolean(KEY_BLOCKED);
109         String conversationId = actionParameters.getString(KEY_CONVERSATION_ID);
110         final DatabaseWrapper db = DataModel.get().getDatabase();
111         BugleDatabaseOperations.updateDestination(db, destination, isBlocked);
112         if (conversationId == null) {
113             conversationId = BugleDatabaseOperations
114                     .getConversationFromOtherParticipantDestination(db, destination);
115         }
116         if (conversationId != null) {
117             if (isBlocked) {
118                 UpdateConversationArchiveStatusAction.archiveConversation(conversationId);
119             } else {
120                 UpdateConversationArchiveStatusAction.unarchiveConversation(conversationId);
121             }
122             MessagingContentProvider.notifyParticipantsChanged(conversationId);
123         }
124         return null;
125     }
126 
UpdateDestinationBlockedAction(final Parcel in)127     protected UpdateDestinationBlockedAction(final Parcel in) {
128         super(in);
129     }
130 
131     public static final Parcelable.Creator<UpdateDestinationBlockedAction> CREATOR
132             = new Parcelable.Creator<UpdateDestinationBlockedAction>() {
133         @Override
134         public UpdateDestinationBlockedAction createFromParcel(final Parcel in) {
135             return new UpdateDestinationBlockedAction(in);
136         }
137 
138         @Override
139         public UpdateDestinationBlockedAction[] newArray(final int size) {
140             return new UpdateDestinationBlockedAction[size];
141         }
142     };
143 
144     @Override
writeToParcel(final Parcel parcel, final int flags)145     public void writeToParcel(final Parcel parcel, final int flags) {
146         writeActionToParcel(parcel, flags);
147     }
148 }
149