1/*
2 * Copyright (C) 2008 Esmertec AG.
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mms.ui;
19
20import com.android.mms.R;
21import com.android.mms.LogTag;
22import com.android.mms.data.Conversation;
23
24import android.content.Context;
25import android.database.Cursor;
26import android.util.Log;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.AbsListView;
31import android.widget.CursorAdapter;
32
33/**
34 * The back-end data adapter for ConversationList.
35 */
36//TODO: This should be public class ConversationListAdapter extends ArrayAdapter<Conversation>
37public class ConversationListAdapter extends CursorAdapter implements AbsListView.RecyclerListener {
38    private static final String TAG = "ConversationListAdapter";
39    private static final boolean LOCAL_LOGV = false;
40
41    private final LayoutInflater mFactory;
42    private OnContentChangedListener mOnContentChangedListener;
43
44    public ConversationListAdapter(Context context, Cursor cursor) {
45        super(context, cursor, false /* auto-requery */);
46        mFactory = LayoutInflater.from(context);
47    }
48
49    @Override
50    public void bindView(View view, Context context, Cursor cursor) {
51        if (!(view instanceof ConversationListItem)) {
52            Log.e(TAG, "Unexpected bound view: " + view);
53            return;
54        }
55
56        ConversationListItem headerView = (ConversationListItem) view;
57        Conversation conv = Conversation.from(context, cursor);
58        headerView.bind(context, conv);
59    }
60
61    public void onMovedToScrapHeap(View view) {
62        ConversationListItem headerView = (ConversationListItem)view;
63        headerView.unbind();
64    }
65
66    @Override
67    public View newView(Context context, Cursor cursor, ViewGroup parent) {
68        if (LOCAL_LOGV) Log.v(TAG, "inflating new view");
69        return mFactory.inflate(R.layout.conversation_list_item, parent, false);
70    }
71
72    public interface OnContentChangedListener {
73        void onContentChanged(ConversationListAdapter adapter);
74    }
75
76    public void setOnContentChangedListener(OnContentChangedListener l) {
77        mOnContentChangedListener = l;
78    }
79
80    @Override
81    protected void onContentChanged() {
82        if (mCursor != null && !mCursor.isClosed()) {
83            if (mOnContentChangedListener != null) {
84                mOnContentChangedListener.onContentChanged(this);
85            }
86        }
87    }
88
89    public void uncheckAll() {
90        int count = getCount();
91        for (int i = 0; i < count; i++) {
92            Cursor cursor = (Cursor)getItem(i);
93            Conversation conv = Conversation.from(mContext, cursor);
94            conv.setIsChecked(false);
95        }
96    }
97}
98