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