Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 17 | package com.android.contacts; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 18 | |
Jay Shrauner | 615ed9c | 2015-07-29 11:27:56 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.WRITE_CONTACTS; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 20 | import android.app.Activity; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 21 | import android.app.IntentService; |
| 22 | import android.content.ContentProviderOperation; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 23 | import android.content.ContentProviderOperation.Builder; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 24 | import android.content.ContentProviderResult; |
| 25 | import android.content.ContentResolver; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 26 | import android.content.ContentUris; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 27 | import android.content.ContentValues; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 28 | import android.content.Context; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 29 | import android.content.Intent; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 30 | import android.content.OperationApplicationException; |
| 31 | import android.database.Cursor; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
Daniel Lehmann | c42ea4e | 2012-02-16 21:22:37 -0800 | [diff] [blame] | 33 | import android.os.Bundle; |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.Looper; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 36 | import android.os.Parcelable; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 37 | import android.os.RemoteException; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 38 | import android.provider.ContactsContract; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 39 | import android.provider.ContactsContract.AggregationExceptions; |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 40 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 41 | import android.provider.ContactsContract.CommonDataKinds.StructuredName; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 42 | import android.provider.ContactsContract.Contacts; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 43 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 44 | import android.provider.ContactsContract.Groups; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 45 | import android.provider.ContactsContract.Profile; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 46 | import android.provider.ContactsContract.RawContacts; |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 47 | import android.provider.ContactsContract.RawContactsEntity; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 48 | import android.util.Log; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 49 | import android.widget.Toast; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 50 | |
Wenyi Wang | dd7d456 | 2015-12-08 13:33:43 -0800 | [diff] [blame] | 51 | import com.android.contacts.activities.ContactEditorBaseActivity; |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 52 | import com.android.contacts.common.compat.CompatUtils; |
Chiao Cheng | d7ca03e | 2012-10-24 15:14:08 -0700 | [diff] [blame] | 53 | import com.android.contacts.common.database.ContactUpdateUtils; |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 54 | import com.android.contacts.common.model.AccountTypeManager; |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 55 | import com.android.contacts.common.model.CPOWrapper; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 56 | import com.android.contacts.common.model.RawContactDelta; |
| 57 | import com.android.contacts.common.model.RawContactDeltaList; |
| 58 | import com.android.contacts.common.model.RawContactModifier; |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 59 | import com.android.contacts.common.model.account.AccountType; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 60 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Jay Shrauner | 615ed9c | 2015-07-29 11:27:56 -0700 | [diff] [blame] | 61 | import com.android.contacts.common.util.PermissionsUtil; |
Wenyi Wang | aac0e66 | 2015-12-18 17:17:33 -0800 | [diff] [blame] | 62 | import com.android.contacts.compat.PinnedPositionsCompat; |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 63 | import com.android.contacts.util.ContactPhotoUtils; |
| 64 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 65 | import com.google.common.collect.Lists; |
| 66 | import com.google.common.collect.Sets; |
| 67 | |
Daniel Lehmann | c42ea4e | 2012-02-16 21:22:37 -0800 | [diff] [blame] | 68 | import java.util.ArrayList; |
| 69 | import java.util.HashSet; |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 70 | import java.util.Iterator; |
Daniel Lehmann | c42ea4e | 2012-02-16 21:22:37 -0800 | [diff] [blame] | 71 | import java.util.List; |
| 72 | import java.util.concurrent.CopyOnWriteArrayList; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 73 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 74 | /** |
| 75 | * A service responsible for saving changes to the content provider. |
| 76 | */ |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 77 | public class ContactSaveService extends IntentService { |
| 78 | private static final String TAG = "ContactSaveService"; |
| 79 | |
Katherine Kuan | a007e44 | 2011-07-07 09:25:34 -0700 | [diff] [blame] | 80 | /** Set to true in order to view logs on content provider operations */ |
| 81 | private static final boolean DEBUG = false; |
| 82 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 83 | public static final String ACTION_NEW_RAW_CONTACT = "newRawContact"; |
| 84 | |
| 85 | public static final String EXTRA_ACCOUNT_NAME = "accountName"; |
| 86 | public static final String EXTRA_ACCOUNT_TYPE = "accountType"; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 87 | public static final String EXTRA_DATA_SET = "dataSet"; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 88 | public static final String EXTRA_CONTENT_VALUES = "contentValues"; |
| 89 | public static final String EXTRA_CALLBACK_INTENT = "callbackIntent"; |
| 90 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 91 | public static final String ACTION_SAVE_CONTACT = "saveContact"; |
| 92 | public static final String EXTRA_CONTACT_STATE = "state"; |
| 93 | public static final String EXTRA_SAVE_MODE = "saveMode"; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 94 | public static final String EXTRA_SAVE_IS_PROFILE = "saveIsProfile"; |
Dave Santoro | 36d24d7 | 2011-09-25 17:08:10 -0700 | [diff] [blame] | 95 | public static final String EXTRA_SAVE_SUCCEEDED = "saveSucceeded"; |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 96 | public static final String EXTRA_UPDATED_PHOTOS = "updatedPhotos"; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 97 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 98 | public static final String ACTION_CREATE_GROUP = "createGroup"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 99 | public static final String ACTION_RENAME_GROUP = "renameGroup"; |
| 100 | public static final String ACTION_DELETE_GROUP = "deleteGroup"; |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 101 | public static final String ACTION_UPDATE_GROUP = "updateGroup"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 102 | public static final String EXTRA_GROUP_ID = "groupId"; |
| 103 | public static final String EXTRA_GROUP_LABEL = "groupLabel"; |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 104 | public static final String EXTRA_RAW_CONTACTS_TO_ADD = "rawContactsToAdd"; |
| 105 | public static final String EXTRA_RAW_CONTACTS_TO_REMOVE = "rawContactsToRemove"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 106 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 107 | public static final String ACTION_SET_STARRED = "setStarred"; |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 108 | public static final String ACTION_DELETE_CONTACT = "delete"; |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 109 | public static final String ACTION_DELETE_MULTIPLE_CONTACTS = "deleteMultipleContacts"; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 110 | public static final String EXTRA_CONTACT_URI = "contactUri"; |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 111 | public static final String EXTRA_CONTACT_IDS = "contactIds"; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 112 | public static final String EXTRA_STARRED_FLAG = "starred"; |
| 113 | |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 114 | public static final String ACTION_SET_SUPER_PRIMARY = "setSuperPrimary"; |
| 115 | public static final String ACTION_CLEAR_PRIMARY = "clearPrimary"; |
| 116 | public static final String EXTRA_DATA_ID = "dataId"; |
| 117 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 118 | public static final String ACTION_JOIN_CONTACTS = "joinContacts"; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 119 | public static final String ACTION_JOIN_SEVERAL_CONTACTS = "joinSeveralContacts"; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 120 | public static final String EXTRA_CONTACT_ID1 = "contactId1"; |
| 121 | public static final String EXTRA_CONTACT_ID2 = "contactId2"; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 122 | |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 123 | public static final String ACTION_SET_SEND_TO_VOICEMAIL = "sendToVoicemail"; |
| 124 | public static final String EXTRA_SEND_TO_VOICEMAIL_FLAG = "sendToVoicemailFlag"; |
| 125 | |
| 126 | public static final String ACTION_SET_RINGTONE = "setRingtone"; |
| 127 | public static final String EXTRA_CUSTOM_RINGTONE = "customRingtone"; |
| 128 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 129 | private static final HashSet<String> ALLOWED_DATA_COLUMNS = Sets.newHashSet( |
| 130 | Data.MIMETYPE, |
| 131 | Data.IS_PRIMARY, |
| 132 | Data.DATA1, |
| 133 | Data.DATA2, |
| 134 | Data.DATA3, |
| 135 | Data.DATA4, |
| 136 | Data.DATA5, |
| 137 | Data.DATA6, |
| 138 | Data.DATA7, |
| 139 | Data.DATA8, |
| 140 | Data.DATA9, |
| 141 | Data.DATA10, |
| 142 | Data.DATA11, |
| 143 | Data.DATA12, |
| 144 | Data.DATA13, |
| 145 | Data.DATA14, |
| 146 | Data.DATA15 |
| 147 | ); |
| 148 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 149 | private static final int PERSIST_TRIES = 3; |
| 150 | |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 151 | private static final int MAX_CONTACTS_PROVIDER_BATCH_SIZE = 499; |
| 152 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 153 | public interface Listener { |
| 154 | public void onServiceCompleted(Intent callbackIntent); |
| 155 | } |
| 156 | |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 157 | private static final CopyOnWriteArrayList<Listener> sListeners = |
| 158 | new CopyOnWriteArrayList<Listener>(); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 159 | |
| 160 | private Handler mMainHandler; |
| 161 | |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 162 | public ContactSaveService() { |
| 163 | super(TAG); |
| 164 | setIntentRedelivery(true); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 165 | mMainHandler = new Handler(Looper.getMainLooper()); |
| 166 | } |
| 167 | |
| 168 | public static void registerListener(Listener listener) { |
| 169 | if (!(listener instanceof Activity)) { |
| 170 | throw new ClassCastException("Only activities can be registered to" |
| 171 | + " receive callback from " + ContactSaveService.class.getName()); |
| 172 | } |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 173 | sListeners.add(0, listener); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | public static void unregisterListener(Listener listener) { |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 177 | sListeners.remove(listener); |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Wenyi Wang | dd7d456 | 2015-12-08 13:33:43 -0800 | [diff] [blame] | 180 | /** |
| 181 | * Returns true if the ContactSaveService was started successfully and false if an exception |
| 182 | * was thrown and a Toast error message was displayed. |
| 183 | */ |
| 184 | public static boolean startService(Context context, Intent intent, int saveMode) { |
| 185 | try { |
| 186 | context.startService(intent); |
| 187 | } catch (Exception exception) { |
| 188 | final int resId; |
| 189 | switch (saveMode) { |
| 190 | case ContactEditorBaseActivity.ContactEditor.SaveMode.SPLIT: |
| 191 | resId = R.string.contactUnlinkErrorToast; |
| 192 | break; |
| 193 | case ContactEditorBaseActivity.ContactEditor.SaveMode.RELOAD: |
| 194 | resId = R.string.contactJoinErrorToast; |
| 195 | break; |
| 196 | case ContactEditorBaseActivity.ContactEditor.SaveMode.CLOSE: |
| 197 | resId = R.string.contactSavedErrorToast; |
| 198 | break; |
| 199 | default: |
| 200 | resId = R.string.contactGenericErrorToast; |
| 201 | } |
| 202 | Toast.makeText(context, resId, Toast.LENGTH_SHORT).show(); |
| 203 | return false; |
| 204 | } |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Utility method that starts service and handles exception. |
| 210 | */ |
| 211 | public static void startService(Context context, Intent intent) { |
| 212 | try { |
| 213 | context.startService(intent); |
| 214 | } catch (Exception exception) { |
| 215 | Toast.makeText(context, R.string.contactGenericErrorToast, Toast.LENGTH_SHORT).show(); |
| 216 | } |
| 217 | } |
| 218 | |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 219 | @Override |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 220 | public Object getSystemService(String name) { |
| 221 | Object service = super.getSystemService(name); |
| 222 | if (service != null) { |
| 223 | return service; |
| 224 | } |
| 225 | |
| 226 | return getApplicationContext().getSystemService(name); |
| 227 | } |
| 228 | |
| 229 | @Override |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 230 | protected void onHandleIntent(Intent intent) { |
Jay Shrauner | 3a7cc76 | 2014-12-01 17:16:33 -0800 | [diff] [blame] | 231 | if (intent == null) { |
| 232 | Log.d(TAG, "onHandleIntent: could not handle null intent"); |
| 233 | return; |
| 234 | } |
Jay Shrauner | 615ed9c | 2015-07-29 11:27:56 -0700 | [diff] [blame] | 235 | if (!PermissionsUtil.hasPermission(this, WRITE_CONTACTS)) { |
| 236 | Log.w(TAG, "No WRITE_CONTACTS permission, unable to write to CP2"); |
| 237 | // TODO: add more specific error string such as "Turn on Contacts |
| 238 | // permission to update your contacts" |
| 239 | showToast(R.string.contactSavedErrorToast); |
| 240 | return; |
| 241 | } |
| 242 | |
Daisuke Miyakawa | 2f21c44 | 2012-03-22 19:12:31 -0700 | [diff] [blame] | 243 | // Call an appropriate method. If we're sure it affects how incoming phone calls are |
| 244 | // handled, then notify the fact to in-call screen. |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 245 | String action = intent.getAction(); |
| 246 | if (ACTION_NEW_RAW_CONTACT.equals(action)) { |
| 247 | createRawContact(intent); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 248 | } else if (ACTION_SAVE_CONTACT.equals(action)) { |
| 249 | saveContact(intent); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 250 | } else if (ACTION_CREATE_GROUP.equals(action)) { |
| 251 | createGroup(intent); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 252 | } else if (ACTION_RENAME_GROUP.equals(action)) { |
| 253 | renameGroup(intent); |
| 254 | } else if (ACTION_DELETE_GROUP.equals(action)) { |
| 255 | deleteGroup(intent); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 256 | } else if (ACTION_UPDATE_GROUP.equals(action)) { |
| 257 | updateGroup(intent); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 258 | } else if (ACTION_SET_STARRED.equals(action)) { |
| 259 | setStarred(intent); |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 260 | } else if (ACTION_SET_SUPER_PRIMARY.equals(action)) { |
| 261 | setSuperPrimary(intent); |
| 262 | } else if (ACTION_CLEAR_PRIMARY.equals(action)) { |
| 263 | clearPrimary(intent); |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 264 | } else if (ACTION_DELETE_MULTIPLE_CONTACTS.equals(action)) { |
| 265 | deleteMultipleContacts(intent); |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 266 | } else if (ACTION_DELETE_CONTACT.equals(action)) { |
| 267 | deleteContact(intent); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 268 | } else if (ACTION_JOIN_CONTACTS.equals(action)) { |
| 269 | joinContacts(intent); |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 270 | } else if (ACTION_JOIN_SEVERAL_CONTACTS.equals(action)) { |
| 271 | joinSeveralContacts(intent); |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 272 | } else if (ACTION_SET_SEND_TO_VOICEMAIL.equals(action)) { |
| 273 | setSendToVoicemail(intent); |
| 274 | } else if (ACTION_SET_RINGTONE.equals(action)) { |
| 275 | setRingtone(intent); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 279 | /** |
| 280 | * Creates an intent that can be sent to this service to create a new raw contact |
| 281 | * using data presented as a set of ContentValues. |
| 282 | */ |
| 283 | public static Intent createNewRawContactIntent(Context context, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 284 | ArrayList<ContentValues> values, AccountWithDataSet account, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 285 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 286 | Intent serviceIntent = new Intent( |
| 287 | context, ContactSaveService.class); |
| 288 | serviceIntent.setAction(ContactSaveService.ACTION_NEW_RAW_CONTACT); |
| 289 | if (account != null) { |
| 290 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_NAME, account.name); |
| 291 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_TYPE, account.type); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 292 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_SET, account.dataSet); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 293 | } |
| 294 | serviceIntent.putParcelableArrayListExtra( |
| 295 | ContactSaveService.EXTRA_CONTENT_VALUES, values); |
| 296 | |
| 297 | // Callback intent will be invoked by the service once the new contact is |
| 298 | // created. The service will put the URI of the new contact as "data" on |
| 299 | // the callback intent. |
| 300 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 301 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 302 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 303 | return serviceIntent; |
| 304 | } |
| 305 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 306 | private void createRawContact(Intent intent) { |
| 307 | String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME); |
| 308 | String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 309 | String dataSet = intent.getStringExtra(EXTRA_DATA_SET); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 310 | List<ContentValues> valueList = intent.getParcelableArrayListExtra(EXTRA_CONTENT_VALUES); |
| 311 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 312 | |
| 313 | ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
| 314 | operations.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) |
| 315 | .withValue(RawContacts.ACCOUNT_NAME, accountName) |
| 316 | .withValue(RawContacts.ACCOUNT_TYPE, accountType) |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 317 | .withValue(RawContacts.DATA_SET, dataSet) |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 318 | .build()); |
| 319 | |
| 320 | int size = valueList.size(); |
| 321 | for (int i = 0; i < size; i++) { |
| 322 | ContentValues values = valueList.get(i); |
| 323 | values.keySet().retainAll(ALLOWED_DATA_COLUMNS); |
| 324 | operations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) |
| 325 | .withValueBackReference(Data.RAW_CONTACT_ID, 0) |
| 326 | .withValues(values) |
| 327 | .build()); |
| 328 | } |
| 329 | |
| 330 | ContentResolver resolver = getContentResolver(); |
| 331 | ContentProviderResult[] results; |
| 332 | try { |
| 333 | results = resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
| 334 | } catch (Exception e) { |
| 335 | throw new RuntimeException("Failed to store new contact", e); |
| 336 | } |
| 337 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 338 | Uri rawContactUri = results[0].uri; |
| 339 | callbackIntent.setData(RawContacts.getContactLookupUri(resolver, rawContactUri)); |
| 340 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 341 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 344 | /** |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 345 | * Creates an intent that can be sent to this service to create a new raw contact |
| 346 | * using data presented as a set of ContentValues. |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 347 | * This variant is more convenient to use when there is only one photo that can |
| 348 | * possibly be updated, as in the Contact Details screen. |
| 349 | * @param rawContactId identifies a writable raw-contact whose photo is to be updated. |
| 350 | * @param updatedPhotoPath denotes a temporary file containing the contact's new photo. |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 351 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 352 | public static Intent createSaveContactIntent(Context context, RawContactDeltaList state, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 353 | String saveModeExtraKey, int saveMode, boolean isProfile, |
| 354 | Class<? extends Activity> callbackActivity, String callbackAction, long rawContactId, |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 355 | Uri updatedPhotoPath) { |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 356 | Bundle bundle = new Bundle(); |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 357 | bundle.putParcelable(String.valueOf(rawContactId), updatedPhotoPath); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 358 | return createSaveContactIntent(context, state, saveModeExtraKey, saveMode, isProfile, |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 359 | callbackActivity, callbackAction, bundle, |
| 360 | /* joinContactIdExtraKey */ null, /* joinContactId */ null); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Creates an intent that can be sent to this service to create a new raw contact |
| 365 | * using data presented as a set of ContentValues. |
| 366 | * This variant is used when multiple contacts' photos may be updated, as in the |
| 367 | * Contact Editor. |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 368 | * |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 369 | * @param updatedPhotos maps each raw-contact's ID to the file-path of the new photo. |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 370 | * @param joinContactIdExtraKey the key used to pass the joinContactId in the callback intent. |
| 371 | * @param joinContactId the raw contact ID to join to the contact after doing the save. |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 372 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 373 | public static Intent createSaveContactIntent(Context context, RawContactDeltaList state, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 374 | String saveModeExtraKey, int saveMode, boolean isProfile, |
| 375 | Class<? extends Activity> callbackActivity, String callbackAction, |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 376 | Bundle updatedPhotos, String joinContactIdExtraKey, Long joinContactId) { |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 377 | // Don't pass read-only RawContactDeltas in RawContactDeltaList to contact save service, |
| 378 | // because 1. read-only RawContactDeltas are not writable anyway; 2. read-only |
| 379 | // RawContactDeltas may be problematic, see b/23896510. |
| 380 | removeReadOnlyContacts(context, state); |
| 381 | |
| 382 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 383 | serviceIntent.setAction(ContactSaveService.ACTION_SAVE_CONTACT); |
| 384 | serviceIntent.putExtra(EXTRA_CONTACT_STATE, (Parcelable) state); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 385 | serviceIntent.putExtra(EXTRA_SAVE_IS_PROFILE, isProfile); |
benny.lin | 3a4e7a2 | 2014-01-08 10:58:08 +0800 | [diff] [blame] | 386 | serviceIntent.putExtra(EXTRA_SAVE_MODE, saveMode); |
| 387 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 388 | if (updatedPhotos != null) { |
| 389 | serviceIntent.putExtra(EXTRA_UPDATED_PHOTOS, (Parcelable) updatedPhotos); |
| 390 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 391 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 392 | if (callbackActivity != null) { |
| 393 | // Callback intent will be invoked by the service once the contact is |
| 394 | // saved. The service will put the URI of the new contact as "data" on |
| 395 | // the callback intent. |
| 396 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 397 | callbackIntent.putExtra(saveModeExtraKey, saveMode); |
Walter Jang | e3373dc | 2015-10-27 15:35:12 -0700 | [diff] [blame] | 398 | if (joinContactIdExtraKey != null && joinContactId != null) { |
| 399 | callbackIntent.putExtra(joinContactIdExtraKey, joinContactId); |
| 400 | } |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 401 | callbackIntent.setAction(callbackAction); |
| 402 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 403 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 404 | return serviceIntent; |
| 405 | } |
| 406 | |
Wenyi Wang | e5bac73 | 2016-02-03 11:55:21 -0800 | [diff] [blame] | 407 | private static void removeReadOnlyContacts(Context context, RawContactDeltaList state) { |
| 408 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 409 | Log.v(TAG, "Before trimming: " + state.size()); |
| 410 | } |
| 411 | int countReadOnly = 0; |
| 412 | final Iterator<RawContactDelta> iterator = state.iterator(); |
| 413 | while (iterator.hasNext()) { |
| 414 | final RawContactDelta rawContactDelta = iterator.next(); |
| 415 | final AccountType accountType = rawContactDelta.getRawContactAccountType(context); |
| 416 | if (accountType != null && !accountType.areContactsWritable()) { |
| 417 | countReadOnly++; |
| 418 | iterator.remove(); |
| 419 | } |
| 420 | } |
| 421 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 422 | Log.v(TAG, "# of read-only removed: " + countReadOnly); |
| 423 | Log.v(TAG, "After trimming: " + state.size()); |
| 424 | } |
| 425 | } |
| 426 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 427 | private void saveContact(Intent intent) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 428 | RawContactDeltaList state = intent.getParcelableExtra(EXTRA_CONTACT_STATE); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 429 | boolean isProfile = intent.getBooleanExtra(EXTRA_SAVE_IS_PROFILE, false); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 430 | Bundle updatedPhotos = intent.getParcelableExtra(EXTRA_UPDATED_PHOTOS); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 431 | |
Jay Shrauner | 0809978 | 2015-03-25 14:17:11 -0700 | [diff] [blame] | 432 | if (state == null) { |
| 433 | Log.e(TAG, "Invalid arguments for saveContact request"); |
| 434 | return; |
| 435 | } |
| 436 | |
benny.lin | 3a4e7a2 | 2014-01-08 10:58:08 +0800 | [diff] [blame] | 437 | int saveMode = intent.getIntExtra(EXTRA_SAVE_MODE, -1); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 438 | // Trim any empty fields, and RawContacts, before persisting |
| 439 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 440 | RawContactModifier.trimEmpty(state, accountTypes); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 441 | |
| 442 | Uri lookupUri = null; |
| 443 | |
| 444 | final ContentResolver resolver = getContentResolver(); |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 445 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 446 | boolean succeeded = false; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 447 | |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 448 | // Keep track of the id of a newly raw-contact (if any... there can be at most one). |
| 449 | long insertedRawContactId = -1; |
| 450 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 451 | // Attempt to persist changes |
| 452 | int tries = 0; |
| 453 | while (tries++ < PERSIST_TRIES) { |
| 454 | try { |
| 455 | // Build operations and try applying |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 456 | final ArrayList<CPOWrapper> diffWrapper = state.buildDiffWrapper(); |
| 457 | |
| 458 | final ArrayList<ContentProviderOperation> diff = Lists.newArrayList(); |
| 459 | |
| 460 | for (CPOWrapper cpoWrapper : diffWrapper) { |
| 461 | diff.add(cpoWrapper.getOperation()); |
| 462 | } |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 463 | |
Katherine Kuan | a007e44 | 2011-07-07 09:25:34 -0700 | [diff] [blame] | 464 | if (DEBUG) { |
| 465 | Log.v(TAG, "Content Provider Operations:"); |
| 466 | for (ContentProviderOperation operation : diff) { |
| 467 | Log.v(TAG, operation.toString()); |
| 468 | } |
| 469 | } |
| 470 | |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 471 | int numberProcessed = 0; |
| 472 | boolean batchFailed = false; |
| 473 | final ContentProviderResult[] results = new ContentProviderResult[diff.size()]; |
| 474 | while (numberProcessed < diff.size()) { |
| 475 | final int subsetCount = applyDiffSubset(diff, numberProcessed, results, resolver); |
| 476 | if (subsetCount == -1) { |
Jay Shrauner | 511561d | 2015-04-02 10:35:33 -0700 | [diff] [blame] | 477 | Log.w(TAG, "Resolver.applyBatch failed in saveContacts"); |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 478 | batchFailed = true; |
| 479 | break; |
| 480 | } else { |
| 481 | numberProcessed += subsetCount; |
Jay Shrauner | 511561d | 2015-04-02 10:35:33 -0700 | [diff] [blame] | 482 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 485 | if (batchFailed) { |
| 486 | // Retry save |
| 487 | continue; |
| 488 | } |
| 489 | |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 490 | final long rawContactId = getRawContactId(state, diffWrapper, results); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 491 | if (rawContactId == -1) { |
| 492 | throw new IllegalStateException("Could not determine RawContact ID after save"); |
| 493 | } |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 494 | // We don't have to check to see if the value is still -1. If we reach here, |
| 495 | // the previous loop iteration didn't succeed, so any ID that we obtained is bogus. |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 496 | insertedRawContactId = getInsertedRawContactId(diffWrapper, results); |
Dave Santoro | 7c34c0a | 2011-09-12 14:58:20 -0700 | [diff] [blame] | 497 | if (isProfile) { |
| 498 | // Since the profile supports local raw contacts, which may have been completely |
| 499 | // removed if all information was removed, we need to do a special query to |
| 500 | // get the lookup URI for the profile contact (if it still exists). |
| 501 | Cursor c = resolver.query(Profile.CONTENT_URI, |
| 502 | new String[] {Contacts._ID, Contacts.LOOKUP_KEY}, |
| 503 | null, null, null); |
Jay Shrauner | e320c0b | 2015-03-05 12:45:18 -0800 | [diff] [blame] | 504 | if (c == null) { |
| 505 | continue; |
| 506 | } |
Dave Santoro | 7c34c0a | 2011-09-12 14:58:20 -0700 | [diff] [blame] | 507 | try { |
Erik | 162b7e3 | 2011-09-20 15:23:55 -0700 | [diff] [blame] | 508 | if (c.moveToFirst()) { |
| 509 | final long contactId = c.getLong(0); |
| 510 | final String lookupKey = c.getString(1); |
| 511 | lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
| 512 | } |
Dave Santoro | 7c34c0a | 2011-09-12 14:58:20 -0700 | [diff] [blame] | 513 | } finally { |
| 514 | c.close(); |
| 515 | } |
| 516 | } else { |
| 517 | final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, |
| 518 | rawContactId); |
| 519 | lookupUri = RawContacts.getContactLookupUri(resolver, rawContactUri); |
| 520 | } |
Jay Shrauner | e320c0b | 2015-03-05 12:45:18 -0800 | [diff] [blame] | 521 | if (lookupUri != null) { |
| 522 | Log.v(TAG, "Saved contact. New URI: " + lookupUri); |
| 523 | } |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 524 | |
| 525 | // We can change this back to false later, if we fail to save the contact photo. |
| 526 | succeeded = true; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 527 | break; |
| 528 | |
| 529 | } catch (RemoteException e) { |
| 530 | // Something went wrong, bail without success |
| 531 | Log.e(TAG, "Problem persisting user edits", e); |
| 532 | break; |
| 533 | |
Jay Shrauner | 57fca18 | 2014-01-17 14:20:50 -0800 | [diff] [blame] | 534 | } catch (IllegalArgumentException e) { |
| 535 | // This is thrown by applyBatch on malformed requests |
| 536 | Log.e(TAG, "Problem persisting user edits", e); |
| 537 | showToast(R.string.contactSavedErrorToast); |
| 538 | break; |
| 539 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 540 | } catch (OperationApplicationException e) { |
| 541 | // Version consistency failed, re-parent change and try again |
| 542 | Log.w(TAG, "Version consistency failed, re-parenting: " + e.toString()); |
| 543 | final StringBuilder sb = new StringBuilder(RawContacts._ID + " IN("); |
| 544 | boolean first = true; |
| 545 | final int count = state.size(); |
| 546 | for (int i = 0; i < count; i++) { |
| 547 | Long rawContactId = state.getRawContactId(i); |
| 548 | if (rawContactId != null && rawContactId != -1) { |
| 549 | if (!first) { |
| 550 | sb.append(','); |
| 551 | } |
| 552 | sb.append(rawContactId); |
| 553 | first = false; |
| 554 | } |
| 555 | } |
| 556 | sb.append(")"); |
| 557 | |
| 558 | if (first) { |
Brian Attwell | 3b6c628 | 2014-02-12 17:53:31 -0800 | [diff] [blame] | 559 | throw new IllegalStateException( |
| 560 | "Version consistency failed for a new contact", e); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 563 | final RawContactDeltaList newState = RawContactDeltaList.fromQuery( |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 564 | isProfile |
| 565 | ? RawContactsEntity.PROFILE_CONTENT_URI |
| 566 | : RawContactsEntity.CONTENT_URI, |
| 567 | resolver, sb.toString(), null, null); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 568 | state = RawContactDeltaList.mergeAfter(newState, state); |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 569 | |
| 570 | // Update the new state to use profile URIs if appropriate. |
| 571 | if (isProfile) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 572 | for (RawContactDelta delta : state) { |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 573 | delta.setProfileQueryUri(); |
| 574 | } |
| 575 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 579 | // Now save any updated photos. We do this at the end to ensure that |
| 580 | // the ContactProvider already knows about newly-created contacts. |
| 581 | if (updatedPhotos != null) { |
| 582 | for (String key : updatedPhotos.keySet()) { |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 583 | Uri photoUri = updatedPhotos.getParcelable(key); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 584 | long rawContactId = Long.parseLong(key); |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 585 | |
| 586 | // If the raw-contact ID is negative, we are saving a new raw-contact; |
| 587 | // replace the bogus ID with the new one that we actually saved the contact at. |
| 588 | if (rawContactId < 0) { |
| 589 | rawContactId = insertedRawContactId; |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Jay Shrauner | 511561d | 2015-04-02 10:35:33 -0700 | [diff] [blame] | 592 | // If the save failed, insertedRawContactId will be -1 |
Jay Shrauner | c4698fb | 2015-04-30 12:08:52 -0700 | [diff] [blame] | 593 | if (rawContactId < 0 || !saveUpdatedPhoto(rawContactId, photoUri, saveMode)) { |
Jay Shrauner | 511561d | 2015-04-02 10:35:33 -0700 | [diff] [blame] | 594 | succeeded = false; |
| 595 | } |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 599 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 600 | if (callbackIntent != null) { |
| 601 | if (succeeded) { |
| 602 | // Mark the intent to indicate that the save was successful (even if the lookup URI |
| 603 | // is now null). For local contacts or the local profile, it's possible that the |
| 604 | // save triggered removal of the contact, so no lookup URI would exist.. |
| 605 | callbackIntent.putExtra(EXTRA_SAVE_SUCCEEDED, true); |
| 606 | } |
| 607 | callbackIntent.setData(lookupUri); |
| 608 | deliverCallback(callbackIntent); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 609 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 612 | /** |
Wenyi Wang | ccdf69c | 2015-10-30 11:59:58 -0700 | [diff] [blame] | 613 | * Splits "diff" into subsets based on "MAX_CONTACTS_PROVIDER_BATCH_SIZE", applies each of the |
| 614 | * subsets, adds the returned array to "results". |
| 615 | * |
| 616 | * @return the size of the array, if not null; -1 when the array is null. |
| 617 | */ |
| 618 | private int applyDiffSubset(ArrayList<ContentProviderOperation> diff, int offset, |
| 619 | ContentProviderResult[] results, ContentResolver resolver) |
| 620 | throws RemoteException, OperationApplicationException { |
| 621 | final int subsetCount = Math.min(diff.size() - offset, MAX_CONTACTS_PROVIDER_BATCH_SIZE); |
| 622 | final ArrayList<ContentProviderOperation> subset = new ArrayList<>(); |
| 623 | subset.addAll(diff.subList(offset, offset + subsetCount)); |
| 624 | final ContentProviderResult[] subsetResult = resolver.applyBatch(ContactsContract |
| 625 | .AUTHORITY, subset); |
| 626 | if (subsetResult == null || (offset + subsetResult.length) > results.length) { |
| 627 | return -1; |
| 628 | } |
| 629 | for (ContentProviderResult c : subsetResult) { |
| 630 | results[offset++] = c; |
| 631 | } |
| 632 | return subsetResult.length; |
| 633 | } |
| 634 | |
| 635 | /** |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 636 | * Save updated photo for the specified raw-contact. |
| 637 | * @return true for success, false for failure |
| 638 | */ |
benny.lin | 3a4e7a2 | 2014-01-08 10:58:08 +0800 | [diff] [blame] | 639 | private boolean saveUpdatedPhoto(long rawContactId, Uri photoUri, int saveMode) { |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 640 | final Uri outputUri = Uri.withAppendedPath( |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 641 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), |
| 642 | RawContacts.DisplayPhoto.CONTENT_DIRECTORY); |
| 643 | |
benny.lin | 3a4e7a2 | 2014-01-08 10:58:08 +0800 | [diff] [blame] | 644 | return ContactPhotoUtils.savePhotoFromUriToUri(this, photoUri, outputUri, (saveMode == 0)); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 647 | /** |
| 648 | * Find the ID of an existing or newly-inserted raw-contact. If none exists, return -1. |
| 649 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 650 | private long getRawContactId(RawContactDeltaList state, |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 651 | final ArrayList<CPOWrapper> diffWrapper, |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 652 | final ContentProviderResult[] results) { |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 653 | long existingRawContactId = state.findRawContactId(); |
| 654 | if (existingRawContactId != -1) { |
| 655 | return existingRawContactId; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 658 | return getInsertedRawContactId(diffWrapper, results); |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Find the ID of a newly-inserted raw-contact. If none exists, return -1. |
| 663 | */ |
| 664 | private long getInsertedRawContactId( |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 665 | final ArrayList<CPOWrapper> diffWrapper, final ContentProviderResult[] results) { |
Jay Shrauner | 568f4e7 | 2014-11-26 08:16:25 -0800 | [diff] [blame] | 666 | if (results == null) { |
| 667 | return -1; |
| 668 | } |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 669 | final int diffSize = diffWrapper.size(); |
Jay Shrauner | 3d7edc3 | 2014-11-10 09:58:23 -0800 | [diff] [blame] | 670 | final int numResults = results.length; |
| 671 | for (int i = 0; i < diffSize && i < numResults; i++) { |
Wenyi Wang | 67addcc | 2015-11-23 10:07:48 -0800 | [diff] [blame] | 672 | final CPOWrapper cpoWrapper = diffWrapper.get(i); |
| 673 | final boolean isInsert = CompatUtils.isInsertCompat(cpoWrapper); |
| 674 | if (isInsert && cpoWrapper.getOperation().getUri().getEncodedPath().contains( |
| 675 | RawContacts.CONTENT_URI.getEncodedPath())) { |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 676 | return ContentUris.parseId(results[i].uri); |
| 677 | } |
| 678 | } |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | /** |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 683 | * Creates an intent that can be sent to this service to create a new group as |
| 684 | * well as add new members at the same time. |
| 685 | * |
| 686 | * @param context of the application |
| 687 | * @param account in which the group should be created |
| 688 | * @param label is the name of the group (cannot be null) |
| 689 | * @param rawContactsToAdd is an array of raw contact IDs for contacts that |
| 690 | * should be added to the group |
| 691 | * @param callbackActivity is the activity to send the callback intent to |
| 692 | * @param callbackAction is the intent action for the callback intent |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 693 | */ |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 694 | public static Intent createNewGroupIntent(Context context, AccountWithDataSet account, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 695 | String label, long[] rawContactsToAdd, Class<? extends Activity> callbackActivity, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 696 | String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 697 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 698 | serviceIntent.setAction(ContactSaveService.ACTION_CREATE_GROUP); |
| 699 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_TYPE, account.type); |
| 700 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_NAME, account.name); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 701 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_SET, account.dataSet); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 702 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, label); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 703 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_ADD, rawContactsToAdd); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 704 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 705 | // Callback intent will be invoked by the service once the new group is |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 706 | // created. |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 707 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 708 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 709 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 710 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 711 | return serviceIntent; |
| 712 | } |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 713 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 714 | private void createGroup(Intent intent) { |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 715 | String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); |
| 716 | String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME); |
| 717 | String dataSet = intent.getStringExtra(EXTRA_DATA_SET); |
| 718 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 719 | final long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 720 | |
| 721 | ContentValues values = new ContentValues(); |
| 722 | values.put(Groups.ACCOUNT_TYPE, accountType); |
| 723 | values.put(Groups.ACCOUNT_NAME, accountName); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 724 | values.put(Groups.DATA_SET, dataSet); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 725 | values.put(Groups.TITLE, label); |
| 726 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 727 | final ContentResolver resolver = getContentResolver(); |
| 728 | |
| 729 | // Create the new group |
| 730 | final Uri groupUri = resolver.insert(Groups.CONTENT_URI, values); |
| 731 | |
| 732 | // If there's no URI, then the insertion failed. Abort early because group members can't be |
| 733 | // added if the group doesn't exist |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 734 | if (groupUri == null) { |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 735 | Log.e(TAG, "Couldn't create group with label " + label); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 739 | // Add new group members |
| 740 | addMembersToGroup(resolver, rawContactsToAdd, ContentUris.parseId(groupUri)); |
| 741 | |
| 742 | // TODO: Move this into the contact editor where it belongs. This needs to be integrated |
| 743 | // with the way other intent extras that are passed to the {@link ContactEditorActivity}. |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 744 | values.clear(); |
| 745 | values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); |
| 746 | values.put(GroupMembership.GROUP_ROW_ID, ContentUris.parseId(groupUri)); |
| 747 | |
| 748 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 749 | callbackIntent.setData(groupUri); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 750 | // TODO: This can be taken out when the above TODO is addressed |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 751 | callbackIntent.putExtra(ContactsContract.Intents.Insert.DATA, Lists.newArrayList(values)); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 752 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 756 | * Creates an intent that can be sent to this service to rename a group. |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 757 | */ |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 758 | public static Intent createGroupRenameIntent(Context context, long groupId, String newLabel, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 759 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 760 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 761 | serviceIntent.setAction(ContactSaveService.ACTION_RENAME_GROUP); |
| 762 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
| 763 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, newLabel); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 764 | |
| 765 | // Callback intent will be invoked by the service once the group is renamed. |
| 766 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 767 | callbackIntent.setAction(callbackAction); |
| 768 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 769 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 770 | return serviceIntent; |
| 771 | } |
| 772 | |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 773 | private void renameGroup(Intent intent) { |
| 774 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 775 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
| 776 | |
| 777 | if (groupId == -1) { |
| 778 | Log.e(TAG, "Invalid arguments for renameGroup request"); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | ContentValues values = new ContentValues(); |
| 783 | values.put(Groups.TITLE, label); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 784 | final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); |
| 785 | getContentResolver().update(groupUri, values, null, null); |
| 786 | |
| 787 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 788 | callbackIntent.setData(groupUri); |
| 789 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 793 | * Creates an intent that can be sent to this service to delete a group. |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 794 | */ |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 795 | public static Intent createGroupDeletionIntent(Context context, long groupId) { |
| 796 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 797 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_GROUP); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 798 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 799 | return serviceIntent; |
| 800 | } |
| 801 | |
| 802 | private void deleteGroup(Intent intent) { |
| 803 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 804 | if (groupId == -1) { |
| 805 | Log.e(TAG, "Invalid arguments for deleteGroup request"); |
| 806 | return; |
| 807 | } |
| 808 | |
| 809 | getContentResolver().delete( |
| 810 | ContentUris.withAppendedId(Groups.CONTENT_URI, groupId), null, null); |
| 811 | } |
| 812 | |
| 813 | /** |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 814 | * Creates an intent that can be sent to this service to rename a group as |
| 815 | * well as add and remove members from the group. |
| 816 | * |
| 817 | * @param context of the application |
| 818 | * @param groupId of the group that should be modified |
| 819 | * @param newLabel is the updated name of the group (can be null if the name |
| 820 | * should not be updated) |
| 821 | * @param rawContactsToAdd is an array of raw contact IDs for contacts that |
| 822 | * should be added to the group |
| 823 | * @param rawContactsToRemove is an array of raw contact IDs for contacts |
| 824 | * that should be removed from the group |
| 825 | * @param callbackActivity is the activity to send the callback intent to |
| 826 | * @param callbackAction is the intent action for the callback intent |
| 827 | */ |
| 828 | public static Intent createGroupUpdateIntent(Context context, long groupId, String newLabel, |
| 829 | long[] rawContactsToAdd, long[] rawContactsToRemove, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 830 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 831 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 832 | serviceIntent.setAction(ContactSaveService.ACTION_UPDATE_GROUP); |
| 833 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
| 834 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, newLabel); |
| 835 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_ADD, rawContactsToAdd); |
| 836 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_REMOVE, |
| 837 | rawContactsToRemove); |
| 838 | |
| 839 | // Callback intent will be invoked by the service once the group is updated |
| 840 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 841 | callbackIntent.setAction(callbackAction); |
| 842 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 843 | |
| 844 | return serviceIntent; |
| 845 | } |
| 846 | |
| 847 | private void updateGroup(Intent intent) { |
| 848 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 849 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
| 850 | long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD); |
| 851 | long[] rawContactsToRemove = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_REMOVE); |
| 852 | |
| 853 | if (groupId == -1) { |
| 854 | Log.e(TAG, "Invalid arguments for updateGroup request"); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | final ContentResolver resolver = getContentResolver(); |
| 859 | final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); |
| 860 | |
| 861 | // Update group name if necessary |
| 862 | if (label != null) { |
| 863 | ContentValues values = new ContentValues(); |
| 864 | values.put(Groups.TITLE, label); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 865 | resolver.update(groupUri, values, null, null); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 868 | // Add and remove members if necessary |
| 869 | addMembersToGroup(resolver, rawContactsToAdd, groupId); |
| 870 | removeMembersFromGroup(resolver, rawContactsToRemove, groupId); |
| 871 | |
| 872 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 873 | callbackIntent.setData(groupUri); |
| 874 | deliverCallback(callbackIntent); |
| 875 | } |
| 876 | |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 877 | private static void addMembersToGroup(ContentResolver resolver, long[] rawContactsToAdd, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 878 | long groupId) { |
| 879 | if (rawContactsToAdd == null) { |
| 880 | return; |
| 881 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 882 | for (long rawContactId : rawContactsToAdd) { |
| 883 | try { |
| 884 | final ArrayList<ContentProviderOperation> rawContactOperations = |
| 885 | new ArrayList<ContentProviderOperation>(); |
| 886 | |
| 887 | // Build an assert operation to ensure the contact is not already in the group |
| 888 | final ContentProviderOperation.Builder assertBuilder = ContentProviderOperation |
| 889 | .newAssertQuery(Data.CONTENT_URI); |
| 890 | assertBuilder.withSelection(Data.RAW_CONTACT_ID + "=? AND " + |
| 891 | Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", |
| 892 | new String[] { String.valueOf(rawContactId), |
| 893 | GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); |
| 894 | assertBuilder.withExpectedCount(0); |
| 895 | rawContactOperations.add(assertBuilder.build()); |
| 896 | |
| 897 | // Build an insert operation to add the contact to the group |
| 898 | final ContentProviderOperation.Builder insertBuilder = ContentProviderOperation |
| 899 | .newInsert(Data.CONTENT_URI); |
| 900 | insertBuilder.withValue(Data.RAW_CONTACT_ID, rawContactId); |
| 901 | insertBuilder.withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); |
| 902 | insertBuilder.withValue(GroupMembership.GROUP_ROW_ID, groupId); |
| 903 | rawContactOperations.add(insertBuilder.build()); |
| 904 | |
| 905 | if (DEBUG) { |
| 906 | for (ContentProviderOperation operation : rawContactOperations) { |
| 907 | Log.v(TAG, operation.toString()); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // Apply batch |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 912 | if (!rawContactOperations.isEmpty()) { |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 913 | resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 914 | } |
| 915 | } catch (RemoteException e) { |
| 916 | // Something went wrong, bail without success |
| 917 | Log.e(TAG, "Problem persisting user edits for raw contact ID " + |
| 918 | String.valueOf(rawContactId), e); |
| 919 | } catch (OperationApplicationException e) { |
| 920 | // The assert could have failed because the contact is already in the group, |
| 921 | // just continue to the next contact |
| 922 | Log.w(TAG, "Assert failed in adding raw contact ID " + |
| 923 | String.valueOf(rawContactId) + ". Already exists in group " + |
| 924 | String.valueOf(groupId), e); |
| 925 | } |
| 926 | } |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 927 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 928 | |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 929 | private static void removeMembersFromGroup(ContentResolver resolver, long[] rawContactsToRemove, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 930 | long groupId) { |
| 931 | if (rawContactsToRemove == null) { |
| 932 | return; |
| 933 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 934 | for (long rawContactId : rawContactsToRemove) { |
| 935 | // Apply the delete operation on the data row for the given raw contact's |
| 936 | // membership in the given group. If no contact matches the provided selection, then |
| 937 | // nothing will be done. Just continue to the next contact. |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 938 | resolver.delete(Data.CONTENT_URI, Data.RAW_CONTACT_ID + "=? AND " + |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 939 | Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", |
| 940 | new String[] { String.valueOf(rawContactId), |
| 941 | GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); |
| 942 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 946 | * Creates an intent that can be sent to this service to star or un-star a contact. |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 947 | */ |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 948 | public static Intent createSetStarredIntent(Context context, Uri contactUri, boolean value) { |
| 949 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 950 | serviceIntent.setAction(ContactSaveService.ACTION_SET_STARRED); |
| 951 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 952 | serviceIntent.putExtra(ContactSaveService.EXTRA_STARRED_FLAG, value); |
| 953 | |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 954 | return serviceIntent; |
| 955 | } |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 956 | |
| 957 | private void setStarred(Intent intent) { |
| 958 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 959 | boolean value = intent.getBooleanExtra(EXTRA_STARRED_FLAG, false); |
| 960 | if (contactUri == null) { |
| 961 | Log.e(TAG, "Invalid arguments for setStarred request"); |
| 962 | return; |
| 963 | } |
| 964 | |
| 965 | final ContentValues values = new ContentValues(1); |
| 966 | values.put(Contacts.STARRED, value); |
| 967 | getContentResolver().update(contactUri, values, null, null); |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 968 | |
| 969 | // Undemote the contact if necessary |
| 970 | final Cursor c = getContentResolver().query(contactUri, new String[] {Contacts._ID}, |
| 971 | null, null, null); |
Jay Shrauner | c12a280 | 2014-11-24 10:07:31 -0800 | [diff] [blame] | 972 | if (c == null) { |
| 973 | return; |
| 974 | } |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 975 | try { |
| 976 | if (c.moveToFirst()) { |
| 977 | final long id = c.getLong(0); |
Yorke Lee | bbb8c99 | 2013-09-23 16:20:53 -0700 | [diff] [blame] | 978 | |
| 979 | // Don't bother undemoting if this contact is the user's profile. |
| 980 | if (id < Profile.MIN_ID) { |
Wenyi Wang | aac0e66 | 2015-12-18 17:17:33 -0800 | [diff] [blame] | 981 | PinnedPositionsCompat.undemote(getContentResolver(), id); |
Yorke Lee | bbb8c99 | 2013-09-23 16:20:53 -0700 | [diff] [blame] | 982 | } |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 983 | } |
| 984 | } finally { |
| 985 | c.close(); |
| 986 | } |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 987 | } |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 988 | |
| 989 | /** |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 990 | * Creates an intent that can be sent to this service to set the redirect to voicemail. |
| 991 | */ |
| 992 | public static Intent createSetSendToVoicemail(Context context, Uri contactUri, |
| 993 | boolean value) { |
| 994 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 995 | serviceIntent.setAction(ContactSaveService.ACTION_SET_SEND_TO_VOICEMAIL); |
| 996 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 997 | serviceIntent.putExtra(ContactSaveService.EXTRA_SEND_TO_VOICEMAIL_FLAG, value); |
| 998 | |
| 999 | return serviceIntent; |
| 1000 | } |
| 1001 | |
| 1002 | private void setSendToVoicemail(Intent intent) { |
| 1003 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 1004 | boolean value = intent.getBooleanExtra(EXTRA_SEND_TO_VOICEMAIL_FLAG, false); |
| 1005 | if (contactUri == null) { |
| 1006 | Log.e(TAG, "Invalid arguments for setRedirectToVoicemail"); |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | final ContentValues values = new ContentValues(1); |
| 1011 | values.put(Contacts.SEND_TO_VOICEMAIL, value); |
| 1012 | getContentResolver().update(contactUri, values, null, null); |
| 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Creates an intent that can be sent to this service to save the contact's ringtone. |
| 1017 | */ |
| 1018 | public static Intent createSetRingtone(Context context, Uri contactUri, |
| 1019 | String value) { |
| 1020 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1021 | serviceIntent.setAction(ContactSaveService.ACTION_SET_RINGTONE); |
| 1022 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 1023 | serviceIntent.putExtra(ContactSaveService.EXTRA_CUSTOM_RINGTONE, value); |
| 1024 | |
| 1025 | return serviceIntent; |
| 1026 | } |
| 1027 | |
| 1028 | private void setRingtone(Intent intent) { |
| 1029 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 1030 | String value = intent.getStringExtra(EXTRA_CUSTOM_RINGTONE); |
| 1031 | if (contactUri == null) { |
| 1032 | Log.e(TAG, "Invalid arguments for setRingtone"); |
| 1033 | return; |
| 1034 | } |
| 1035 | ContentValues values = new ContentValues(1); |
| 1036 | values.put(Contacts.CUSTOM_RINGTONE, value); |
| 1037 | getContentResolver().update(contactUri, values, null, null); |
| 1038 | } |
| 1039 | |
| 1040 | /** |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 1041 | * Creates an intent that sets the selected data item as super primary (default) |
| 1042 | */ |
| 1043 | public static Intent createSetSuperPrimaryIntent(Context context, long dataId) { |
| 1044 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1045 | serviceIntent.setAction(ContactSaveService.ACTION_SET_SUPER_PRIMARY); |
| 1046 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_ID, dataId); |
| 1047 | return serviceIntent; |
| 1048 | } |
| 1049 | |
| 1050 | private void setSuperPrimary(Intent intent) { |
| 1051 | long dataId = intent.getLongExtra(EXTRA_DATA_ID, -1); |
| 1052 | if (dataId == -1) { |
| 1053 | Log.e(TAG, "Invalid arguments for setSuperPrimary request"); |
| 1054 | return; |
| 1055 | } |
| 1056 | |
Chiao Cheng | d7ca03e | 2012-10-24 15:14:08 -0700 | [diff] [blame] | 1057 | ContactUpdateUtils.setSuperPrimary(this, dataId); |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /** |
| 1061 | * Creates an intent that clears the primary flag of all data items that belong to the same |
| 1062 | * raw_contact as the given data item. Will only clear, if the data item was primary before |
| 1063 | * this call |
| 1064 | */ |
| 1065 | public static Intent createClearPrimaryIntent(Context context, long dataId) { |
| 1066 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1067 | serviceIntent.setAction(ContactSaveService.ACTION_CLEAR_PRIMARY); |
| 1068 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_ID, dataId); |
| 1069 | return serviceIntent; |
| 1070 | } |
| 1071 | |
| 1072 | private void clearPrimary(Intent intent) { |
| 1073 | long dataId = intent.getLongExtra(EXTRA_DATA_ID, -1); |
| 1074 | if (dataId == -1) { |
| 1075 | Log.e(TAG, "Invalid arguments for clearPrimary request"); |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | // Update the primary values in the data record. |
| 1080 | ContentValues values = new ContentValues(1); |
| 1081 | values.put(Data.IS_SUPER_PRIMARY, 0); |
| 1082 | values.put(Data.IS_PRIMARY, 0); |
| 1083 | |
| 1084 | getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, dataId), |
| 1085 | values, null, null); |
| 1086 | } |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 1087 | |
| 1088 | /** |
| 1089 | * Creates an intent that can be sent to this service to delete a contact. |
| 1090 | */ |
| 1091 | public static Intent createDeleteContactIntent(Context context, Uri contactUri) { |
| 1092 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1093 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_CONTACT); |
| 1094 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 1095 | return serviceIntent; |
| 1096 | } |
| 1097 | |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 1098 | /** |
| 1099 | * Creates an intent that can be sent to this service to delete multiple contacts. |
| 1100 | */ |
| 1101 | public static Intent createDeleteMultipleContactsIntent(Context context, |
| 1102 | long[] contactIds) { |
| 1103 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1104 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_MULTIPLE_CONTACTS); |
| 1105 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_IDS, contactIds); |
| 1106 | return serviceIntent; |
| 1107 | } |
| 1108 | |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 1109 | private void deleteContact(Intent intent) { |
| 1110 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 1111 | if (contactUri == null) { |
| 1112 | Log.e(TAG, "Invalid arguments for deleteContact request"); |
| 1113 | return; |
| 1114 | } |
| 1115 | |
| 1116 | getContentResolver().delete(contactUri, null, null); |
| 1117 | } |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1118 | |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 1119 | private void deleteMultipleContacts(Intent intent) { |
| 1120 | final long[] contactIds = intent.getLongArrayExtra(EXTRA_CONTACT_IDS); |
| 1121 | if (contactIds == null) { |
| 1122 | Log.e(TAG, "Invalid arguments for deleteMultipleContacts request"); |
| 1123 | return; |
| 1124 | } |
| 1125 | for (long contactId : contactIds) { |
| 1126 | final Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); |
| 1127 | getContentResolver().delete(contactUri, null, null); |
| 1128 | } |
Wenyi Wang | 687d218 | 2015-10-28 17:03:18 -0700 | [diff] [blame] | 1129 | final String deleteToastMessage = getResources().getQuantityString(R.plurals |
| 1130 | .contacts_deleted_toast, contactIds.length); |
| 1131 | mMainHandler.post(new Runnable() { |
| 1132 | @Override |
| 1133 | public void run() { |
| 1134 | Toast.makeText(ContactSaveService.this, deleteToastMessage, Toast.LENGTH_LONG) |
| 1135 | .show(); |
| 1136 | } |
| 1137 | }); |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 1138 | } |
| 1139 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1140 | /** |
| 1141 | * Creates an intent that can be sent to this service to join two contacts. |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1142 | * The resulting contact uses the name from {@param contactId1} if possible. |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1143 | */ |
| 1144 | public static Intent createJoinContactsIntent(Context context, long contactId1, |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1145 | long contactId2, Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1146 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1147 | serviceIntent.setAction(ContactSaveService.ACTION_JOIN_CONTACTS); |
| 1148 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_ID1, contactId1); |
| 1149 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_ID2, contactId2); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1150 | |
| 1151 | // Callback intent will be invoked by the service once the contacts are joined. |
| 1152 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 1153 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1154 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 1155 | |
| 1156 | return serviceIntent; |
| 1157 | } |
| 1158 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1159 | /** |
| 1160 | * Creates an intent to join all raw contacts inside {@param contactIds}'s contacts. |
| 1161 | * No special attention is paid to where the resulting contact's name is taken from. |
| 1162 | */ |
| 1163 | public static Intent createJoinSeveralContactsIntent(Context context, long[] contactIds) { |
| 1164 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 1165 | serviceIntent.setAction(ContactSaveService.ACTION_JOIN_SEVERAL_CONTACTS); |
| 1166 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_IDS, contactIds); |
| 1167 | return serviceIntent; |
| 1168 | } |
| 1169 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1170 | |
| 1171 | private interface JoinContactQuery { |
| 1172 | String[] PROJECTION = { |
| 1173 | RawContacts._ID, |
| 1174 | RawContacts.CONTACT_ID, |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1175 | RawContacts.DISPLAY_NAME_SOURCE, |
| 1176 | }; |
| 1177 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1178 | int _ID = 0; |
| 1179 | int CONTACT_ID = 1; |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1180 | int DISPLAY_NAME_SOURCE = 2; |
| 1181 | } |
| 1182 | |
| 1183 | private interface ContactEntityQuery { |
| 1184 | String[] PROJECTION = { |
| 1185 | Contacts.Entity.DATA_ID, |
| 1186 | Contacts.Entity.CONTACT_ID, |
| 1187 | Contacts.Entity.IS_SUPER_PRIMARY, |
| 1188 | }; |
| 1189 | String SELECTION = Data.MIMETYPE + " = '" + StructuredName.CONTENT_ITEM_TYPE + "'" + |
| 1190 | " AND " + StructuredName.DISPLAY_NAME + "=" + Contacts.DISPLAY_NAME + |
| 1191 | " AND " + StructuredName.DISPLAY_NAME + " IS NOT NULL " + |
| 1192 | " AND " + StructuredName.DISPLAY_NAME + " != '' "; |
| 1193 | |
| 1194 | int DATA_ID = 0; |
| 1195 | int CONTACT_ID = 1; |
| 1196 | int IS_SUPER_PRIMARY = 2; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1197 | } |
| 1198 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1199 | private void joinSeveralContacts(Intent intent) { |
| 1200 | final long[] contactIds = intent.getLongArrayExtra(EXTRA_CONTACT_IDS); |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1201 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1202 | // Load raw contact IDs for all contacts involved. |
| 1203 | long rawContactIds[] = getRawContactIdsForAggregation(contactIds); |
| 1204 | if (rawContactIds == null) { |
| 1205 | Log.e(TAG, "Invalid arguments for joinSeveralContacts request"); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1206 | return; |
| 1207 | } |
| 1208 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1209 | // For each pair of raw contacts, insert an aggregation exception |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1210 | final ContentResolver resolver = getContentResolver(); |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1211 | // The maximum number of operations per batch (aka yield point) is 500. See b/22480225 |
| 1212 | final int batchSize = MAX_CONTACTS_PROVIDER_BATCH_SIZE; |
| 1213 | final ArrayList<ContentProviderOperation> operations = new ArrayList<>(batchSize); |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1214 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1215 | for (int j = 0; j < rawContactIds.length; j++) { |
| 1216 | if (i != j) { |
| 1217 | buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]); |
| 1218 | } |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1219 | // Before we get to 500 we need to flush the operations list |
| 1220 | if (operations.size() > 0 && operations.size() % batchSize == 0) { |
| 1221 | if (!applyJoinOperations(resolver, operations)) { |
| 1222 | return; |
| 1223 | } |
| 1224 | operations.clear(); |
| 1225 | } |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1226 | } |
| 1227 | } |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1228 | if (operations.size() > 0 && !applyJoinOperations(resolver, operations)) { |
| 1229 | return; |
| 1230 | } |
| 1231 | showToast(R.string.contactsJoinedMessage); |
| 1232 | } |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1233 | |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1234 | /** Returns true if the batch was successfully applied and false otherwise. */ |
| 1235 | private boolean applyJoinOperations(ContentResolver resolver, |
| 1236 | ArrayList<ContentProviderOperation> operations) { |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1237 | try { |
| 1238 | resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1239 | return true; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1240 | } catch (RemoteException | OperationApplicationException e) { |
| 1241 | Log.e(TAG, "Failed to apply aggregation exception batch", e); |
| 1242 | showToast(R.string.contactSavedErrorToast); |
Walter Jang | 0653de3 | 2015-07-24 12:12:40 -0700 | [diff] [blame] | 1243 | return false; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | private void joinContacts(Intent intent) { |
| 1249 | long contactId1 = intent.getLongExtra(EXTRA_CONTACT_ID1, -1); |
| 1250 | long contactId2 = intent.getLongExtra(EXTRA_CONTACT_ID2, -1); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1251 | |
| 1252 | // Load raw contact IDs for all raw contacts involved - currently edited and selected |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1253 | // in the join UIs. |
| 1254 | long rawContactIds[] = getRawContactIdsForAggregation(contactId1, contactId2); |
| 1255 | if (rawContactIds == null) { |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1256 | Log.e(TAG, "Invalid arguments for joinContacts request"); |
Jay Shrauner | c12a280 | 2014-11-24 10:07:31 -0800 | [diff] [blame] | 1257 | return; |
| 1258 | } |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1259 | |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1260 | ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1261 | |
| 1262 | // For each pair of raw contacts, insert an aggregation exception |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1263 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1264 | for (int j = 0; j < rawContactIds.length; j++) { |
| 1265 | if (i != j) { |
| 1266 | buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]); |
| 1267 | } |
| 1268 | } |
| 1269 | } |
| 1270 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1271 | final ContentResolver resolver = getContentResolver(); |
| 1272 | |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1273 | // Use the name for contactId1 as the name for the newly aggregated contact. |
| 1274 | final Uri contactId1Uri = ContentUris.withAppendedId( |
| 1275 | Contacts.CONTENT_URI, contactId1); |
| 1276 | final Uri entityUri = Uri.withAppendedPath( |
| 1277 | contactId1Uri, Contacts.Entity.CONTENT_DIRECTORY); |
| 1278 | Cursor c = resolver.query(entityUri, |
| 1279 | ContactEntityQuery.PROJECTION, ContactEntityQuery.SELECTION, null, null); |
| 1280 | if (c == null) { |
| 1281 | Log.e(TAG, "Unable to open Contacts DB cursor"); |
| 1282 | showToast(R.string.contactSavedErrorToast); |
| 1283 | return; |
| 1284 | } |
| 1285 | long dataIdToAddSuperPrimary = -1; |
| 1286 | try { |
| 1287 | if (c.moveToFirst()) { |
| 1288 | dataIdToAddSuperPrimary = c.getLong(ContactEntityQuery.DATA_ID); |
| 1289 | } |
| 1290 | } finally { |
| 1291 | c.close(); |
| 1292 | } |
| 1293 | |
| 1294 | // Mark the name from contactId1 IS_SUPER_PRIMARY to make sure that the contact |
| 1295 | // display name does not change as a result of the join. |
| 1296 | if (dataIdToAddSuperPrimary != -1) { |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1297 | Builder builder = ContentProviderOperation.newUpdate( |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1298 | ContentUris.withAppendedId(Data.CONTENT_URI, dataIdToAddSuperPrimary)); |
| 1299 | builder.withValue(Data.IS_SUPER_PRIMARY, 1); |
| 1300 | builder.withValue(Data.IS_PRIMARY, 1); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1301 | operations.add(builder.build()); |
| 1302 | } |
| 1303 | |
| 1304 | boolean success = false; |
| 1305 | // Apply all aggregation exceptions as one batch |
| 1306 | try { |
| 1307 | resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1308 | showToast(R.string.contactsJoinedMessage); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1309 | success = true; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1310 | } catch (RemoteException | OperationApplicationException e) { |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1311 | Log.e(TAG, "Failed to apply aggregation exception batch", e); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1312 | showToast(R.string.contactSavedErrorToast); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 1316 | if (success) { |
| 1317 | Uri uri = RawContacts.getContactLookupUri(resolver, |
| 1318 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactIds[0])); |
| 1319 | callbackIntent.setData(uri); |
| 1320 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1321 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1322 | } |
| 1323 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1324 | private long[] getRawContactIdsForAggregation(long[] contactIds) { |
| 1325 | if (contactIds == null) { |
| 1326 | return null; |
| 1327 | } |
| 1328 | |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1329 | final ContentResolver resolver = getContentResolver(); |
| 1330 | long rawContactIds[]; |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1331 | |
| 1332 | final StringBuilder queryBuilder = new StringBuilder(); |
| 1333 | final String stringContactIds[] = new String[contactIds.length]; |
| 1334 | for (int i = 0; i < contactIds.length; i++) { |
| 1335 | queryBuilder.append(RawContacts.CONTACT_ID + "=?"); |
| 1336 | stringContactIds[i] = String.valueOf(contactIds[i]); |
| 1337 | if (contactIds[i] == -1) { |
| 1338 | return null; |
| 1339 | } |
| 1340 | if (i == contactIds.length -1) { |
| 1341 | break; |
| 1342 | } |
| 1343 | queryBuilder.append(" OR "); |
| 1344 | } |
| 1345 | |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1346 | final Cursor c = resolver.query(RawContacts.CONTENT_URI, |
| 1347 | JoinContactQuery.PROJECTION, |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1348 | queryBuilder.toString(), |
| 1349 | stringContactIds, null); |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1350 | if (c == null) { |
| 1351 | Log.e(TAG, "Unable to open Contacts DB cursor"); |
| 1352 | showToast(R.string.contactSavedErrorToast); |
| 1353 | return null; |
| 1354 | } |
| 1355 | try { |
| 1356 | if (c.getCount() < 2) { |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1357 | Log.e(TAG, "Not enough raw contacts to aggregate together."); |
Brian Attwell | 548f5c6 | 2015-01-27 17:46:46 -0800 | [diff] [blame] | 1358 | return null; |
| 1359 | } |
| 1360 | rawContactIds = new long[c.getCount()]; |
| 1361 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1362 | c.moveToPosition(i); |
| 1363 | long rawContactId = c.getLong(JoinContactQuery._ID); |
| 1364 | rawContactIds[i] = rawContactId; |
| 1365 | } |
| 1366 | } finally { |
| 1367 | c.close(); |
| 1368 | } |
| 1369 | return rawContactIds; |
| 1370 | } |
| 1371 | |
Brian Attwell | d3946ca | 2015-03-03 11:13:49 -0800 | [diff] [blame] | 1372 | private long[] getRawContactIdsForAggregation(long contactId1, long contactId2) { |
| 1373 | return getRawContactIdsForAggregation(new long[] {contactId1, contactId2}); |
| 1374 | } |
| 1375 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1376 | /** |
| 1377 | * Construct a {@link AggregationExceptions#TYPE_KEEP_TOGETHER} ContentProviderOperation. |
| 1378 | */ |
| 1379 | private void buildJoinContactDiff(ArrayList<ContentProviderOperation> operations, |
| 1380 | long rawContactId1, long rawContactId2) { |
| 1381 | Builder builder = |
| 1382 | ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI); |
| 1383 | builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER); |
| 1384 | builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1); |
| 1385 | builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2); |
| 1386 | operations.add(builder.build()); |
| 1387 | } |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1388 | |
| 1389 | /** |
| 1390 | * Shows a toast on the UI thread. |
| 1391 | */ |
| 1392 | private void showToast(final int message) { |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1393 | mMainHandler.post(new Runnable() { |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1394 | |
| 1395 | @Override |
| 1396 | public void run() { |
| 1397 | Toast.makeText(ContactSaveService.this, message, Toast.LENGTH_LONG).show(); |
| 1398 | } |
| 1399 | }); |
| 1400 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1401 | |
| 1402 | private void deliverCallback(final Intent callbackIntent) { |
| 1403 | mMainHandler.post(new Runnable() { |
| 1404 | |
| 1405 | @Override |
| 1406 | public void run() { |
| 1407 | deliverCallbackOnUiThread(callbackIntent); |
| 1408 | } |
| 1409 | }); |
| 1410 | } |
| 1411 | |
| 1412 | void deliverCallbackOnUiThread(final Intent callbackIntent) { |
| 1413 | // TODO: this assumes that if there are multiple instances of the same |
| 1414 | // activity registered, the last one registered is the one waiting for |
| 1415 | // the callback. Validity of this assumption needs to be verified. |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 1416 | for (Listener listener : sListeners) { |
| 1417 | if (callbackIntent.getComponent().equals( |
| 1418 | ((Activity) listener).getIntent().getComponent())) { |
| 1419 | listener.onServiceCompleted(callbackIntent); |
| 1420 | return; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | } |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 1424 | } |