Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 1 | package com.android.contacts.editor; |
| 2 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 3 | import android.content.AsyncTaskLoader; |
| 4 | import android.content.ContentResolver; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 5 | import android.content.Context; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 6 | import android.database.Cursor; |
| 7 | import android.net.Uri; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 8 | import android.os.Parcel; |
| 9 | import android.os.Parcelable; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 10 | import android.provider.ContactsContract; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 11 | import android.provider.ContactsContract.Contacts; |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 12 | import android.provider.ContactsContract.Data; |
| 13 | import android.provider.ContactsContract.Profile; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 14 | import android.provider.ContactsContract.RawContacts; |
| 15 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 16 | import com.android.contacts.model.AccountTypeManager; |
| 17 | import com.android.contacts.model.account.AccountType; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 18 | |
| 19 | import java.util.ArrayList; |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 22 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 23 | /** |
| 24 | * Loader for the pick a raw contact to edit activity. Loads all raw contact metadata for the |
| 25 | * given Contact {@link Uri}. |
| 26 | */ |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 27 | public class PickRawContactLoader extends |
| 28 | AsyncTaskLoader<PickRawContactLoader.RawContactsMetadata> { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 29 | private Uri mContactUri; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 30 | private RawContactsMetadata mCachedResult; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 31 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 32 | private static final String[] RAW_CONTACT_PROJECTION = new String[] { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 33 | RawContacts.ACCOUNT_NAME, |
| 34 | RawContacts.ACCOUNT_TYPE, |
| 35 | RawContacts.DATA_SET, |
| 36 | RawContacts._ID, |
| 37 | RawContacts.DISPLAY_NAME_PRIMARY, |
| 38 | RawContacts.DISPLAY_NAME_ALTERNATIVE |
| 39 | }; |
| 40 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 41 | private static final String RAW_CONTACT_SELECTION = RawContacts.CONTACT_ID + "=?"; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 42 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 43 | private static final int ACCOUNT_NAME = 0; |
| 44 | private static final int ACCOUNT_TYPE = 1; |
| 45 | private static final int DATA_SET = 2; |
| 46 | private static final int RAW_CONTACT_ID = 3; |
| 47 | private static final int DISPLAY_NAME_PRIMARY = 4; |
| 48 | private static final int DISPLAY_NAME_ALTERNATIVE = 5; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 49 | |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 50 | private static final String PHOTO_SELECTION_PREFIX = |
| 51 | ContactsContract.Data.RAW_CONTACT_ID + " IN ("; |
| 52 | private static final String PHOTO_SELECTION_SUFFIX = ") AND " + ContactsContract.Data.MIMETYPE |
| 53 | + "=\"" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "\""; |
| 54 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 55 | public PickRawContactLoader(Context context, Uri contactUri) { |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 56 | super(context); |
| 57 | mContactUri = ensureIsContactUri(contactUri); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 61 | public RawContactsMetadata loadInBackground() { |
| 62 | final ContentResolver resolver = getContext().getContentResolver(); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 63 | // Get the id of the contact we're looking at. |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 64 | final Cursor contactCursor = resolver.query( |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 65 | mContactUri, new String[] {Contacts._ID, Contacts.IS_USER_PROFILE}, null, |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 66 | null, null); |
| 67 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 68 | if (contactCursor == null) { |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 69 | return null; |
| 70 | } |
| 71 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 72 | if (contactCursor.getCount() < 1) { |
| 73 | contactCursor.close(); |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 74 | return null; |
| 75 | } |
| 76 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 77 | final RawContactsMetadata result = new RawContactsMetadata(); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 78 | try { |
| 79 | contactCursor.moveToFirst(); |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 80 | result.contactId = contactCursor.getLong(/* Contacts._ID */ 0); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 81 | result.isUserProfile = contactCursor.getInt(/* Contacts.IS_USER_PROFILE */ 1) == 1; |
| 82 | } finally { |
| 83 | contactCursor.close(); |
| 84 | } |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 85 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 86 | // Load RawContact data |
| 87 | final Uri rawContactUri; |
| 88 | if (result.isUserProfile) { |
| 89 | rawContactUri = ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 90 | } else { |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 91 | rawContactUri = RawContacts.CONTENT_URI; |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 92 | } |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 93 | |
| 94 | final Cursor rawContactCursor = resolver.query( |
| 95 | rawContactUri, RAW_CONTACT_PROJECTION, RAW_CONTACT_SELECTION, |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 96 | new String[] {Long.toString(result.contactId)}, null); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 97 | |
| 98 | if (rawContactCursor == null) { |
| 99 | return null; |
| 100 | } |
| 101 | |
Gary Mai | cea6f3d | 2016-12-09 11:32:31 -0800 | [diff] [blame] | 102 | if (rawContactCursor.getCount() < 1) { |
| 103 | rawContactCursor.close(); |
| 104 | return null; |
| 105 | } |
| 106 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 107 | rawContactCursor.moveToPosition(-1); |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 108 | final StringBuilder photoSelection = new StringBuilder(PHOTO_SELECTION_PREFIX); |
| 109 | final Map<Long, RawContact> rawContactMap = new HashMap<>(); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 110 | try { |
| 111 | while (rawContactCursor.moveToNext()) { |
| 112 | RawContact rawContact = new RawContact(); |
| 113 | rawContact.id = rawContactCursor.getLong(RAW_CONTACT_ID); |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 114 | photoSelection.append(rawContact.id).append(','); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 115 | rawContact.displayName = rawContactCursor.getString(DISPLAY_NAME_PRIMARY); |
| 116 | rawContact.displayNameAlt = rawContactCursor.getString(DISPLAY_NAME_ALTERNATIVE); |
| 117 | rawContact.accountName = rawContactCursor.getString(ACCOUNT_NAME); |
| 118 | rawContact.accountType = rawContactCursor.getString(ACCOUNT_TYPE); |
| 119 | rawContact.accountDataSet = rawContactCursor.getString(DATA_SET); |
| 120 | result.rawContacts.add(rawContact); |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 121 | rawContactMap.put(rawContact.id, rawContact); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 122 | } |
| 123 | } finally { |
| 124 | rawContactCursor.close(); |
| 125 | } |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 126 | |
| 127 | // Remove the last ',' |
| 128 | if (photoSelection.length() > 0) { |
| 129 | photoSelection.deleteCharAt(photoSelection.length() - 1); |
| 130 | } |
| 131 | photoSelection.append(PHOTO_SELECTION_SUFFIX); |
| 132 | |
| 133 | final Uri dataUri = result.isUserProfile |
| 134 | ? Uri.withAppendedPath(Profile.CONTENT_URI, Data.CONTENT_URI.getPath()) |
| 135 | : Data.CONTENT_URI; |
| 136 | final Cursor photoCursor = resolver.query( |
| 137 | dataUri, |
| 138 | new String[] {Data.RAW_CONTACT_ID, Contacts.Photo._ID}, |
| 139 | photoSelection.toString(), null, null); |
| 140 | |
| 141 | if (photoCursor != null) { |
| 142 | try { |
| 143 | photoCursor.moveToPosition(-1); |
| 144 | while (photoCursor.moveToNext()) { |
| 145 | final long rawContactId = photoCursor.getLong(/* Data.RAW_CONTACT_ID */ 0); |
| 146 | rawContactMap.get(rawContactId).photoId = |
| 147 | photoCursor.getLong(/* PHOTO._ID */ 1); |
| 148 | } |
| 149 | } finally { |
| 150 | photoCursor.close(); |
| 151 | } |
| 152 | } |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 153 | return result; |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 156 | @Override |
| 157 | public void deliverResult(RawContactsMetadata data) { |
| 158 | mCachedResult = data; |
| 159 | if (isStarted()) { |
| 160 | super.deliverResult(data); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | protected void onStartLoading() { |
| 166 | super.onStartLoading(); |
| 167 | if (mCachedResult == null) { |
| 168 | forceLoad(); |
| 169 | } else { |
| 170 | deliverResult(mCachedResult); |
| 171 | } |
Gary Mai | ba10be2 | 2016-10-12 17:41:15 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 174 | /** |
| 175 | * Ensures that this is a valid contact URI. If invalid, then an exception is |
| 176 | * thrown. Otherwise, the original URI is returned. |
| 177 | */ |
| 178 | private static Uri ensureIsContactUri(final Uri uri) { |
| 179 | if (uri == null) { |
| 180 | throw new IllegalArgumentException("Uri must not be null"); |
| 181 | } |
| 182 | if (!uri.toString().startsWith(Contacts.CONTENT_URI.toString())) { |
| 183 | throw new IllegalArgumentException("Invalid contact Uri: " + uri); |
| 184 | } |
| 185 | return uri; |
| 186 | } |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 187 | |
| 188 | public static class RawContactsMetadata implements Parcelable { |
| 189 | public static final Parcelable.Creator<RawContactsMetadata> CREATOR = |
| 190 | new Parcelable.Creator<RawContactsMetadata>() { |
| 191 | @Override |
| 192 | public RawContactsMetadata createFromParcel(Parcel source) { |
| 193 | return new RawContactsMetadata(source); |
| 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public RawContactsMetadata[] newArray(int size) { |
| 198 | return new RawContactsMetadata[size]; |
| 199 | } |
| 200 | }; |
| 201 | |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 202 | public long contactId; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 203 | public boolean isUserProfile; |
| 204 | public boolean showReadOnly = false; |
| 205 | public ArrayList<RawContact> rawContacts = new ArrayList<>(); |
| 206 | |
| 207 | public RawContactsMetadata() {} |
| 208 | |
| 209 | private RawContactsMetadata(Parcel in) { |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 210 | contactId = in.readLong(); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 211 | isUserProfile = in.readInt() == 1; |
| 212 | showReadOnly = in.readInt() == 1; |
| 213 | in.readTypedList(rawContacts, RawContact.CREATOR); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Removes all read-only raw contacts. |
| 218 | */ |
| 219 | public void trimReadOnly(AccountTypeManager accountManager) { |
| 220 | for (int i = rawContacts.size() - 1; i >= 0 ; i--) { |
| 221 | final RawContact rawContact = rawContacts.get(i); |
| 222 | final AccountType account = accountManager.getAccountType( |
| 223 | rawContact.accountType, rawContact.accountDataSet); |
| 224 | if (!account.areContactsWritable()) { |
| 225 | rawContacts.remove(i); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns the index of the first writable account in this contact or -1 if none exist. |
| 232 | */ |
| 233 | public int getIndexOfFirstWritableAccount(AccountTypeManager accountManager) { |
| 234 | for (int i = 0; i < rawContacts.size(); i++) { |
| 235 | final RawContact rawContact = rawContacts.get(i); |
| 236 | final AccountType account = accountManager.getAccountType( |
| 237 | rawContact.accountType, rawContact.accountDataSet); |
| 238 | if (account.areContactsWritable()) { |
| 239 | return i; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public int describeContents() { |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public void writeToParcel(Parcel dest, int flags) { |
Gary Mai | b9065dd | 2016-11-08 10:49:00 -0800 | [diff] [blame] | 253 | dest.writeLong(contactId); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 254 | dest.writeInt(isUserProfile ? 1 : 0); |
| 255 | dest.writeInt(showReadOnly ? 1 : 0); |
| 256 | dest.writeTypedList(rawContacts); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | public static class RawContact implements Parcelable { |
| 261 | public static final Parcelable.Creator<RawContact> CREATOR = |
| 262 | new Parcelable.Creator<RawContact>() { |
| 263 | @Override |
| 264 | public RawContact createFromParcel(Parcel source) { |
| 265 | return new RawContact(source); |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public RawContact[] newArray(int size) { |
| 270 | return new RawContact[size]; |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | public long id; |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 275 | public long photoId; |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 276 | public String displayName; |
| 277 | public String displayNameAlt; |
| 278 | public String accountName; |
| 279 | public String accountType; |
| 280 | public String accountDataSet; |
| 281 | |
| 282 | public RawContact() {} |
| 283 | |
| 284 | private RawContact(Parcel in) { |
| 285 | id = in.readLong(); |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 286 | photoId = in.readLong(); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 287 | displayName = in.readString(); |
| 288 | displayNameAlt = in.readString(); |
| 289 | accountName = in.readString(); |
| 290 | accountType = in.readString(); |
| 291 | accountDataSet = in.readString(); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public int describeContents() { |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public void writeToParcel(Parcel dest, int flags) { |
| 301 | dest.writeLong(id); |
Gary Mai | 3a53328 | 2016-11-08 19:02:26 +0000 | [diff] [blame] | 302 | dest.writeLong(photoId); |
Gary Mai | a172180 | 2016-11-01 17:55:31 -0700 | [diff] [blame] | 303 | dest.writeString(displayName); |
| 304 | dest.writeString(displayNameAlt); |
| 305 | dest.writeString(accountName); |
| 306 | dest.writeString(accountType); |
| 307 | dest.writeString(accountDataSet); |
| 308 | } |
| 309 | } |
Gary Mai | a6c80b3 | 2016-09-30 16:34:55 -0700 | [diff] [blame] | 310 | } |