StreamItemEntry.java revision da9cdc10fca76c960b43923d7da3abc627655fef
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.util;
18
19import com.android.contacts.detail.ContactDetailDisplayUtils;
20import com.android.contacts.test.NeededForTesting;
21
22import android.content.Context;
23import android.database.Cursor;
24import android.provider.ContactsContract.StreamItems;
25import android.text.Html;
26
27import java.util.ArrayList;
28import java.util.Collections;
29import java.util.List;
30
31/**
32 * Data object for a social stream item.  Social stream items may contain multiple
33 * mPhotos.  Social stream item entries are comparable; entries with more recent
34 * timestamps will be displayed on top.
35 */
36public class StreamItemEntry implements Comparable<StreamItemEntry> {
37
38    // Basic stream item fields.
39    private final long mId;
40    private final String mText;
41    private final String mComments;
42    private CharSequence mDecodedText;
43    private CharSequence mDecodedComments;
44    private final long mTimestamp;
45    private final String mAccountType;
46    private final String mAccountName;
47    private final String mDataSet;
48
49    // Package references for label and icon resources.
50    private final String mResPackage;
51    private final String mIconRes;
52    private final String mLabelRes;
53
54    // Photos associated with this stream item.
55    private List<StreamItemPhotoEntry> mPhotos;
56
57    @NeededForTesting
58    public StreamItemEntry(long id, String text, String comments, long timestamp,
59            String accountType, String accountName, String dataSet, String resPackage,
60            String iconRes, String labelRes) {
61        mId = id;
62        mText = text;
63        mComments = comments;
64        mTimestamp = timestamp;
65        mAccountType = accountType;
66        mAccountName = accountName;
67        mDataSet = dataSet;
68        mResPackage = resPackage;
69        mIconRes = iconRes;
70        mLabelRes = labelRes;
71        mPhotos = new ArrayList<StreamItemPhotoEntry>();
72    }
73
74    public StreamItemEntry(Cursor cursor) {
75        // This is expected to be populated via a cursor containing all StreamItems columns in
76        // its projection.
77        mId = getLong(cursor, StreamItems._ID);
78        mText = getString(cursor, StreamItems.TEXT);
79        mComments = getString(cursor, StreamItems.COMMENTS);
80        mTimestamp = getLong(cursor, StreamItems.TIMESTAMP);
81        mAccountType = getString(cursor, StreamItems.ACCOUNT_TYPE);
82        mAccountName = getString(cursor, StreamItems.ACCOUNT_NAME);
83        mDataSet = getString(cursor, StreamItems.DATA_SET);
84        mResPackage = getString(cursor, StreamItems.RES_PACKAGE);
85        mIconRes = getString(cursor, StreamItems.RES_ICON);
86        mLabelRes = getString(cursor, StreamItems.RES_LABEL);
87        mPhotos = new ArrayList<StreamItemPhotoEntry>();
88    }
89
90    public void addPhoto(StreamItemPhotoEntry photoEntry) {
91        mPhotos.add(photoEntry);
92    }
93
94    @Override
95    public int compareTo(StreamItemEntry other) {
96        return mTimestamp == other.mTimestamp ? 0 : mTimestamp > other.mTimestamp ? -1 : 1;
97    }
98
99    public long getId() {
100        return mId;
101    }
102
103    public String getText() {
104        return mText;
105    }
106
107    public String getComments() {
108        return mComments;
109    }
110
111    public long getTimestamp() {
112        return mTimestamp;
113    }
114
115    public String getAccountType() {
116        return mAccountType;
117    }
118
119    public String getAccountName() {
120        return mAccountName;
121    }
122
123    public String getDataSet() {
124        return mDataSet;
125    }
126
127    public String getResPackage() {
128        return mResPackage;
129    }
130
131    public String getIconRes() {
132        return mIconRes;
133    }
134
135    public String getLabelRes() {
136        return mLabelRes;
137    }
138
139    public List<StreamItemPhotoEntry> getPhotos() {
140        Collections.sort(mPhotos);
141        return mPhotos;
142    }
143
144    public void decodeHtml(Context context) {
145        final Html.ImageGetter imageGetter = ContactDetailDisplayUtils.getImageGetter(context);
146        if (mText != null) {
147            mDecodedText = HtmlUtils.fromHtml(context, mText, imageGetter, null);
148        }
149        if (mComments != null) {
150            mDecodedComments = HtmlUtils.fromHtml(context, mComments, imageGetter, null);
151        }
152    }
153
154    public CharSequence getDecodedText() {
155        return mDecodedText;
156    }
157
158    public CharSequence getDecodedComments() {
159        return mDecodedComments;
160    }
161
162    private static String getString(Cursor cursor, String columnName) {
163        return cursor.getString(cursor.getColumnIndex(columnName));
164    }
165
166    private static long getLong(Cursor cursor, String columnName) {
167        final int columnIndex = cursor.getColumnIndex(columnName);
168        return cursor.getLong(columnIndex);
169    }
170}
171