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