Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1 | /* |
Daniel Lehmann | ef3f8f0 | 2010-07-26 18:55:25 -0700 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 17 | package com.android.contacts; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 18 | |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 19 | import com.android.contacts.model.AccountType; |
| 20 | import com.android.contacts.model.AccountTypeManager; |
Makoto Onuki | 6ad227f | 2011-08-15 13:46:59 -0700 | [diff] [blame] | 21 | import com.android.contacts.model.AccountTypeWithDataSet; |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 22 | import com.android.contacts.util.ContactLoaderUtils; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 23 | import com.android.contacts.util.DataStatus; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 24 | import com.android.contacts.util.StreamItemEntry; |
| 25 | import com.android.contacts.util.StreamItemPhotoEntry; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 26 | import com.google.android.collect.Lists; |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 27 | import com.google.common.annotations.VisibleForTesting; |
Makoto Onuki | 6ad227f | 2011-08-15 13:46:59 -0700 | [diff] [blame] | 28 | import com.google.common.collect.Maps; |
Makoto Onuki | aba2b83 | 2011-08-12 15:44:53 -0700 | [diff] [blame] | 29 | import com.google.common.collect.Sets; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 30 | |
| 31 | import android.content.ContentResolver; |
| 32 | import android.content.ContentUris; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 33 | import android.content.ContentValues; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 34 | import android.content.Context; |
| 35 | import android.content.Entity; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 36 | import android.content.Entity.NamedContentValues; |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 37 | import android.content.Intent; |
Jeff Hamilton | 3c46291 | 2010-05-15 02:20:01 -0500 | [diff] [blame] | 38 | import android.content.Loader; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 39 | import android.content.pm.PackageManager; |
| 40 | import android.content.pm.PackageManager.NameNotFoundException; |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 41 | import android.content.res.AssetFileDescriptor; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 42 | import android.content.res.Resources; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 43 | import android.database.Cursor; |
| 44 | import android.net.Uri; |
| 45 | import android.os.AsyncTask; |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 46 | import android.provider.ContactsContract; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 47 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 48 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 49 | import android.provider.ContactsContract.Contacts; |
| 50 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 51 | import android.provider.ContactsContract.Directory; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 52 | import android.provider.ContactsContract.DisplayNameSources; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 53 | import android.provider.ContactsContract.Groups; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 54 | import android.provider.ContactsContract.RawContacts; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 55 | import android.provider.ContactsContract.StreamItemPhotos; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 56 | import android.provider.ContactsContract.StreamItems; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 57 | import android.text.TextUtils; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 58 | import android.util.Log; |
| 59 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 60 | import java.io.ByteArrayOutputStream; |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 61 | import java.io.FileInputStream; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 62 | import java.io.IOException; |
| 63 | import java.io.InputStream; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 64 | import java.util.ArrayList; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 65 | import java.util.Collections; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 66 | import java.util.HashMap; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 67 | import java.util.List; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 68 | import java.util.Map; |
Makoto Onuki | aba2b83 | 2011-08-12 15:44:53 -0700 | [diff] [blame] | 69 | import java.util.Set; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 70 | |
| 71 | /** |
| 72 | * Loads a single Contact and all it constituent RawContacts. |
| 73 | */ |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 74 | public class ContactLoader extends Loader<ContactLoader.Result> { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 75 | private static final String TAG = "ContactLoader"; |
| 76 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 77 | private final Uri mRequestedUri; |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 78 | private Uri mLookupUri; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 79 | private boolean mLoadGroupMetaData; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 80 | private boolean mLoadStreamItems; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 81 | private final boolean mLoadInvitableAccountTypes; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 82 | private Result mContact; |
| 83 | private ForceLoadContentObserver mObserver; |
| 84 | private boolean mDestroyed; |
Makoto Onuki | aba2b83 | 2011-08-12 15:44:53 -0700 | [diff] [blame] | 85 | private final Set<Long> mNotifiedRawContactIds = Sets.newHashSet(); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 86 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 87 | public interface Listener { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 88 | public void onContactLoaded(Result contact); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * The result of a load operation. Contains all data necessary to display the contact. |
| 93 | */ |
| 94 | public static final class Result { |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 95 | private enum Status { |
| 96 | /** Contact is successfully loaded */ |
| 97 | LOADED, |
| 98 | /** There was an error loading the contact */ |
| 99 | ERROR, |
| 100 | /** Contact is not found */ |
| 101 | NOT_FOUND, |
| 102 | } |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 103 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 104 | private final Uri mRequestedUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 105 | private final Uri mLookupUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 106 | private final Uri mUri; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 107 | private final long mDirectoryId; |
| 108 | private final String mLookupKey; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 109 | private final long mId; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 110 | private final long mNameRawContactId; |
| 111 | private final int mDisplayNameSource; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 112 | private final long mPhotoId; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 113 | private final String mPhotoUri; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 114 | private final String mDisplayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 115 | private final String mAltDisplayName; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 116 | private final String mPhoneticName; |
| 117 | private final boolean mStarred; |
| 118 | private final Integer mPresence; |
| 119 | private final ArrayList<Entity> mEntities; |
Makoto Onuki | 870a87e | 2011-08-12 13:40:31 -0700 | [diff] [blame] | 120 | private final ArrayList<StreamItemEntry> mStreamItems; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 121 | private final HashMap<Long, DataStatus> mStatuses; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 122 | private final ArrayList<AccountType> mInvitableAccountTypes; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 123 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 124 | private String mDirectoryDisplayName; |
| 125 | private String mDirectoryType; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 126 | private String mDirectoryAccountType; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 127 | private String mDirectoryAccountName; |
| 128 | private int mDirectoryExportSupport; |
| 129 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 130 | private ArrayList<GroupMetaData> mGroups; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 131 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 132 | private boolean mLoadingPhoto; |
| 133 | private byte[] mPhotoBinaryData; |
Makoto Onuki | 870a87e | 2011-08-12 13:40:31 -0700 | [diff] [blame] | 134 | private final boolean mSendToVoicemail; |
| 135 | private final String mCustomRingtone; |
| 136 | private final boolean mIsUserProfile; |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 137 | |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 138 | private final Status mStatus; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 139 | private final Exception mException; |
| 140 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 141 | /** |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 142 | * Constructor for special results, namely "no contact found" and "error". |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 143 | */ |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 144 | private Result(Uri requestedUri, Status status, Exception exception) { |
| 145 | if (status == Status.ERROR && exception == null) { |
| 146 | throw new IllegalArgumentException("ERROR result must have exception"); |
| 147 | } |
| 148 | mStatus = status; |
| 149 | mException = exception; |
| 150 | mRequestedUri = requestedUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 151 | mLookupUri = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 152 | mUri = null; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 153 | mDirectoryId = -1; |
| 154 | mLookupKey = null; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 155 | mId = -1; |
| 156 | mEntities = null; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 157 | mStreamItems = new ArrayList<StreamItemEntry>(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 158 | mStatuses = null; |
| 159 | mNameRawContactId = -1; |
| 160 | mDisplayNameSource = DisplayNameSources.UNDEFINED; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 161 | mPhotoId = -1; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 162 | mPhotoUri = null; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 163 | mDisplayName = null; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 164 | mAltDisplayName = null; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 165 | mPhoneticName = null; |
| 166 | mStarred = false; |
| 167 | mPresence = null; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 168 | mInvitableAccountTypes = null; |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 169 | mSendToVoicemail = false; |
| 170 | mCustomRingtone = null; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 171 | mIsUserProfile = false; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 172 | } |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 173 | |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 174 | private static Result forError(Uri requestedUri, Exception exception) { |
| 175 | return new Result(requestedUri, Status.ERROR, exception); |
| 176 | } |
| 177 | |
| 178 | private static Result forNotFound(Uri requestedUri) { |
| 179 | return new Result(requestedUri, Status.NOT_FOUND, null); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Constructor to call when contact was found |
| 184 | */ |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 185 | private Result(Uri requestedUri, Uri uri, Uri lookupUri, long directoryId, String lookupKey, |
| 186 | long id, long nameRawContactId, int displayNameSource, long photoId, |
| 187 | String photoUri, String displayName, String altDisplayName, String phoneticName, |
| 188 | boolean starred, Integer presence, boolean sendToVoicemail, String customRingtone, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 189 | boolean isUserProfile) { |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 190 | mStatus = Status.LOADED; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 191 | mException = null; |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 192 | mRequestedUri = requestedUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 193 | mLookupUri = lookupUri; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 194 | mUri = uri; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 195 | mDirectoryId = directoryId; |
| 196 | mLookupKey = lookupKey; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 197 | mId = id; |
| 198 | mEntities = new ArrayList<Entity>(); |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 199 | mStreamItems = new ArrayList<StreamItemEntry>(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 200 | mStatuses = new HashMap<Long, DataStatus>(); |
| 201 | mNameRawContactId = nameRawContactId; |
| 202 | mDisplayNameSource = displayNameSource; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 203 | mPhotoId = photoId; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 204 | mPhotoUri = photoUri; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 205 | mDisplayName = displayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 206 | mAltDisplayName = altDisplayName; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 207 | mPhoneticName = phoneticName; |
| 208 | mStarred = starred; |
| 209 | mPresence = presence; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 210 | mInvitableAccountTypes = Lists.newArrayList(); |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 211 | mSendToVoicemail = sendToVoicemail; |
| 212 | mCustomRingtone = customRingtone; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 213 | mIsUserProfile = isUserProfile; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 216 | private Result(Result from) { |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 217 | mStatus = from.mStatus; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 218 | mException = from.mException; |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 219 | mRequestedUri = from.mRequestedUri; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 220 | mLookupUri = from.mLookupUri; |
| 221 | mUri = from.mUri; |
| 222 | mDirectoryId = from.mDirectoryId; |
| 223 | mLookupKey = from.mLookupKey; |
| 224 | mId = from.mId; |
| 225 | mNameRawContactId = from.mNameRawContactId; |
| 226 | mDisplayNameSource = from.mDisplayNameSource; |
| 227 | mPhotoId = from.mPhotoId; |
| 228 | mPhotoUri = from.mPhotoUri; |
| 229 | mDisplayName = from.mDisplayName; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 230 | mAltDisplayName = from.mAltDisplayName; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 231 | mPhoneticName = from.mPhoneticName; |
| 232 | mStarred = from.mStarred; |
| 233 | mPresence = from.mPresence; |
| 234 | mEntities = from.mEntities; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 235 | mStreamItems = from.mStreamItems; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 236 | mStatuses = from.mStatuses; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 237 | mInvitableAccountTypes = from.mInvitableAccountTypes; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 238 | |
| 239 | mDirectoryDisplayName = from.mDirectoryDisplayName; |
| 240 | mDirectoryType = from.mDirectoryType; |
| 241 | mDirectoryAccountType = from.mDirectoryAccountType; |
| 242 | mDirectoryAccountName = from.mDirectoryAccountName; |
| 243 | mDirectoryExportSupport = from.mDirectoryExportSupport; |
| 244 | |
| 245 | mGroups = from.mGroups; |
| 246 | |
| 247 | mLoadingPhoto = from.mLoadingPhoto; |
| 248 | mPhotoBinaryData = from.mPhotoBinaryData; |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 249 | mSendToVoicemail = from.mSendToVoicemail; |
| 250 | mCustomRingtone = from.mCustomRingtone; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 251 | mIsUserProfile = from.mIsUserProfile; |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 252 | } |
| 253 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 254 | /** |
| 255 | * @param exportSupport See {@link Directory#EXPORT_SUPPORT}. |
| 256 | */ |
Daniel Lehmann | 1ad4d1b | 2010-10-18 19:20:41 -0700 | [diff] [blame] | 257 | private void setDirectoryMetaData(String displayName, String directoryType, |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 258 | String accountType, String accountName, int exportSupport) { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 259 | mDirectoryDisplayName = displayName; |
| 260 | mDirectoryType = directoryType; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 261 | mDirectoryAccountType = accountType; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 262 | mDirectoryAccountName = accountName; |
| 263 | mDirectoryExportSupport = exportSupport; |
| 264 | } |
| 265 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 266 | private void setLoadingPhoto(boolean flag) { |
| 267 | mLoadingPhoto = flag; |
| 268 | } |
| 269 | |
| 270 | private void setPhotoBinaryData(byte[] photoBinaryData) { |
| 271 | mPhotoBinaryData = photoBinaryData; |
| 272 | } |
| 273 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 274 | /** |
| 275 | * Returns the URI for the contact that contains both the lookup key and the ID. This is |
| 276 | * the best URI to reference a contact. |
| 277 | * For directory contacts, this is the same a the URI as returned by {@link #getUri()} |
| 278 | */ |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 279 | public Uri getLookupUri() { |
| 280 | return mLookupUri; |
| 281 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 282 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 283 | public String getLookupKey() { |
| 284 | return mLookupKey; |
| 285 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 286 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 287 | /** |
| 288 | * Returns the contact Uri that was passed to the provider to make the query. This is |
| 289 | * the same as the requested Uri, unless the requested Uri doesn't specify a Contact: |
| 290 | * If it either references a Raw-Contact or a Person (a pre-Eclair style Uri), this Uri will |
| 291 | * always reference the full aggregate contact. |
| 292 | */ |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 293 | public Uri getUri() { |
| 294 | return mUri; |
| 295 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 296 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 297 | /** |
| 298 | * Returns the URI for which this {@link ContactLoader) was initially requested. |
| 299 | */ |
| 300 | public Uri getRequestedUri() { |
| 301 | return mRequestedUri; |
| 302 | } |
| 303 | |
Dave Santoro | 6fa7384 | 2011-09-28 14:37:06 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Returns the contact ID. |
| 306 | */ |
| 307 | public long getId() { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 308 | return mId; |
| 309 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 310 | |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 311 | /** |
| 312 | * @return true when an exception happened during loading, in which case |
| 313 | * {@link #getException} returns the actual exception object. |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 314 | * Note {@link #isNotFound()} and {@link #isError()} are mutually exclusive; If |
| 315 | * {@link #isError()} is {@code true}, {@link #isNotFound()} is always {@code false}, |
| 316 | * and vice versa. |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 317 | */ |
| 318 | public boolean isError() { |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 319 | return mStatus == Status.ERROR; |
Makoto Onuki | 9e7b5da | 2011-08-22 15:51:28 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | public Exception getException() { |
| 323 | return mException; |
| 324 | } |
| 325 | |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 326 | /** |
| 327 | * @return true when the specified contact is not found. |
| 328 | * Note {@link #isNotFound()} and {@link #isError()} are mutually exclusive; If |
| 329 | * {@link #isError()} is {@code true}, {@link #isNotFound()} is always {@code false}, |
| 330 | * and vice versa. |
| 331 | */ |
| 332 | public boolean isNotFound() { |
| 333 | return mStatus == Status.NOT_FOUND; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * @return true if the specified contact is successfully loaded. |
| 338 | * i.e. neither {@link #isError()} nor {@link #isNotFound()}. |
| 339 | */ |
| 340 | public boolean isLoaded() { |
| 341 | return mStatus == Status.LOADED; |
| 342 | } |
| 343 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 344 | public long getNameRawContactId() { |
| 345 | return mNameRawContactId; |
| 346 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 347 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 348 | public int getDisplayNameSource() { |
| 349 | return mDisplayNameSource; |
| 350 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 351 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 352 | public long getPhotoId() { |
| 353 | return mPhotoId; |
| 354 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 355 | |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 356 | public String getPhotoUri() { |
| 357 | return mPhotoUri; |
| 358 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 359 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 360 | public String getDisplayName() { |
| 361 | return mDisplayName; |
| 362 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 363 | |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 364 | public String getAltDisplayName() { |
| 365 | return mAltDisplayName; |
| 366 | } |
| 367 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 368 | public String getPhoneticName() { |
| 369 | return mPhoneticName; |
| 370 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 371 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 372 | public boolean getStarred() { |
| 373 | return mStarred; |
| 374 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 375 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 376 | public Integer getPresence() { |
| 377 | return mPresence; |
| 378 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 379 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 380 | public ArrayList<AccountType> getInvitableAccountTypes() { |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 381 | return mInvitableAccountTypes; |
| 382 | } |
| 383 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 384 | public ArrayList<Entity> getEntities() { |
| 385 | return mEntities; |
| 386 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 387 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 388 | public ArrayList<StreamItemEntry> getStreamItems() { |
| 389 | return mStreamItems; |
| 390 | } |
| 391 | |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 392 | public HashMap<Long, DataStatus> getStatuses() { |
| 393 | return mStatuses; |
| 394 | } |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 395 | |
| 396 | public long getDirectoryId() { |
| 397 | return mDirectoryId; |
| 398 | } |
| 399 | |
| 400 | public boolean isDirectoryEntry() { |
Dmitri Plotnikov | 5f72c1f | 2010-09-01 21:21:04 -0700 | [diff] [blame] | 401 | return mDirectoryId != -1 && mDirectoryId != Directory.DEFAULT |
| 402 | && mDirectoryId != Directory.LOCAL_INVISIBLE; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | public int getDirectoryExportSupport() { |
| 406 | return mDirectoryExportSupport; |
| 407 | } |
| 408 | |
| 409 | public String getDirectoryDisplayName() { |
| 410 | return mDirectoryDisplayName; |
| 411 | } |
| 412 | |
| 413 | public String getDirectoryType() { |
| 414 | return mDirectoryType; |
| 415 | } |
| 416 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 417 | public String getDirectoryAccountType() { |
| 418 | return mDirectoryAccountType; |
| 419 | } |
| 420 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 421 | public String getDirectoryAccountName() { |
| 422 | return mDirectoryAccountName; |
| 423 | } |
| 424 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 425 | public boolean isLoadingPhoto() { |
| 426 | return mLoadingPhoto; |
| 427 | } |
| 428 | |
| 429 | public byte[] getPhotoBinaryData() { |
| 430 | return mPhotoBinaryData; |
| 431 | } |
| 432 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 433 | public ArrayList<ContentValues> getContentValues() { |
| 434 | if (mEntities.size() != 1) { |
| 435 | throw new IllegalStateException( |
| 436 | "Cannot extract content values from an aggregated contact"); |
| 437 | } |
| 438 | |
| 439 | Entity entity = mEntities.get(0); |
| 440 | ArrayList<ContentValues> result = new ArrayList<ContentValues>(); |
| 441 | ArrayList<NamedContentValues> subValues = entity.getSubValues(); |
| 442 | if (subValues != null) { |
| 443 | int size = subValues.size(); |
| 444 | for (int i = 0; i < size; i++) { |
| 445 | NamedContentValues pair = subValues.get(i); |
| 446 | if (Data.CONTENT_URI.equals(pair.uri)) { |
| 447 | result.add(pair.values); |
| 448 | } |
| 449 | } |
| 450 | } |
Dmitri Plotnikov | 40ec3a8 | 2010-11-10 11:25:33 -0800 | [diff] [blame] | 451 | |
| 452 | // If the photo was loaded using the URI, create an entry for the photo |
| 453 | // binary data. |
| 454 | if (mPhotoId == 0 && mPhotoBinaryData != null) { |
| 455 | ContentValues photo = new ContentValues(); |
| 456 | photo.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE); |
| 457 | photo.put(Photo.PHOTO, mPhotoBinaryData); |
| 458 | result.add(photo); |
| 459 | } |
| 460 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 461 | return result; |
| 462 | } |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 463 | |
Daniel Lehmann | 1ad4d1b | 2010-10-18 19:20:41 -0700 | [diff] [blame] | 464 | private void addGroupMetaData(GroupMetaData group) { |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 465 | if (mGroups == null) { |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 466 | mGroups = new ArrayList<GroupMetaData>(); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 467 | } |
| 468 | mGroups.add(group); |
| 469 | } |
| 470 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 471 | public List<GroupMetaData> getGroupMetaData() { |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 472 | return mGroups; |
| 473 | } |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 474 | |
| 475 | public boolean isSendToVoicemail() { |
| 476 | return mSendToVoicemail; |
| 477 | } |
| 478 | |
| 479 | public String getCustomRingtone() { |
| 480 | return mCustomRingtone; |
| 481 | } |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 482 | |
| 483 | public boolean isUserProfile() { |
| 484 | return mIsUserProfile; |
| 485 | } |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 488 | /** |
| 489 | * Projection used for the query that loads all data for the entire contact (except for |
| 490 | * social stream items). |
| 491 | */ |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 492 | private static class ContactQuery { |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 493 | final static String[] COLUMNS = new String[] { |
| 494 | Contacts.NAME_RAW_CONTACT_ID, |
| 495 | Contacts.DISPLAY_NAME_SOURCE, |
| 496 | Contacts.LOOKUP_KEY, |
| 497 | Contacts.DISPLAY_NAME, |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 498 | Contacts.DISPLAY_NAME_ALTERNATIVE, |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 499 | Contacts.PHONETIC_NAME, |
| 500 | Contacts.PHOTO_ID, |
| 501 | Contacts.STARRED, |
| 502 | Contacts.CONTACT_PRESENCE, |
| 503 | Contacts.CONTACT_STATUS, |
| 504 | Contacts.CONTACT_STATUS_TIMESTAMP, |
| 505 | Contacts.CONTACT_STATUS_RES_PACKAGE, |
| 506 | Contacts.CONTACT_STATUS_LABEL, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 507 | Contacts.Entity.CONTACT_ID, |
| 508 | Contacts.Entity.RAW_CONTACT_ID, |
| 509 | |
| 510 | RawContacts.ACCOUNT_NAME, |
| 511 | RawContacts.ACCOUNT_TYPE, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 512 | RawContacts.DATA_SET, |
| 513 | RawContacts.ACCOUNT_TYPE_AND_DATA_SET, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 514 | RawContacts.DIRTY, |
| 515 | RawContacts.VERSION, |
| 516 | RawContacts.SOURCE_ID, |
| 517 | RawContacts.SYNC1, |
| 518 | RawContacts.SYNC2, |
| 519 | RawContacts.SYNC3, |
| 520 | RawContacts.SYNC4, |
| 521 | RawContacts.DELETED, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 522 | RawContacts.NAME_VERIFIED, |
| 523 | |
| 524 | Contacts.Entity.DATA_ID, |
| 525 | Data.DATA1, |
| 526 | Data.DATA2, |
| 527 | Data.DATA3, |
| 528 | Data.DATA4, |
| 529 | Data.DATA5, |
| 530 | Data.DATA6, |
| 531 | Data.DATA7, |
| 532 | Data.DATA8, |
| 533 | Data.DATA9, |
| 534 | Data.DATA10, |
| 535 | Data.DATA11, |
| 536 | Data.DATA12, |
| 537 | Data.DATA13, |
| 538 | Data.DATA14, |
| 539 | Data.DATA15, |
| 540 | Data.SYNC1, |
| 541 | Data.SYNC2, |
| 542 | Data.SYNC3, |
| 543 | Data.SYNC4, |
| 544 | Data.DATA_VERSION, |
| 545 | Data.IS_PRIMARY, |
| 546 | Data.IS_SUPER_PRIMARY, |
| 547 | Data.MIMETYPE, |
| 548 | Data.RES_PACKAGE, |
| 549 | |
| 550 | GroupMembership.GROUP_SOURCE_ID, |
| 551 | |
| 552 | Data.PRESENCE, |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 553 | Data.CHAT_CAPABILITY, |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 554 | Data.STATUS, |
| 555 | Data.STATUS_RES_PACKAGE, |
| 556 | Data.STATUS_ICON, |
| 557 | Data.STATUS_LABEL, |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 558 | Data.STATUS_TIMESTAMP, |
| 559 | |
| 560 | Contacts.PHOTO_URI, |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 561 | Contacts.SEND_TO_VOICEMAIL, |
| 562 | Contacts.CUSTOM_RINGTONE, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 563 | Contacts.IS_USER_PROFILE, |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 564 | }; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 565 | |
| 566 | public final static int NAME_RAW_CONTACT_ID = 0; |
| 567 | public final static int DISPLAY_NAME_SOURCE = 1; |
| 568 | public final static int LOOKUP_KEY = 2; |
| 569 | public final static int DISPLAY_NAME = 3; |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 570 | public final static int ALT_DISPLAY_NAME = 4; |
| 571 | public final static int PHONETIC_NAME = 5; |
| 572 | public final static int PHOTO_ID = 6; |
| 573 | public final static int STARRED = 7; |
| 574 | public final static int CONTACT_PRESENCE = 8; |
| 575 | public final static int CONTACT_STATUS = 9; |
| 576 | public final static int CONTACT_STATUS_TIMESTAMP = 10; |
| 577 | public final static int CONTACT_STATUS_RES_PACKAGE = 11; |
| 578 | public final static int CONTACT_STATUS_LABEL = 12; |
| 579 | public final static int CONTACT_ID = 13; |
| 580 | public final static int RAW_CONTACT_ID = 14; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 581 | |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 582 | public final static int ACCOUNT_NAME = 15; |
| 583 | public final static int ACCOUNT_TYPE = 16; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 584 | public final static int DATA_SET = 17; |
| 585 | public final static int ACCOUNT_TYPE_AND_DATA_SET = 18; |
| 586 | public final static int DIRTY = 19; |
| 587 | public final static int VERSION = 20; |
| 588 | public final static int SOURCE_ID = 21; |
| 589 | public final static int SYNC1 = 22; |
| 590 | public final static int SYNC2 = 23; |
| 591 | public final static int SYNC3 = 24; |
| 592 | public final static int SYNC4 = 25; |
| 593 | public final static int DELETED = 26; |
| 594 | public final static int NAME_VERIFIED = 27; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 595 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 596 | public final static int DATA_ID = 28; |
| 597 | public final static int DATA1 = 29; |
| 598 | public final static int DATA2 = 30; |
| 599 | public final static int DATA3 = 31; |
| 600 | public final static int DATA4 = 32; |
| 601 | public final static int DATA5 = 33; |
| 602 | public final static int DATA6 = 34; |
| 603 | public final static int DATA7 = 35; |
| 604 | public final static int DATA8 = 36; |
| 605 | public final static int DATA9 = 37; |
| 606 | public final static int DATA10 = 38; |
| 607 | public final static int DATA11 = 39; |
| 608 | public final static int DATA12 = 40; |
| 609 | public final static int DATA13 = 41; |
| 610 | public final static int DATA14 = 42; |
| 611 | public final static int DATA15 = 43; |
| 612 | public final static int DATA_SYNC1 = 44; |
| 613 | public final static int DATA_SYNC2 = 45; |
| 614 | public final static int DATA_SYNC3 = 46; |
| 615 | public final static int DATA_SYNC4 = 47; |
| 616 | public final static int DATA_VERSION = 48; |
| 617 | public final static int IS_PRIMARY = 49; |
| 618 | public final static int IS_SUPERPRIMARY = 50; |
| 619 | public final static int MIMETYPE = 51; |
| 620 | public final static int RES_PACKAGE = 52; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 621 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 622 | public final static int GROUP_SOURCE_ID = 53; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 623 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 624 | public final static int PRESENCE = 54; |
| 625 | public final static int CHAT_CAPABILITY = 55; |
| 626 | public final static int STATUS = 56; |
| 627 | public final static int STATUS_RES_PACKAGE = 57; |
| 628 | public final static int STATUS_ICON = 58; |
| 629 | public final static int STATUS_LABEL = 59; |
| 630 | public final static int STATUS_TIMESTAMP = 60; |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 631 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 632 | public final static int PHOTO_URI = 61; |
| 633 | public final static int SEND_TO_VOICEMAIL = 62; |
| 634 | public final static int CUSTOM_RINGTONE = 63; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 635 | public final static int IS_USER_PROFILE = 64; |
Daniel Lehmann | d3e0cdb | 2010-04-19 13:45:53 -0700 | [diff] [blame] | 636 | } |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 637 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 638 | /** |
| 639 | * Projection used for the query that loads all data for the entire contact. |
| 640 | */ |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 641 | private static class DirectoryQuery { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 642 | final static String[] COLUMNS = new String[] { |
| 643 | Directory.DISPLAY_NAME, |
| 644 | Directory.PACKAGE_NAME, |
| 645 | Directory.TYPE_RESOURCE_ID, |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 646 | Directory.ACCOUNT_TYPE, |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 647 | Directory.ACCOUNT_NAME, |
| 648 | Directory.EXPORT_SUPPORT, |
| 649 | }; |
| 650 | |
| 651 | public final static int DISPLAY_NAME = 0; |
| 652 | public final static int PACKAGE_NAME = 1; |
| 653 | public final static int TYPE_RESOURCE_ID = 2; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 654 | public final static int ACCOUNT_TYPE = 3; |
| 655 | public final static int ACCOUNT_NAME = 4; |
| 656 | public final static int EXPORT_SUPPORT = 5; |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 659 | private static class GroupQuery { |
| 660 | final static String[] COLUMNS = new String[] { |
| 661 | Groups.ACCOUNT_NAME, |
| 662 | Groups.ACCOUNT_TYPE, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 663 | Groups.DATA_SET, |
| 664 | Groups.ACCOUNT_TYPE_AND_DATA_SET, |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 665 | Groups._ID, |
| 666 | Groups.TITLE, |
| 667 | Groups.AUTO_ADD, |
| 668 | Groups.FAVORITES, |
| 669 | }; |
| 670 | |
| 671 | public final static int ACCOUNT_NAME = 0; |
| 672 | public final static int ACCOUNT_TYPE = 1; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 673 | public final static int DATA_SET = 2; |
| 674 | public final static int ACCOUNT_TYPE_AND_DATA_SET = 3; |
| 675 | public final static int ID = 4; |
| 676 | public final static int TITLE = 5; |
| 677 | public final static int AUTO_ADD = 6; |
| 678 | public final static int FAVORITES = 7; |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 681 | private final class LoadContactTask extends AsyncTask<Void, Void, Result> { |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 682 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 683 | @Override |
| 684 | protected Result doInBackground(Void... args) { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 685 | try { |
| 686 | final ContentResolver resolver = getContext().getContentResolver(); |
Katherine Kuan | 6cd5b0a | 2011-09-16 11:46:01 -0700 | [diff] [blame] | 687 | final Uri uriCurrentFormat = ContactLoaderUtils.ensureIsContactUri( |
| 688 | resolver, mLookupUri); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 689 | Result result = loadContactEntity(resolver, uriCurrentFormat); |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 690 | if (!result.isNotFound()) { |
Dmitri Plotnikov | 217245c | 2010-09-18 13:04:50 -0700 | [diff] [blame] | 691 | if (result.isDirectoryEntry()) { |
| 692 | loadDirectoryMetaData(result); |
| 693 | } else if (mLoadGroupMetaData) { |
| 694 | loadGroupMetaData(result); |
| 695 | } |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 696 | if (mLoadStreamItems) { |
| 697 | loadStreamItems(result); |
| 698 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 699 | loadPhotoBinaryData(result); |
Makoto Onuki | 870a87e | 2011-08-12 13:40:31 -0700 | [diff] [blame] | 700 | |
| 701 | // Note ME profile should never have "Add connection" |
| 702 | if (mLoadInvitableAccountTypes && !result.isUserProfile()) { |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 703 | loadInvitableAccountTypes(result); |
| 704 | } |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 705 | } |
| 706 | return result; |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 707 | } catch (Exception e) { |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 708 | Log.e(TAG, "Error loading the contact: " + mLookupUri, e); |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 709 | return Result.forError(mRequestedUri, e); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 710 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 713 | private Result loadContactEntity(ContentResolver resolver, Uri contactUri) { |
| 714 | Uri entityUri = Uri.withAppendedPath(contactUri, Contacts.Entity.CONTENT_DIRECTORY); |
| 715 | Cursor cursor = resolver.query(entityUri, ContactQuery.COLUMNS, null, null, |
| 716 | Contacts.Entity.RAW_CONTACT_ID); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 717 | if (cursor == null) { |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 718 | Log.e(TAG, "No cursor returned in loadContactEntity"); |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 719 | return Result.forNotFound(mRequestedUri); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 720 | } |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 721 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 722 | try { |
| 723 | if (!cursor.moveToFirst()) { |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 724 | cursor.close(); |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 725 | return Result.forNotFound(mRequestedUri); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 728 | long currentRawContactId = -1; |
| 729 | Entity entity = null; |
| 730 | Result result = loadContactHeaderData(cursor, contactUri); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 731 | ArrayList<Entity> entities = result.getEntities(); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 732 | HashMap<Long, DataStatus> statuses = result.getStatuses(); |
| 733 | for (; !cursor.isAfterLast(); cursor.moveToNext()) { |
| 734 | long rawContactId = cursor.getLong(ContactQuery.RAW_CONTACT_ID); |
| 735 | if (rawContactId != currentRawContactId) { |
| 736 | currentRawContactId = rawContactId; |
| 737 | entity = new android.content.Entity(loadRawContact(cursor)); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 738 | entities.add(entity); |
| 739 | } |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 740 | if (!cursor.isNull(ContactQuery.DATA_ID)) { |
| 741 | ContentValues data = loadData(cursor); |
| 742 | entity.addSubValue(ContactsContract.Data.CONTENT_URI, data); |
| 743 | |
| 744 | if (!cursor.isNull(ContactQuery.PRESENCE) |
| 745 | || !cursor.isNull(ContactQuery.STATUS)) { |
| 746 | final DataStatus status = new DataStatus(cursor); |
| 747 | final long dataId = cursor.getLong(ContactQuery.DATA_ID); |
| 748 | statuses.put(dataId, status); |
| 749 | } |
| 750 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 751 | } |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 752 | |
| 753 | return result; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 754 | } finally { |
| 755 | cursor.close(); |
| 756 | } |
| 757 | } |
| 758 | |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 759 | /** |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 760 | * Looks for the photo data item in entities. If found, creates a new Bitmap instance. If |
| 761 | * not found, returns null |
| 762 | */ |
| 763 | private void loadPhotoBinaryData(Result contactData) { |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 764 | |
| 765 | // If we have a photo URI, try loading that first. |
| 766 | String photoUri = contactData.getPhotoUri(); |
| 767 | if (photoUri != null) { |
| 768 | try { |
| 769 | AssetFileDescriptor fd = getContext().getContentResolver() |
| 770 | .openAssetFileDescriptor(Uri.parse(photoUri), "r"); |
| 771 | byte[] buffer = new byte[16 * 1024]; |
| 772 | FileInputStream fis = fd.createInputStream(); |
| 773 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 774 | try { |
| 775 | int size; |
| 776 | while ((size = fis.read(buffer)) != -1) { |
| 777 | baos.write(buffer, 0, size); |
| 778 | } |
| 779 | contactData.setPhotoBinaryData(baos.toByteArray()); |
| 780 | } finally { |
| 781 | fis.close(); |
| 782 | fd.close(); |
| 783 | } |
| 784 | return; |
| 785 | } catch (IOException ioe) { |
| 786 | // Just fall back to the case below. |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | // If we couldn't load from a file, fall back to the data blob. |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 791 | final long photoId = contactData.getPhotoId(); |
| 792 | if (photoId <= 0) { |
| 793 | // No photo ID |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | for (Entity entity : contactData.getEntities()) { |
| 798 | for (NamedContentValues subValue : entity.getSubValues()) { |
| 799 | final ContentValues entryValues = subValue.values; |
| 800 | final long dataId = entryValues.getAsLong(Data._ID); |
| 801 | if (dataId == photoId) { |
| 802 | final String mimeType = entryValues.getAsString(Data.MIMETYPE); |
| 803 | // Correct Data Id but incorrect MimeType? Don't load |
| 804 | if (!Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 805 | return; |
| 806 | } |
| 807 | contactData.setPhotoBinaryData(entryValues.getAsByteArray(Photo.PHOTO)); |
| 808 | break; |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
Makoto Onuki | 3e6991e | 2011-07-24 14:51:20 -0700 | [diff] [blame] | 814 | /** |
| 815 | * Sets the "invitable" account types to {@link Result#mInvitableAccountTypes}. |
Makoto Onuki | 3e6991e | 2011-07-24 14:51:20 -0700 | [diff] [blame] | 816 | */ |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 817 | private void loadInvitableAccountTypes(Result contactData) { |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 818 | Map<AccountTypeWithDataSet, AccountType> invitables = |
| 819 | AccountTypeManager.getInstance(getContext()).getUsableInvitableAccountTypes(); |
| 820 | if (invitables.isEmpty()) { |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 821 | return; |
| 822 | } |
| 823 | |
Katherine Kuan | 08bcf71 | 2011-10-09 13:43:53 -0700 | [diff] [blame] | 824 | HashMap<AccountTypeWithDataSet, AccountType> result = Maps.newHashMap(invitables); |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 825 | |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 826 | // Remove the ones that already have a raw contact in the current contact |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 827 | for (Entity entity : contactData.getEntities()) { |
Makoto Onuki | 6ad227f | 2011-08-15 13:46:59 -0700 | [diff] [blame] | 828 | final ContentValues values = entity.getEntityValues(); |
| 829 | final AccountTypeWithDataSet type = AccountTypeWithDataSet.get( |
| 830 | values.getAsString(RawContacts.ACCOUNT_TYPE), |
| 831 | values.getAsString(RawContacts.DATA_SET)); |
| 832 | result.remove(type); |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | // Set to mInvitableAccountTypes |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 836 | contactData.mInvitableAccountTypes.addAll(result.values()); |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 839 | /** |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 840 | * Extracts Contact level columns from the cursor. |
| 841 | */ |
| 842 | private Result loadContactHeaderData(final Cursor cursor, Uri contactUri) { |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 843 | final String directoryParameter = |
| 844 | contactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY); |
| 845 | final long directoryId = directoryParameter == null |
| 846 | ? Directory.DEFAULT |
| 847 | : Long.parseLong(directoryParameter); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 848 | final long contactId = cursor.getLong(ContactQuery.CONTACT_ID); |
| 849 | final String lookupKey = cursor.getString(ContactQuery.LOOKUP_KEY); |
| 850 | final long nameRawContactId = cursor.getLong(ContactQuery.NAME_RAW_CONTACT_ID); |
| 851 | final int displayNameSource = cursor.getInt(ContactQuery.DISPLAY_NAME_SOURCE); |
| 852 | final String displayName = cursor.getString(ContactQuery.DISPLAY_NAME); |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 853 | final String altDisplayName = cursor.getString(ContactQuery.ALT_DISPLAY_NAME); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 854 | final String phoneticName = cursor.getString(ContactQuery.PHONETIC_NAME); |
| 855 | final long photoId = cursor.getLong(ContactQuery.PHOTO_ID); |
Dmitri Plotnikov | f9eb73f | 2010-10-21 11:48:56 -0700 | [diff] [blame] | 856 | final String photoUri = cursor.getString(ContactQuery.PHOTO_URI); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 857 | final boolean starred = cursor.getInt(ContactQuery.STARRED) != 0; |
| 858 | final Integer presence = cursor.isNull(ContactQuery.CONTACT_PRESENCE) |
| 859 | ? null |
| 860 | : cursor.getInt(ContactQuery.CONTACT_PRESENCE); |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 861 | final boolean sendToVoicemail = cursor.getInt(ContactQuery.SEND_TO_VOICEMAIL) == 1; |
| 862 | final String customRingtone = cursor.getString(ContactQuery.CUSTOM_RINGTONE); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 863 | final boolean isUserProfile = cursor.getInt(ContactQuery.IS_USER_PROFILE) == 1; |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 864 | |
Dmitri Plotnikov | 1536ea1 | 2010-10-29 11:51:05 -0700 | [diff] [blame] | 865 | Uri lookupUri; |
| 866 | if (directoryId == Directory.DEFAULT || directoryId == Directory.LOCAL_INVISIBLE) { |
| 867 | lookupUri = ContentUris.withAppendedId( |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 868 | Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey), contactId); |
Dmitri Plotnikov | 1536ea1 | 2010-10-29 11:51:05 -0700 | [diff] [blame] | 869 | } else { |
| 870 | lookupUri = contactUri; |
| 871 | } |
| 872 | |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 873 | return new Result(mRequestedUri, contactUri, lookupUri, directoryId, lookupKey, |
| 874 | contactId, nameRawContactId, displayNameSource, photoId, photoUri, displayName, |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 875 | altDisplayName, phoneticName, starred, presence, sendToVoicemail, |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 876 | customRingtone, isUserProfile); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Extracts RawContact level columns from the cursor. |
| 881 | */ |
| 882 | private ContentValues loadRawContact(Cursor cursor) { |
| 883 | ContentValues cv = new ContentValues(); |
| 884 | |
| 885 | cv.put(RawContacts._ID, cursor.getLong(ContactQuery.RAW_CONTACT_ID)); |
| 886 | |
| 887 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_NAME); |
| 888 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 889 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SET); |
| 890 | cursorColumnToContentValues(cursor, cv, ContactQuery.ACCOUNT_TYPE_AND_DATA_SET); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 891 | cursorColumnToContentValues(cursor, cv, ContactQuery.DIRTY); |
| 892 | cursorColumnToContentValues(cursor, cv, ContactQuery.VERSION); |
| 893 | cursorColumnToContentValues(cursor, cv, ContactQuery.SOURCE_ID); |
| 894 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC1); |
| 895 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC2); |
| 896 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC3); |
| 897 | cursorColumnToContentValues(cursor, cv, ContactQuery.SYNC4); |
| 898 | cursorColumnToContentValues(cursor, cv, ContactQuery.DELETED); |
| 899 | cursorColumnToContentValues(cursor, cv, ContactQuery.CONTACT_ID); |
| 900 | cursorColumnToContentValues(cursor, cv, ContactQuery.STARRED); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 901 | cursorColumnToContentValues(cursor, cv, ContactQuery.NAME_VERIFIED); |
| 902 | |
| 903 | return cv; |
| 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Extracts Data level columns from the cursor. |
| 908 | */ |
| 909 | private ContentValues loadData(Cursor cursor) { |
| 910 | ContentValues cv = new ContentValues(); |
| 911 | |
| 912 | cv.put(Data._ID, cursor.getLong(ContactQuery.DATA_ID)); |
| 913 | |
| 914 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA1); |
| 915 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA2); |
| 916 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA3); |
| 917 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA4); |
| 918 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA5); |
| 919 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA6); |
| 920 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA7); |
| 921 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA8); |
| 922 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA9); |
| 923 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA10); |
| 924 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA11); |
| 925 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA12); |
| 926 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA13); |
| 927 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA14); |
| 928 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA15); |
| 929 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC1); |
| 930 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC2); |
| 931 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC3); |
| 932 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_SYNC4); |
| 933 | cursorColumnToContentValues(cursor, cv, ContactQuery.DATA_VERSION); |
| 934 | cursorColumnToContentValues(cursor, cv, ContactQuery.IS_PRIMARY); |
| 935 | cursorColumnToContentValues(cursor, cv, ContactQuery.IS_SUPERPRIMARY); |
| 936 | cursorColumnToContentValues(cursor, cv, ContactQuery.MIMETYPE); |
| 937 | cursorColumnToContentValues(cursor, cv, ContactQuery.RES_PACKAGE); |
| 938 | cursorColumnToContentValues(cursor, cv, ContactQuery.GROUP_SOURCE_ID); |
Daniel Lehmann | 8fd7bb6 | 2010-08-13 20:50:31 -0700 | [diff] [blame] | 939 | cursorColumnToContentValues(cursor, cv, ContactQuery.CHAT_CAPABILITY); |
Dmitri Plotnikov | 4d44424 | 2010-07-30 11:39:39 -0700 | [diff] [blame] | 940 | |
| 941 | return cv; |
| 942 | } |
| 943 | |
| 944 | private void cursorColumnToContentValues( |
| 945 | Cursor cursor, ContentValues values, int index) { |
| 946 | switch (cursor.getType(index)) { |
| 947 | case Cursor.FIELD_TYPE_NULL: |
| 948 | // don't put anything in the content values |
| 949 | break; |
| 950 | case Cursor.FIELD_TYPE_INTEGER: |
| 951 | values.put(ContactQuery.COLUMNS[index], cursor.getLong(index)); |
| 952 | break; |
| 953 | case Cursor.FIELD_TYPE_STRING: |
| 954 | values.put(ContactQuery.COLUMNS[index], cursor.getString(index)); |
| 955 | break; |
| 956 | case Cursor.FIELD_TYPE_BLOB: |
| 957 | values.put(ContactQuery.COLUMNS[index], cursor.getBlob(index)); |
| 958 | break; |
| 959 | default: |
| 960 | throw new IllegalStateException("Invalid or unhandled data type"); |
| 961 | } |
| 962 | } |
| 963 | |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 964 | private void loadDirectoryMetaData(Result result) { |
| 965 | long directoryId = result.getDirectoryId(); |
| 966 | |
| 967 | Cursor cursor = getContext().getContentResolver().query( |
| 968 | ContentUris.withAppendedId(Directory.CONTENT_URI, directoryId), |
| 969 | DirectoryQuery.COLUMNS, null, null, null); |
| 970 | if (cursor == null) { |
| 971 | return; |
| 972 | } |
| 973 | try { |
| 974 | if (cursor.moveToFirst()) { |
| 975 | final String displayName = cursor.getString(DirectoryQuery.DISPLAY_NAME); |
| 976 | final String packageName = cursor.getString(DirectoryQuery.PACKAGE_NAME); |
| 977 | final int typeResourceId = cursor.getInt(DirectoryQuery.TYPE_RESOURCE_ID); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 978 | final String accountType = cursor.getString(DirectoryQuery.ACCOUNT_TYPE); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 979 | final String accountName = cursor.getString(DirectoryQuery.ACCOUNT_NAME); |
| 980 | final int exportSupport = cursor.getInt(DirectoryQuery.EXPORT_SUPPORT); |
| 981 | String directoryType = null; |
| 982 | if (!TextUtils.isEmpty(packageName)) { |
| 983 | PackageManager pm = getContext().getPackageManager(); |
| 984 | try { |
| 985 | Resources resources = pm.getResourcesForApplication(packageName); |
| 986 | directoryType = resources.getString(typeResourceId); |
| 987 | } catch (NameNotFoundException e) { |
| 988 | Log.w(TAG, "Contact directory resource not found: " |
| 989 | + packageName + "." + typeResourceId); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | result.setDirectoryMetaData( |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 994 | displayName, directoryType, accountType, accountName, exportSupport); |
Dmitri Plotnikov | 02cd491 | 2010-09-01 20:42:17 -0700 | [diff] [blame] | 995 | } |
| 996 | } finally { |
| 997 | cursor.close(); |
| 998 | } |
| 999 | } |
| 1000 | |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 1001 | /** |
| 1002 | * Loads groups meta-data for all groups associated with all constituent raw contacts' |
| 1003 | * accounts. |
| 1004 | */ |
| 1005 | private void loadGroupMetaData(Result result) { |
| 1006 | StringBuilder selection = new StringBuilder(); |
| 1007 | ArrayList<String> selectionArgs = new ArrayList<String>(); |
| 1008 | for (Entity entity : result.mEntities) { |
| 1009 | ContentValues values = entity.getEntityValues(); |
| 1010 | String accountName = values.getAsString(RawContacts.ACCOUNT_NAME); |
| 1011 | String accountType = values.getAsString(RawContacts.ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 1012 | String dataSet = values.getAsString(RawContacts.DATA_SET); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 1013 | if (accountName != null && accountType != null) { |
| 1014 | if (selection.length() != 0) { |
| 1015 | selection.append(" OR "); |
| 1016 | } |
| 1017 | selection.append( |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 1018 | "(" + Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=?"); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 1019 | selectionArgs.add(accountName); |
| 1020 | selectionArgs.add(accountType); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 1021 | |
| 1022 | if (dataSet != null) { |
| 1023 | selection.append(" AND " + Groups.DATA_SET + "=?"); |
| 1024 | selectionArgs.add(dataSet); |
| 1025 | } else { |
| 1026 | selection.append(" AND " + Groups.DATA_SET + " IS NULL"); |
| 1027 | } |
| 1028 | selection.append(")"); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | Cursor cursor = getContext().getContentResolver().query(Groups.CONTENT_URI, |
| 1032 | GroupQuery.COLUMNS, selection.toString(), selectionArgs.toArray(new String[0]), |
| 1033 | null); |
| 1034 | try { |
| 1035 | while (cursor.moveToNext()) { |
| 1036 | final String accountName = cursor.getString(GroupQuery.ACCOUNT_NAME); |
| 1037 | final String accountType = cursor.getString(GroupQuery.ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 1038 | final String dataSet = cursor.getString(GroupQuery.DATA_SET); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 1039 | final long groupId = cursor.getLong(GroupQuery.ID); |
| 1040 | final String title = cursor.getString(GroupQuery.TITLE); |
| 1041 | final boolean defaultGroup = cursor.isNull(GroupQuery.AUTO_ADD) |
| 1042 | ? false |
| 1043 | : cursor.getInt(GroupQuery.AUTO_ADD) != 0; |
| 1044 | final boolean favorites = cursor.isNull(GroupQuery.FAVORITES) |
| 1045 | ? false |
| 1046 | : cursor.getInt(GroupQuery.FAVORITES) != 0; |
| 1047 | |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 1048 | result.addGroupMetaData(new GroupMetaData( |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 1049 | accountName, accountType, dataSet, groupId, title, defaultGroup, |
| 1050 | favorites)); |
Dmitri Plotnikov | 2deaee1 | 2010-09-15 15:42:08 -0700 | [diff] [blame] | 1051 | } |
| 1052 | } finally { |
| 1053 | cursor.close(); |
| 1054 | } |
| 1055 | } |
| 1056 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1057 | /** |
| 1058 | * Loads all stream items and stream item photos belonging to this contact. |
| 1059 | */ |
| 1060 | private void loadStreamItems(Result result) { |
| 1061 | Cursor cursor = getContext().getContentResolver().query( |
| 1062 | Contacts.CONTENT_LOOKUP_URI.buildUpon() |
| 1063 | .appendPath(result.getLookupKey()) |
| 1064 | .appendPath(Contacts.StreamItems.CONTENT_DIRECTORY).build(), |
| 1065 | null, null, null, null); |
| 1066 | Map<Long, StreamItemEntry> streamItemsById = new HashMap<Long, StreamItemEntry>(); |
| 1067 | ArrayList<StreamItemEntry> streamItems = new ArrayList<StreamItemEntry>(); |
| 1068 | try { |
| 1069 | while (cursor.moveToNext()) { |
| 1070 | StreamItemEntry streamItem = new StreamItemEntry(cursor); |
| 1071 | streamItemsById.put(streamItem.getId(), streamItem); |
| 1072 | streamItems.add(streamItem); |
| 1073 | } |
| 1074 | } finally { |
| 1075 | cursor.close(); |
| 1076 | } |
| 1077 | |
| 1078 | // Now retrieve any photo records associated with the stream items. |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1079 | if (!streamItems.isEmpty()) { |
Dave Santoro | a4400d5 | 2011-09-02 16:14:53 -0700 | [diff] [blame] | 1080 | if (result.isUserProfile()) { |
| 1081 | // If the stream items we're loading are for the profile, we can't bulk-load the |
| 1082 | // stream items with a custom selection. |
| 1083 | for (StreamItemEntry entry : streamItems) { |
| 1084 | Cursor siCursor = getContext().getContentResolver().query( |
| 1085 | Uri.withAppendedPath( |
| 1086 | ContentUris.withAppendedId( |
| 1087 | StreamItems.CONTENT_URI, entry.getId()), |
| 1088 | StreamItems.StreamItemPhotos.CONTENT_DIRECTORY), |
| 1089 | null, null, null, null); |
| 1090 | try { |
| 1091 | while (siCursor.moveToNext()) { |
| 1092 | entry.addPhoto(new StreamItemPhotoEntry(siCursor)); |
| 1093 | } |
| 1094 | } finally { |
| 1095 | siCursor.close(); |
| 1096 | } |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1097 | } |
Dave Santoro | a4400d5 | 2011-09-02 16:14:53 -0700 | [diff] [blame] | 1098 | } else { |
| 1099 | String[] streamItemIdArr = new String[streamItems.size()]; |
| 1100 | StringBuilder streamItemPhotoSelection = new StringBuilder(); |
| 1101 | streamItemPhotoSelection.append(StreamItemPhotos.STREAM_ITEM_ID + " IN ("); |
| 1102 | for (int i = 0; i < streamItems.size(); i++) { |
| 1103 | if (i > 0) { |
| 1104 | streamItemPhotoSelection.append(","); |
| 1105 | } |
| 1106 | streamItemPhotoSelection.append("?"); |
| 1107 | streamItemIdArr[i] = String.valueOf(streamItems.get(i).getId()); |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1108 | } |
Dave Santoro | a4400d5 | 2011-09-02 16:14:53 -0700 | [diff] [blame] | 1109 | streamItemPhotoSelection.append(")"); |
| 1110 | Cursor sipCursor = getContext().getContentResolver().query( |
| 1111 | StreamItems.CONTENT_PHOTO_URI, |
| 1112 | null, streamItemPhotoSelection.toString(), streamItemIdArr, |
| 1113 | StreamItemPhotos.STREAM_ITEM_ID); |
| 1114 | try { |
| 1115 | while (sipCursor.moveToNext()) { |
| 1116 | long streamItemId = sipCursor.getLong( |
| 1117 | sipCursor.getColumnIndex(StreamItemPhotos.STREAM_ITEM_ID)); |
| 1118 | StreamItemEntry streamItem = streamItemsById.get(streamItemId); |
| 1119 | streamItem.addPhoto(new StreamItemPhotoEntry(sipCursor)); |
| 1120 | } |
| 1121 | } finally { |
| 1122 | sipCursor.close(); |
| 1123 | } |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | // Set the sorted stream items on the result. |
| 1128 | Collections.sort(streamItems); |
| 1129 | result.mStreamItems.addAll(streamItems); |
| 1130 | } |
| 1131 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1132 | @Override |
| 1133 | protected void onPostExecute(Result result) { |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1134 | unregisterObserver(); |
| 1135 | |
Daniel Lehmann | 1316b13 | 2010-04-13 15:08:53 -0700 | [diff] [blame] | 1136 | // The creator isn't interested in any further updates |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1137 | if (mDestroyed || result == null) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | mContact = result; |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1142 | |
Makoto Onuki | 2621c5b | 2011-10-03 12:56:16 -0700 | [diff] [blame] | 1143 | if (result.isLoaded()) { |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1144 | mLookupUri = result.getLookupUri(); |
| 1145 | |
| 1146 | if (!result.isDirectoryEntry()) { |
| 1147 | Log.i(TAG, "Registering content observer for " + mLookupUri); |
| 1148 | if (mObserver == null) { |
| 1149 | mObserver = new ForceLoadContentObserver(); |
| 1150 | } |
| 1151 | getContext().getContentResolver().registerContentObserver( |
| 1152 | mLookupUri, true, mObserver); |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1153 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1154 | |
| 1155 | if (mContact.getPhotoBinaryData() == null && mContact.getPhotoUri() != null) { |
| 1156 | mContact.setLoadingPhoto(true); |
| 1157 | new AsyncPhotoLoader().execute(mContact.getPhotoUri()); |
| 1158 | } |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1159 | |
| 1160 | // inform the source of the data that this contact is being looked at |
| 1161 | postViewNotificationToSyncAdapter(); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1162 | } |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1163 | |
| 1164 | deliverResult(mContact); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1165 | } |
| 1166 | } |
| 1167 | |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1168 | /** |
| 1169 | * Posts a message to the contributing sync adapters that have opted-in, notifying them |
| 1170 | * that the contact has just been loaded |
| 1171 | */ |
| 1172 | private void postViewNotificationToSyncAdapter() { |
| 1173 | Context context = getContext(); |
| 1174 | for (Entity entity : mContact.getEntities()) { |
| 1175 | final ContentValues entityValues = entity.getEntityValues(); |
Makoto Onuki | aba2b83 | 2011-08-12 15:44:53 -0700 | [diff] [blame] | 1176 | final long rawContactId = entityValues.getAsLong(RawContacts.Entity._ID); |
| 1177 | if (mNotifiedRawContactIds.contains(rawContactId)) { |
| 1178 | continue; // Already notified for this raw contact. |
| 1179 | } |
| 1180 | mNotifiedRawContactIds.add(rawContactId); |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1181 | final String type = entityValues.getAsString(RawContacts.ACCOUNT_TYPE); |
| 1182 | final String dataSet = entityValues.getAsString(RawContacts.DATA_SET); |
Flavio Lerda | 59a887e | 2011-08-14 18:13:17 +0100 | [diff] [blame] | 1183 | final AccountType accountType = AccountTypeManager.getInstance(context).getAccountType( |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1184 | type, dataSet); |
| 1185 | final String serviceName = accountType.getViewContactNotifyServiceClassName(); |
| 1186 | final String resPackageName = accountType.resPackageName; |
| 1187 | if (!TextUtils.isEmpty(serviceName) && !TextUtils.isEmpty(resPackageName)) { |
Daniel Lehmann | 3ef27fb | 2011-08-09 14:31:29 -0700 | [diff] [blame] | 1188 | final Uri uri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId); |
| 1189 | final Intent intent = new Intent(); |
| 1190 | intent.setClassName(resPackageName, serviceName); |
| 1191 | intent.setAction(Intent.ACTION_VIEW); |
| 1192 | intent.setDataAndType(uri, RawContacts.CONTENT_ITEM_TYPE); |
| 1193 | try { |
| 1194 | context.startService(intent); |
| 1195 | } catch (Exception e) { |
| 1196 | Log.e(TAG, "Error sending message to source-app", e); |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1202 | private class AsyncPhotoLoader extends AsyncTask<String, Void, byte[]> { |
| 1203 | |
| 1204 | private static final int BUFFER_SIZE = 1024*16; |
| 1205 | |
| 1206 | @Override |
| 1207 | protected byte[] doInBackground(String... params) { |
| 1208 | Uri uri = Uri.parse(params[0]); |
| 1209 | byte[] data = null; |
| 1210 | try { |
| 1211 | InputStream is = getContext().getContentResolver().openInputStream(uri); |
| 1212 | if (is != null) { |
| 1213 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 1214 | try { |
| 1215 | byte[] mBuffer = new byte[BUFFER_SIZE]; |
| 1216 | |
| 1217 | int size; |
| 1218 | while ((size = is.read(mBuffer)) != -1) { |
| 1219 | baos.write(mBuffer, 0, size); |
| 1220 | } |
| 1221 | data = baos.toByteArray(); |
| 1222 | } finally { |
| 1223 | is.close(); |
| 1224 | } |
| 1225 | } else { |
| 1226 | Log.v(TAG, "Cannot load photo " + uri); |
| 1227 | } |
| 1228 | } catch (IOException e) { |
| 1229 | Log.e(TAG, "Cannot load photo " + uri, e); |
| 1230 | } |
| 1231 | |
| 1232 | return data; |
| 1233 | } |
| 1234 | |
| 1235 | @Override |
| 1236 | protected void onPostExecute(byte[] data) { |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1237 | if (mContact != null) { |
Dmitri Plotnikov | 7cee774 | 2011-01-13 17:11:06 -0800 | [diff] [blame] | 1238 | mContact = new Result(mContact); |
Dmitri Plotnikov | c3f2a52 | 2010-11-17 18:36:17 -0800 | [diff] [blame] | 1239 | mContact.setPhotoBinaryData(data); |
| 1240 | mContact.setLoadingPhoto(false); |
| 1241 | deliverResult(mContact); |
| 1242 | } |
Dmitri Plotnikov | 7f4f8d1 | 2010-11-10 10:22:19 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | |
Daniel Lehmann | 3a12077 | 2010-06-21 16:21:35 -0700 | [diff] [blame] | 1246 | private void unregisterObserver() { |
| 1247 | if (mObserver != null) { |
| 1248 | getContext().getContentResolver().unregisterContentObserver(mObserver); |
| 1249 | mObserver = null; |
| 1250 | } |
| 1251 | } |
| 1252 | |
Daniel Lehmann | cdef2b6 | 2010-06-06 18:25:49 -0700 | [diff] [blame] | 1253 | public ContactLoader(Context context, Uri lookupUri) { |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 1254 | this(context, lookupUri, false, false, false); |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1257 | public ContactLoader(Context context, Uri lookupUri, boolean loadGroupMetaData, |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 1258 | boolean loadStreamItems, boolean loadInvitableAccountTypes) { |
Daniel Lehmann | 74a2dc5 | 2010-04-15 16:52:33 -0700 | [diff] [blame] | 1259 | super(context); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1260 | mLookupUri = lookupUri; |
Daniel Lehmann | 685157e | 2011-08-29 21:07:01 -0700 | [diff] [blame] | 1261 | mRequestedUri = lookupUri; |
Dmitri Plotnikov | e843f91 | 2010-09-16 15:21:48 -0700 | [diff] [blame] | 1262 | mLoadGroupMetaData = loadGroupMetaData; |
Dave Santoro | 3915600 | 2011-07-19 01:18:14 -0700 | [diff] [blame] | 1263 | mLoadStreamItems = loadStreamItems; |
Makoto Onuki | 69b4a88 | 2011-07-22 10:05:10 -0700 | [diff] [blame] | 1264 | mLoadInvitableAccountTypes = loadInvitableAccountTypes; |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Dmitri Plotnikov | 5a30d9a | 2010-11-23 14:59:50 -0800 | [diff] [blame] | 1267 | public Uri getLookupUri() { |
| 1268 | return mLookupUri; |
| 1269 | } |
| 1270 | |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1271 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1272 | protected void onStartLoading() { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1273 | if (mContact != null) { |
Daniel Lehmann | cbcc449 | 2010-04-12 18:03:54 -0700 | [diff] [blame] | 1274 | deliverResult(mContact); |
Dmitri Plotnikov | 97e90c6 | 2011-01-03 11:58:13 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | if (takeContentChanged() || mContact == null) { |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1278 | forceLoad(); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1283 | protected void onForceLoad() { |
Daniel Lehmann | 18f104f | 2010-05-07 15:41:11 -0700 | [diff] [blame] | 1284 | final LoadContactTask task = new LoadContactTask(); |
Makoto Onuki | 73c341f | 2011-08-29 09:31:44 -0700 | [diff] [blame] | 1285 | task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null); |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | @Override |
Dianne Hackborn | c04fc27 | 2010-12-20 23:13:10 -0800 | [diff] [blame] | 1289 | protected void onReset() { |
Dianne Hackborn | 4ef95cc | 2010-12-16 00:44:33 -0800 | [diff] [blame] | 1290 | unregisterObserver(); |
| 1291 | mContact = null; |
| 1292 | mDestroyed = true; |
| 1293 | } |
Daniel Lehmann | 4cd9441 | 2010-04-08 16:44:36 -0700 | [diff] [blame] | 1294 | } |