The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1 | /* |
| 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 Plotnikov | 19d51ac | 2011-01-04 14:30:24 -0800 | [diff] [blame] | 17 | package com.android.contacts.activities; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 18 | |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Jay Shrauner | 66f778e | 2014-05-31 21:36:09 -0700 | [diff] [blame] | 20 | import android.content.ActivityNotFoundException; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 22 | import android.content.ContentValues; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 24 | import android.content.Loader; |
| 25 | import android.content.Loader.OnLoadCompleteListener; |
Brian Attwell | 5234be9 | 2015-05-13 19:26:43 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 28 | import android.database.Cursor; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
| 30 | import android.net.Uri; |
| 31 | import android.os.Bundle; |
Dmitri Plotnikov | 19d51ac | 2011-01-04 14:30:24 -0800 | [diff] [blame] | 32 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Neel Parekh | be406ff | 2009-09-16 15:31:22 -0700 | [diff] [blame] | 33 | import android.provider.ContactsContract.Contacts; |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 34 | import android.provider.ContactsContract.DisplayPhoto; |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract.Intents; |
| 36 | import android.provider.ContactsContract.RawContacts; |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 37 | import android.util.Log; |
Jay Shrauner | 66f778e | 2014-05-31 21:36:09 -0700 | [diff] [blame] | 38 | import android.widget.Toast; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 39 | |
Chiao Cheng | 61414c2 | 2012-06-18 16:59:06 -0700 | [diff] [blame] | 40 | import com.android.contacts.ContactSaveService; |
| 41 | import com.android.contacts.ContactsActivity; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 42 | import com.android.contacts.ContactsUtils; |
Jay Shrauner | 66f778e | 2014-05-31 21:36:09 -0700 | [diff] [blame] | 43 | import com.android.contacts.R; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 44 | import com.android.contacts.editor.ContactEditorUtils; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 45 | import com.android.contacts.model.Contact; |
| 46 | import com.android.contacts.model.ContactLoader; |
| 47 | import com.android.contacts.model.RawContactDelta; |
| 48 | import com.android.contacts.model.RawContactDeltaList; |
| 49 | import com.android.contacts.model.RawContactModifier; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 50 | import com.android.contacts.model.ValuesDelta; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 51 | import com.android.contacts.model.account.AccountType; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 52 | import com.android.contacts.model.account.AccountWithDataSet; |
Chiao Cheng | 61414c2 | 2012-06-18 16:59:06 -0700 | [diff] [blame] | 53 | import com.android.contacts.util.ContactPhotoUtils; |
| 54 | |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 55 | import java.io.FileNotFoundException; |
Brian Attwell | 5234be9 | 2015-05-13 19:26:43 -0700 | [diff] [blame] | 56 | import java.util.List; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 57 | |
| 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 Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 64 | public class AttachPhotoActivity extends ContactsActivity { |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 65 | private static final String TAG = AttachPhotoActivity.class.getSimpleName(); |
| 66 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 67 | private static final int REQUEST_PICK_CONTACT = 1; |
| 68 | private static final int REQUEST_CROP_PHOTO = 2; |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 69 | private static final int REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT = 3; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 70 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 71 | private static final String KEY_CONTACT_URI = "contact_uri"; |
| 72 | private static final String KEY_TEMP_PHOTO_URI = "temp_photo_uri"; |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 73 | private static final String KEY_CROPPED_PHOTO_URI = "cropped_photo_uri"; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 74 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 75 | private Uri mTempPhotoUri; |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 76 | private Uri mCroppedPhotoUri; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 77 | |
| 78 | private ContentResolver mContentResolver; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 79 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 80 | // Height and width (in pixels) to request for the photo - queried from the provider. |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 81 | private static int mPhotoDim; |
Jay Shrauner | 946ebfa | 2014-01-14 16:07:01 -0800 | [diff] [blame] | 82 | // Default photo dimension to use if unable to query the provider. |
| 83 | private static final int mDefaultPhotoDim = 720; |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 84 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 85 | private Uri mContactUri; |
| 86 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 87 | @Override |
| 88 | public void onCreate(Bundle icicle) { |
| 89 | super.onCreate(icicle); |
| 90 | |
Brian Attwell | bdd3264 | 2015-05-08 17:03:15 -0700 | [diff] [blame] | 91 | if (RequestPermissionsActivity.startPermissionActivity(this)) { |
| 92 | return; |
| 93 | } |
| 94 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 95 | if (icicle != null) { |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 96 | final String uri = icicle.getString(KEY_CONTACT_URI); |
| 97 | mContactUri = (uri == null) ? null : Uri.parse(uri); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 98 | mTempPhotoUri = Uri.parse(icicle.getString(KEY_TEMP_PHOTO_URI)); |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 99 | mCroppedPhotoUri = Uri.parse(icicle.getString(KEY_CROPPED_PHOTO_URI)); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 100 | } else { |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 101 | mTempPhotoUri = ContactPhotoUtils.generateTempImageUri(this); |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 102 | mCroppedPhotoUri = ContactPhotoUtils.generateTempCroppedImageUri(this); |
Josh Gargus | 4a964d9 | 2012-05-11 13:18:48 -0700 | [diff] [blame] | 103 | Intent intent = new Intent(Intent.ACTION_PICK); |
| 104 | intent.setType(Contacts.CONTENT_TYPE); |
Brian Attwell | 4785c51 | 2015-06-26 12:23:25 -0700 | [diff] [blame] | 105 | intent.setPackage(getPackageName()); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 106 | startActivityForResult(intent, REQUEST_PICK_CONTACT); |
| 107 | } |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 108 | |
| 109 | mContentResolver = getContentResolver(); |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 110 | |
Jay Shrauner | 946ebfa | 2014-01-14 16:07:01 -0800 | [diff] [blame] | 111 | // 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 Shrauner | de047f4 | 2014-01-18 10:45:01 -0800 | [diff] [blame] | 119 | if (c.moveToFirst()) { |
| 120 | mPhotoDim = c.getInt(0); |
| 121 | } |
Jay Shrauner | 946ebfa | 2014-01-14 16:07:01 -0800 | [diff] [blame] | 122 | } finally { |
| 123 | c.close(); |
| 124 | } |
| 125 | } |
Dave Santoro | 0a2a5db | 2011-06-29 00:37:06 -0700 | [diff] [blame] | 126 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | protected void onSaveInstanceState(Bundle outState) { |
| 131 | super.onSaveInstanceState(outState); |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 132 | 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 Lee | 2518abd | 2013-09-29 14:47:36 -0700 | [diff] [blame] | 138 | if (mCroppedPhotoUri != null) { |
| 139 | outState.putString(KEY_CROPPED_PHOTO_URI, mCroppedPhotoUri.toString()); |
| 140 | } |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 141 | } |
| 142 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 143 | @Override |
| 144 | protected void onActivityResult(int requestCode, int resultCode, Intent result) { |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 145 | 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 Attwell | 4a1c574 | 2015-01-26 15:59:58 -0800 | [diff] [blame] | 154 | AccountWithDataSet account = result.getParcelableExtra( |
| 155 | Intents.Insert.EXTRA_ACCOUNT); |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 156 | 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 Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 164 | if (resultCode != RESULT_OK) { |
| 165 | finish(); |
| 166 | return; |
| 167 | } |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 168 | // A contact was picked. Launch the cropper to get face detection, the right size, etc. |
| 169 | // TODO: get these values from constants somewhere |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 170 | final Intent myIntent = getIntent(); |
| 171 | final Uri inputUri = myIntent.getData(); |
| 172 | |
Brian Attwell | 5234be9 | 2015-05-13 19:26:43 -0700 | [diff] [blame] | 173 | |
Jay Shrauner | 946ebfa | 2014-01-14 16:07:01 -0800 | [diff] [blame] | 174 | // 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 Shrauner | a943371 | 2015-02-27 15:47:09 -0800 | [diff] [blame] | 178 | if (!ContactPhotoUtils.savePhotoFromUriToUri(this, inputUri, mTempPhotoUri, false)) { |
| 179 | finish(); |
| 180 | return; |
| 181 | } |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 182 | |
Brian Attwell | 5234be9 | 2015-05-13 19:26:43 -0700 | [diff] [blame] | 183 | final Intent intent = new Intent("com.android.camera.action.CROP", mTempPhotoUri); |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 184 | if (myIntent.getStringExtra("mimeType") != null) { |
Brian Attwell | 5234be9 | 2015-05-13 19:26:43 -0700 | [diff] [blame] | 185 | intent.setDataAndType(mTempPhotoUri, myIntent.getStringExtra("mimeType")); |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 186 | } |
| 187 | ContactPhotoUtils.addPhotoPickerExtras(intent, mCroppedPhotoUri); |
Jay Shrauner | 946ebfa | 2014-01-14 16:07:01 -0800 | [diff] [blame] | 188 | ContactPhotoUtils.addCropExtras(intent, mPhotoDim != 0 ? mPhotoDim : mDefaultPhotoDim); |
Brian Attwell | 5234be9 | 2015-05-13 19:26:43 -0700 | [diff] [blame] | 189 | 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 Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 202 | |
Jay Shrauner | 66f778e | 2014-05-31 21:36:09 -0700 | [diff] [blame] | 203 | 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 Parekh | be406ff | 2009-09-16 15:31:22 -0700 | [diff] [blame] | 209 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 210 | mContactUri = result.getData(); |
Neel Parekh | be406ff | 2009-09-16 15:31:22 -0700 | [diff] [blame] | 211 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 212 | } else if (requestCode == REQUEST_CROP_PHOTO) { |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 213 | // 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 Lehmann | dd3dc56 | 2012-04-24 20:33:19 -0700 | [diff] [blame] | 220 | loadContact(mContactUri, new Listener() { |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 221 | @Override |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 222 | public void onContactLoaded(Contact contact) { |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 223 | saveContact(contact); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 224 | } |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 225 | }); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 228 | |
Brian Attwell | 5234be9 | 2015-05-13 19:26:43 -0700 | [diff] [blame] | 229 | 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 Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 235 | // 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 Lehmann | dd3dc56 | 2012-04-24 20:33:19 -0700 | [diff] [blame] | 239 | private void loadContact(Uri contactUri, final Listener listener) { |
Makoto Onuki | 45ee872 | 2012-05-21 16:33:25 -0700 | [diff] [blame] | 240 | final ContactLoader loader = new ContactLoader(this, contactUri, true); |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 241 | loader.registerListener(0, new OnLoadCompleteListener<Contact>() { |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 242 | @Override |
| 243 | public void onLoadComplete( |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 244 | Loader<Contact> loader, Contact contact) { |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 245 | try { |
| 246 | loader.reset(); |
Dmitri Plotnikov | 19d51ac | 2011-01-04 14:30:24 -0800 | [diff] [blame] | 247 | } |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 248 | catch (RuntimeException e) { |
| 249 | Log.e(TAG, "Error resetting loader", e); |
| 250 | } |
| 251 | listener.onContactLoaded(contact); |
Dmitri Plotnikov | 19d51ac | 2011-01-04 14:30:24 -0800 | [diff] [blame] | 252 | } |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 253 | }); |
| 254 | loader.startLoading(); |
Dmitri Plotnikov | 19d51ac | 2011-01-04 14:30:24 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Daniel Lehmann | dd3dc56 | 2012-04-24 20:33:19 -0700 | [diff] [blame] | 257 | private interface Listener { |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 258 | public void onContactLoaded(Contact contact); |
Daniel Lehmann | dd3dc56 | 2012-04-24 20:33:19 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 261 | /** |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 262 | * 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 Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 266 | */ |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 267 | private void saveContact(Contact contact) { |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 268 | |
jiezha2x | 342de10 | 2013-06-05 16:23:24 +0800 | [diff] [blame] | 269 | if (contact.getRawContacts() == null) { |
| 270 | Log.w(TAG, "No raw contacts found for contact"); |
| 271 | finish(); |
| 272 | return; |
| 273 | } |
| 274 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 275 | // Obtain the raw-contact that we will save to. |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 276 | RawContactDeltaList deltaList = contact.createRawContactDeltaList(); |
| 277 | RawContactDelta raw = deltaList.getFirstWritableRawContact(this); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 278 | if (raw == null) { |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 279 | // We can't directly insert this photo since no raw contacts exist in the contact. |
| 280 | selectAccountAndCreateContact(); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 281 | return; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 284 | saveToContact(contact, deltaList, raw); |
| 285 | } |
| 286 | |
| 287 | private void saveToContact(Contact contact, RawContactDeltaList deltaList, |
| 288 | RawContactDelta raw) { |
| 289 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 290 | // Create a scaled, compressed bitmap to add to the entity-delta list. |
| 291 | final int size = ContactsUtils.getThumbnailSize(this); |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 292 | Bitmap bitmap; |
| 293 | try { |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 294 | bitmap = ContactPhotoUtils.getBitmapFromUri(this, mCroppedPhotoUri); |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 295 | } catch (FileNotFoundException e) { |
| 296 | Log.w(TAG, "Could not find bitmap"); |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 297 | finish(); |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 298 | return; |
| 299 | } |
Jay Shrauner | 1e090bf | 2014-01-30 12:21:01 -0800 | [diff] [blame] | 300 | if (bitmap == null) { |
| 301 | Log.w(TAG, "Could not decode bitmap"); |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 302 | finish(); |
Jay Shrauner | 1e090bf | 2014-01-30 12:21:01 -0800 | [diff] [blame] | 303 | return; |
| 304 | } |
Yorke Lee | 637a38e | 2013-09-14 08:36:33 -0700 | [diff] [blame] | 305 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 306 | 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 Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 310 | finish(); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 311 | return; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 312 | } |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 313 | |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 314 | // 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 Cheng | aa690d4 | 2012-11-28 18:10:43 -0800 | [diff] [blame] | 319 | ValuesDelta values = |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 320 | RawContactModifier.ensureKindExists(raw, account, Photo.CONTENT_ITEM_TYPE); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 321 | if (values == null) { |
| 322 | Log.w(TAG, "cannot attach photo to this account type"); |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 323 | finish(); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 324 | return; |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 325 | } |
Maurice Chu | 851222a | 2012-06-21 11:43:08 -0700 | [diff] [blame] | 326 | values.setPhoto(compressed); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 327 | |
| 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 Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 336 | raw.getRawContactId() != null ? raw.getRawContactId() : -1, |
Yorke Lee | adeadcf | 2013-09-19 17:10:07 -0700 | [diff] [blame] | 337 | mCroppedPhotoUri |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 338 | ); |
Wenyi Wang | dd7d456 | 2015-12-08 13:33:43 -0800 | [diff] [blame] | 339 | ContactSaveService.startService(this, intent); |
Josh Gargus | e5d3f89 | 2012-04-11 11:56:15 -0700 | [diff] [blame] | 340 | finish(); |
Neel Parekh | 2ad90a3 | 2009-09-20 19:08:50 -0700 | [diff] [blame] | 341 | } |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 342 | |
| 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 Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 346 | final ContactEditorUtils editorUtils = ContactEditorUtils.create(this); |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 347 | if (editorUtils.shouldShowAccountChangedNotification()) { |
| 348 | Intent intent = new Intent(this, ContactEditorAccountsChangedActivity.class); |
| 349 | startActivityForResult(intent, REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT); |
| 350 | } else { |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 351 | // Otherwise, there should be a default account. Then either create a null contact |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 352 | // (if default account is null) or create a contact with the specified account. |
Marcus Hagerott | 949d4e8 | 2016-09-20 13:23:05 -0700 | [diff] [blame] | 353 | final AccountWithDataSet targetAccount = editorUtils.getOnlyOrDefaultAccount(); |
| 354 | createNewRawContact(targetAccount); |
Brian Attwell | b76f86d | 2014-10-09 17:39:06 -0700 | [diff] [blame] | 355 | } |
| 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 Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 380 | } |