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 */
16package com.android.messaging.ui.conversation;
17
18import android.content.Context;
19import android.database.Cursor;
20import android.support.v7.widget.RecyclerView;
21import android.view.LayoutInflater;
22import android.view.View;
23import android.view.ViewGroup;
24
25import com.android.messaging.R;
26import com.android.messaging.ui.AsyncImageView;
27import com.android.messaging.ui.CursorRecyclerAdapter;
28import com.android.messaging.ui.AsyncImageView.AsyncImageViewDelayLoader;
29import com.android.messaging.ui.conversation.ConversationMessageView.ConversationMessageViewHost;
30import com.android.messaging.util.Assert;
31
32import java.util.HashSet;
33import java.util.List;
34
35/**
36 * Provides an interface to expose Conversation Message Cursor data to a UI widget like a
37 * RecyclerView.
38 */
39public 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
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
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
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
83    public void setSelectedMessage(final String messageId) {
84        mSelectedMessageId = messageId;
85        notifyDataSetChanged();
86    }
87
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         */
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