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