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 | |
| 17 | package com.android.contacts; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.Intent; |
| 21 | import android.graphics.Bitmap; |
| 22 | import android.net.Uri; |
| 23 | import android.os.Bundle; |
| 24 | import android.provider.Contacts; |
| 25 | import android.provider.Contacts.People; |
| 26 | |
| 27 | import java.io.ByteArrayOutputStream; |
| 28 | |
| 29 | /** |
| 30 | * Provides an external interface for other applications to attach images |
| 31 | * to contacts. It will first present a contact picker and then run the |
| 32 | * image that is handed to it through the cropper to make the image the proper |
| 33 | * size and give the user a chance to use the face detector. |
| 34 | */ |
| 35 | public class AttachImage extends Activity { |
| 36 | private static final int REQUEST_PICK_CONTACT = 1; |
| 37 | private static final int REQUEST_CROP_PHOTO = 2; |
| 38 | |
| 39 | private static final String CONTACT_URI_KEY = "contact_uri"; |
| 40 | |
| 41 | public AttachImage() { |
| 42 | |
| 43 | } |
| 44 | |
| 45 | Uri mContactUri; |
| 46 | |
| 47 | @Override |
| 48 | public void onCreate(Bundle icicle) { |
| 49 | super.onCreate(icicle); |
| 50 | |
| 51 | if (icicle != null) { |
| 52 | mContactUri = icicle.getParcelable(CONTACT_URI_KEY); |
| 53 | } else { |
| 54 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 55 | intent.setType(People.CONTENT_ITEM_TYPE); |
| 56 | startActivityForResult(intent, REQUEST_PICK_CONTACT); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void onSaveInstanceState(Bundle outState) { |
| 62 | super.onSaveInstanceState(outState); |
| 63 | |
| 64 | if (mContactUri != null) { |
| 65 | outState.putParcelable(CONTACT_URI_KEY, mContactUri); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void onActivityResult(int requestCode, int resultCode, Intent result) { |
| 71 | if (resultCode != RESULT_OK) { |
| 72 | finish(); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (requestCode == REQUEST_PICK_CONTACT) { |
| 77 | mContactUri = result.getData(); |
| 78 | // A contact was picked. Launch the cropper to get face detection, the right size, etc. |
| 79 | // TODO: get these values from constants somewhere |
| 80 | Intent myIntent = getIntent(); |
| 81 | Intent intent = new Intent("com.android.camera.action.CROP", myIntent.getData()); |
| 82 | if (myIntent.getStringExtra("mimeType") != null) { |
| 83 | intent.setDataAndType(myIntent.getData(), myIntent.getStringExtra("mimeType")); |
| 84 | } |
| 85 | intent.putExtra("crop", "true"); |
| 86 | intent.putExtra("aspectX", 1); |
| 87 | intent.putExtra("aspectY", 1); |
| 88 | intent.putExtra("outputX", 96); |
| 89 | intent.putExtra("outputY", 96); |
| 90 | intent.putExtra("return-data", true); |
| 91 | startActivityForResult(intent, REQUEST_CROP_PHOTO); |
| 92 | } else if (requestCode == REQUEST_CROP_PHOTO) { |
| 93 | final Bundle extras = result.getExtras(); |
| 94 | if (extras != null) { |
| 95 | Bitmap photo = extras.getParcelable("data"); |
| 96 | if (photo != null) { |
| 97 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 98 | photo.compress(Bitmap.CompressFormat.JPEG, 75, stream); |
| 99 | Contacts.People.setPhotoData(getContentResolver(), mContactUri, |
| 100 | stream.toByteArray()); |
| 101 | } |
| 102 | } |
| 103 | finish(); |
| 104 | } |
| 105 | } |
| 106 | } |