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