• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             if (!hasIntentHandler(intent)) {
201                 // No activity supports the crop action. So skip cropping and set the photo
202                 // without performing any cropping.
203                 mCroppedPhotoUri = mTempPhotoUri;
204                 mContactUri = result.getData();
205                 loadContact(mContactUri, new Listener() {
206                     @Override
207                     public void onContactLoaded(Contact contact) {
208                         saveContact(contact);
209                     }
210                 });
211                 return;
212             }
213 
214             try {
215                 startActivityForResult(intent, REQUEST_CROP_PHOTO);
216             } catch (ActivityNotFoundException ex) {
217                 Toast.makeText(this, R.string.missing_app, Toast.LENGTH_SHORT).show();
218                 return;
219             }
220 
221             mContactUri = result.getData();
222 
223         } else if (requestCode == REQUEST_CROP_PHOTO) {
224             // Delete the temporary photo from cache now that we have a cropped version.
225             // We should do this even if the crop failed and we eventually bail
226             getContentResolver().delete(mTempPhotoUri, null, null);
227             if (resultCode != RESULT_OK) {
228                 finish();
229                 return;
230             }
231             loadContact(mContactUri, new Listener() {
232                 @Override
233                 public void onContactLoaded(Contact contact) {
234                     saveContact(contact);
235                 }
236             });
237         }
238     }
239 
hasIntentHandler(Intent intent)240     private boolean hasIntentHandler(Intent intent) {
241         final List<ResolveInfo> resolveInfo = getPackageManager()
242                 .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
243         return resolveInfo != null && resolveInfo.size() > 0;
244     }
245 
246     // TODO: consider moving this to ContactLoader, especially if we keep adding similar
247     // code elsewhere (ViewNotificationService is another case).  The only concern is that,
248     // although this is convenient, it isn't quite as robust as using LoaderManager... for
249     // instance, the loader doesn't persist across Activity restarts.
loadContact(Uri contactUri, final Listener listener)250     private void loadContact(Uri contactUri, final Listener listener) {
251         final ContactLoader loader = new ContactLoader(this, contactUri, true);
252         loader.registerListener(0, new OnLoadCompleteListener<Contact>() {
253             @Override
254             public void onLoadComplete(
255                     Loader<Contact> loader, Contact contact) {
256                 try {
257                     loader.reset();
258                 }
259                 catch (RuntimeException e) {
260                     Log.e(TAG, "Error resetting loader", e);
261                 }
262                 listener.onContactLoaded(contact);
263             }
264         });
265         loader.startLoading();
266     }
267 
268     private interface Listener {
onContactLoaded(Contact contact)269         public void onContactLoaded(Contact contact);
270     }
271 
272     /**
273      * If prerequisites have been met, attach the photo to a raw-contact and save.
274      * The prerequisites are:
275      * - photo has been cropped
276      * - contact has been loaded
277      */
saveContact(Contact contact)278     private void saveContact(Contact contact) {
279 
280         if (contact.getRawContacts() == null) {
281             Log.w(TAG, "No raw contacts found for contact");
282             finish();
283             return;
284         }
285 
286         // Obtain the raw-contact that we will save to.
287         RawContactDeltaList deltaList = contact.createRawContactDeltaList();
288         RawContactDelta raw = deltaList.getFirstWritableRawContact(this);
289         if (raw == null) {
290             // We can't directly insert this photo since no raw contacts exist in the contact.
291             selectAccountAndCreateContact();
292             return;
293         }
294 
295         saveToContact(contact, deltaList, raw);
296     }
297 
saveToContact(Contact contact, RawContactDeltaList deltaList, RawContactDelta raw)298     private void saveToContact(Contact contact, RawContactDeltaList deltaList,
299             RawContactDelta raw) {
300 
301         // Create a scaled, compressed bitmap to add to the entity-delta list.
302         final int size = ContactsUtils.getThumbnailSize(this);
303         Bitmap bitmap;
304         try {
305             bitmap = ContactPhotoUtils.getBitmapFromUri(this, mCroppedPhotoUri);
306         } catch (FileNotFoundException e) {
307             Log.w(TAG, "Could not find bitmap");
308             finish();
309             return;
310         }
311         if (bitmap == null) {
312             Log.w(TAG, "Could not decode bitmap");
313             finish();
314             return;
315         }
316 
317         final Bitmap scaled = Bitmap.createScaledBitmap(bitmap, size, size, false);
318         final byte[] compressed = ContactPhotoUtils.compressBitmap(scaled);
319         if (compressed == null) {
320             Log.w(TAG, "could not create scaled and compressed Bitmap");
321             finish();
322             return;
323         }
324 
325         // Add compressed bitmap to entity-delta... this allows us to save to
326         // a new contact; otherwise the entity-delta-list would be empty, and
327         // the ContactSaveService would not create the new contact, and the
328         // full-res photo would fail to be saved to the non-existent contact.
329         AccountType account = raw.getRawContactAccountType(this);
330         ValuesDelta values =
331                 RawContactModifier.ensureKindExists(raw, account, Photo.CONTENT_ITEM_TYPE);
332         if (values == null) {
333             Log.w(TAG, "cannot attach photo to this account type");
334             finish();
335             return;
336         }
337         values.setPhoto(compressed);
338 
339         // Finally, invoke the ContactSaveService.
340         if (Log.isLoggable(TAG, Log.VERBOSE)) {
341             Log.v(TAG, "all prerequisites met, about to save photo to contact");
342         }
343         Intent intent = ContactSaveService.createSaveContactIntent(
344                 this,
345                 deltaList,
346                 "", 0,
347                 contact.isUserProfile(),
348                 null, null,
349                 raw.getRawContactId() != null ? raw.getRawContactId() : -1,
350                 mCroppedPhotoUri
351         );
352         ContactSaveService.startService(this, intent);
353         finish();
354     }
355 
selectAccountAndCreateContact()356     private void selectAccountAndCreateContact() {
357         Preconditions.checkNotNull(mAccountsFuture, "Accounts future must be initialized first");
358         // If there is no default account or the accounts have changed such that we need to
359         // prompt the user again, then launch the account prompt.
360         final ContactEditorUtils editorUtils = ContactEditorUtils.create(this);
361 
362         // Technically this could block but in reality this method won't be called until the user
363         // presses the save button which should allow plenty of time for the accounts to
364         // finish loading. Note also that this could be stale if the accounts have changed since
365         // we requested them but that's OK since ContactEditorAccountsChangedActivity will reload
366         // the accounts
367         final List<AccountInfo> accountInfos = Futures.getUnchecked(mAccountsFuture);
368 
369         final List<AccountWithDataSet> accounts = AccountInfo.extractAccounts(accountInfos);
370         if (editorUtils.shouldShowAccountChangedNotification(accounts)) {
371             Intent intent = new Intent(this, ContactEditorAccountsChangedActivity.class)
372                     .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
373             startActivityForResult(intent, REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT);
374         } else {
375             // Otherwise, there should be a default account. Then either create a null contact
376             // (if default account is null) or create a contact with the specified account.
377             final AccountWithDataSet targetAccount = editorUtils.getOnlyOrDefaultAccount(accounts);
378             createNewRawContact(targetAccount);
379         }
380     }
381 
382     /**
383      * Create a new writeable raw contact to store mCroppedPhotoUri.
384      */
createNewRawContact(final AccountWithDataSet account)385     private void createNewRawContact(final AccountWithDataSet account) {
386         // Reload the contact from URI instead of trying to pull the contact from a member variable,
387         // since this function can be called after the activity stops and resumes.
388         loadContact(mContactUri, new Listener() {
389             @Override
390             public void onContactLoaded(Contact contactToSave) {
391                 final RawContactDeltaList deltaList = contactToSave.createRawContactDeltaList();
392                 final ContentValues after = new ContentValues();
393                 after.put(RawContacts.ACCOUNT_TYPE, account != null ? account.type : null);
394                 after.put(RawContacts.ACCOUNT_NAME, account != null ? account.name : null);
395                 after.put(RawContacts.DATA_SET, account != null ? account.dataSet : null);
396 
397                 final RawContactDelta newRawContactDelta
398                         = new RawContactDelta(ValuesDelta.fromAfter(after));
399                 deltaList.add(newRawContactDelta);
400                 saveToContact(contactToSave, deltaList, newRawContactDelta);
401             }
402         });
403     }
404 }
405