blob: ea9eda5072226e67e479e357f5b3a3804ea987a4 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2006 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 Plotnikov19d51ac2011-01-04 14:30:24 -080017package com.android.contacts.activities;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080018
Brian Attwellb76f86d2014-10-09 17:39:06 -070019import android.app.Activity;
Jay Shrauner66f778e2014-05-31 21:36:09 -070020import android.content.ActivityNotFoundException;
Neel Parekh2ad90a32009-09-20 19:08:50 -070021import android.content.ContentResolver;
Brian Attwellb76f86d2014-10-09 17:39:06 -070022import android.content.ContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080023import android.content.Intent;
Josh Garguse5d3f892012-04-11 11:56:15 -070024import android.content.Loader;
25import android.content.Loader.OnLoadCompleteListener;
Brian Attwell5234be92015-05-13 19:26:43 -070026import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
Neel Parekh2ad90a32009-09-20 19:08:50 -070028import android.database.Cursor;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029import android.graphics.Bitmap;
30import android.net.Uri;
31import android.os.Bundle;
Dmitri Plotnikov19d51ac2011-01-04 14:30:24 -080032import android.provider.ContactsContract.CommonDataKinds.Photo;
Neel Parekhbe406ff2009-09-16 15:31:22 -070033import android.provider.ContactsContract.Contacts;
Dave Santoro0a2a5db2011-06-29 00:37:06 -070034import android.provider.ContactsContract.DisplayPhoto;
Brian Attwellb76f86d2014-10-09 17:39:06 -070035import android.provider.ContactsContract.Intents;
36import android.provider.ContactsContract.RawContacts;
Josh Garguse5d3f892012-04-11 11:56:15 -070037import android.util.Log;
Jay Shrauner66f778e2014-05-31 21:36:09 -070038import android.widget.Toast;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080039
Chiao Cheng61414c22012-06-18 16:59:06 -070040import com.android.contacts.ContactSaveService;
41import com.android.contacts.ContactsActivity;
Gary Mai0a49afa2016-12-05 15:53:58 -080042import com.android.contacts.ContactsUtils;
Jay Shrauner66f778e2014-05-31 21:36:09 -070043import com.android.contacts.R;
Gary Mai0a49afa2016-12-05 15:53:58 -080044import com.android.contacts.editor.ContactEditorUtils;
Gary Mai69c182a2016-12-05 13:07:03 -080045import com.android.contacts.model.Contact;
46import com.android.contacts.model.ContactLoader;
47import com.android.contacts.model.RawContactDelta;
48import com.android.contacts.model.RawContactDeltaList;
49import com.android.contacts.model.RawContactModifier;
Gary Mai69c182a2016-12-05 13:07:03 -080050import com.android.contacts.model.ValuesDelta;
Gary Mai0a49afa2016-12-05 15:53:58 -080051import com.android.contacts.model.account.AccountType;
Gary Mai69c182a2016-12-05 13:07:03 -080052import com.android.contacts.model.account.AccountWithDataSet;
Chiao Cheng61414c22012-06-18 16:59:06 -070053import com.android.contacts.util.ContactPhotoUtils;
54
Yorke Lee637a38e2013-09-14 08:36:33 -070055import java.io.FileNotFoundException;
Brian Attwell5234be92015-05-13 19:26:43 -070056import java.util.List;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057
58/**
59 * Provides an external interface for other applications to attach images
60 * to contacts. It will first present a contact picker and then run the
61 * image that is handed to it through the cropper to make the image the proper
62 * size and give the user a chance to use the face detector.
63 */
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080064public class AttachPhotoActivity extends ContactsActivity {
Josh Garguse5d3f892012-04-11 11:56:15 -070065 private static final String TAG = AttachPhotoActivity.class.getSimpleName();
66
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080067 private static final int REQUEST_PICK_CONTACT = 1;
68 private static final int REQUEST_CROP_PHOTO = 2;
Brian Attwellb76f86d2014-10-09 17:39:06 -070069 private static final int REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080070
Josh Garguse5d3f892012-04-11 11:56:15 -070071 private static final String KEY_CONTACT_URI = "contact_uri";
72 private static final String KEY_TEMP_PHOTO_URI = "temp_photo_uri";
Yorke Leeadeadcf2013-09-19 17:10:07 -070073 private static final String KEY_CROPPED_PHOTO_URI = "cropped_photo_uri";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080074
Josh Garguse5d3f892012-04-11 11:56:15 -070075 private Uri mTempPhotoUri;
Yorke Leeadeadcf2013-09-19 17:10:07 -070076 private Uri mCroppedPhotoUri;
Neel Parekh2ad90a32009-09-20 19:08:50 -070077
78 private ContentResolver mContentResolver;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080079
Josh Garguse5d3f892012-04-11 11:56:15 -070080 // Height and width (in pixels) to request for the photo - queried from the provider.
Dave Santoro0a2a5db2011-06-29 00:37:06 -070081 private static int mPhotoDim;
Jay Shrauner946ebfa2014-01-14 16:07:01 -080082 // Default photo dimension to use if unable to query the provider.
83 private static final int mDefaultPhotoDim = 720;
Dave Santoro0a2a5db2011-06-29 00:37:06 -070084
Josh Garguse5d3f892012-04-11 11:56:15 -070085 private Uri mContactUri;
86
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087 @Override
88 public void onCreate(Bundle icicle) {
89 super.onCreate(icicle);
90
Brian Attwellbdd32642015-05-08 17:03:15 -070091 if (RequestPermissionsActivity.startPermissionActivity(this)) {
92 return;
93 }
94
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095 if (icicle != null) {
Josh Garguse5d3f892012-04-11 11:56:15 -070096 final String uri = icicle.getString(KEY_CONTACT_URI);
97 mContactUri = (uri == null) ? null : Uri.parse(uri);
Josh Garguse5d3f892012-04-11 11:56:15 -070098 mTempPhotoUri = Uri.parse(icicle.getString(KEY_TEMP_PHOTO_URI));
Yorke Leeadeadcf2013-09-19 17:10:07 -070099 mCroppedPhotoUri = Uri.parse(icicle.getString(KEY_CROPPED_PHOTO_URI));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800100 } else {
Yorke Lee637a38e2013-09-14 08:36:33 -0700101 mTempPhotoUri = ContactPhotoUtils.generateTempImageUri(this);
Yorke Leeadeadcf2013-09-19 17:10:07 -0700102 mCroppedPhotoUri = ContactPhotoUtils.generateTempCroppedImageUri(this);
Josh Gargus4a964d92012-05-11 13:18:48 -0700103 Intent intent = new Intent(Intent.ACTION_PICK);
104 intent.setType(Contacts.CONTENT_TYPE);
Brian Attwell4785c512015-06-26 12:23:25 -0700105 intent.setPackage(getPackageName());
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800106 startActivityForResult(intent, REQUEST_PICK_CONTACT);
107 }
Neel Parekh2ad90a32009-09-20 19:08:50 -0700108
109 mContentResolver = getContentResolver();
Dave Santoro0a2a5db2011-06-29 00:37:06 -0700110
Jay Shrauner946ebfa2014-01-14 16:07:01 -0800111 // Load the photo dimension to request. mPhotoDim is a static class
112 // member varible so only need to load this if this is the first time
113 // through.
114 if (mPhotoDim == 0) {
115 Cursor c = mContentResolver.query(DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
116 new String[]{DisplayPhoto.DISPLAY_MAX_DIM}, null, null, null);
117 if (c != null) {
118 try {
Jay Shraunerde047f42014-01-18 10:45:01 -0800119 if (c.moveToFirst()) {
120 mPhotoDim = c.getInt(0);
121 }
Jay Shrauner946ebfa2014-01-14 16:07:01 -0800122 } finally {
123 c.close();
124 }
125 }
Dave Santoro0a2a5db2011-06-29 00:37:06 -0700126 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800127 }
128
129 @Override
130 protected void onSaveInstanceState(Bundle outState) {
131 super.onSaveInstanceState(outState);
Yorke Lee637a38e2013-09-14 08:36:33 -0700132 if (mContactUri != null) {
133 outState.putString(KEY_CONTACT_URI, mContactUri.toString());
134 }
135 if (mTempPhotoUri != null) {
136 outState.putString(KEY_TEMP_PHOTO_URI, mTempPhotoUri.toString());
137 }
Yorke Lee2518abd2013-09-29 14:47:36 -0700138 if (mCroppedPhotoUri != null) {
139 outState.putString(KEY_CROPPED_PHOTO_URI, mCroppedPhotoUri.toString());
140 }
Neel Parekh2ad90a32009-09-20 19:08:50 -0700141 }
142
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800143 @Override
144 protected void onActivityResult(int requestCode, int resultCode, Intent result) {
Brian Attwellb76f86d2014-10-09 17:39:06 -0700145 if (requestCode == REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT) {
146 // Bail if the account selector was not successful.
147 if (resultCode != Activity.RESULT_OK) {
148 Log.w(TAG, "account selector was not successful");
149 finish();
150 return;
151 }
152 // If there's an account specified, use it.
153 if (result != null) {
Brian Attwell4a1c5742015-01-26 15:59:58 -0800154 AccountWithDataSet account = result.getParcelableExtra(
155 Intents.Insert.EXTRA_ACCOUNT);
Brian Attwellb76f86d2014-10-09 17:39:06 -0700156 if (account != null) {
157 createNewRawContact(account);
158 return;
159 }
160 }
161 // If there isn't an account specified, then the user opted to keep the contact local.
162 createNewRawContact(null);
163 } else if (requestCode == REQUEST_PICK_CONTACT) {
Yorke Leeadeadcf2013-09-19 17:10:07 -0700164 if (resultCode != RESULT_OK) {
165 finish();
166 return;
167 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800168 // A contact was picked. Launch the cropper to get face detection, the right size, etc.
169 // TODO: get these values from constants somewhere
Yorke Leeadeadcf2013-09-19 17:10:07 -0700170 final Intent myIntent = getIntent();
171 final Uri inputUri = myIntent.getData();
172
Brian Attwell5234be92015-05-13 19:26:43 -0700173
Jay Shrauner946ebfa2014-01-14 16:07:01 -0800174 // Save the URI into a temporary file provider URI so that
175 // we can add the FLAG_GRANT_WRITE_URI_PERMISSION flag to the eventual
176 // crop intent for read-only URI's.
177 // TODO: With b/10837468 fixed should be able to avoid this copy.
Jay Shraunera9433712015-02-27 15:47:09 -0800178 if (!ContactPhotoUtils.savePhotoFromUriToUri(this, inputUri, mTempPhotoUri, false)) {
179 finish();
180 return;
181 }
Yorke Lee637a38e2013-09-14 08:36:33 -0700182
Brian Attwell5234be92015-05-13 19:26:43 -0700183 final Intent intent = new Intent("com.android.camera.action.CROP", mTempPhotoUri);
Yorke Leeadeadcf2013-09-19 17:10:07 -0700184 if (myIntent.getStringExtra("mimeType") != null) {
Brian Attwell5234be92015-05-13 19:26:43 -0700185 intent.setDataAndType(mTempPhotoUri, myIntent.getStringExtra("mimeType"));
Yorke Leeadeadcf2013-09-19 17:10:07 -0700186 }
187 ContactPhotoUtils.addPhotoPickerExtras(intent, mCroppedPhotoUri);
Jay Shrauner946ebfa2014-01-14 16:07:01 -0800188 ContactPhotoUtils.addCropExtras(intent, mPhotoDim != 0 ? mPhotoDim : mDefaultPhotoDim);
Brian Attwell5234be92015-05-13 19:26:43 -0700189 if (!hasIntentHandler(intent)) {
190 // No activity supports the crop action. So skip cropping and set the photo
191 // without performing any cropping.
192 mCroppedPhotoUri = mTempPhotoUri;
193 mContactUri = result.getData();
194 loadContact(mContactUri, new Listener() {
195 @Override
196 public void onContactLoaded(Contact contact) {
197 saveContact(contact);
198 }
199 });
200 return;
201 }
Josh Garguse5d3f892012-04-11 11:56:15 -0700202
Jay Shrauner66f778e2014-05-31 21:36:09 -0700203 try {
204 startActivityForResult(intent, REQUEST_CROP_PHOTO);
205 } catch (ActivityNotFoundException ex) {
206 Toast.makeText(this, R.string.missing_app, Toast.LENGTH_SHORT).show();
207 return;
208 }
Neel Parekhbe406ff2009-09-16 15:31:22 -0700209
Josh Garguse5d3f892012-04-11 11:56:15 -0700210 mContactUri = result.getData();
Neel Parekhbe406ff2009-09-16 15:31:22 -0700211
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800212 } else if (requestCode == REQUEST_CROP_PHOTO) {
Yorke Leeadeadcf2013-09-19 17:10:07 -0700213 // Delete the temporary photo from cache now that we have a cropped version.
214 // We should do this even if the crop failed and we eventually bail
215 getContentResolver().delete(mTempPhotoUri, null, null);
216 if (resultCode != RESULT_OK) {
217 finish();
218 return;
219 }
Daniel Lehmanndd3dc562012-04-24 20:33:19 -0700220 loadContact(mContactUri, new Listener() {
Josh Garguse5d3f892012-04-11 11:56:15 -0700221 @Override
Maurice Chu851222a2012-06-21 11:43:08 -0700222 public void onContactLoaded(Contact contact) {
Josh Garguse5d3f892012-04-11 11:56:15 -0700223 saveContact(contact);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800224 }
Josh Garguse5d3f892012-04-11 11:56:15 -0700225 });
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800226 }
227 }
Neel Parekh2ad90a32009-09-20 19:08:50 -0700228
Brian Attwell5234be92015-05-13 19:26:43 -0700229 private boolean hasIntentHandler(Intent intent) {
230 final List<ResolveInfo> resolveInfo = getPackageManager()
231 .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
232 return resolveInfo != null && resolveInfo.size() > 0;
233 }
234
Josh Garguse5d3f892012-04-11 11:56:15 -0700235 // TODO: consider moving this to ContactLoader, especially if we keep adding similar
236 // code elsewhere (ViewNotificationService is another case). The only concern is that,
237 // although this is convenient, it isn't quite as robust as using LoaderManager... for
238 // instance, the loader doesn't persist across Activity restarts.
Daniel Lehmanndd3dc562012-04-24 20:33:19 -0700239 private void loadContact(Uri contactUri, final Listener listener) {
Makoto Onuki45ee8722012-05-21 16:33:25 -0700240 final ContactLoader loader = new ContactLoader(this, contactUri, true);
Maurice Chu851222a2012-06-21 11:43:08 -0700241 loader.registerListener(0, new OnLoadCompleteListener<Contact>() {
Josh Garguse5d3f892012-04-11 11:56:15 -0700242 @Override
243 public void onLoadComplete(
Maurice Chu851222a2012-06-21 11:43:08 -0700244 Loader<Contact> loader, Contact contact) {
Josh Garguse5d3f892012-04-11 11:56:15 -0700245 try {
246 loader.reset();
Dmitri Plotnikov19d51ac2011-01-04 14:30:24 -0800247 }
Josh Garguse5d3f892012-04-11 11:56:15 -0700248 catch (RuntimeException e) {
249 Log.e(TAG, "Error resetting loader", e);
250 }
251 listener.onContactLoaded(contact);
Dmitri Plotnikov19d51ac2011-01-04 14:30:24 -0800252 }
Josh Garguse5d3f892012-04-11 11:56:15 -0700253 });
254 loader.startLoading();
Dmitri Plotnikov19d51ac2011-01-04 14:30:24 -0800255 }
256
Daniel Lehmanndd3dc562012-04-24 20:33:19 -0700257 private interface Listener {
Maurice Chu851222a2012-06-21 11:43:08 -0700258 public void onContactLoaded(Contact contact);
Daniel Lehmanndd3dc562012-04-24 20:33:19 -0700259 }
260
Neel Parekh2ad90a32009-09-20 19:08:50 -0700261 /**
Josh Garguse5d3f892012-04-11 11:56:15 -0700262 * If prerequisites have been met, attach the photo to a raw-contact and save.
263 * The prerequisites are:
264 * - photo has been cropped
265 * - contact has been loaded
Neel Parekh2ad90a32009-09-20 19:08:50 -0700266 */
Maurice Chu851222a2012-06-21 11:43:08 -0700267 private void saveContact(Contact contact) {
Neel Parekh2ad90a32009-09-20 19:08:50 -0700268
jiezha2x342de102013-06-05 16:23:24 +0800269 if (contact.getRawContacts() == null) {
270 Log.w(TAG, "No raw contacts found for contact");
271 finish();
272 return;
273 }
274
Josh Garguse5d3f892012-04-11 11:56:15 -0700275 // Obtain the raw-contact that we will save to.
Maurice Chu851222a2012-06-21 11:43:08 -0700276 RawContactDeltaList deltaList = contact.createRawContactDeltaList();
277 RawContactDelta raw = deltaList.getFirstWritableRawContact(this);
Josh Garguse5d3f892012-04-11 11:56:15 -0700278 if (raw == null) {
Brian Attwellb76f86d2014-10-09 17:39:06 -0700279 // We can't directly insert this photo since no raw contacts exist in the contact.
280 selectAccountAndCreateContact();
Josh Garguse5d3f892012-04-11 11:56:15 -0700281 return;
Neel Parekh2ad90a32009-09-20 19:08:50 -0700282 }
283
Brian Attwellb76f86d2014-10-09 17:39:06 -0700284 saveToContact(contact, deltaList, raw);
285 }
286
287 private void saveToContact(Contact contact, RawContactDeltaList deltaList,
288 RawContactDelta raw) {
289
Josh Garguse5d3f892012-04-11 11:56:15 -0700290 // Create a scaled, compressed bitmap to add to the entity-delta list.
291 final int size = ContactsUtils.getThumbnailSize(this);
Yorke Lee637a38e2013-09-14 08:36:33 -0700292 Bitmap bitmap;
293 try {
Yorke Leeadeadcf2013-09-19 17:10:07 -0700294 bitmap = ContactPhotoUtils.getBitmapFromUri(this, mCroppedPhotoUri);
Yorke Lee637a38e2013-09-14 08:36:33 -0700295 } catch (FileNotFoundException e) {
296 Log.w(TAG, "Could not find bitmap");
Brian Attwellb76f86d2014-10-09 17:39:06 -0700297 finish();
Yorke Lee637a38e2013-09-14 08:36:33 -0700298 return;
299 }
Jay Shrauner1e090bf2014-01-30 12:21:01 -0800300 if (bitmap == null) {
301 Log.w(TAG, "Could not decode bitmap");
Brian Attwellb76f86d2014-10-09 17:39:06 -0700302 finish();
Jay Shrauner1e090bf2014-01-30 12:21:01 -0800303 return;
304 }
Yorke Lee637a38e2013-09-14 08:36:33 -0700305
Josh Garguse5d3f892012-04-11 11:56:15 -0700306 final Bitmap scaled = Bitmap.createScaledBitmap(bitmap, size, size, false);
307 final byte[] compressed = ContactPhotoUtils.compressBitmap(scaled);
308 if (compressed == null) {
309 Log.w(TAG, "could not create scaled and compressed Bitmap");
Brian Attwellb76f86d2014-10-09 17:39:06 -0700310 finish();
Josh Garguse5d3f892012-04-11 11:56:15 -0700311 return;
Neel Parekh2ad90a32009-09-20 19:08:50 -0700312 }
Brian Attwellb76f86d2014-10-09 17:39:06 -0700313
Josh Garguse5d3f892012-04-11 11:56:15 -0700314 // Add compressed bitmap to entity-delta... this allows us to save to
315 // a new contact; otherwise the entity-delta-list would be empty, and
316 // the ContactSaveService would not create the new contact, and the
317 // full-res photo would fail to be saved to the non-existent contact.
318 AccountType account = raw.getRawContactAccountType(this);
Chiao Chengaa690d42012-11-28 18:10:43 -0800319 ValuesDelta values =
Maurice Chu851222a2012-06-21 11:43:08 -0700320 RawContactModifier.ensureKindExists(raw, account, Photo.CONTENT_ITEM_TYPE);
Josh Garguse5d3f892012-04-11 11:56:15 -0700321 if (values == null) {
322 Log.w(TAG, "cannot attach photo to this account type");
Brian Attwellb76f86d2014-10-09 17:39:06 -0700323 finish();
Josh Garguse5d3f892012-04-11 11:56:15 -0700324 return;
Neel Parekh2ad90a32009-09-20 19:08:50 -0700325 }
Maurice Chu851222a2012-06-21 11:43:08 -0700326 values.setPhoto(compressed);
Josh Garguse5d3f892012-04-11 11:56:15 -0700327
328 // Finally, invoke the ContactSaveService.
329 Log.v(TAG, "all prerequisites met, about to save photo to contact");
330 Intent intent = ContactSaveService.createSaveContactIntent(
331 this,
332 deltaList,
333 "", 0,
334 contact.isUserProfile(),
335 null, null,
Brian Attwellb76f86d2014-10-09 17:39:06 -0700336 raw.getRawContactId() != null ? raw.getRawContactId() : -1,
Yorke Leeadeadcf2013-09-19 17:10:07 -0700337 mCroppedPhotoUri
Brian Attwellb76f86d2014-10-09 17:39:06 -0700338 );
Wenyi Wangdd7d4562015-12-08 13:33:43 -0800339 ContactSaveService.startService(this, intent);
Josh Garguse5d3f892012-04-11 11:56:15 -0700340 finish();
Neel Parekh2ad90a32009-09-20 19:08:50 -0700341 }
Brian Attwellb76f86d2014-10-09 17:39:06 -0700342
343 private void selectAccountAndCreateContact() {
344 // If there is no default account or the accounts have changed such that we need to
345 // prompt the user again, then launch the account prompt.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700346 final ContactEditorUtils editorUtils = ContactEditorUtils.create(this);
Brian Attwellb76f86d2014-10-09 17:39:06 -0700347 if (editorUtils.shouldShowAccountChangedNotification()) {
348 Intent intent = new Intent(this, ContactEditorAccountsChangedActivity.class);
349 startActivityForResult(intent, REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT);
350 } else {
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700351 // Otherwise, there should be a default account. Then either create a null contact
Brian Attwellb76f86d2014-10-09 17:39:06 -0700352 // (if default account is null) or create a contact with the specified account.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700353 final AccountWithDataSet targetAccount = editorUtils.getOnlyOrDefaultAccount();
354 createNewRawContact(targetAccount);
Brian Attwellb76f86d2014-10-09 17:39:06 -0700355 }
356 }
357
358 /**
359 * Create a new writeable raw contact to store mCroppedPhotoUri.
360 */
361 private void createNewRawContact(final AccountWithDataSet account) {
362 // Reload the contact from URI instead of trying to pull the contact from a member variable,
363 // since this function can be called after the activity stops and resumes.
364 loadContact(mContactUri, new Listener() {
365 @Override
366 public void onContactLoaded(Contact contactToSave) {
367 final RawContactDeltaList deltaList = contactToSave.createRawContactDeltaList();
368 final ContentValues after = new ContentValues();
369 after.put(RawContacts.ACCOUNT_TYPE, account != null ? account.type : null);
370 after.put(RawContacts.ACCOUNT_NAME, account != null ? account.name : null);
371 after.put(RawContacts.DATA_SET, account != null ? account.dataSet : null);
372
373 final RawContactDelta newRawContactDelta
374 = new RawContactDelta(ValuesDelta.fromAfter(after));
375 deltaList.add(newRawContactDelta);
376 saveToContact(contactToSave, deltaList, newRawContactDelta);
377 }
378 });
379 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800380}