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.data;
18 
19 import com.android.messaging.datamodel.action.ActionMonitor;
20 import com.android.messaging.datamodel.action.GetOrCreateConversationAction;
21 import com.android.messaging.datamodel.action.GetOrCreateConversationAction.GetOrCreateConversationActionListener;
22 import com.android.messaging.datamodel.action.GetOrCreateConversationAction.GetOrCreateConversationActionMonitor;
23 import com.android.messaging.datamodel.binding.BindableData;
24 import com.android.messaging.datamodel.binding.BindingBase;
25 import com.android.messaging.util.Assert;
26 import com.android.messaging.util.Assert.RunsOnMainThread;
27 import com.android.messaging.util.LogUtil;
28 
29 public class LaunchConversationData extends BindableData implements
30         GetOrCreateConversationActionListener {
31     public interface LaunchConversationDataListener {
onGetOrCreateNewConversation(String conversationId)32         void onGetOrCreateNewConversation(String conversationId);
onGetOrCreateNewConversationFailed()33         void onGetOrCreateNewConversationFailed();
34     }
35 
36     private LaunchConversationDataListener mListener;
37     private GetOrCreateConversationActionMonitor mMonitor;
38 
LaunchConversationData(final LaunchConversationDataListener listener)39     public LaunchConversationData(final LaunchConversationDataListener listener) {
40         mListener = listener;
41     }
42 
43     @Override
unregisterListeners()44     protected void unregisterListeners() {
45         mListener = null;
46         if (mMonitor != null) {
47             mMonitor.unregister();
48         }
49         mMonitor = null;
50     }
51 
getOrCreateConversation(final BindingBase<LaunchConversationData> binding, final String[] recipients)52     public void getOrCreateConversation(final BindingBase<LaunchConversationData> binding,
53             final String[] recipients) {
54         final String bindingId = binding.getBindingId();
55 
56         // Start a new conversation from the list of contacts.
57         if (isBound(bindingId) && mMonitor == null) {
58             mMonitor = GetOrCreateConversationAction.getOrCreateConversation(recipients,
59                     bindingId, this);
60         }
61     }
62 
63     @Override
64     @RunsOnMainThread
onGetOrCreateConversationSucceeded(final ActionMonitor monitor, final Object data, final String conversationId)65     public void onGetOrCreateConversationSucceeded(final ActionMonitor monitor,
66             final Object data, final String conversationId) {
67         Assert.isTrue(monitor == mMonitor);
68         Assert.isTrue(conversationId != null);
69 
70         final String bindingId = (String) data;
71         if (isBound(bindingId) && mListener != null) {
72             mListener.onGetOrCreateNewConversation(conversationId);
73         }
74 
75         mMonitor = null;
76     }
77 
78     @Override
79     @RunsOnMainThread
onGetOrCreateConversationFailed(final ActionMonitor monitor, final Object data)80     public void onGetOrCreateConversationFailed(final ActionMonitor monitor,
81             final Object data) {
82         Assert.isTrue(monitor == mMonitor);
83         final String bindingId = (String) data;
84         if (isBound(bindingId) && mListener != null) {
85             mListener.onGetOrCreateNewConversationFailed();
86         }
87         LogUtil.e(LogUtil.BUGLE_TAG, "onGetOrCreateConversationFailed");
88         mMonitor = null;
89     }
90 }
91