summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/detail/StreamItemAdapter.java
blob: 15219cd5cb90136bc36fada0465007557e374b31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.contacts.detail;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.android.contacts.R;
import com.android.contacts.model.AccountTypeManager;
import com.android.contacts.model.account.AccountType;
import com.android.contacts.util.StreamItemEntry;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * List adapter for stream items of a given contact.
 */
public class StreamItemAdapter extends BaseAdapter {
    /** The header view, hidden under the tab carousel, if present. */
    private static final int ITEM_VIEW_TYPE_HEADER = 0;
    /** The updates in the list. */
    private static final int ITEM_VIEW_TYPE_STREAM_ITEM = 1;

    private final Context mContext;
    private final View.OnClickListener mItemClickListener;
    private final View.OnClickListener mPhotoClickListener;
    private final LayoutInflater mInflater;

    private List<StreamItemEntry> mStreamItems;

    public StreamItemAdapter(Context context, View.OnClickListener itemClickListener,
            View.OnClickListener photoClickListener) {
        mContext = context;
        mItemClickListener = itemClickListener;
        mPhotoClickListener = photoClickListener;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mStreamItems = Lists.newArrayList();
    }

    @Override
    public int getCount() {
        // The header should only be included as items in the list if there are other
        // stream items.
        int count = mStreamItems.size();
        return (count == 0) ? 0 : (count + 1);
    }

    @Override
    public Object getItem(int position) {
        if (position == 0) {
            return null;
        }
        return mStreamItems.get(position - 1);
    }

    @Override
    public long getItemId(int position) {
        if (position == 0) {
            return -1;
        }
        return position - 1;
    }

    @Override
    public boolean isEnabled(int position) {
        // Make all list items disabled, so they're not clickable.
        // We make child views clickable in getvView() if the account type supports
        // viewStreamItemActivity or viewStreamItemPhotoActivity.
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        // See isEnabled().
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            return mInflater.inflate(R.layout.updates_header_contact, null);
        }
        final StreamItemEntry streamItem = (StreamItemEntry) getItem(position);
        final AccountTypeManager manager = AccountTypeManager.getInstance(mContext);
        final AccountType accountType =
                manager.getAccountType(streamItem.getAccountType(), streamItem.getDataSet());

        final View view = ContactDetailDisplayUtils.createStreamItemView(
                mInflater, mContext, convertView, streamItem,
                // Only pass the photo click listener if the account type has the photo
                // view activity.
                (accountType.getViewStreamItemPhotoActivity() == null) ? null : mPhotoClickListener
                );
        final View contentView = view.findViewById(R.id.stream_item_content);

        // If the account type has the stream item view activity, make the stream container
        // clickable.
        if (accountType.getViewStreamItemActivity() != null) {
            contentView.setTag(streamItem);
            contentView.setFocusable(true);
            contentView.setOnClickListener(mItemClickListener);
            contentView.setEnabled(true);
        } else {
            contentView.setTag(null);
            contentView.setFocusable(false);
            contentView.setOnClickListener(null);
            // setOnClickListener makes it clickable, so we need to overwrite it.
            contentView.setClickable(false);
            contentView.setEnabled(false);
        }
        return view;
    }

    @Override
    public int getViewTypeCount() {
        // ITEM_VIEW_TYPE_HEADER and ITEM_VIEW_TYPE_STREAM_ITEM
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_VIEW_TYPE_HEADER;
        }
        return ITEM_VIEW_TYPE_STREAM_ITEM;
    }

    public void setStreamItems(List<StreamItemEntry> streamItems) {
        mStreamItems = streamItems;
        notifyDataSetChanged();
    }
}