blob: 096349c760da8ccc7a61200803d6f8118a3a6d31 [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;
Jay Shrauner66f778e2014-05-31 21:36:09 -070042import com.android.contacts.R;
Brian Attwellbdd32642015-05-08 17:03:15 -070043import com.android.contacts.common.activity.RequestPermissionsActivity;
Yorke Leecd321f62013-10-28 15:20:15 -070044import com.android.contacts.common.model.Contact;
45import com.android.contacts.common.model.ContactLoader;
46import com.android.contacts.common.model.RawContactDelta;
47import com.android.contacts.common.model.RawContactDeltaList;
48import com.android.contacts.common.model.RawContactModifier;
49import com.android.contacts.common.ContactsUtils;
Chiao Cheng428f0082012-11-13 18:38:56 -080050import com.android.contacts.common.model.account.AccountType;
Chiao Cheng738ff862012-11-30 12:06:03 -080051import com.android.contacts.common.model.ValuesDelta;
Brian Attwellb76f86d2014-10-09 17:39:06 -070052import com.android.contacts.common.model.account.AccountWithDataSet;
53import com.android.contacts.editor.ContactEditorUtils;
Chiao Cheng61414c22012-06-18 16:59:06 -070054import com.android.contacts.util.ContactPhotoUtils;
55
Yorke Lee637a38e2013-09-14 08:36:33 -070056import java.io.FileNotFoundException;
Brian Attwell5234be92015-05-13 19:26:43 -070057import java.util.List;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080058
59/**
60 * Provides an external interface for other applications to attach images
61 * to contacts. It will first present a contact picker and then run the
62 * image that is handed to it through the cropper to make the image the proper
63 * size and give the user a chance to use the face detector.
64 */
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080065public class AttachPhotoActivity extends ContactsActivity {
Josh Garguse5d3f892012-04-11 11:56:15 -070066 private static final String TAG = AttachPhotoActivity.class.getSimpleName();
67
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068 private static final int REQUEST_PICK_CONTACT = 1;
69 private static final int REQUEST_CROP_PHOTO = 2;
Brian Attwellb76f86d2014-10-09 17:39:06 -070070 private static final int REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT = 3;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071
Josh Garguse5d3f892012-04-11 11:56:15 -070072 private static final String KEY_CONTACT_URI = "contact_uri";
73 private static final String KEY_TEMP_PHOTO_URI = "temp_photo_uri";
Yorke Leeadeadcf2013-09-19 17:10:07 -070074 private static final String KEY_CROPPED_PHOTO_URI = "cropped_photo_uri";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075
Josh Garguse5d3f892012-04-11 11:56:15 -070076 private Uri mTempPhotoUri;
Yorke Leeadeadcf2013-09-19 17:10:07 -070077 private Uri mCroppedPhotoUri;
Neel Parekh2ad90a32009-09-20 19:08:50 -070078
79 private ContentResolver mContentResolver;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080080
Josh Garguse5d3f892012-04-11 11:56:15 -070081 // Height and width (in pixels) to request for the photo - queried from the provider.
Dave Santoro0a2a5db2011-06-29 00:37:06 -070082 private static int mPhotoDim;
Jay Shrauner946ebfa2014-01-14 16:07:01 -080083 // Default photo dimension to use if unable to query the provider.
84 private static final int mDefaultPhotoDim = 720;
Dave Santoro0a2a5db2011-06-29 00:37:06 -070085
Josh Garguse5d3f892012-04-11 11:56:15 -070086 private Uri mContactUri;
87
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080088 @Override
89 public void onCreate(Bundle icicle) {
90 super.onCreate(icicle);
91
Brian Attwellbdd32642015-05-08 17:03:15 -070092 if (RequestPermissionsActivity.startPermissionActivity(this)) {
93 return;
94 }
95
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080096 if (icicle != null) {
Josh Garguse5d3f892012-04-11 11:56:15 -070097 final String uri = icicle.getString(KEY_CONTACT_URI);
98 mContactUri = (uri == null) ? null : Uri.parse(uri);
Josh Garguse5d3f892012-04-11 11:56:15 -070099 mTempPhotoUri = Uri.parse(icicle.getString(KEY_TEMP_PHOTO_URI));
Yorke Leeadeadcf2013-09-19 17:10:07 -0700100 mCroppedPhotoUri = Uri.parse(icicle.getString(KEY_CROPPED_PHOTO_URI));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800101 } else {
Yorke Lee637a38e2013-09-14 08:36:33 -0700102 mTempPhotoUri = ContactPhotoUtils.generateTempImageUri(this);
Yorke Leeadeadcf2013-09-19 17:10:07 -0700103 mCroppedPhotoUri = ContactPhotoUtils.generateTempCroppedImageUri(this);
Josh Gargus4a964d92012-05-11 13:18:48 -0700104 Intent intent = new Intent(Intent.ACTION_PICK);
105 intent.setType(Contacts.CONTENT_TYPE);
Brian Attwell4785c512015-06-26 12:23:25 -0700106 intent.setPackage(getPackageName());
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107 startActivityForResult(intent, REQUEST_PICK_CONTACT);
108 }
Neel Parekh2ad90a32009-09-20 19:08:50 -0700109
110 mContentResolver = getContentResolver();
Dave Santoro0a2a5db2011-06-29 00:37:06 -0700111
Jay Shrauner946ebfa2014-01-14 16:07:01 -0800112 // Load the photo dimension to request. mPhotoDim is a static class
113 // member varible so only need to load this if this is the first time
114 // through.
115 if (mPhotoDim == 0) {
116 Cursor c = mContentResolver.query(DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
117 new String[]{DisplayPhoto.DISPLAY_MAX_DIM}, null, null, null);
118 if (c != null) {
119 try {
Jay Shraunerde047f42014-01-18 10:45:01 -0800120 if (c.moveToFirst()) {
121 mPhotoDim = c.getInt(0);
122 }
Jay Shrauner946ebfa2014-01-14 16:07:01 -0800123 } finally {
124 c.close();
125 }
126 }
Dave Santoro0a2a5db2011-06-29 00:37:06 -0700127 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800128 }
129
130 @Override
131 protected void onSaveInstanceState(Bundle outState) {
132 super.onSaveInstanceState(outState);
Yorke Lee637a38e2013-09-14 08:36:33 -0700133 if (mContactUri != null) {
134 outState.putString(KEY_CONTACT_URI, mContactUri.toString());
135 }
136 if (mTempPhotoUri != null) {
137 outState.putString(KEY_TEMP_PHOTO_URI, mTempPhotoUri.toString());
138 }
Yorke Lee2518abd2013-09-29 14:47:36 -0700139 if (mCroppedPhotoUri != null) {
140 outState.putString(KEY_CROPPED_PHOTO_URI, mCroppedPhotoUri.toString());
141 }
Neel Parekh2ad90a32009-09-20 19:08:50 -0700142 }
143
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800144 @Override
145 protected void onActivityResult(int requestCode, int resultCode, Intent result) {
Brian Attwellb76f86d2014-10-09 17:39:06 -0700146 if (requestCode == REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT) {
147 // Bail if the account selector was not successful.
148 if (resultCode != Activity.RESULT_OK) {
149 Log.w(TAG, "account selector was not successful");
150 finish();
151 return;
152 }
153 // If there's an account specified, use it.
154 if (result != null) {
Brian Attwell4a1c5742015-01-26 15:59:58 -0800155 AccountWithDataSet account = result.getParcelableExtra(
156 Intents.Insert.EXTRA_ACCOUNT);
Brian Attwellb76f86d2014-10-09 17:39:06 -0700157 if (account != null) {
158 createNewRawContact(account);
159 return;
160 }
161 }
162 // If there isn't an account specified, then the user opted to keep the contact local.
163 createNewRawContact(null);
164 } else if (requestCode == REQUEST_PICK_CONTACT) {
Yorke Leeadeadcf2013-09-19 17:10:07 -0700165 if (resultCode != RESULT_OK) {
166 finish();
167 return;
168 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800169 // A contact was picked. Launch the cropper to get face detection, the right size, etc.
170 // TODO: get these values from constants somewhere
Yorke Leeadeadcf2013-09-19 17:10:07 -0700171 final Intent myIntent = getIntent();
172 final Uri inputUri = myIntent.getData();
173
Brian Attwell5234be92015-05-13 19:26:43 -0700174
Jay Shrauner946ebfa2014-01-14 16:07:01 -0800175 // Save the URI into a temporary file provider URI so that
176 // we can add the FLAG_GRANT_WRITE_URI_PERMISSION flag to the eventual
177 // crop intent for read-only URI's.
178 // TODO: With b/10837468 fixed should be able to avoid this copy.
Jay Shraunera9433712015-02-27 15:47:09 -0800179 if (!ContactPhotoUtils.savePhotoFromUriToUri(this, inputUri, mTempPhotoUri, false)) {
180 finish();
181 return;
182 }
Yorke Lee637a38e2013-09-14 08:36:33 -0700183
Brian Attwell5234be92015-05-13 19:26:43 -0700184 final Intent intent = new Intent("com.android.camera.action.CROP", mTempPhotoUri);
Yorke Leeadeadcf2013-09-19 17:10:07 -0700185 if (myIntent.getStringExtra("mimeType") != null) {
Brian Attwell5234be92015-05-13 19:26:43 -0700186 intent.setDataAndType(mTempPhotoUri, myIntent.getStringExtra("mimeType"));
Yorke Leeadeadcf2013-09-19 17:10:07 -0700187 }
188 ContactPhotoUtils.addPhotoPickerExtras(intent, mCroppedPhotoUri);
Jay Shrauner946ebfa2014-01-14 16:07:01 -0800189 ContactPhotoUtils.addCropExtras(intent, mPhotoDim != 0 ? mPhotoDim : mDefaultPhotoDim);
Brian Attwell5234be92015-05-13 19:26:43 -0700190 if (!hasIntentHandler(intent)) {
191 // No activity supports the crop action. So skip cropping and set the photo
192 // without performing any cropping.
193 mCroppedPhotoUri = mTempPhotoUri;
194 mContactUri = result.getData();
195 loadContact(mContactUri, new Listener() {
196 @Override
197 public void onContactLoaded(Contact contact) {
198 saveContact(contact);
199 }
200 });
201 return;
202 }
Josh Garguse5d3f892012-04-11 11:56:15 -0700203
Jay Shrauner66f778e2014-05-31 21:36:09 -0700204 try {
205 startActivityForResult(intent, REQUEST_CROP_PHOTO);
206 } catch (ActivityNotFoundException ex) {
207 Toast.makeText(this, R.string.missing_app, Toast.LENGTH_SHORT).show();
208 return;
209 }
Neel Parekhbe406ff2009-09-16 15:31:22 -0700210
Josh Garguse5d3f892012-04-11 11:56:15 -0700211 mContactUri = result.getData();
Neel Parekhbe406ff2009-09-16 15:31:22 -0700212
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800213 } else if (requestCode == REQUEST_CROP_PHOTO) {
Yorke Leeadeadcf2013-09-19 17:10:07 -0700214 // Delete the temporary photo from cache now that we have a cropped version.
215 // We should do this even if the crop failed and we eventually bail
216 getContentResolver().delete(mTempPhotoUri, null, null);
217 if (resultCode != RESULT_OK) {
218 finish();
219 return;
220 }
Daniel Lehmanndd3dc562012-04-24 20:33:19 -0700221 loadContact(mContactUri, new Listener() {
Josh Garguse5d3f892012-04-11 11:56:15 -0700222 @Override
Maurice Chu851222a2012-06-21 11:43:08 -0700223 public void onContactLoaded(Contact contact) {
Josh Garguse5d3f892012-04-11 11:56:15 -0700224 saveContact(contact);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800225 }
Josh Garguse5d3f892012-04-11 11:56:15 -0700226 });
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800227 }
228 }
Neel Parekh2ad90a32009-09-20 19:08:50 -0700229
Brian Attwell5234be92015-05-13 19:26:43 -0700230 private boolean hasIntentHandler(Intent intent) {
231 final List<ResolveInfo> resolveInfo = getPackageManager()
232 .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
233 return resolveInfo != null && resolveInfo.size() > 0;
234 }
235
Josh Garguse5d3f892012-04-11 11:56:15 -0700236 // TODO: consider moving this to ContactLoader, especially if we keep adding similar
237 // code elsewhere (ViewNotificationService is another case). The only concern is that,
238 // although this is convenient, it isn't quite as robust as using LoaderManager... for
239 // instance, the loader doesn't persist across Activity restarts.
Daniel Lehmanndd3dc562012-04-24 20:33:19 -0700240 private void loadContact(Uri contactUri, final Listener listener) {
Makoto Onuki45ee8722012-05-21 16:33:25 -0700241 final ContactLoader loader = new ContactLoader(this, contactUri, true);
Maurice Chu851222a2012-06-21 11:43:08 -0700242 loader.registerListener(0, new OnLoadCompleteListener<Contact>() {
Josh Garguse5d3f892012-04-11 11:56:15 -0700243 @Override
244 public void onLoadComplete(
Maurice Chu851222a2012-06-21 11:43:08 -0700245 Loader<Contact> loader, Contact contact) {
Josh Garguse5d3f892012-04-11 11:56:15 -0700246 try {
247 loader.reset();
Dmitri Plotnikov19d51ac2011-01-04 14:30:24 -0800248 }
Josh Garguse5d3f892012-04-11 11:56:15 -0700249 catch (RuntimeException e) {
250 Log.e(TAG, "Error resetting loader", e);
251 }
252 listener.onContactLoaded(contact);
Dmitri Plotnikov19d51ac2011-01-04 14:30:24 -0800253 }
Josh Garguse5d3f892012-04-11 11:56:15 -0700254 });
255 loader.startLoading();
Dmitri Plotnikov19d51ac2011-01-04 14:30:24 -0800256 }
257
Daniel Lehmanndd3dc562012-04-24 20:33:19 -0700258 private interface Listener {
Maurice Chu851222a2012-06-21 11:43:08 -0700259 public void onContactLoaded(Contact contact);
Daniel Lehmanndd3dc562012-04-24 20:33:19 -0700260 }
261
Neel Parekh2ad90a32009-09-20 19:08:50 -0700262 /**
Josh Garguse5d3f892012-04-11 11:56:15 -0700263 * If prerequisites have been met, attach the photo to a raw-contact and save.
264 * The prerequisites are:
265 * - photo has been cropped
266 * - contact has been loaded
Neel Parekh2ad90a32009-09-20 19:08:50 -0700267 */
Maurice Chu851222a2012-06-21 11:43:08 -0700268 private void saveContact(Contact contact) {
Neel Parekh2ad90a32009-09-20 19:08:50 -0700269
jiezha2x342de102013-06-05 16:23:24 +0800270 if (contact.getRawContacts() == null) {
271 Log.w(TAG, "No raw contacts found for contact");
272 finish();
273 return;
274 }
275
Josh Garguse5d3f892012-04-11 11:56:15 -0700276 // Obtain the raw-contact that we will save to.
Maurice Chu851222a2012-06-21 11:43:08 -0700277 RawContactDeltaList deltaList = contact.createRawContactDeltaList();
278 RawContactDelta raw = deltaList.getFirstWritableRawContact(this);
Josh Garguse5d3f892012-04-11 11:56:15 -0700279 if (raw == null) {
Brian Attwellb76f86d2014-10-09 17:39:06 -0700280 // We can't directly insert this photo since no raw contacts exist in the contact.
281 selectAccountAndCreateContact();
Josh Garguse5d3f892012-04-11 11:56:15 -0700282 return;
Neel Parekh2ad90a32009-09-20 19:08:50 -0700283 }
284
Brian Attwellb76f86d2014-10-09 17:39:06 -0700285 saveToContact(contact, deltaList, raw);
286 }
287
288 private void saveToContact(Contact contact, RawContactDeltaList deltaList,
289 RawContactDelta raw) {
290
Josh Garguse5d3f892012-04-11 11:56:15 -0700291 // Create a scaled, compressed bitmap to add to the entity-delta list.
292 final int size = ContactsUtils.getThumbnailSize(this);
Yorke Lee637a38e2013-09-14 08:36:33 -0700293 Bitmap bitmap;
294 try {
Yorke Leeadeadcf2013-09-19 17:10:07 -0700295 bitmap = ContactPhotoUtils.getBitmapFromUri(this, mCroppedPhotoUri);
Yorke Lee637a38e2013-09-14 08:36:33 -0700296 } catch (FileNotFoundException e) {
297 Log.w(TAG, "Could not find bitmap");
Brian Attwellb76f86d2014-10-09 17:39:06 -0700298 finish();
Yorke Lee637a38e2013-09-14 08:36:33 -0700299 return;
300 }
Jay Shrauner1e090bf2014-01-30 12:21:01 -0800301 if (bitmap == null) {
302 Log.w(TAG, "Could not decode bitmap");
Brian Attwellb76f86d2014-10-09 17:39:06 -0700303 finish();
Jay Shrauner1e090bf2014-01-30 12:21:01 -0800304 return;
305 }
Yorke Lee637a38e2013-09-14 08:36:33 -0700306
Josh Garguse5d3f892012-04-11 11:56:15 -0700307 final Bitmap scaled = Bitmap.createScaledBitmap(bitmap, size, size, false);
308 final byte[] compressed = ContactPhotoUtils.compressBitmap(scaled);
309 if (compressed == null) {
310 Log.w(TAG, "could not create scaled and compressed Bitmap");
Brian Attwellb76f86d2014-10-09 17:39:06 -0700311 finish();
Josh Garguse5d3f892012-04-11 11:56:15 -0700312 return;
Neel Parekh2ad90a32009-09-20 19:08:50 -0700313 }
Brian Attwellb76f86d2014-10-09 17:39:06 -0700314
Josh Garguse5d3f892012-04-11 11:56:15 -0700315 // Add compressed bitmap to entity-delta... this allows us to save to
316 // a new contact; otherwise the entity-delta-list would be empty, and
317 // the ContactSaveService would not create the new contact, and the
318 // full-res photo would fail to be saved to the non-existent contact.
319 AccountType account = raw.getRawContactAccountType(this);
Chiao Chengaa690d42012-11-28 18:10:43 -0800320 ValuesDelta values =
Maurice Chu851222a2012-06-21 11:43:08 -0700321 RawContactModifier.ensureKindExists(raw, account, Photo.CONTENT_ITEM_TYPE);
Josh Garguse5d3f892012-04-11 11:56:15 -0700322 if (values == null) {
323 Log.w(TAG, "cannot attach photo to this account type");
Brian Attwellb76f86d2014-10-09 17:39:06 -0700324 finish();
Josh Garguse5d3f892012-04-11 11:56:15 -0700325 return;
Neel Parekh2ad90a32009-09-20 19:08:50 -0700326 }
Maurice Chu851222a2012-06-21 11:43:08 -0700327 values.setPhoto(compressed);
Josh Garguse5d3f892012-04-11 11:56:15 -0700328
329 // Finally, invoke the ContactSaveService.
330 Log.v(TAG, "all prerequisites met, about to save photo to contact");
331 Intent intent = ContactSaveService.createSaveContactIntent(
332 this,
333 deltaList,
334 "", 0,
335 contact.isUserProfile(),
336 null, null,
Brian Attwellb76f86d2014-10-09 17:39:06 -0700337 raw.getRawContactId() != null ? raw.getRawContactId() : -1,
Yorke Leeadeadcf2013-09-19 17:10:07 -0700338 mCroppedPhotoUri
Brian Attwellb76f86d2014-10-09 17:39:06 -0700339 );
Wenyi Wangdd7d4562015-12-08 13:33:43 -0800340 ContactSaveService.startService(this, intent);
Josh Garguse5d3f892012-04-11 11:56:15 -0700341 finish();
Neel Parekh2ad90a32009-09-20 19:08:50 -0700342 }
Brian Attwellb76f86d2014-10-09 17:39:06 -0700343
344 private void selectAccountAndCreateContact() {
345 // If there is no default account or the accounts have changed such that we need to
346 // prompt the user again, then launch the account prompt.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700347 final ContactEditorUtils editorUtils = ContactEditorUtils.create(this);
Brian Attwellb76f86d2014-10-09 17:39:06 -0700348 if (editorUtils.shouldShowAccountChangedNotification()) {
349 Intent intent = new Intent(this, ContactEditorAccountsChangedActivity.class);
350 startActivityForResult(intent, REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT);
351 } else {
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700352 // Otherwise, there should be a default account. Then either create a null contact
Brian Attwellb76f86d2014-10-09 17:39:06 -0700353 // (if default account is null) or create a contact with the specified account.
Marcus Hagerott949d4e82016-09-20 13:23:05 -0700354 final AccountWithDataSet targetAccount = editorUtils.getOnlyOrDefaultAccount();
355 createNewRawContact(targetAccount);
Brian Attwellb76f86d2014-10-09 17:39:06 -0700356 }
357 }
358
359 /**
360 * Create a new writeable raw contact to store mCroppedPhotoUri.
361 */
362 private void createNewRawContact(final AccountWithDataSet account) {
363 // Reload the contact from URI instead of trying to pull the contact from a member variable,
364 // since this function can be called after the activity stops and resumes.
365 loadContact(mContactUri, new Listener() {
366 @Override
367 public void onContactLoaded(Contact contactToSave) {
368 final RawContactDeltaList deltaList = contactToSave.createRawContactDeltaList();
369 final ContentValues after = new ContentValues();
370 after.put(RawContacts.ACCOUNT_TYPE, account != null ? account.type : null);
371 after.put(RawContacts.ACCOUNT_NAME, account != null ? account.name : null);
372 after.put(RawContacts.DATA_SET, account != null ? account.dataSet : null);
373
374 final RawContactDelta newRawContactDelta
375 = new RawContactDelta(ValuesDelta.fromAfter(after));
376 deltaList.add(newRawContactDelta);
377 saveToContact(contactToSave, deltaList, newRawContactDelta);
378 }
379 });
380 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800381}