Home | History | Annotate | Download | only in ui
      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 
     18 package com.android.mms.ui;
     19 
     20 import android.content.Context;
     21 import android.database.Cursor;
     22 import android.util.Log;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.AbsListView;
     27 import android.widget.CursorAdapter;
     28 
     29 import com.android.mms.R;
     30 import com.android.mms.data.Conversation;
     31 
     32 /**
     33  * The back-end data adapter for ConversationList.
     34  */
     35 //TODO: This should be public class ConversationListAdapter extends ArrayAdapter<Conversation>
     36 public class ConversationListAdapter extends CursorAdapter implements AbsListView.RecyclerListener {
     37     private static final String TAG = "ConversationListAdapter";
     38     private static final boolean LOCAL_LOGV = false;
     39 
     40     private final LayoutInflater mFactory;
     41     private OnContentChangedListener mOnContentChangedListener;
     42 
     43     public ConversationListAdapter(Context context, Cursor cursor) {
     44         super(context, cursor, false /* auto-requery */);
     45         mFactory = LayoutInflater.from(context);
     46     }
     47 
     48     @Override
     49     public void bindView(View view, Context context, Cursor cursor) {
     50         if (!(view instanceof ConversationListItem)) {
     51             Log.e(TAG, "Unexpected bound view: " + view);
     52             return;
     53         }
     54 
     55         ConversationListItem headerView = (ConversationListItem) view;
     56         Conversation conv = Conversation.from(context, cursor);
     57         headerView.bind(context, conv);
     58     }
     59 
     60     public void onMovedToScrapHeap(View view) {
     61         ConversationListItem headerView = (ConversationListItem)view;
     62         headerView.unbind();
     63     }
     64 
     65     @Override
     66     public View newView(Context context, Cursor cursor, ViewGroup parent) {
     67         if (LOCAL_LOGV) Log.v(TAG, "inflating new view");
     68         return mFactory.inflate(R.layout.conversation_list_item, parent, false);
     69     }
     70 
     71     public interface OnContentChangedListener {
     72         void onContentChanged(ConversationListAdapter adapter);
     73     }
     74 
     75     public void setOnContentChangedListener(OnContentChangedListener l) {
     76         mOnContentChangedListener = l;
     77     }
     78 
     79     @Override
     80     protected void onContentChanged() {
     81         if (mCursor != null && !mCursor.isClosed()) {
     82             if (mOnContentChangedListener != null) {
     83                 mOnContentChangedListener.onContentChanged(this);
     84             }
     85         }
     86     }
     87 
     88     public void uncheckAll() {
     89         int count = getCount();
     90         for (int i = 0; i < count; i++) {
     91             Cursor cursor = (Cursor)getItem(i);
     92             Conversation conv = Conversation.from(mContext, cursor);
     93             conv.setIsChecked(false);
     94         }
     95     }
     96 }
     97