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.R;
43 import com.android.contacts.common.activity.RequestPermissionsActivity;
44 import com.android.contacts.common.model.Contact;
45 import com.android.contacts.common.model.ContactLoader;
46 import com.android.contacts.common.model.RawContactDelta;
47 import com.android.contacts.common.model.RawContactDeltaList;
48 import com.android.contacts.common.model.RawContactModifier;
49 import com.android.contacts.common.ContactsUtils;
50 import com.android.contacts.common.model.account.AccountType;
51 import com.android.contacts.common.model.ValuesDelta;
52 import com.android.contacts.common.model.account.AccountWithDataSet;
53 import com.android.contacts.editor.ContactEditorUtils;
54 import com.android.contacts.util.ContactPhotoUtils;
55 
56 import java.io.FileNotFoundException;
57 import java.util.List;
58 
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  */
65 public class AttachPhotoActivity extends ContactsActivity {
66     private static final String TAG = AttachPhotoActivity.class.getSimpleName();
67 
68     private static final int REQUEST_PICK_CONTACT = 1;
69     private static final int REQUEST_CROP_PHOTO = 2;
70     private static final int REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT = 3;
71 
72     private static final String KEY_CONTACT_URI = "contact_uri";
73     private static final String KEY_TEMP_PHOTO_URI = "temp_photo_uri";
74     private static final String KEY_CROPPED_PHOTO_URI = "cropped_photo_uri";
75 
76     private Uri mTempPhotoUri;
77     private Uri mCroppedPhotoUri;
78 
79     private ContentResolver mContentResolver;
80 
81     // Height and width (in pixels) to request for the photo - queried from the provider.
82     private static int mPhotoDim;
83     // Default photo dimension to use if unable to query the provider.
84     private static final int mDefaultPhotoDim = 720;
85 
86     private Uri mContactUri;
87 
88     @Override
onCreate(Bundle icicle)89     public void onCreate(Bundle icicle) {
90         super.onCreate(icicle);
91 
92         if (RequestPermissionsActivity.startPermissionActivity(this)) {
93             return;
94         }
95 
96         if (icicle != null) {
97             final String uri = icicle.getString(KEY_CONTACT_URI);
98             mContactUri = (uri == null) ? null : Uri.parse(uri);
99             mTempPhotoUri = Uri.parse(icicle.getString(KEY_TEMP_PHOTO_URI));
100             mCroppedPhotoUri = Uri.parse(icicle.getString(KEY_CROPPED_PHOTO_URI));
101         } else {
102             mTempPhotoUri = ContactPhotoUtils.generateTempImageUri(this);
103             mCroppedPhotoUri = ContactPhotoUtils.generateTempCroppedImageUri(this);
104             Intent intent = new Intent(Intent.ACTION_PICK);
105             intent.setType(Contacts.CONTENT_TYPE);
106             intent.setPackage(getPackageName());
107             startActivityForResult(intent, REQUEST_PICK_CONTACT);
108         }
109 
110         mContentResolver = getContentResolver();
111 
112         // 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 {
120                     if (c.moveToFirst()) {
121                         mPhotoDim = c.getInt(0);
122                     }
123                 } finally {
124                     c.close();
125                 }
126             }
127         }
128     }
129 
130     @Override
onSaveInstanceState(Bundle outState)131     protected void onSaveInstanceState(Bundle outState) {
132         super.onSaveInstanceState(outState);
133         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         }
139         if (mCroppedPhotoUri != null) {
140             outState.putString(KEY_CROPPED_PHOTO_URI, mCroppedPhotoUri.toString());
141         }
142     }
143 
144     @Override
onActivityResult(int requestCode, int resultCode, Intent result)145     protected void onActivityResult(int requestCode, int resultCode, Intent result) {
146         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) {
155                 AccountWithDataSet account = result.getParcelableExtra(
156                         Intents.Insert.EXTRA_ACCOUNT);
157                 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) {
165             if (resultCode != RESULT_OK) {
166                 finish();
167                 return;
168             }
169             // A contact was picked. Launch the cropper to get face detection, the right size, etc.
170             // TODO: get these values from constants somewhere
171             final Intent myIntent = getIntent();
172             final Uri inputUri = myIntent.getData();
173 
174 
175             // 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.
179             if (!ContactPhotoUtils.savePhotoFromUriToUri(this, inputUri, mTempPhotoUri, false)) {
180                 finish();
181                 return;
182             }
183 
184             final Intent intent = new Intent("com.android.camera.action.CROP", mTempPhotoUri);
185             if (myIntent.getStringExtra("mimeType") != null) {
186                 intent.setDataAndType(mTempPhotoUri, myIntent.getStringExtra("mimeType"));
187             }
188             ContactPhotoUtils.addPhotoPickerExtras(intent, mCroppedPhotoUri);
189             ContactPhotoUtils.addCropExtras(intent, mPhotoDim != 0 ? mPhotoDim : mDefaultPhotoDim);
190             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             }
203 
204             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             }
210 
211             mContactUri = result.getData();
212 
213         } else if (requestCode == REQUEST_CROP_PHOTO) {
214             // 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             }
221             loadContact(mContactUri, new Listener() {
222                 @Override
223                 public void onContactLoaded(Contact contact) {
224                     saveContact(contact);
225                 }
226             });
227         }
228     }
229 
hasIntentHandler(Intent intent)230     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 
236     // 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.
loadContact(Uri contactUri, final Listener listener)240     private void loadContact(Uri contactUri, final Listener listener) {
241         final ContactLoader loader = new ContactLoader(this, contactUri, true);
242         loader.registerListener(0, new OnLoadCompleteListener<Contact>() {
243             @Override
244             public void onLoadComplete(
245                     Loader<Contact> loader, Contact contact) {
246                 try {
247                     loader.reset();
248                 }
249                 catch (RuntimeException e) {
250                     Log.e(TAG, "Error resetting loader", e);
251                 }
252                 listener.onContactLoaded(contact);
253             }
254         });
255         loader.startLoading();
256     }
257 
258     private interface Listener {
onContactLoaded(Contact contact)259         public void onContactLoaded(Contact contact);
260     }
261 
262     /**
263      * 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
267      */
saveContact(Contact contact)268     private void saveContact(Contact contact) {
269 
270         if (contact.getRawContacts() == null) {
271             Log.w(TAG, "No raw contacts found for contact");
272             finish();
273             return;
274         }
275 
276         // Obtain the raw-contact that we will save to.
277         RawContactDeltaList deltaList = contact.createRawContactDeltaList();
278         RawContactDelta raw = deltaList.getFirstWritableRawContact(this);
279         if (raw == null) {
280             // We can't directly insert this photo since no raw contacts exist in the contact.
281             selectAccountAndCreateContact();
282             return;
283         }
284 
285         saveToContact(contact, deltaList, raw);
286     }
287 
saveToContact(Contact contact, RawContactDeltaList deltaList, RawContactDelta raw)288     private void saveToContact(Contact contact, RawContactDeltaList deltaList,
289             RawContactDelta raw) {
290 
291         // Create a scaled, compressed bitmap to add to the entity-delta list.
292         final int size = ContactsUtils.getThumbnailSize(this);
293         Bitmap bitmap;
294         try {
295             bitmap = ContactPhotoUtils.getBitmapFromUri(this, mCroppedPhotoUri);
296         } catch (FileNotFoundException e) {
297             Log.w(TAG, "Could not find bitmap");
298             finish();
299             return;
300         }
301         if (bitmap == null) {
302             Log.w(TAG, "Could not decode bitmap");
303             finish();
304             return;
305         }
306 
307         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");
311             finish();
312             return;
313         }
314 
315         // 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);
320         ValuesDelta values =
321                 RawContactModifier.ensureKindExists(raw, account, Photo.CONTENT_ITEM_TYPE);
322         if (values == null) {
323             Log.w(TAG, "cannot attach photo to this account type");
324             finish();
325             return;
326         }
327         values.setPhoto(compressed);
328 
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,
337                 raw.getRawContactId() != null ? raw.getRawContactId() : -1,
338                 mCroppedPhotoUri
339         );
340         ContactSaveService.startService(this, intent);
341         finish();
342     }
343 
selectAccountAndCreateContact()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.
347         final ContactEditorUtils editorUtils = ContactEditorUtils.getInstance(this);
348         if (editorUtils.shouldShowAccountChangedNotification()) {
349             Intent intent = new Intent(this, ContactEditorAccountsChangedActivity.class);
350             startActivityForResult(intent, REQUEST_PICK_DEFAULT_ACCOUNT_FOR_NEW_CONTACT);
351         } else {
352             // Otherwise, there should be a default account. Then either create a local contact
353             // (if default account is null) or create a contact with the specified account.
354             AccountWithDataSet defaultAccount = editorUtils.getDefaultAccount();
355             createNewRawContact(defaultAccount);
356         }
357     }
358 
359     /**
360      * Create a new writeable raw contact to store mCroppedPhotoUri.
361      */
createNewRawContact(final AccountWithDataSet account)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     }
381 }
382