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