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; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 31 | import android.net.Uri; |
Daniel Lehmann | c42ea4e | 2012-02-16 21:22:37 -0800 | [diff] [blame] | 32 | import android.os.Bundle; |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.Looper; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 35 | import android.os.Parcelable; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 36 | import android.os.RemoteException; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 37 | import android.provider.ContactsContract; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 38 | import android.provider.ContactsContract.AggregationExceptions; |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 39 | import android.provider.ContactsContract.CommonDataKinds.GroupMembership; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 40 | import android.provider.ContactsContract.Contacts; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 41 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 42 | import android.provider.ContactsContract.Groups; |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 43 | import android.provider.ContactsContract.PinnedPositions; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 44 | import android.provider.ContactsContract.Profile; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 45 | import android.provider.ContactsContract.RawContacts; |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 46 | import android.provider.ContactsContract.RawContactsEntity; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 47 | import android.util.Log; |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 48 | import android.widget.Toast; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 49 | |
Chiao Cheng | d7ca03e | 2012-10-24 15:14:08 -0700 | [diff] [blame] | 50 | import com.android.contacts.common.database.ContactUpdateUtils; |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 51 | import com.android.contacts.common.model.AccountTypeManager; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 52 | import com.android.contacts.common.model.RawContactDelta; |
| 53 | import com.android.contacts.common.model.RawContactDeltaList; |
| 54 | import com.android.contacts.common.model.RawContactModifier; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 55 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 56 | import com.android.contacts.util.ContactPhotoUtils; |
| 57 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 58 | import com.google.common.collect.Lists; |
| 59 | import com.google.common.collect.Sets; |
| 60 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 61 | import java.io.File; |
| 62 | import java.io.FileInputStream; |
| 63 | import java.io.FileOutputStream; |
| 64 | import java.io.IOException; |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 65 | import java.io.InputStream; |
Daniel Lehmann | c42ea4e | 2012-02-16 21:22:37 -0800 | [diff] [blame] | 66 | import java.util.ArrayList; |
| 67 | import java.util.HashSet; |
| 68 | import java.util.List; |
| 69 | import java.util.concurrent.CopyOnWriteArrayList; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 70 | |
Dmitri Plotnikov | 18ffaa2 | 2010-12-03 14:28:00 -0800 | [diff] [blame] | 71 | /** |
| 72 | * A service responsible for saving changes to the content provider. |
| 73 | */ |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 74 | public class ContactSaveService extends IntentService { |
| 75 | private static final String TAG = "ContactSaveService"; |
| 76 | |
Katherine Kuan | a007e44 | 2011-07-07 09:25:34 -0700 | [diff] [blame] | 77 | /** Set to true in order to view logs on content provider operations */ |
| 78 | private static final boolean DEBUG = false; |
| 79 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 80 | public static final String ACTION_NEW_RAW_CONTACT = "newRawContact"; |
| 81 | |
| 82 | public static final String EXTRA_ACCOUNT_NAME = "accountName"; |
| 83 | public static final String EXTRA_ACCOUNT_TYPE = "accountType"; |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 84 | public static final String EXTRA_DATA_SET = "dataSet"; |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 85 | public static final String EXTRA_CONTENT_VALUES = "contentValues"; |
| 86 | public static final String EXTRA_CALLBACK_INTENT = "callbackIntent"; |
| 87 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 88 | public static final String ACTION_SAVE_CONTACT = "saveContact"; |
| 89 | public static final String EXTRA_CONTACT_STATE = "state"; |
| 90 | public static final String EXTRA_SAVE_MODE = "saveMode"; |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 91 | public static final String EXTRA_SAVE_IS_PROFILE = "saveIsProfile"; |
Dave Santoro | 36d24d7 | 2011-09-25 17:08:10 -0700 | [diff] [blame] | 92 | public static final String EXTRA_SAVE_SUCCEEDED = "saveSucceeded"; |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 93 | public static final String EXTRA_UPDATED_PHOTOS = "updatedPhotos"; |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 94 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 95 | public static final String ACTION_CREATE_GROUP = "createGroup"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 96 | public static final String ACTION_RENAME_GROUP = "renameGroup"; |
| 97 | public static final String ACTION_DELETE_GROUP = "deleteGroup"; |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 98 | public static final String ACTION_UPDATE_GROUP = "updateGroup"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 99 | public static final String EXTRA_GROUP_ID = "groupId"; |
| 100 | public static final String EXTRA_GROUP_LABEL = "groupLabel"; |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 101 | public static final String EXTRA_RAW_CONTACTS_TO_ADD = "rawContactsToAdd"; |
| 102 | public static final String EXTRA_RAW_CONTACTS_TO_REMOVE = "rawContactsToRemove"; |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 103 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 104 | public static final String ACTION_SET_STARRED = "setStarred"; |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 105 | public static final String ACTION_DELETE_CONTACT = "delete"; |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 106 | public static final String EXTRA_CONTACT_URI = "contactUri"; |
| 107 | public static final String EXTRA_STARRED_FLAG = "starred"; |
| 108 | |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 109 | public static final String ACTION_SET_SUPER_PRIMARY = "setSuperPrimary"; |
| 110 | public static final String ACTION_CLEAR_PRIMARY = "clearPrimary"; |
| 111 | public static final String EXTRA_DATA_ID = "dataId"; |
| 112 | |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 113 | public static final String ACTION_JOIN_CONTACTS = "joinContacts"; |
| 114 | public static final String EXTRA_CONTACT_ID1 = "contactId1"; |
| 115 | public static final String EXTRA_CONTACT_ID2 = "contactId2"; |
| 116 | public static final String EXTRA_CONTACT_WRITABLE = "contactWritable"; |
| 117 | |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 118 | public static final String ACTION_SET_SEND_TO_VOICEMAIL = "sendToVoicemail"; |
| 119 | public static final String EXTRA_SEND_TO_VOICEMAIL_FLAG = "sendToVoicemailFlag"; |
| 120 | |
| 121 | public static final String ACTION_SET_RINGTONE = "setRingtone"; |
| 122 | public static final String EXTRA_CUSTOM_RINGTONE = "customRingtone"; |
| 123 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 124 | private static final HashSet<String> ALLOWED_DATA_COLUMNS = Sets.newHashSet( |
| 125 | Data.MIMETYPE, |
| 126 | Data.IS_PRIMARY, |
| 127 | Data.DATA1, |
| 128 | Data.DATA2, |
| 129 | Data.DATA3, |
| 130 | Data.DATA4, |
| 131 | Data.DATA5, |
| 132 | Data.DATA6, |
| 133 | Data.DATA7, |
| 134 | Data.DATA8, |
| 135 | Data.DATA9, |
| 136 | Data.DATA10, |
| 137 | Data.DATA11, |
| 138 | Data.DATA12, |
| 139 | Data.DATA13, |
| 140 | Data.DATA14, |
| 141 | Data.DATA15 |
| 142 | ); |
| 143 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 144 | private static final int PERSIST_TRIES = 3; |
| 145 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 146 | public interface Listener { |
| 147 | public void onServiceCompleted(Intent callbackIntent); |
| 148 | } |
| 149 | |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 150 | private static final CopyOnWriteArrayList<Listener> sListeners = |
| 151 | new CopyOnWriteArrayList<Listener>(); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 152 | |
| 153 | private Handler mMainHandler; |
| 154 | |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 155 | public ContactSaveService() { |
| 156 | super(TAG); |
| 157 | setIntentRedelivery(true); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 158 | mMainHandler = new Handler(Looper.getMainLooper()); |
| 159 | } |
| 160 | |
| 161 | public static void registerListener(Listener listener) { |
| 162 | if (!(listener instanceof Activity)) { |
| 163 | throw new ClassCastException("Only activities can be registered to" |
| 164 | + " receive callback from " + ContactSaveService.class.getName()); |
| 165 | } |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 166 | sListeners.add(0, listener); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | public static void unregisterListener(Listener listener) { |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 170 | sListeners.remove(listener); |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Override |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 174 | public Object getSystemService(String name) { |
| 175 | Object service = super.getSystemService(name); |
| 176 | if (service != null) { |
| 177 | return service; |
| 178 | } |
| 179 | |
| 180 | return getApplicationContext().getSystemService(name); |
| 181 | } |
| 182 | |
| 183 | @Override |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 184 | protected void onHandleIntent(Intent intent) { |
Daisuke Miyakawa | 2f21c44 | 2012-03-22 19:12:31 -0700 | [diff] [blame] | 185 | // Call an appropriate method. If we're sure it affects how incoming phone calls are |
| 186 | // handled, then notify the fact to in-call screen. |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 187 | String action = intent.getAction(); |
| 188 | if (ACTION_NEW_RAW_CONTACT.equals(action)) { |
| 189 | createRawContact(intent); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 190 | } else if (ACTION_SAVE_CONTACT.equals(action)) { |
| 191 | saveContact(intent); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 192 | } else if (ACTION_CREATE_GROUP.equals(action)) { |
| 193 | createGroup(intent); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 194 | } else if (ACTION_RENAME_GROUP.equals(action)) { |
| 195 | renameGroup(intent); |
| 196 | } else if (ACTION_DELETE_GROUP.equals(action)) { |
| 197 | deleteGroup(intent); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 198 | } else if (ACTION_UPDATE_GROUP.equals(action)) { |
| 199 | updateGroup(intent); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 200 | } else if (ACTION_SET_STARRED.equals(action)) { |
| 201 | setStarred(intent); |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 202 | } else if (ACTION_SET_SUPER_PRIMARY.equals(action)) { |
| 203 | setSuperPrimary(intent); |
| 204 | } else if (ACTION_CLEAR_PRIMARY.equals(action)) { |
| 205 | clearPrimary(intent); |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 206 | } else if (ACTION_DELETE_CONTACT.equals(action)) { |
| 207 | deleteContact(intent); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 208 | } else if (ACTION_JOIN_CONTACTS.equals(action)) { |
| 209 | joinContacts(intent); |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 210 | } else if (ACTION_SET_SEND_TO_VOICEMAIL.equals(action)) { |
| 211 | setSendToVoicemail(intent); |
| 212 | } else if (ACTION_SET_RINGTONE.equals(action)) { |
| 213 | setRingtone(intent); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 217 | /** |
| 218 | * Creates an intent that can be sent to this service to create a new raw contact |
| 219 | * using data presented as a set of ContentValues. |
| 220 | */ |
| 221 | public static Intent createNewRawContactIntent(Context context, |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 222 | ArrayList<ContentValues> values, AccountWithDataSet account, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 223 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 224 | Intent serviceIntent = new Intent( |
| 225 | context, ContactSaveService.class); |
| 226 | serviceIntent.setAction(ContactSaveService.ACTION_NEW_RAW_CONTACT); |
| 227 | if (account != null) { |
| 228 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_NAME, account.name); |
| 229 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_TYPE, account.type); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 230 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_SET, account.dataSet); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 231 | } |
| 232 | serviceIntent.putParcelableArrayListExtra( |
| 233 | ContactSaveService.EXTRA_CONTENT_VALUES, values); |
| 234 | |
| 235 | // Callback intent will be invoked by the service once the new contact is |
| 236 | // created. The service will put the URI of the new contact as "data" on |
| 237 | // the callback intent. |
| 238 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 239 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 240 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 241 | return serviceIntent; |
| 242 | } |
| 243 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 244 | private void createRawContact(Intent intent) { |
| 245 | String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME); |
| 246 | String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 247 | String dataSet = intent.getStringExtra(EXTRA_DATA_SET); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 248 | List<ContentValues> valueList = intent.getParcelableArrayListExtra(EXTRA_CONTENT_VALUES); |
| 249 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 250 | |
| 251 | ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
| 252 | operations.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) |
| 253 | .withValue(RawContacts.ACCOUNT_NAME, accountName) |
| 254 | .withValue(RawContacts.ACCOUNT_TYPE, accountType) |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 255 | .withValue(RawContacts.DATA_SET, dataSet) |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 256 | .build()); |
| 257 | |
| 258 | int size = valueList.size(); |
| 259 | for (int i = 0; i < size; i++) { |
| 260 | ContentValues values = valueList.get(i); |
| 261 | values.keySet().retainAll(ALLOWED_DATA_COLUMNS); |
| 262 | operations.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) |
| 263 | .withValueBackReference(Data.RAW_CONTACT_ID, 0) |
| 264 | .withValues(values) |
| 265 | .build()); |
| 266 | } |
| 267 | |
| 268 | ContentResolver resolver = getContentResolver(); |
| 269 | ContentProviderResult[] results; |
| 270 | try { |
| 271 | results = resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
| 272 | } catch (Exception e) { |
| 273 | throw new RuntimeException("Failed to store new contact", e); |
| 274 | } |
| 275 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 276 | Uri rawContactUri = results[0].uri; |
| 277 | callbackIntent.setData(RawContacts.getContactLookupUri(resolver, rawContactUri)); |
| 278 | |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 279 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 282 | /** |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 283 | * Creates an intent that can be sent to this service to create a new raw contact |
| 284 | * using data presented as a set of ContentValues. |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 285 | * This variant is more convenient to use when there is only one photo that can |
| 286 | * possibly be updated, as in the Contact Details screen. |
| 287 | * @param rawContactId identifies a writable raw-contact whose photo is to be updated. |
| 288 | * @param updatedPhotoPath denotes a temporary file containing the contact's new photo. |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 289 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 290 | public static Intent createSaveContactIntent(Context context, RawContactDeltaList state, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 291 | String saveModeExtraKey, int saveMode, boolean isProfile, |
| 292 | Class<? extends Activity> callbackActivity, String callbackAction, long rawContactId, |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 293 | Uri updatedPhotoPath) { |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 294 | Bundle bundle = new Bundle(); |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 295 | bundle.putParcelable(String.valueOf(rawContactId), updatedPhotoPath); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 296 | return createSaveContactIntent(context, state, saveModeExtraKey, saveMode, isProfile, |
| 297 | callbackActivity, callbackAction, bundle); |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Creates an intent that can be sent to this service to create a new raw contact |
| 302 | * using data presented as a set of ContentValues. |
| 303 | * This variant is used when multiple contacts' photos may be updated, as in the |
| 304 | * Contact Editor. |
| 305 | * @param updatedPhotos maps each raw-contact's ID to the file-path of the new photo. |
| 306 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 307 | public static Intent createSaveContactIntent(Context context, RawContactDeltaList state, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 308 | String saveModeExtraKey, int saveMode, boolean isProfile, |
| 309 | Class<? extends Activity> callbackActivity, String callbackAction, |
| 310 | Bundle updatedPhotos) { |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 311 | Intent serviceIntent = new Intent( |
| 312 | context, ContactSaveService.class); |
| 313 | serviceIntent.setAction(ContactSaveService.ACTION_SAVE_CONTACT); |
| 314 | serviceIntent.putExtra(EXTRA_CONTACT_STATE, (Parcelable) state); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 315 | serviceIntent.putExtra(EXTRA_SAVE_IS_PROFILE, isProfile); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 316 | if (updatedPhotos != null) { |
| 317 | serviceIntent.putExtra(EXTRA_UPDATED_PHOTOS, (Parcelable) updatedPhotos); |
| 318 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 319 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 320 | if (callbackActivity != null) { |
| 321 | // Callback intent will be invoked by the service once the contact is |
| 322 | // saved. The service will put the URI of the new contact as "data" on |
| 323 | // the callback intent. |
| 324 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 325 | callbackIntent.putExtra(saveModeExtraKey, saveMode); |
| 326 | callbackIntent.setAction(callbackAction); |
| 327 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 328 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 329 | return serviceIntent; |
| 330 | } |
| 331 | |
| 332 | private void saveContact(Intent intent) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 333 | RawContactDeltaList state = intent.getParcelableExtra(EXTRA_CONTACT_STATE); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 334 | boolean isProfile = intent.getBooleanExtra(EXTRA_SAVE_IS_PROFILE, false); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 335 | Bundle updatedPhotos = intent.getParcelableExtra(EXTRA_UPDATED_PHOTOS); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 336 | |
| 337 | // Trim any empty fields, and RawContacts, before persisting |
| 338 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 339 | RawContactModifier.trimEmpty(state, accountTypes); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 340 | |
| 341 | Uri lookupUri = null; |
| 342 | |
| 343 | final ContentResolver resolver = getContentResolver(); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 344 | boolean succeeded = false; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 345 | |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 346 | // Keep track of the id of a newly raw-contact (if any... there can be at most one). |
| 347 | long insertedRawContactId = -1; |
| 348 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 349 | // Attempt to persist changes |
| 350 | int tries = 0; |
| 351 | while (tries++ < PERSIST_TRIES) { |
| 352 | try { |
| 353 | // Build operations and try applying |
| 354 | final ArrayList<ContentProviderOperation> diff = state.buildDiff(); |
Katherine Kuan | a007e44 | 2011-07-07 09:25:34 -0700 | [diff] [blame] | 355 | if (DEBUG) { |
| 356 | Log.v(TAG, "Content Provider Operations:"); |
| 357 | for (ContentProviderOperation operation : diff) { |
| 358 | Log.v(TAG, operation.toString()); |
| 359 | } |
| 360 | } |
| 361 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 362 | ContentProviderResult[] results = null; |
| 363 | if (!diff.isEmpty()) { |
| 364 | results = resolver.applyBatch(ContactsContract.AUTHORITY, diff); |
| 365 | } |
| 366 | |
| 367 | final long rawContactId = getRawContactId(state, diff, results); |
| 368 | if (rawContactId == -1) { |
| 369 | throw new IllegalStateException("Could not determine RawContact ID after save"); |
| 370 | } |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 371 | // We don't have to check to see if the value is still -1. If we reach here, |
| 372 | // the previous loop iteration didn't succeed, so any ID that we obtained is bogus. |
| 373 | insertedRawContactId = getInsertedRawContactId(diff, results); |
Dave Santoro | 7c34c0a | 2011-09-12 14:58:20 -0700 | [diff] [blame] | 374 | if (isProfile) { |
| 375 | // Since the profile supports local raw contacts, which may have been completely |
| 376 | // removed if all information was removed, we need to do a special query to |
| 377 | // get the lookup URI for the profile contact (if it still exists). |
| 378 | Cursor c = resolver.query(Profile.CONTENT_URI, |
| 379 | new String[] {Contacts._ID, Contacts.LOOKUP_KEY}, |
| 380 | null, null, null); |
| 381 | try { |
Erik | 162b7e3 | 2011-09-20 15:23:55 -0700 | [diff] [blame] | 382 | if (c.moveToFirst()) { |
| 383 | final long contactId = c.getLong(0); |
| 384 | final String lookupKey = c.getString(1); |
| 385 | lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
| 386 | } |
Dave Santoro | 7c34c0a | 2011-09-12 14:58:20 -0700 | [diff] [blame] | 387 | } finally { |
| 388 | c.close(); |
| 389 | } |
| 390 | } else { |
| 391 | final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, |
| 392 | rawContactId); |
| 393 | lookupUri = RawContacts.getContactLookupUri(resolver, rawContactUri); |
| 394 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 395 | Log.v(TAG, "Saved contact. New URI: " + lookupUri); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 396 | |
| 397 | // We can change this back to false later, if we fail to save the contact photo. |
| 398 | succeeded = true; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 399 | break; |
| 400 | |
| 401 | } catch (RemoteException e) { |
| 402 | // Something went wrong, bail without success |
| 403 | Log.e(TAG, "Problem persisting user edits", e); |
| 404 | break; |
| 405 | |
Jay Shrauner | 57fca18 | 2014-01-17 14:20:50 -0800 | [diff] [blame] | 406 | } catch (IllegalArgumentException e) { |
| 407 | // This is thrown by applyBatch on malformed requests |
| 408 | Log.e(TAG, "Problem persisting user edits", e); |
| 409 | showToast(R.string.contactSavedErrorToast); |
| 410 | break; |
| 411 | |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 412 | } catch (OperationApplicationException e) { |
| 413 | // Version consistency failed, re-parent change and try again |
| 414 | Log.w(TAG, "Version consistency failed, re-parenting: " + e.toString()); |
| 415 | final StringBuilder sb = new StringBuilder(RawContacts._ID + " IN("); |
| 416 | boolean first = true; |
| 417 | final int count = state.size(); |
| 418 | for (int i = 0; i < count; i++) { |
| 419 | Long rawContactId = state.getRawContactId(i); |
| 420 | if (rawContactId != null && rawContactId != -1) { |
| 421 | if (!first) { |
| 422 | sb.append(','); |
| 423 | } |
| 424 | sb.append(rawContactId); |
| 425 | first = false; |
| 426 | } |
| 427 | } |
| 428 | sb.append(")"); |
| 429 | |
| 430 | if (first) { |
Brian Attwell | 3b6c628 | 2014-02-12 17:53:31 -0800 | [diff] [blame] | 431 | throw new IllegalStateException( |
| 432 | "Version consistency failed for a new contact", e); |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 433 | } |
| 434 | |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 435 | final RawContactDeltaList newState = RawContactDeltaList.fromQuery( |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 436 | isProfile |
| 437 | ? RawContactsEntity.PROFILE_CONTENT_URI |
| 438 | : RawContactsEntity.CONTENT_URI, |
| 439 | resolver, sb.toString(), null, null); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 440 | state = RawContactDeltaList.mergeAfter(newState, state); |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 441 | |
| 442 | // Update the new state to use profile URIs if appropriate. |
| 443 | if (isProfile) { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 444 | for (RawContactDelta delta : state) { |
Dave Santoro | c90f95e | 2011-09-07 17:47:15 -0700 | [diff] [blame] | 445 | delta.setProfileQueryUri(); |
| 446 | } |
| 447 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 451 | // Now save any updated photos. We do this at the end to ensure that |
| 452 | // the ContactProvider already knows about newly-created contacts. |
| 453 | if (updatedPhotos != null) { |
| 454 | for (String key : updatedPhotos.keySet()) { |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 455 | Uri photoUri = updatedPhotos.getParcelable(key); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 456 | long rawContactId = Long.parseLong(key); |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 457 | |
| 458 | // If the raw-contact ID is negative, we are saving a new raw-contact; |
| 459 | // replace the bogus ID with the new one that we actually saved the contact at. |
| 460 | if (rawContactId < 0) { |
| 461 | rawContactId = insertedRawContactId; |
| 462 | if (rawContactId == -1) { |
| 463 | throw new IllegalStateException( |
| 464 | "Could not determine RawContact ID for image insertion"); |
| 465 | } |
| 466 | } |
| 467 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 468 | if (!saveUpdatedPhoto(rawContactId, photoUri)) succeeded = false; |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 472 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 473 | if (callbackIntent != null) { |
| 474 | if (succeeded) { |
| 475 | // Mark the intent to indicate that the save was successful (even if the lookup URI |
| 476 | // is now null). For local contacts or the local profile, it's possible that the |
| 477 | // save triggered removal of the contact, so no lookup URI would exist.. |
| 478 | callbackIntent.putExtra(EXTRA_SAVE_SUCCEEDED, true); |
| 479 | } |
| 480 | callbackIntent.setData(lookupUri); |
| 481 | deliverCallback(callbackIntent); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 482 | } |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 485 | /** |
| 486 | * Save updated photo for the specified raw-contact. |
| 487 | * @return true for success, false for failure |
| 488 | */ |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 489 | private boolean saveUpdatedPhoto(long rawContactId, Uri photoUri) { |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 490 | final Uri outputUri = Uri.withAppendedPath( |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 491 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), |
| 492 | RawContacts.DisplayPhoto.CONTENT_DIRECTORY); |
| 493 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 494 | return ContactPhotoUtils.savePhotoFromUriToUri(this, photoUri, outputUri, true); |
Josh Gargus | e692e01 | 2012-01-18 14:53:11 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 497 | /** |
| 498 | * Find the ID of an existing or newly-inserted raw-contact. If none exists, return -1. |
| 499 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 500 | private long getRawContactId(RawContactDeltaList state, |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 501 | final ArrayList<ContentProviderOperation> diff, |
| 502 | final ContentProviderResult[] results) { |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 503 | long existingRawContactId = state.findRawContactId(); |
| 504 | if (existingRawContactId != -1) { |
| 505 | return existingRawContactId; |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Josh Gargus | ef15c8e | 2012-01-30 16:42:02 -0800 | [diff] [blame] | 508 | return getInsertedRawContactId(diff, results); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Find the ID of a newly-inserted raw-contact. If none exists, return -1. |
| 513 | */ |
| 514 | private long getInsertedRawContactId( |
| 515 | final ArrayList<ContentProviderOperation> diff, |
| 516 | final ContentProviderResult[] results) { |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 517 | final int diffSize = diff.size(); |
Jay Shrauner | 3d7edc3 | 2014-11-10 09:58:23 -0800 | [diff] [blame^] | 518 | final int numResults = results.length; |
| 519 | for (int i = 0; i < diffSize && i < numResults; i++) { |
Dmitri Plotnikov | a011414 | 2011-02-15 13:53:21 -0800 | [diff] [blame] | 520 | ContentProviderOperation operation = diff.get(i); |
| 521 | if (operation.getType() == ContentProviderOperation.TYPE_INSERT |
| 522 | && operation.getUri().getEncodedPath().contains( |
| 523 | RawContacts.CONTENT_URI.getEncodedPath())) { |
| 524 | return ContentUris.parseId(results[i].uri); |
| 525 | } |
| 526 | } |
| 527 | return -1; |
| 528 | } |
| 529 | |
| 530 | /** |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 531 | * Creates an intent that can be sent to this service to create a new group as |
| 532 | * well as add new members at the same time. |
| 533 | * |
| 534 | * @param context of the application |
| 535 | * @param account in which the group should be created |
| 536 | * @param label is the name of the group (cannot be null) |
| 537 | * @param rawContactsToAdd is an array of raw contact IDs for contacts that |
| 538 | * should be added to the group |
| 539 | * @param callbackActivity is the activity to send the callback intent to |
| 540 | * @param callbackAction is the intent action for the callback intent |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 541 | */ |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 542 | public static Intent createNewGroupIntent(Context context, AccountWithDataSet account, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 543 | String label, long[] rawContactsToAdd, Class<? extends Activity> callbackActivity, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 544 | String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 545 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 546 | serviceIntent.setAction(ContactSaveService.ACTION_CREATE_GROUP); |
| 547 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_TYPE, account.type); |
| 548 | serviceIntent.putExtra(ContactSaveService.EXTRA_ACCOUNT_NAME, account.name); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 549 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_SET, account.dataSet); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 550 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, label); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 551 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_ADD, rawContactsToAdd); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 552 | |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 553 | // 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] | 554 | // created. |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 555 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 556 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 557 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 558 | |
Dmitri Plotnikov | caf0bc7 | 2010-09-03 15:16:21 -0700 | [diff] [blame] | 559 | return serviceIntent; |
| 560 | } |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 561 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 562 | private void createGroup(Intent intent) { |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 563 | String accountType = intent.getStringExtra(EXTRA_ACCOUNT_TYPE); |
| 564 | String accountName = intent.getStringExtra(EXTRA_ACCOUNT_NAME); |
| 565 | String dataSet = intent.getStringExtra(EXTRA_DATA_SET); |
| 566 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 567 | final long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 568 | |
| 569 | ContentValues values = new ContentValues(); |
| 570 | values.put(Groups.ACCOUNT_TYPE, accountType); |
| 571 | values.put(Groups.ACCOUNT_NAME, accountName); |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 572 | values.put(Groups.DATA_SET, dataSet); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 573 | values.put(Groups.TITLE, label); |
| 574 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 575 | final ContentResolver resolver = getContentResolver(); |
| 576 | |
| 577 | // Create the new group |
| 578 | final Uri groupUri = resolver.insert(Groups.CONTENT_URI, values); |
| 579 | |
| 580 | // If there's no URI, then the insertion failed. Abort early because group members can't be |
| 581 | // added if the group doesn't exist |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 582 | if (groupUri == null) { |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 583 | Log.e(TAG, "Couldn't create group with label " + label); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 587 | // Add new group members |
| 588 | addMembersToGroup(resolver, rawContactsToAdd, ContentUris.parseId(groupUri)); |
| 589 | |
| 590 | // TODO: Move this into the contact editor where it belongs. This needs to be integrated |
| 591 | // with the way other intent extras that are passed to the {@link ContactEditorActivity}. |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 592 | values.clear(); |
| 593 | values.put(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); |
| 594 | values.put(GroupMembership.GROUP_ROW_ID, ContentUris.parseId(groupUri)); |
| 595 | |
| 596 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 597 | callbackIntent.setData(groupUri); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 598 | // TODO: This can be taken out when the above TODO is addressed |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 599 | callbackIntent.putExtra(ContactsContract.Intents.Insert.DATA, Lists.newArrayList(values)); |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 600 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 604 | * 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] | 605 | */ |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 606 | public static Intent createGroupRenameIntent(Context context, long groupId, String newLabel, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 607 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 608 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 609 | serviceIntent.setAction(ContactSaveService.ACTION_RENAME_GROUP); |
| 610 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
| 611 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, newLabel); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 612 | |
| 613 | // Callback intent will be invoked by the service once the group is renamed. |
| 614 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 615 | callbackIntent.setAction(callbackAction); |
| 616 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 617 | |
Dmitri Plotnikov | 1ac58b6 | 2010-11-19 16:12:09 -0800 | [diff] [blame] | 618 | return serviceIntent; |
| 619 | } |
| 620 | |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 621 | private void renameGroup(Intent intent) { |
| 622 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 623 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
| 624 | |
| 625 | if (groupId == -1) { |
| 626 | Log.e(TAG, "Invalid arguments for renameGroup request"); |
| 627 | return; |
| 628 | } |
| 629 | |
| 630 | ContentValues values = new ContentValues(); |
| 631 | values.put(Groups.TITLE, label); |
Katherine Kuan | c6b8afe | 2011-06-22 19:03:50 -0700 | [diff] [blame] | 632 | final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); |
| 633 | getContentResolver().update(groupUri, values, null, null); |
| 634 | |
| 635 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 636 | callbackIntent.setData(groupUri); |
| 637 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 641 | * 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] | 642 | */ |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 643 | public static Intent createGroupDeletionIntent(Context context, long groupId) { |
| 644 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 645 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_GROUP); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 646 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 647 | return serviceIntent; |
| 648 | } |
| 649 | |
| 650 | private void deleteGroup(Intent intent) { |
| 651 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 652 | if (groupId == -1) { |
| 653 | Log.e(TAG, "Invalid arguments for deleteGroup request"); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | getContentResolver().delete( |
| 658 | ContentUris.withAppendedId(Groups.CONTENT_URI, groupId), null, null); |
| 659 | } |
| 660 | |
| 661 | /** |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 662 | * Creates an intent that can be sent to this service to rename a group as |
| 663 | * well as add and remove members from the group. |
| 664 | * |
| 665 | * @param context of the application |
| 666 | * @param groupId of the group that should be modified |
| 667 | * @param newLabel is the updated name of the group (can be null if the name |
| 668 | * should not be updated) |
| 669 | * @param rawContactsToAdd is an array of raw contact IDs for contacts that |
| 670 | * should be added to the group |
| 671 | * @param rawContactsToRemove is an array of raw contact IDs for contacts |
| 672 | * that should be removed from the group |
| 673 | * @param callbackActivity is the activity to send the callback intent to |
| 674 | * @param callbackAction is the intent action for the callback intent |
| 675 | */ |
| 676 | public static Intent createGroupUpdateIntent(Context context, long groupId, String newLabel, |
| 677 | long[] rawContactsToAdd, long[] rawContactsToRemove, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 678 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 679 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 680 | serviceIntent.setAction(ContactSaveService.ACTION_UPDATE_GROUP); |
| 681 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_ID, groupId); |
| 682 | serviceIntent.putExtra(ContactSaveService.EXTRA_GROUP_LABEL, newLabel); |
| 683 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_ADD, rawContactsToAdd); |
| 684 | serviceIntent.putExtra(ContactSaveService.EXTRA_RAW_CONTACTS_TO_REMOVE, |
| 685 | rawContactsToRemove); |
| 686 | |
| 687 | // Callback intent will be invoked by the service once the group is updated |
| 688 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 689 | callbackIntent.setAction(callbackAction); |
| 690 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 691 | |
| 692 | return serviceIntent; |
| 693 | } |
| 694 | |
| 695 | private void updateGroup(Intent intent) { |
| 696 | long groupId = intent.getLongExtra(EXTRA_GROUP_ID, -1); |
| 697 | String label = intent.getStringExtra(EXTRA_GROUP_LABEL); |
| 698 | long[] rawContactsToAdd = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_ADD); |
| 699 | long[] rawContactsToRemove = intent.getLongArrayExtra(EXTRA_RAW_CONTACTS_TO_REMOVE); |
| 700 | |
| 701 | if (groupId == -1) { |
| 702 | Log.e(TAG, "Invalid arguments for updateGroup request"); |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | final ContentResolver resolver = getContentResolver(); |
| 707 | final Uri groupUri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); |
| 708 | |
| 709 | // Update group name if necessary |
| 710 | if (label != null) { |
| 711 | ContentValues values = new ContentValues(); |
| 712 | values.put(Groups.TITLE, label); |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 713 | resolver.update(groupUri, values, null, null); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 716 | // Add and remove members if necessary |
| 717 | addMembersToGroup(resolver, rawContactsToAdd, groupId); |
| 718 | removeMembersFromGroup(resolver, rawContactsToRemove, groupId); |
| 719 | |
| 720 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 721 | callbackIntent.setData(groupUri); |
| 722 | deliverCallback(callbackIntent); |
| 723 | } |
| 724 | |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 725 | private static void addMembersToGroup(ContentResolver resolver, long[] rawContactsToAdd, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 726 | long groupId) { |
| 727 | if (rawContactsToAdd == null) { |
| 728 | return; |
| 729 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 730 | for (long rawContactId : rawContactsToAdd) { |
| 731 | try { |
| 732 | final ArrayList<ContentProviderOperation> rawContactOperations = |
| 733 | new ArrayList<ContentProviderOperation>(); |
| 734 | |
| 735 | // Build an assert operation to ensure the contact is not already in the group |
| 736 | final ContentProviderOperation.Builder assertBuilder = ContentProviderOperation |
| 737 | .newAssertQuery(Data.CONTENT_URI); |
| 738 | assertBuilder.withSelection(Data.RAW_CONTACT_ID + "=? AND " + |
| 739 | Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", |
| 740 | new String[] { String.valueOf(rawContactId), |
| 741 | GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); |
| 742 | assertBuilder.withExpectedCount(0); |
| 743 | rawContactOperations.add(assertBuilder.build()); |
| 744 | |
| 745 | // Build an insert operation to add the contact to the group |
| 746 | final ContentProviderOperation.Builder insertBuilder = ContentProviderOperation |
| 747 | .newInsert(Data.CONTENT_URI); |
| 748 | insertBuilder.withValue(Data.RAW_CONTACT_ID, rawContactId); |
| 749 | insertBuilder.withValue(Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE); |
| 750 | insertBuilder.withValue(GroupMembership.GROUP_ROW_ID, groupId); |
| 751 | rawContactOperations.add(insertBuilder.build()); |
| 752 | |
| 753 | if (DEBUG) { |
| 754 | for (ContentProviderOperation operation : rawContactOperations) { |
| 755 | Log.v(TAG, operation.toString()); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // Apply batch |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 760 | if (!rawContactOperations.isEmpty()) { |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 761 | resolver.applyBatch(ContactsContract.AUTHORITY, rawContactOperations); |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 762 | } |
| 763 | } catch (RemoteException e) { |
| 764 | // Something went wrong, bail without success |
| 765 | Log.e(TAG, "Problem persisting user edits for raw contact ID " + |
| 766 | String.valueOf(rawContactId), e); |
| 767 | } catch (OperationApplicationException e) { |
| 768 | // The assert could have failed because the contact is already in the group, |
| 769 | // just continue to the next contact |
| 770 | Log.w(TAG, "Assert failed in adding raw contact ID " + |
| 771 | String.valueOf(rawContactId) + ". Already exists in group " + |
| 772 | String.valueOf(groupId), e); |
| 773 | } |
| 774 | } |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 775 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 776 | |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 777 | private static void removeMembersFromGroup(ContentResolver resolver, long[] rawContactsToRemove, |
Katherine Kuan | 717e343 | 2011-07-13 17:03:24 -0700 | [diff] [blame] | 778 | long groupId) { |
| 779 | if (rawContactsToRemove == null) { |
| 780 | return; |
| 781 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 782 | for (long rawContactId : rawContactsToRemove) { |
| 783 | // Apply the delete operation on the data row for the given raw contact's |
| 784 | // membership in the given group. If no contact matches the provided selection, then |
| 785 | // nothing will be done. Just continue to the next contact. |
Daniel Lehmann | 18958a2 | 2012-02-28 17:45:25 -0800 | [diff] [blame] | 786 | resolver.delete(Data.CONTENT_URI, Data.RAW_CONTACT_ID + "=? AND " + |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 787 | Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", |
| 788 | new String[] { String.valueOf(rawContactId), |
| 789 | GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId)}); |
| 790 | } |
Katherine Kuan | 2d851cc | 2011-07-05 16:23:27 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /** |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 794 | * 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] | 795 | */ |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 796 | public static Intent createSetStarredIntent(Context context, Uri contactUri, boolean value) { |
| 797 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 798 | serviceIntent.setAction(ContactSaveService.ACTION_SET_STARRED); |
| 799 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 800 | serviceIntent.putExtra(ContactSaveService.EXTRA_STARRED_FLAG, value); |
| 801 | |
Dmitri Plotnikov | e898a9f | 2010-11-18 16:58:25 -0800 | [diff] [blame] | 802 | return serviceIntent; |
| 803 | } |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 804 | |
| 805 | private void setStarred(Intent intent) { |
| 806 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 807 | boolean value = intent.getBooleanExtra(EXTRA_STARRED_FLAG, false); |
| 808 | if (contactUri == null) { |
| 809 | Log.e(TAG, "Invalid arguments for setStarred request"); |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | final ContentValues values = new ContentValues(1); |
| 814 | values.put(Contacts.STARRED, value); |
| 815 | getContentResolver().update(contactUri, values, null, null); |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 816 | |
| 817 | // Undemote the contact if necessary |
| 818 | final Cursor c = getContentResolver().query(contactUri, new String[] {Contacts._ID}, |
| 819 | null, null, null); |
| 820 | try { |
| 821 | if (c.moveToFirst()) { |
| 822 | final long id = c.getLong(0); |
Yorke Lee | bbb8c99 | 2013-09-23 16:20:53 -0700 | [diff] [blame] | 823 | |
| 824 | // Don't bother undemoting if this contact is the user's profile. |
| 825 | if (id < Profile.MIN_ID) { |
Yorke Lee | de1c78c | 2014-06-19 11:30:55 -0700 | [diff] [blame] | 826 | getContentResolver().call(ContactsContract.AUTHORITY_URI, |
| 827 | PinnedPositions.UNDEMOTE_METHOD, String.valueOf(id), null); |
Yorke Lee | bbb8c99 | 2013-09-23 16:20:53 -0700 | [diff] [blame] | 828 | } |
Yorke Lee | e8e3fb8 | 2013-09-12 17:53:31 -0700 | [diff] [blame] | 829 | } |
| 830 | } finally { |
| 831 | c.close(); |
| 832 | } |
Dmitri Plotnikov | 9d730dd | 2010-11-24 13:22:23 -0800 | [diff] [blame] | 833 | } |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 834 | |
| 835 | /** |
Isaac Katzenelson | 683b57e | 2011-07-20 17:06:11 -0700 | [diff] [blame] | 836 | * Creates an intent that can be sent to this service to set the redirect to voicemail. |
| 837 | */ |
| 838 | public static Intent createSetSendToVoicemail(Context context, Uri contactUri, |
| 839 | boolean value) { |
| 840 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 841 | serviceIntent.setAction(ContactSaveService.ACTION_SET_SEND_TO_VOICEMAIL); |
| 842 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 843 | serviceIntent.putExtra(ContactSaveService.EXTRA_SEND_TO_VOICEMAIL_FLAG, value); |
| 844 | |
| 845 | return serviceIntent; |
| 846 | } |
| 847 | |
| 848 | private void setSendToVoicemail(Intent intent) { |
| 849 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 850 | boolean value = intent.getBooleanExtra(EXTRA_SEND_TO_VOICEMAIL_FLAG, false); |
| 851 | if (contactUri == null) { |
| 852 | Log.e(TAG, "Invalid arguments for setRedirectToVoicemail"); |
| 853 | return; |
| 854 | } |
| 855 | |
| 856 | final ContentValues values = new ContentValues(1); |
| 857 | values.put(Contacts.SEND_TO_VOICEMAIL, value); |
| 858 | getContentResolver().update(contactUri, values, null, null); |
| 859 | } |
| 860 | |
| 861 | /** |
| 862 | * Creates an intent that can be sent to this service to save the contact's ringtone. |
| 863 | */ |
| 864 | public static Intent createSetRingtone(Context context, Uri contactUri, |
| 865 | String value) { |
| 866 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 867 | serviceIntent.setAction(ContactSaveService.ACTION_SET_RINGTONE); |
| 868 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 869 | serviceIntent.putExtra(ContactSaveService.EXTRA_CUSTOM_RINGTONE, value); |
| 870 | |
| 871 | return serviceIntent; |
| 872 | } |
| 873 | |
| 874 | private void setRingtone(Intent intent) { |
| 875 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 876 | String value = intent.getStringExtra(EXTRA_CUSTOM_RINGTONE); |
| 877 | if (contactUri == null) { |
| 878 | Log.e(TAG, "Invalid arguments for setRingtone"); |
| 879 | return; |
| 880 | } |
| 881 | ContentValues values = new ContentValues(1); |
| 882 | values.put(Contacts.CUSTOM_RINGTONE, value); |
| 883 | getContentResolver().update(contactUri, values, null, null); |
| 884 | } |
| 885 | |
| 886 | /** |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 887 | * Creates an intent that sets the selected data item as super primary (default) |
| 888 | */ |
| 889 | public static Intent createSetSuperPrimaryIntent(Context context, long dataId) { |
| 890 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 891 | serviceIntent.setAction(ContactSaveService.ACTION_SET_SUPER_PRIMARY); |
| 892 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_ID, dataId); |
| 893 | return serviceIntent; |
| 894 | } |
| 895 | |
| 896 | private void setSuperPrimary(Intent intent) { |
| 897 | long dataId = intent.getLongExtra(EXTRA_DATA_ID, -1); |
| 898 | if (dataId == -1) { |
| 899 | Log.e(TAG, "Invalid arguments for setSuperPrimary request"); |
| 900 | return; |
| 901 | } |
| 902 | |
Chiao Cheng | d7ca03e | 2012-10-24 15:14:08 -0700 | [diff] [blame] | 903 | ContactUpdateUtils.setSuperPrimary(this, dataId); |
Daniel Lehmann | 0f78e8b | 2010-11-24 17:32:03 -0800 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Creates an intent that clears the primary flag of all data items that belong to the same |
| 908 | * raw_contact as the given data item. Will only clear, if the data item was primary before |
| 909 | * this call |
| 910 | */ |
| 911 | public static Intent createClearPrimaryIntent(Context context, long dataId) { |
| 912 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 913 | serviceIntent.setAction(ContactSaveService.ACTION_CLEAR_PRIMARY); |
| 914 | serviceIntent.putExtra(ContactSaveService.EXTRA_DATA_ID, dataId); |
| 915 | return serviceIntent; |
| 916 | } |
| 917 | |
| 918 | private void clearPrimary(Intent intent) { |
| 919 | long dataId = intent.getLongExtra(EXTRA_DATA_ID, -1); |
| 920 | if (dataId == -1) { |
| 921 | Log.e(TAG, "Invalid arguments for clearPrimary request"); |
| 922 | return; |
| 923 | } |
| 924 | |
| 925 | // Update the primary values in the data record. |
| 926 | ContentValues values = new ContentValues(1); |
| 927 | values.put(Data.IS_SUPER_PRIMARY, 0); |
| 928 | values.put(Data.IS_PRIMARY, 0); |
| 929 | |
| 930 | getContentResolver().update(ContentUris.withAppendedId(Data.CONTENT_URI, dataId), |
| 931 | values, null, null); |
| 932 | } |
Dmitri Plotnikov | 7d8cabb | 2010-11-24 17:40:01 -0800 | [diff] [blame] | 933 | |
| 934 | /** |
| 935 | * Creates an intent that can be sent to this service to delete a contact. |
| 936 | */ |
| 937 | public static Intent createDeleteContactIntent(Context context, Uri contactUri) { |
| 938 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 939 | serviceIntent.setAction(ContactSaveService.ACTION_DELETE_CONTACT); |
| 940 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_URI, contactUri); |
| 941 | return serviceIntent; |
| 942 | } |
| 943 | |
| 944 | private void deleteContact(Intent intent) { |
| 945 | Uri contactUri = intent.getParcelableExtra(EXTRA_CONTACT_URI); |
| 946 | if (contactUri == null) { |
| 947 | Log.e(TAG, "Invalid arguments for deleteContact request"); |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | getContentResolver().delete(contactUri, null, null); |
| 952 | } |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 953 | |
| 954 | /** |
| 955 | * Creates an intent that can be sent to this service to join two contacts. |
| 956 | */ |
| 957 | public static Intent createJoinContactsIntent(Context context, long contactId1, |
| 958 | long contactId2, boolean contactWritable, |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 959 | Class<? extends Activity> callbackActivity, String callbackAction) { |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 960 | Intent serviceIntent = new Intent(context, ContactSaveService.class); |
| 961 | serviceIntent.setAction(ContactSaveService.ACTION_JOIN_CONTACTS); |
| 962 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_ID1, contactId1); |
| 963 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_ID2, contactId2); |
| 964 | serviceIntent.putExtra(ContactSaveService.EXTRA_CONTACT_WRITABLE, contactWritable); |
| 965 | |
| 966 | // Callback intent will be invoked by the service once the contacts are joined. |
| 967 | Intent callbackIntent = new Intent(context, callbackActivity); |
| 968 | callbackIntent.setAction(callbackAction); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 969 | serviceIntent.putExtra(ContactSaveService.EXTRA_CALLBACK_INTENT, callbackIntent); |
| 970 | |
| 971 | return serviceIntent; |
| 972 | } |
| 973 | |
| 974 | |
| 975 | private interface JoinContactQuery { |
| 976 | String[] PROJECTION = { |
| 977 | RawContacts._ID, |
| 978 | RawContacts.CONTACT_ID, |
| 979 | RawContacts.NAME_VERIFIED, |
| 980 | RawContacts.DISPLAY_NAME_SOURCE, |
| 981 | }; |
| 982 | |
| 983 | String SELECTION = RawContacts.CONTACT_ID + "=? OR " + RawContacts.CONTACT_ID + "=?"; |
| 984 | |
| 985 | int _ID = 0; |
| 986 | int CONTACT_ID = 1; |
| 987 | int NAME_VERIFIED = 2; |
| 988 | int DISPLAY_NAME_SOURCE = 3; |
| 989 | } |
| 990 | |
| 991 | private void joinContacts(Intent intent) { |
| 992 | long contactId1 = intent.getLongExtra(EXTRA_CONTACT_ID1, -1); |
| 993 | long contactId2 = intent.getLongExtra(EXTRA_CONTACT_ID2, -1); |
| 994 | boolean writable = intent.getBooleanExtra(EXTRA_CONTACT_WRITABLE, false); |
| 995 | if (contactId1 == -1 || contactId2 == -1) { |
| 996 | Log.e(TAG, "Invalid arguments for joinContacts request"); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | final ContentResolver resolver = getContentResolver(); |
| 1001 | |
| 1002 | // Load raw contact IDs for all raw contacts involved - currently edited and selected |
| 1003 | // in the join UIs |
| 1004 | Cursor c = resolver.query(RawContacts.CONTENT_URI, |
| 1005 | JoinContactQuery.PROJECTION, |
| 1006 | JoinContactQuery.SELECTION, |
| 1007 | new String[]{String.valueOf(contactId1), String.valueOf(contactId2)}, null); |
| 1008 | |
| 1009 | long rawContactIds[]; |
| 1010 | long verifiedNameRawContactId = -1; |
| 1011 | try { |
Jay Shrauner | 7f42b90 | 2013-01-09 14:43:09 -0800 | [diff] [blame] | 1012 | if (c.getCount() == 0) { |
| 1013 | return; |
| 1014 | } |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1015 | int maxDisplayNameSource = -1; |
| 1016 | rawContactIds = new long[c.getCount()]; |
| 1017 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1018 | c.moveToPosition(i); |
| 1019 | long rawContactId = c.getLong(JoinContactQuery._ID); |
| 1020 | rawContactIds[i] = rawContactId; |
| 1021 | int nameSource = c.getInt(JoinContactQuery.DISPLAY_NAME_SOURCE); |
| 1022 | if (nameSource > maxDisplayNameSource) { |
| 1023 | maxDisplayNameSource = nameSource; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | // Find an appropriate display name for the joined contact: |
| 1028 | // if should have a higher DisplayNameSource or be the name |
| 1029 | // of the original contact that we are joining with another. |
| 1030 | if (writable) { |
| 1031 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1032 | c.moveToPosition(i); |
| 1033 | if (c.getLong(JoinContactQuery.CONTACT_ID) == contactId1) { |
| 1034 | int nameSource = c.getInt(JoinContactQuery.DISPLAY_NAME_SOURCE); |
| 1035 | if (nameSource == maxDisplayNameSource |
| 1036 | && (verifiedNameRawContactId == -1 |
| 1037 | || c.getInt(JoinContactQuery.NAME_VERIFIED) != 0)) { |
| 1038 | verifiedNameRawContactId = c.getLong(JoinContactQuery._ID); |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | } |
| 1043 | } finally { |
| 1044 | c.close(); |
| 1045 | } |
| 1046 | |
| 1047 | // For each pair of raw contacts, insert an aggregation exception |
| 1048 | ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); |
| 1049 | for (int i = 0; i < rawContactIds.length; i++) { |
| 1050 | for (int j = 0; j < rawContactIds.length; j++) { |
| 1051 | if (i != j) { |
| 1052 | buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]); |
| 1053 | } |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | // Mark the original contact as "name verified" to make sure that the contact |
| 1058 | // display name does not change as a result of the join |
| 1059 | if (verifiedNameRawContactId != -1) { |
| 1060 | Builder builder = ContentProviderOperation.newUpdate( |
| 1061 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, verifiedNameRawContactId)); |
| 1062 | builder.withValue(RawContacts.NAME_VERIFIED, 1); |
| 1063 | operations.add(builder.build()); |
| 1064 | } |
| 1065 | |
| 1066 | boolean success = false; |
| 1067 | // Apply all aggregation exceptions as one batch |
| 1068 | try { |
| 1069 | resolver.applyBatch(ContactsContract.AUTHORITY, operations); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1070 | showToast(R.string.contactsJoinedMessage); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1071 | success = true; |
| 1072 | } catch (RemoteException e) { |
| 1073 | Log.e(TAG, "Failed to apply aggregation exception batch", e); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1074 | showToast(R.string.contactSavedErrorToast); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1075 | } catch (OperationApplicationException e) { |
| 1076 | Log.e(TAG, "Failed to apply aggregation exception batch", e); |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1077 | showToast(R.string.contactSavedErrorToast); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT); |
| 1081 | if (success) { |
| 1082 | Uri uri = RawContacts.getContactLookupUri(resolver, |
| 1083 | ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactIds[0])); |
| 1084 | callbackIntent.setData(uri); |
| 1085 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1086 | deliverCallback(callbackIntent); |
Dmitri Plotnikov | 2b46f03 | 2010-11-29 16:41:43 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /** |
| 1090 | * Construct a {@link AggregationExceptions#TYPE_KEEP_TOGETHER} ContentProviderOperation. |
| 1091 | */ |
| 1092 | private void buildJoinContactDiff(ArrayList<ContentProviderOperation> operations, |
| 1093 | long rawContactId1, long rawContactId2) { |
| 1094 | Builder builder = |
| 1095 | ContentProviderOperation.newUpdate(AggregationExceptions.CONTENT_URI); |
| 1096 | builder.withValue(AggregationExceptions.TYPE, AggregationExceptions.TYPE_KEEP_TOGETHER); |
| 1097 | builder.withValue(AggregationExceptions.RAW_CONTACT_ID1, rawContactId1); |
| 1098 | builder.withValue(AggregationExceptions.RAW_CONTACT_ID2, rawContactId2); |
| 1099 | operations.add(builder.build()); |
| 1100 | } |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1101 | |
| 1102 | /** |
| 1103 | * Shows a toast on the UI thread. |
| 1104 | */ |
| 1105 | private void showToast(final int message) { |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1106 | mMainHandler.post(new Runnable() { |
Dmitri Plotnikov | 886d3d6 | 2011-01-03 10:08:47 -0800 | [diff] [blame] | 1107 | |
| 1108 | @Override |
| 1109 | public void run() { |
| 1110 | Toast.makeText(ContactSaveService.this, message, Toast.LENGTH_LONG).show(); |
| 1111 | } |
| 1112 | }); |
| 1113 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1114 | |
| 1115 | private void deliverCallback(final Intent callbackIntent) { |
| 1116 | mMainHandler.post(new Runnable() { |
| 1117 | |
| 1118 | @Override |
| 1119 | public void run() { |
| 1120 | deliverCallbackOnUiThread(callbackIntent); |
| 1121 | } |
| 1122 | }); |
| 1123 | } |
| 1124 | |
| 1125 | void deliverCallbackOnUiThread(final Intent callbackIntent) { |
| 1126 | // TODO: this assumes that if there are multiple instances of the same |
| 1127 | // activity registered, the last one registered is the one waiting for |
| 1128 | // the callback. Validity of this assumption needs to be verified. |
Hugo Hudson | a831c0b | 2011-08-13 11:50:15 +0100 | [diff] [blame] | 1129 | for (Listener listener : sListeners) { |
| 1130 | if (callbackIntent.getComponent().equals( |
| 1131 | ((Activity) listener).getIntent().getComponent())) { |
| 1132 | listener.onServiceCompleted(callbackIntent); |
| 1133 | return; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | } |
Daniel Lehmann | 173ffe1 | 2010-06-14 18:19:10 -0700 | [diff] [blame] | 1137 | } |