StreamItemEntry.java revision 155c0708cc28642836360b820833e22217130f0c
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 mAction; 41 private final String mActionUri; 42 43 // Package references for label and icon resources. 44 private final String mResPackage; 45 private final int mIconRes; 46 private final int mLabelRes; 47 48 // Photos associated with this stream item. 49 private List<StreamItemPhotoEntry> mPhotos; 50 51 @NeededForTesting 52 public StreamItemEntry(long id, String text, String comments, long timestamp, String action, 53 String actionUri, String resPackage, int iconRes, int labelRes) { 54 mId = id; 55 mText = text; 56 mComments = comments; 57 mTimestamp = timestamp; 58 mAction = action; 59 mActionUri = actionUri; 60 mResPackage = resPackage; 61 mIconRes = iconRes; 62 mLabelRes = labelRes; 63 mPhotos = new ArrayList<StreamItemPhotoEntry>(); 64 } 65 66 public StreamItemEntry(Cursor cursor) { 67 // This is expected to be populated via a cursor containing all StreamItems columns in 68 // its projection. 69 mId = getLong(cursor, StreamItems._ID); 70 mText = getString(cursor, StreamItems.TEXT); 71 mComments = getString(cursor, StreamItems.COMMENTS); 72 mTimestamp = getLong(cursor, StreamItems.TIMESTAMP); 73 mAction = getString(cursor, StreamItems.ACTION); 74 mActionUri = getString(cursor, StreamItems.ACTION_URI); 75 mResPackage = getString(cursor, StreamItems.RES_PACKAGE); 76 mIconRes = getInt(cursor, StreamItems.RES_ICON, -1); 77 mLabelRes = getInt(cursor, StreamItems.RES_LABEL, -1); 78 mPhotos = new ArrayList<StreamItemPhotoEntry>(); 79 } 80 81 public void addPhoto(StreamItemPhotoEntry photoEntry) { 82 mPhotos.add(photoEntry); 83 } 84 85 @Override 86 public int compareTo(StreamItemEntry other) { 87 return mTimestamp == other.mTimestamp ? 0 : mTimestamp > other.mTimestamp ? -1 : 1; 88 } 89 90 public long getId() { 91 return mId; 92 } 93 94 public String getText() { 95 return mText; 96 } 97 98 public String getComments() { 99 return mComments; 100 } 101 102 public long getTimestamp() { 103 return mTimestamp; 104 } 105 106 public String getAction() { 107 return mAction; 108 } 109 110 public String getActionUri() { 111 return mActionUri; 112 } 113 114 public String getResPackage() { 115 return mResPackage; 116 } 117 118 public int getIconRes() { 119 return mIconRes; 120 } 121 122 public int getLabelRes() { 123 return mLabelRes; 124 } 125 126 public List<StreamItemPhotoEntry> getPhotos() { 127 Collections.sort(mPhotos); 128 return mPhotos; 129 } 130 131 private static String getString(Cursor cursor, String columnName) { 132 return cursor.getString(cursor.getColumnIndex(columnName)); 133 } 134 135 private static int getInt(Cursor cursor, String columnName, int missingValue) { 136 final int columnIndex = cursor.getColumnIndex(columnName); 137 return cursor.isNull(columnIndex) ? missingValue : cursor.getInt(columnIndex); 138 } 139 140 private static long getLong(Cursor cursor, String columnName) { 141 final int columnIndex = cursor.getColumnIndex(columnName); 142 return cursor.getLong(columnIndex); 143 } 144} 145