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 
17 package com.android.contacts.activities;
18 
19 import android.app.Activity;
20 import android.content.ActivityNotFoundException;
21 import android.content.ContentResolver;
22 import android.content.ContentValues;
23 import android.content.Intent;
24 import android.content.Loader;
25 import android.content.Loader.OnLoadCompleteListener;
26 import android.content.pm.PackageManager;
27 import android.content.pm.ResolveInfo;
28 import android.database.Cursor;
29 import android.graphics.Bitmap;
30 import android.net.Uri;
31 import android.os.Bundle;
32 import android.provider.ContactsContract.CommonDataKinds.Photo;
33 import android.provider.ContactsContract.Contacts;
34 import android.provider.ContactsContract.DisplayPhoto;
35 import android.provider.ContactsContract.Intents;
36 import android.provider.ContactsContract.RawContacts;
37 import android.util.Log;
38 import android.widget.Toast;
39 
40 import com.android.contacts.ContactSaveService;
41 import com.android.contacts.ContactsActivity;
42 import com.android.contacts.ContactsUtils;
43 import com.android.contacts.R;
44 import com.android.contacts.editor.ContactEditorUtils;
45 import com.android.contacts.model.AccountTypeManager;
46 import com.android.contacts.model.Contact;
47 import com.android.contacts.model.ContactLoader;
48 import com.android.contacts.model.RawContactDelta;
49 import com.android.contacts.model.RawContactDeltaList;
50 import com.android.contacts.model.RawContactModifier;
51 import com.android.contacts.model.ValuesDelta;
52 import com.android.contacts.model.account.AccountInfo;
53 import com.android.contacts.model.account.AccountType;
54 import com.android.contacts.model.account.AccountWithDataSet;
55 import com.android.contacts.util.ContactPhotoUtils;
56 import com.google.common.base.Preconditions;
57 import com.google.common.util.concurrent.Futures;
58 import com.google.common.util.concurrent.ListenableFuture;
59 
60 import java.io.FileNotFoundException;
61 import java.util.List;
62 
63 /**
64  * Provides an external interface for other applications to attach images
65  * to contacts. It will first present a contact picker and then run the
66  * image that is handed to it through the cropper to make the image the proper
67  * size and give the user a chance to use the face detector.
68  */
69 public class AttachPhotoActivity extends ContactsActivity {
70     private static final String TAG = AttachPhotoActivity.class.getSimpleName();
71 
72     private static final int REQUEST_PICK_CONTACT = 1;
73     private static final int REQUEST_CROP_PHOTO = 2;
74     private static final int REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT = 3;
75 
76     private static final String KEY_CONTACT_URI = "contact_uri";
77     private static final String KEY_TEMP_PHOTO_URI = "temp_photo_uri";
78     private static final String KEY_CROPPED_PHOTO_URI = "cropped_photo_uri";
79 
80     private Uri mTempPhotoUri;
81     private Uri mCroppedPhotoUri;
82 
83     private ContentResolver mContentResolver;
84 
85     private ListenableFuture<List<AccountInfo>> mAccountsFuture;
86 
87     // Height and width (in pixels) to request for the photo - queried from the provider.
88     private static int mPhotoDim;
89     // Default photo dimension to use if unable to query the provider.
90     private static final int mDefaultPhotoDim = 720;
91 
92     private Uri mContactUri;
93 
94     @Override
onCreate(Bundle icicle)95     public void onCreate(Bundle icicle) {
96         super.onCreate(icicle);
97 
98         if (RequestPermissionsActivity.startPermissionActivityIfNeeded(this)) {
99             return;
100         }
101 
102         if (icicle != null) {
103             final String uri = icicle.getString(KEY_CONTACT_URI);
104             mContactUri = (uri == null) ? null : Uri.parse(uri);
105             mTempPhotoUri = Uri.parse(icicle.getString(KEY_TEMP_PHOTO_URI));
106             mCroppedPhotoUri = Uri.parse(icicle.getString(KEY_CROPPED_PHOTO_URI));
107         } else {
108             mTempPhotoUri = ContactPhotoUtils.generateTempImageUri(this);
109             mCroppedPhotoUri = ContactPhotoUtils.generateTempCroppedImageUri(this);
110             Intent intent = new Intent(Intent.ACTION_PICK);
111             intent.setType(Contacts.CONTENT_TYPE);
112             intent.setPackage(getPackageName());
113             startActivityForResult(intent, REQUEST_PICK_CONTACT);
114         }
115 
116         mContentResolver = getContentResolver();
117 
118         // Load the photo dimension to request. mPhotoDim is a static class
119         // member varible so only need to load this if this is the first time
120         // through.
121         if (mPhotoDim == 0) {
122             Cursor c = mContentResolver.query(DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
123                     new String[]{DisplayPhoto.DISPLAY_MAX_DIM}, null, null, null);
124             if (c != null) {
125                 try {
126                     if (c.moveToFirst()) {
127                         mPhotoDim = c.getInt(0);
128                     }
129                 } finally {
130                     c.close();
131                 }
132             }
133         }
134 
135         // Start loading accounts in case they are needed.
136         mAccountsFuture = AccountTypeManager.getInstance(this).filterAccountsAsync(
137                 AccountTypeManager.writableFilter());
138     }
139 
140     @Override
onSaveInstanceState(Bundle outState)141     protected void onSaveInstanceState(Bundle outState) {
142         super.onSaveInstanceState(outState);
143         if (mContactUri != null) {
144             outState.putString(KEY_CONTACT_URI, mContactUri.toString());
145         }
146         if (mTempPhotoUri != null) {
147             outState.putString(KEY_TEMP_PHOTO_URI, mTempPhotoUri.toString());
148         }
149         if (mCroppedPhotoUri != null) {
150             outState.putString(KEY_CROPPED_PHOTO_URI, mCroppedPhotoUri.toString());
151         }
152     }
153 
154     @Override
onActivityResult(int requestCode, int resultCode, Intent result)155     protected void onActivityResult(int requestCode, int resultCode, Intent result) {
156         if (requestCode == REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT) {
157             // Bail if the account selector was not successful.
158             if (resultCode != Activity.RESULT_OK) {
159                 Log.w(TAG, "account selector was not successful");
160                 finish();
161                 return;
162             }
163             // If there's an account specified, use it.
164             if (result != null) {
165                 AccountWithDataSet account = result.getParcelableExtra(
166                         Intents.Insert.EXTRA_ACCOUNT);
167                 if (account != null) {
168                     createNewRawContact(account);
169                     return;
170                 }
171             }
172             // If there isn't an account specified, then the user opted to keep the contact local.
173             createNewRawContact(null);
174         } else if (requestCode == REQUEST_PICK_CONTACT) {
175             if (resultCode != RESULT_OK) {
176                 finish();
177                 return;
178             }
179             // A contact was picked. Launch the cropper to get face detection, the right size, etc.
180             // TODO: get these values from constants somewhere
181             final Intent myIntent = getIntent();
182             final Uri inputUri = myIntent.getData();
183 
184 
185             // Save the URI into a temporary file provider URI so that
186             // we can add the FLAG_GRANT_WRITE_URI_PERMISSION flag to the eventual
187             // crop intent for read-only URI's.
188             // TODO: With b/10837468 fixed should be able to avoid this copy.
189             if (!ContactPhotoUtils.savePhotoFromUriToUri(this, inputUri, mTempPhotoUri, false)) {
190                 finish();
191                 return;
192             }
193 
194             final Intent intent = new Intent("com.android.camera.action.CROP", mTempPhotoUri);
195             if (myIntent.getStringExtra("mimeType") != null) {
196                 intent.setDataAndType(mTempPhotoUri, myIntent.getStringExtra("mimeType"));
197             }
198             ContactPhotoUtils.addPhotoPickerExtras(intent, mCroppedPhotoUri);
199             ContactPhotoUtils.addCropExtras(intent, mPhotoDim != 0 ? mPhotoDim : mDefaultPhotoDim);
200             final ResolveInfo intentHandler = getIntentHandler(intent);
201             if (intentHandler == null) {
202                 // No activity supports the crop action. So skip cropping and set the photo
203                 // without performing any cropping.
204                 mCroppedPhotoUri = mTempPhotoUri;
205                 mContactUri = result.getData();
206                 loadContact(mContactUri, new Listener() {
207                     @Override
208                     public void onContactLoaded(Contact contact) {
209                         saveContact(contact);
210                     }
211                 });
212                 return;
213             }
214 
215             intent.setPackage(intentHandler.activityInfo.packageName);
216             try {
217                 startActivityForResult(intent, REQUEST_CROP_PHOTO);
218             } catch (ActivityNotFoundException ex) {
219                 Toast.makeText(this, R.string.missing_app, Toast.LENGTH_SHORT).show();
220                 return;
221             }
222 
223             mContactUri = result.getData();
224 
225         } else if (requestCode == REQUEST_CROP_PHOTO) {
226             // Delete the temporary photo from cache now that we have a cropped version.
227             // We should do this even if the crop failed and we eventually bail
228             getContentResolver().delete(mTempPhotoUri, null, null);
229             if (resultCode != RESULT_OK) {
230                 finish();
231                 return;
232             }
233             loadContact(mContactUri, new Listener() {
234                 @Override
235                 public void onContactLoaded(Contact contact) {
236                     saveContact(contact);
237                 }
238             });
239         }
240     }
241 
getIntentHandler(Intent intent)242     private ResolveInfo getIntentHandler(Intent intent) {
243         final List<ResolveInfo> resolveInfos = getPackageManager()
244                 .queryIntentActivities(intent,
245                         PackageManager.MATCH_DEFAULT_ONLY | PackageManager.MATCH_SYSTEM_ONLY);
246         return (resolveInfos != null && resolveInfos.size() > 0) ? resolveInfos.get(0) : null;
247     }
248 
249     // TODO: consider moving this to ContactLoader, especially if we keep adding similar
250     // code elsewhere (ViewNotificationService is another case).  The only concern is that,
251     // although this is convenient, it isn't quite as robust as using LoaderManager... for
252     // instance, the loader doesn't persist across Activity restarts.
loadContact(Uri contactUri, final Listener listener)253     private void loadContact(Uri contactUri, final Listener listener) {
254         final ContactLoader loader = new ContactLoader(this, contactUri, true);
255         loader.registerListener(0, new OnLoadCompleteListener<Contact>() {
256             @Override
257             public void onLoadComplete(
258                     Loader<Contact> loader, Contact contact) {
259                 try {
260                     loader.reset();
261                 }
262                 catch (RuntimeException e) {
263                     Log.e(TAG, "Error resetting loader", e);
264                 }
265                 listener.onContactLoaded(contact);
266             }
267         });
268         loader.startLoading();
269     }
270 
271     private interface Listener {
onContactLoaded(Contact contact)272         public void onContactLoaded(Contact contact);
273     }
274 
275     /**
276      * If prerequisites have been met, attach the photo to a raw-contact and save.
277      * The prerequisites are:
278      * - photo has been cropped
279      * - contact has been loaded
280      */
saveContact(Contact contact)281     private void saveContact(Contact contact) {
282 
283         if (contact.getRawContacts() == null) {
284             Log.w(TAG, "No raw contacts found for contact");
285             finish();
286             return;
287         }
288 
289         // Obtain the raw-contact that we will save to.
290         RawContactDeltaList deltaList = contact.createRawContactDeltaList();
291         RawContactDelta raw = deltaList.getFirstWritableRawContact(this);
292         if (raw == null) {
293             // We can't directly insert this photo since no raw contacts exist in the contact.
294             selectAccountAndCreateContact();
295             return;
296         }
297 
298         saveToContact(contact, deltaList, raw);
299     }
300 
saveToContact(Contact contact, RawContactDeltaList deltaList, RawContactDelta raw)301     private void saveToContact(Contact contact, RawContactDeltaList deltaList,
302             RawContactDelta raw) {
303 
304         // Create a scaled, compressed bitmap to add to the entity-delta list.
305         final int size = ContactsUtils.getThumbnailSize(this);
306         Bitmap bitmap;
307         try {
308             bitmap = ContactPhotoUtils.getBitmapFromUri(this, mCroppedPhotoUri);
309         } catch (FileNotFoundException e) {
310             Log.w(TAG, "Could not find bitmap");
311             finish();
312             return;
313         }
314         if (bitmap == null) {
315             Log.w(TAG, "Could not decode bitmap");
316             finish();
317             return;
318         }
319 
320         final Bitmap scaled = Bitmap.createScaledBitmap(bitmap, size, size, false);
321         final byte[] compressed = ContactPhotoUtils.compressBitmap(scaled);
322         if (compressed == null) {
323             Log.w(TAG, "could not create scaled and compressed Bitmap");
324             finish();
325             return;
326         }
327 
328         // Add compressed bitmap to entity-delta... this allows us to save to
329         // a new contact; otherwise the entity-delta-list would be empty, and
330         // the ContactSaveService would not create the new contact, and the
331         // full-res photo would fail to be saved to the non-existent contact.
332         AccountType account = raw.getRawContactAccountType(this);
333         ValuesDelta values =
334                 RawContactModifier.ensureKindExists(raw, account, Photo.CONTENT_ITEM_TYPE);
335         if (values == null) {
336             Log.w(TAG, "cannot attach photo to this account type");
337             finish();
338             return;
339         }
340         values.setPhoto(compressed);
341 
342         // Finally, invoke the ContactSaveService.
343         if (Log.isLoggable(TAG, Log.VERBOSE)) {
344             Log.v(TAG, "all prerequisites met, about to save photo to contact");
345         }
346         Intent intent = ContactSaveService.createSaveContactIntent(
347                 this,
348                 deltaList,
349                 "", 0,
350                 contact.isUserProfile(),
351                 null, null,
352                 raw.getRawContactId() != null ? raw.getRawContactId() : -1,
353                 mCroppedPhotoUri
354         );
355         ContactSaveService.startService(this, intent);
356         finish();
357     }
358 
selectAccountAndCreateContact()359     private void selectAccountAndCreateContact() {
360         Preconditions.checkNotNull(mAccountsFuture, "Accounts future must be initialized first");
361         // If there is no default account or the accounts have changed such that we need to
362         // prompt the user again, then launch the account prompt.
363         final ContactEditorUtils editorUtils = ContactEditorUtils.create(this);
364 
365         // Technically this could block but in reality this method won't be called until the user
366         // presses the save button which should allow plenty of time for the accounts to
367         // finish loading. Note also that this could be stale if the accounts have changed since
368         // we requested them but that's OK since ContactEditorAccountsChangedActivity will reload
369         // the accounts
370         final List<AccountInfo> accountInfos = Futures.getUnchecked(mAccountsFuture);
371 
372         final List<AccountWithDataSet> accounts = AccountInfo.extractAccounts(accountInfos);
373         if (editorUtils.shouldShowAccountChangedNotification(accounts)) {
374             Intent intent = new Intent(this, ContactEditorAccountsChangedActivity.class)
375                     .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
376             startActivityForResult(intent, REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT);
377         } else {
378             // Otherwise, there should be a default account. Then either create a null contact
379             // (if default account is null) or create a contact with the specified account.
380             final AccountWithDataSet targetAccount = editorUtils.getOnlyOrDefaultAccount(accounts);
381             createNewRawContact(targetAccount);
382         }
383     }
384 
385     /**
386      * Create a new writeable raw contact to store mCroppedPhotoUri.
387      */
createNewRawContact(final AccountWithDataSet account)388     private void createNewRawContact(final AccountWithDataSet account) {
389         // Reload the contact from URI instead of trying to pull the contact from a member variable,
390         // since this function can be called after the activity stops and resumes.
391         loadContact(mContactUri, new Listener() {
392             @Override
393             public void onContactLoaded(Contact contactToSave) {
394                 final RawContactDeltaList deltaList = contactToSave.createRawContactDeltaList();
395                 final ContentValues after = new ContentValues();
396                 after.put(RawContacts.ACCOUNT_TYPE, account != null ? account.type : null);
397                 after.put(RawContacts.ACCOUNT_NAME, account != null ? account.name : null);
398                 after.put(RawContacts.DATA_SET, account != null ? account.dataSet : null);
399 
400                 final RawContactDelta newRawContactDelta
401                         = new RawContactDelta(ValuesDelta.fromAfter(after));
402                 deltaList.add(newRawContactDelta);
403                 saveToContact(contactToSave, deltaList, newRawContactDelta);
404             }
405         });
406     }
407 }
408