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