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
17package com.android.messaging.ui.conversationlist;
18
19import android.content.Context;
20import android.database.Cursor;
21import android.support.v7.widget.RecyclerView;
22import android.view.LayoutInflater;
23import android.view.ViewGroup;
24
25import com.android.messaging.R;
26import com.android.messaging.ui.CursorRecyclerAdapter;
27import com.android.messaging.ui.conversationlist.ConversationListItemView.HostInterface;
28
29/**
30 * Provides an interface to expose Conversation List Cursor data to a UI widget like a ListView.
31 */
32public class ConversationListAdapter
33        extends CursorRecyclerAdapter<ConversationListAdapter.ConversationListViewHolder> {
34
35    private final ConversationListItemView.HostInterface mClivHostInterface;
36
37    public ConversationListAdapter(final Context context, final Cursor cursor,
38            final ConversationListItemView.HostInterface clivHostInterface) {
39        super(context, cursor, 0);
40        mClivHostInterface = clivHostInterface;
41        setHasStableIds(true);
42    }
43
44    /**
45     * @see com.android.messaging.ui.CursorRecyclerAdapter#bindViewHolder(
46     * android.support.v7.widget.RecyclerView.ViewHolder, android.content.Context,
47     * android.database.Cursor)
48     */
49    @Override
50    public void bindViewHolder(final ConversationListViewHolder holder, final Context context,
51            final Cursor cursor) {
52        final ConversationListItemView conversationListItemView = holder.mView;
53        conversationListItemView.bind(cursor, mClivHostInterface);
54    }
55
56    @Override
57    public ConversationListViewHolder createViewHolder(final Context context,
58            final ViewGroup parent, final int viewType) {
59        final LayoutInflater layoutInflater = LayoutInflater.from(context);
60        final ConversationListItemView itemView =
61                (ConversationListItemView) layoutInflater.inflate(
62                        R.layout.conversation_list_item_view, null);
63        return new ConversationListViewHolder(itemView);
64    }
65
66    /**
67     * ViewHolder that holds a ConversationListItemView.
68     */
69    public static class ConversationListViewHolder extends RecyclerView.ViewHolder {
70        final ConversationListItemView mView;
71
72        public ConversationListViewHolder(final ConversationListItemView itemView) {
73            super(itemView);
74            mView = itemView;
75        }
76    }
77}
78