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