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