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 17 package com.android.contacts.util; 18 19 import android.database.Cursor; 20 import android.provider.ContactsContract.PhotoFiles; 21 import android.provider.ContactsContract.StreamItemPhotos; 22 23 /** 24 * Data object for a photo associated with a social stream item. These are comparable; 25 * entries with a lower sort index will be displayed on top (with the ID used as a 26 * tie-breaker). 27 */ 28 public class StreamItemPhotoEntry implements Comparable<StreamItemPhotoEntry> { 29 private final long mId; 30 private final int mSortIndex; 31 private final long mPhotoFileId; 32 private final String mPhotoUri; 33 private final int mHeight; 34 private final int mWidth; 35 private final int mFileSize; 36 37 public StreamItemPhotoEntry(long id, int sortIndex, long photoFileId, String photoUri, 38 int height, int width, int fileSize) { 39 mId = id; 40 mSortIndex = sortIndex; 41 mPhotoFileId = photoFileId; 42 mPhotoUri = photoUri; 43 mHeight = height; 44 mWidth = width; 45 mFileSize = fileSize; 46 } 47 48 public StreamItemPhotoEntry(Cursor cursor) { 49 // This is expected to be populated via a cursor containing a join of all 50 // StreamItemPhotos columns and all PhotoFiles columns (except for ID). 51 mId = getLong(cursor, StreamItemPhotos._ID); 52 mSortIndex = getInt(cursor, StreamItemPhotos.SORT_INDEX, -1); 53 mPhotoFileId = getLong(cursor, StreamItemPhotos.PHOTO_FILE_ID); 54 mPhotoUri = getString(cursor, StreamItemPhotos.PHOTO_URI); 55 mHeight = getInt(cursor, PhotoFiles.HEIGHT, -1); 56 mWidth = getInt(cursor, PhotoFiles.WIDTH, -1); 57 mFileSize = getInt(cursor, PhotoFiles.FILESIZE, -1); 58 } 59 60 public long getId() { 61 return mId; 62 } 63 64 public int getSortIndex() { 65 return mSortIndex; 66 } 67 68 public long getPhotoFileId() { 69 return mPhotoFileId; 70 } 71 72 public String getPhotoUri() { 73 return mPhotoUri; 74 } 75 76 public int getHeight() { 77 return mHeight; 78 } 79 80 public int getWidth() { 81 return mWidth; 82 } 83 84 public int getFileSize() { 85 return mFileSize; 86 } 87 88 @Override 89 public int compareTo(StreamItemPhotoEntry streamItemPhotoEntry) { 90 // Sort index is used to compare, falling back to ID if neither entry has a 91 // sort index specified (entries without a sort index are sorted after entries 92 // that have one). 93 if (mSortIndex == streamItemPhotoEntry.mSortIndex) { 94 if (mSortIndex == -1) { 95 return mId == streamItemPhotoEntry.mId ? 0 96 : mId < streamItemPhotoEntry.mId ? -1 : 1; 97 } else { 98 return 0; 99 } 100 } else { 101 if (mSortIndex == -1) { 102 return 1; 103 } 104 if (streamItemPhotoEntry.mSortIndex == -1) { 105 return -1; 106 } 107 return mSortIndex == streamItemPhotoEntry.mSortIndex ? 0 108 : mSortIndex < streamItemPhotoEntry.mSortIndex ? -1 : 1; 109 } 110 } 111 112 private static String getString(Cursor cursor, String columnName) { 113 return cursor.getString(cursor.getColumnIndex(columnName)); 114 } 115 116 private static int getInt(Cursor cursor, String columnName, int missingValue) { 117 final int columnIndex = cursor.getColumnIndex(columnName); 118 return cursor.isNull(columnIndex) ? missingValue : cursor.getInt(columnIndex); 119 } 120 121 private static long getLong(Cursor cursor, String columnName) { 122 final int columnIndex = cursor.getColumnIndex(columnName); 123 return cursor.getLong(columnIndex); 124 } 125 } 126