blob: fd820e2d7752d435f0f723f2431779d439880545 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
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
17package com.android.contacts;
18
19import android.app.Activity;
Neel Parekhbe406ff2009-09-16 15:31:22 -070020import android.content.ContentUris;
21import android.content.ContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080022import android.content.Intent;
23import android.graphics.Bitmap;
24import android.net.Uri;
25import android.os.Bundle;
Neel Parekhbe406ff2009-09-16 15:31:22 -070026import android.provider.ContactsContract.Contacts;
27import android.provider.ContactsContract.RawContacts;
28import android.provider.ContactsContract.CommonDataKinds.Photo;
29import android.widget.Toast;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080030
31import java.io.ByteArrayOutputStream;
32
33/**
34 * Provides an external interface for other applications to attach images
35 * to contacts. It will first present a contact picker and then run the
36 * image that is handed to it through the cropper to make the image the proper
37 * size and give the user a chance to use the face detector.
38 */
39public class AttachImage extends Activity {
40 private static final int REQUEST_PICK_CONTACT = 1;
41 private static final int REQUEST_CROP_PHOTO = 2;
42
43 private static final String CONTACT_URI_KEY = "contact_uri";
44
45 public AttachImage() {
46
47 }
48
Neel Parekhbe406ff2009-09-16 15:31:22 -070049 /**
50 * Is the raw_contact uri for the contact the user selected
51 */
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080052 Uri mContactUri;
53
54 @Override
55 public void onCreate(Bundle icicle) {
56 super.onCreate(icicle);
57
58 if (icicle != null) {
59 mContactUri = icicle.getParcelable(CONTACT_URI_KEY);
60 } else {
61 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Neel Parekhbe406ff2009-09-16 15:31:22 -070062 intent.setType(Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080063 startActivityForResult(intent, REQUEST_PICK_CONTACT);
64 }
65 }
66
67 @Override
68 protected void onSaveInstanceState(Bundle outState) {
69 super.onSaveInstanceState(outState);
70
71 if (mContactUri != null) {
72 outState.putParcelable(CONTACT_URI_KEY, mContactUri);
73 }
74 }
75
76 @Override
77 protected void onActivityResult(int requestCode, int resultCode, Intent result) {
78 if (resultCode != RESULT_OK) {
79 finish();
80 return;
81 }
82
83 if (requestCode == REQUEST_PICK_CONTACT) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080084 // A contact was picked. Launch the cropper to get face detection, the right size, etc.
85 // TODO: get these values from constants somewhere
86 Intent myIntent = getIntent();
87 Intent intent = new Intent("com.android.camera.action.CROP", myIntent.getData());
88 if (myIntent.getStringExtra("mimeType") != null) {
89 intent.setDataAndType(myIntent.getData(), myIntent.getStringExtra("mimeType"));
90 }
91 intent.putExtra("crop", "true");
92 intent.putExtra("aspectX", 1);
93 intent.putExtra("aspectY", 1);
94 intent.putExtra("outputX", 96);
95 intent.putExtra("outputY", 96);
96 intent.putExtra("return-data", true);
97 startActivityForResult(intent, REQUEST_CROP_PHOTO);
Neel Parekhbe406ff2009-09-16 15:31:22 -070098
99 // while they're cropping, convert the contact into a raw_contact
100 final long contactId = ContentUris.parseId(result.getData());
101 final long rawContactId = ContactsUtils.queryForRawContactId(getContentResolver(),
102 contactId);
103
104 if (rawContactId == -1) {
105 Toast.makeText(this, R.string.contactSavedErrorToast, Toast.LENGTH_LONG).show();
106 }
107
108 mContactUri = Uri.withAppendedPath(
109 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
110 RawContacts.Data.CONTENT_DIRECTORY);
111
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112 } else if (requestCode == REQUEST_CROP_PHOTO) {
113 final Bundle extras = result.getExtras();
114 if (extras != null) {
115 Bitmap photo = extras.getParcelable("data");
116 if (photo != null) {
117 ByteArrayOutputStream stream = new ByteArrayOutputStream();
118 photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
Neel Parekhbe406ff2009-09-16 15:31:22 -0700119
120 final ContentValues imageValues = new ContentValues();
121 imageValues.put(Photo.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
122 imageValues.put(Photo.PHOTO, stream.toByteArray());
123 imageValues.put(RawContacts.Data.IS_SUPER_PRIMARY, 1);
124 getContentResolver().insert(mContactUri, imageValues);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800125 }
126 }
127 finish();
128 }
129 }
130}