ContactStatusLoader.java revision b1ea9c3c12d8d9da5c1e49a8752076ce60861e9f
1/* 2 * Copyright (C) 2010 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.email.activity; 18 19import com.android.email.R; 20import com.android.email.Utility; 21 22import android.content.AsyncTaskLoader; 23import android.content.ContentUris; 24import android.content.Context; 25import android.database.Cursor; 26import android.graphics.Bitmap; 27import android.graphics.BitmapFactory; 28import android.net.Uri; 29import android.provider.ContactsContract.CommonDataKinds.Email; 30import android.provider.ContactsContract.CommonDataKinds.Photo; 31import android.provider.ContactsContract.Contacts; 32import android.provider.ContactsContract.Data; 33import android.provider.ContactsContract.StatusUpdates; 34 35/** 36 * Loader to load presence statuses and the contact photoes. 37 */ 38public class ContactStatusLoader extends AsyncTaskLoader<ContactStatusLoader.Result> { 39 public static final int PRESENCE_UNKNOWN_RESOURCE_ID = R.drawable.presence_inactive; 40 41 /** email address -> photo id, presence */ 42 /* package */ static final String[] PROJECTION_PHOTO_ID_PRESENCE = new String[] { 43 Contacts.PHOTO_ID, 44 Contacts.CONTACT_PRESENCE 45 }; 46 private static final int COLUMN_PHOTO_ID = 0; 47 private static final int COLUMN_PRESENCE = 1; 48 49 /** photo id -> photo data */ 50 /* package */ static final String[] PHOTO_PROJECTION = new String[] { 51 Photo.PHOTO 52 }; 53 private static final int PHOTO_COLUMN = 0; 54 55 private final Context mContext; 56 private final String mEmailAddress; 57 58 /** 59 * Class that encapsulates the result. 60 */ 61 public static class Result { 62 public static final Result UNKNOWN = new Result(null, PRESENCE_UNKNOWN_RESOURCE_ID, null); 63 64 /** Contact photo. Null if unknown */ 65 public final Bitmap mPhoto; 66 67 /** Presence image resource ID. Always has a valid value, even if unknown. */ 68 public final int mPresenceResId; 69 70 /** URI for opening quick contact. Null if unknown. */ 71 public final Uri mLookupUri; 72 73 public Result(Bitmap photo, int presenceResId, Uri lookupUri) { 74 mPhoto = photo; 75 mPresenceResId = presenceResId; 76 mLookupUri = lookupUri; 77 } 78 } 79 80 public ContactStatusLoader(Context context, String emailAddress) { 81 super(context); 82 mContext = context; 83 mEmailAddress = emailAddress; 84 } 85 86 @Override 87 public Result loadInBackground() { 88 // Load photo-id and presence status. 89 Uri uri = Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mEmailAddress)); 90 Cursor c = mContext.getContentResolver().query( 91 uri, 92 PROJECTION_PHOTO_ID_PRESENCE, null, null, null); 93 if (c == null) { 94 return Result.UNKNOWN; 95 } 96 final long photoId; 97 final int presenceStatus; 98 try { 99 if (!c.moveToFirst()) { 100 return Result.UNKNOWN; 101 } 102 photoId = c.getLong(COLUMN_PHOTO_ID); 103 presenceStatus = c.getInt(COLUMN_PRESENCE); 104 } finally { 105 c.close(); 106 } 107 108 // Convert presence status into the res id. 109 final int presenceStatusResId = StatusUpdates.getPresenceIconResourceId(presenceStatus); 110 111 // load photo from photo-id. 112 Bitmap photo = null; 113 if (photoId != -1) { 114 final byte[] photoData = Utility.getFirstRowBlob(mContext, 115 ContentUris.withAppendedId(Data.CONTENT_URI, photoId), PHOTO_PROJECTION, 116 null, null, null, PHOTO_COLUMN, null); 117 if (photoData != null) { 118 photo = BitmapFactory.decodeByteArray(photoData, 0, photoData.length, null); 119 } 120 } 121 122 // Get lookup URI 123 final Uri lookupUri = Data.getContactLookupUri(mContext.getContentResolver(), uri); 124 return new Result(photo, presenceStatusResId, lookupUri); 125 } 126 127 @Override 128 public void startLoading() { 129 cancelLoad(); 130 forceLoad(); 131 } 132 133 @Override 134 public void stopLoading() { 135 cancelLoad(); 136 } 137 138 @Override 139 public void destroy() { 140 stopLoading(); 141 } 142} 143