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