Home | History | Annotate | Download | only in detail
      1 /*
      2  * Copyright (C) 2011 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 
     17 package com.android.contacts.detail;
     18 
     19 import android.content.Context;
     20 import android.view.LayoutInflater;
     21 import android.view.View;
     22 import android.view.ViewGroup;
     23 import android.widget.BaseAdapter;
     24 
     25 import com.android.contacts.R;
     26 import com.android.contacts.common.model.AccountTypeManager;
     27 import com.android.contacts.common.model.account.AccountType;
     28 import com.android.contacts.util.StreamItemEntry;
     29 
     30 import com.google.common.collect.Lists;
     31 
     32 import java.util.List;
     33 
     34 /**
     35  * List adapter for stream items of a given contact.
     36  */
     37 public class StreamItemAdapter extends BaseAdapter {
     38     /** The header view, hidden under the tab carousel, if present. */
     39     private static final int ITEM_VIEW_TYPE_HEADER = 0;
     40     /** The updates in the list. */
     41     private static final int ITEM_VIEW_TYPE_STREAM_ITEM = 1;
     42 
     43     private final Context mContext;
     44     private final View.OnClickListener mItemClickListener;
     45     private final View.OnClickListener mPhotoClickListener;
     46     private final LayoutInflater mInflater;
     47 
     48     private List<StreamItemEntry> mStreamItems;
     49 
     50     public StreamItemAdapter(Context context, View.OnClickListener itemClickListener,
     51             View.OnClickListener photoClickListener) {
     52         mContext = context;
     53         mItemClickListener = itemClickListener;
     54         mPhotoClickListener = photoClickListener;
     55         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     56         mStreamItems = Lists.newArrayList();
     57     }
     58 
     59     @Override
     60     public int getCount() {
     61         // The header should only be included as items in the list if there are other
     62         // stream items.
     63         int count = mStreamItems.size();
     64         return (count == 0) ? 0 : (count + 1);
     65     }
     66 
     67     @Override
     68     public Object getItem(int position) {
     69         if (position == 0) {
     70             return null;
     71         }
     72         return mStreamItems.get(position - 1);
     73     }
     74 
     75     @Override
     76     public long getItemId(int position) {
     77         if (position == 0) {
     78             return -1;
     79         }
     80         return position - 1;
     81     }
     82 
     83     @Override
     84     public boolean isEnabled(int position) {
     85         // Make all list items disabled, so they're not clickable.
     86         // We make child views clickable in getvView() if the account type supports
     87         // viewStreamItemActivity or viewStreamItemPhotoActivity.
     88         return false;
     89     }
     90 
     91     @Override
     92     public boolean areAllItemsEnabled() {
     93         // See isEnabled().
     94         return false;
     95     }
     96 
     97     @Override
     98     public View getView(int position, View convertView, ViewGroup parent) {
     99         if (position == 0) {
    100             return mInflater.inflate(R.layout.updates_header_contact, null);
    101         }
    102         final StreamItemEntry streamItem = (StreamItemEntry) getItem(position);
    103         final AccountTypeManager manager = AccountTypeManager.getInstance(mContext);
    104         final AccountType accountType =
    105                 manager.getAccountType(streamItem.getAccountType(), streamItem.getDataSet());
    106 
    107         final View view = ContactDetailDisplayUtils.createStreamItemView(
    108                 mInflater, mContext, convertView, streamItem,
    109                 // Only pass the photo click listener if the account type has the photo
    110                 // view activity.
    111                 (accountType.getViewStreamItemPhotoActivity() == null) ? null : mPhotoClickListener
    112                 );
    113         final View contentView = view.findViewById(R.id.stream_item_content);
    114 
    115         // If the account type has the stream item view activity, make the stream container
    116         // clickable.
    117         if (accountType.getViewStreamItemActivity() != null) {
    118             contentView.setTag(streamItem);
    119             contentView.setFocusable(true);
    120             contentView.setOnClickListener(mItemClickListener);
    121             contentView.setEnabled(true);
    122         } else {
    123             contentView.setTag(null);
    124             contentView.setFocusable(false);
    125             contentView.setOnClickListener(null);
    126             // setOnClickListener makes it clickable, so we need to overwrite it.
    127             contentView.setClickable(false);
    128             contentView.setEnabled(false);
    129         }
    130         return view;
    131     }
    132 
    133     @Override
    134     public int getViewTypeCount() {
    135         // ITEM_VIEW_TYPE_HEADER and ITEM_VIEW_TYPE_STREAM_ITEM
    136         return 2;
    137     }
    138 
    139     @Override
    140     public int getItemViewType(int position) {
    141         if (position == 0) {
    142             return ITEM_VIEW_TYPE_HEADER;
    143         }
    144         return ITEM_VIEW_TYPE_STREAM_ITEM;
    145     }
    146 
    147     public void setStreamItems(List<StreamItemEntry> streamItems) {
    148         mStreamItems = streamItems;
    149         notifyDataSetChanged();
    150     }
    151 }
    152