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.ui.conversation;
17 
18 import android.content.Context;
19 import android.database.Cursor;
20 import androidx.recyclerview.widget.RecyclerView;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 
25 import com.android.messaging.R;
26 import com.android.messaging.ui.AsyncImageView;
27 import com.android.messaging.ui.CursorRecyclerAdapter;
28 import com.android.messaging.ui.AsyncImageView.AsyncImageViewDelayLoader;
29 import com.android.messaging.ui.conversation.ConversationMessageView.ConversationMessageViewHost;
30 import com.android.messaging.util.Assert;
31 
32 import java.util.HashSet;
33 import java.util.List;
34 
35 /**
36  * Provides an interface to expose Conversation Message Cursor data to a UI widget like a
37  * RecyclerView.
38  */
39 public class ConversationMessageAdapter extends
40     CursorRecyclerAdapter<ConversationMessageAdapter.ConversationMessageViewHolder> {
41 
42     private final ConversationMessageViewHost mHost;
43     private final AsyncImageViewDelayLoader mImageViewDelayLoader;
44     private final View.OnClickListener mViewClickListener;
45     private final View.OnLongClickListener mViewLongClickListener;
46     private boolean mOneOnOne;
47     private String mSelectedMessageId;
48 
ConversationMessageAdapter(final Context context, final Cursor cursor, final ConversationMessageViewHost host, final AsyncImageViewDelayLoader imageViewDelayLoader, final View.OnClickListener viewClickListener, final View.OnLongClickListener longClickListener)49     public ConversationMessageAdapter(final Context context, final Cursor cursor,
50         final ConversationMessageViewHost host,
51         final AsyncImageViewDelayLoader imageViewDelayLoader,
52         final View.OnClickListener viewClickListener,
53         final View.OnLongClickListener longClickListener) {
54         super(context, cursor, 0);
55         mHost = host;
56         mViewClickListener = viewClickListener;
57         mViewLongClickListener = longClickListener;
58         mImageViewDelayLoader = imageViewDelayLoader;
59         setHasStableIds(true);
60     }
61 
62     @Override
bindViewHolder(final ConversationMessageViewHolder holder, final Context context, final Cursor cursor)63     public void bindViewHolder(final ConversationMessageViewHolder holder,
64             final Context context, final Cursor cursor) {
65         Assert.isTrue(holder.mView instanceof ConversationMessageView);
66         final ConversationMessageView conversationMessageView =
67                 (ConversationMessageView) holder.mView;
68         conversationMessageView.bind(cursor, mOneOnOne, mSelectedMessageId);
69     }
70 
71     @Override
createViewHolder(final Context context, final ViewGroup parent, final int viewType)72     public ConversationMessageViewHolder createViewHolder(final Context context,
73             final ViewGroup parent, final int viewType) {
74         final LayoutInflater layoutInflater = LayoutInflater.from(context);
75         final ConversationMessageView conversationMessageView = (ConversationMessageView)
76                 layoutInflater.inflate(R.layout.conversation_message_view, null);
77         conversationMessageView.setHost(mHost);
78         conversationMessageView.setImageViewDelayLoader(mImageViewDelayLoader);
79         return new ConversationMessageViewHolder(conversationMessageView,
80                             mViewClickListener, mViewLongClickListener);
81     }
82 
setSelectedMessage(final String messageId)83     public void setSelectedMessage(final String messageId) {
84         mSelectedMessageId = messageId;
85         notifyDataSetChanged();
86     }
87 
setOneOnOne(final boolean oneOnOne, final boolean invalidate)88     public void setOneOnOne(final boolean oneOnOne, final boolean invalidate) {
89         if (mOneOnOne != oneOnOne) {
90             mOneOnOne = oneOnOne;
91             if (invalidate) {
92                 notifyDataSetChanged();
93             }
94         }
95     }
96 
97     /**
98     * ViewHolder that holds a ConversationMessageView.
99     */
100     public static class ConversationMessageViewHolder extends RecyclerView.ViewHolder {
101         final View mView;
102 
103         /**
104          * @param viewClickListener a View.OnClickListener that should define the interaction when
105          *        an item in the RecyclerView is clicked.
106          */
ConversationMessageViewHolder(final View itemView, final View.OnClickListener viewClickListener, final View.OnLongClickListener viewLongClickListener)107         public ConversationMessageViewHolder(final View itemView,
108                 final View.OnClickListener viewClickListener,
109                 final View.OnLongClickListener viewLongClickListener) {
110             super(itemView);
111             mView = itemView;
112 
113             mView.setOnClickListener(viewClickListener);
114             mView.setOnLongClickListener(viewLongClickListener);
115         }
116     }
117 }
118