blob: 5e0ead4098b02b2c7604362aac5ae26a70ab6111 [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
Neel Parekh2ad90a32009-09-20 19:08:50 -070019import com.google.android.collect.Maps;
20
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080021import android.app.Activity;
Neel Parekh2ad90a32009-09-20 19:08:50 -070022import android.content.ContentProviderOperation;
23import android.content.ContentResolver;
Neel Parekhbe406ff2009-09-16 15:31:22 -070024import android.content.ContentUris;
25import android.content.ContentValues;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080026import android.content.Intent;
Neel Parekh2ad90a32009-09-20 19:08:50 -070027import android.content.OperationApplicationException;
28import android.database.Cursor;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029import android.graphics.Bitmap;
30import android.net.Uri;
31import android.os.Bundle;
Neel Parekh2ad90a32009-09-20 19:08:50 -070032import android.os.RemoteException;
33import android.provider.ContactsContract;
Neel Parekhbe406ff2009-09-16 15:31:22 -070034import android.provider.ContactsContract.Contacts;
35import android.provider.ContactsContract.RawContacts;
36import android.provider.ContactsContract.CommonDataKinds.Photo;
37import android.widget.Toast;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080038
Daniel Lehmann35769b82010-10-18 16:33:29 -070039import com.android.contacts.model.ExchangeAccountType;
40import com.android.contacts.model.GoogleAccountType;
Neel Parekh2ad90a32009-09-20 19:08:50 -070041
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080042import java.io.ByteArrayOutputStream;
Neel Parekh2ad90a32009-09-20 19:08:50 -070043import java.util.ArrayList;
44import java.util.HashMap;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080045
46/**
47 * Provides an external interface for other applications to attach images
48 * to contacts. It will first present a contact picker and then run the
49 * image that is handed to it through the cropper to make the image the proper
50 * size and give the user a chance to use the face detector.
51 */
52public class AttachImage extends Activity {
53 private static final int REQUEST_PICK_CONTACT = 1;
54 private static final int REQUEST_CROP_PHOTO = 2;
55
Neel Parekh2ad90a32009-09-20 19:08:50 -070056 private static final String RAW_CONTACT_URIS_KEY = "raw_contact_uris";
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080057
58 public AttachImage() {
59
60 }
61
Neel Parekhb30b0452009-10-05 14:56:48 -070062 private Long[] mRawContactIds;
Neel Parekh2ad90a32009-09-20 19:08:50 -070063
64 private ContentResolver mContentResolver;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080065
66 @Override
67 public void onCreate(Bundle icicle) {
68 super.onCreate(icicle);
69
70 if (icicle != null) {
Neel Parekhb30b0452009-10-05 14:56:48 -070071 mRawContactIds = toClassArray(icicle.getLongArray(RAW_CONTACT_URIS_KEY));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072 } else {
73 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Neel Parekhbe406ff2009-09-16 15:31:22 -070074 intent.setType(Contacts.CONTENT_ITEM_TYPE);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080075 startActivityForResult(intent, REQUEST_PICK_CONTACT);
76 }
Neel Parekh2ad90a32009-09-20 19:08:50 -070077
78 mContentResolver = getContentResolver();
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080079 }
80
81 @Override
82 protected void onSaveInstanceState(Bundle outState) {
83 super.onSaveInstanceState(outState);
84
Neel Parekhb30b0452009-10-05 14:56:48 -070085 if (mRawContactIds != null && mRawContactIds.length != 0) {
86 outState.putLongArray(RAW_CONTACT_URIS_KEY, toPrimativeArray(mRawContactIds));
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087 }
88 }
89
Neel Parekh2ad90a32009-09-20 19:08:50 -070090 private static long[] toPrimativeArray(Long[] in) {
Neel Parekhb30b0452009-10-05 14:56:48 -070091 if (in == null) {
92 return null;
93 }
Neel Parekh2ad90a32009-09-20 19:08:50 -070094 long[] out = new long[in.length];
95 for (int i = 0; i < in.length; i++) {
96 out[i] = in[i];
97 }
98 return out;
99 }
100
101 private static Long[] toClassArray(long[] in) {
Neel Parekhb30b0452009-10-05 14:56:48 -0700102 if (in == null) {
103 return null;
104 }
Neel Parekh2ad90a32009-09-20 19:08:50 -0700105 Long[] out = new Long[in.length];
106 for (int i = 0; i < in.length; i++) {
107 out[i] = in[i];
108 }
109 return out;
110 }
111
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800112 @Override
113 protected void onActivityResult(int requestCode, int resultCode, Intent result) {
114 if (resultCode != RESULT_OK) {
115 finish();
116 return;
117 }
118
119 if (requestCode == REQUEST_PICK_CONTACT) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800120 // A contact was picked. Launch the cropper to get face detection, the right size, etc.
121 // TODO: get these values from constants somewhere
122 Intent myIntent = getIntent();
123 Intent intent = new Intent("com.android.camera.action.CROP", myIntent.getData());
124 if (myIntent.getStringExtra("mimeType") != null) {
125 intent.setDataAndType(myIntent.getData(), myIntent.getStringExtra("mimeType"));
126 }
127 intent.putExtra("crop", "true");
128 intent.putExtra("aspectX", 1);
129 intent.putExtra("aspectY", 1);
130 intent.putExtra("outputX", 96);
131 intent.putExtra("outputY", 96);
132 intent.putExtra("return-data", true);
133 startActivityForResult(intent, REQUEST_CROP_PHOTO);
Neel Parekhbe406ff2009-09-16 15:31:22 -0700134
135 // while they're cropping, convert the contact into a raw_contact
136 final long contactId = ContentUris.parseId(result.getData());
Neel Parekh2ad90a32009-09-20 19:08:50 -0700137 final ArrayList<Long> rawContactIdsList = ContactsUtils.queryForAllRawContactIds(
138 mContentResolver, contactId);
Neel Parekhb30b0452009-10-05 14:56:48 -0700139 mRawContactIds = new Long[rawContactIdsList.size()];
140 mRawContactIds = rawContactIdsList.toArray(mRawContactIds);
Neel Parekhbe406ff2009-09-16 15:31:22 -0700141
Neel Parekhb30b0452009-10-05 14:56:48 -0700142 if (mRawContactIds == null || rawContactIdsList.isEmpty()) {
Neel Parekhbe406ff2009-09-16 15:31:22 -0700143 Toast.makeText(this, R.string.contactSavedErrorToast, Toast.LENGTH_LONG).show();
144 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800145 } else if (requestCode == REQUEST_CROP_PHOTO) {
146 final Bundle extras = result.getExtras();
Neel Parekhb30b0452009-10-05 14:56:48 -0700147 if (extras != null && mRawContactIds != null) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800148 Bitmap photo = extras.getParcelable("data");
149 if (photo != null) {
150 ByteArrayOutputStream stream = new ByteArrayOutputStream();
151 photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);
Neel Parekhbe406ff2009-09-16 15:31:22 -0700152
153 final ContentValues imageValues = new ContentValues();
Neel Parekhbe406ff2009-09-16 15:31:22 -0700154 imageValues.put(Photo.PHOTO, stream.toByteArray());
155 imageValues.put(RawContacts.Data.IS_SUPER_PRIMARY, 1);
Neel Parekh2ad90a32009-09-20 19:08:50 -0700156
157 // attach the photo to every raw contact
Neel Parekhb30b0452009-10-05 14:56:48 -0700158 for (Long rawContactId : mRawContactIds) {
Neel Parekh2ad90a32009-09-20 19:08:50 -0700159
160 // exchange and google only allow one image, so do an update rather than insert
161 boolean shouldUpdate = false;
162
163 final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI,
164 rawContactId);
165 final Uri rawContactDataUri = Uri.withAppendedPath(rawContactUri,
166 RawContacts.Data.CONTENT_DIRECTORY);
167 insertPhoto(imageValues, rawContactDataUri, true);
168 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800169 }
170 }
171 finish();
172 }
173 }
Neel Parekh2ad90a32009-09-20 19:08:50 -0700174
175 /**
176 * Inserts a photo on the raw contact.
177 * @param values the photo values
Andre Johanssonfee84832010-11-18 22:38:14 +0100178 * @param assertAccount if true, will check to verify that no photos exist for Google,
179 * Exchange and unsynced phone account types. These account types only take one picture,
180 * so if one exists, the account will be updated with the new photo.
Neel Parekh2ad90a32009-09-20 19:08:50 -0700181 */
182 private void insertPhoto(ContentValues values, Uri rawContactDataUri,
183 boolean assertAccount) {
184
185 ArrayList<ContentProviderOperation> operations =
186 new ArrayList<ContentProviderOperation>();
187
188 if (assertAccount) {
Andre Johanssonfee84832010-11-18 22:38:14 +0100189 // Make sure no pictures exist for Google, Exchange and unsynced phone accounts.
Neel Parekh2ad90a32009-09-20 19:08:50 -0700190 operations.add(ContentProviderOperation.newAssertQuery(rawContactDataUri)
Andre Johanssonfee84832010-11-18 22:38:14 +0100191 .withSelection(Photo.MIMETYPE + "=? AND ("
192 + RawContacts.ACCOUNT_TYPE + " IN (?,?) OR "
193 + RawContacts.ACCOUNT_TYPE + " IS NULL)",
Daniel Lehmann35769b82010-10-18 16:33:29 -0700194 new String[] {Photo.CONTENT_ITEM_TYPE, GoogleAccountType.ACCOUNT_TYPE,
195 ExchangeAccountType.ACCOUNT_TYPE})
Neel Parekh2ad90a32009-09-20 19:08:50 -0700196 .withExpectedCount(0).build());
197 }
198
199 // insert the photo
200 values.put(Photo.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
201 operations.add(ContentProviderOperation.newInsert(rawContactDataUri)
202 .withValues(values).build());
203
204 try {
205 mContentResolver.applyBatch(ContactsContract.AUTHORITY, operations);
206 } catch (RemoteException e) {
207 throw new IllegalStateException("Problem querying raw_contacts/data", e);
208 } catch (OperationApplicationException e) {
209 // the account doesn't allow multiple photos, so update
210 if (assertAccount) {
211 updatePhoto(values, rawContactDataUri, false);
212 } else {
213 throw new IllegalStateException("Problem inserting photo into raw_contacts/data", e);
214 }
215 }
216 }
217
218 /**
219 * Tries to update the photo on the raw_contact. If no photo exists, and allowInsert == true,
220 * then will try to {@link #updatePhoto(ContentValues, boolean)}
221 */
222 private void updatePhoto(ContentValues values, Uri rawContactDataUri,
223 boolean allowInsert) {
224 ArrayList<ContentProviderOperation> operations =
225 new ArrayList<ContentProviderOperation>();
226
227 values.remove(Photo.MIMETYPE);
228
229 // check that a photo exists
230 operations.add(ContentProviderOperation.newAssertQuery(rawContactDataUri)
231 .withSelection(Photo.MIMETYPE + "=?", new String[] {
232 Photo.CONTENT_ITEM_TYPE
233 }).withExpectedCount(1).build());
234
235 // update that photo
236 operations.add(ContentProviderOperation.newUpdate(rawContactDataUri).withSelection(Photo.MIMETYPE + "=?", new String[] {
237 Photo.CONTENT_ITEM_TYPE}).withValues(values).build());
238
239 try {
240 mContentResolver.applyBatch(ContactsContract.AUTHORITY, operations);
241 } catch (RemoteException e) {
242 throw new IllegalStateException("Problem querying raw_contacts/data", e);
243 } catch (OperationApplicationException e) {
244 if (allowInsert) {
245 // they deleted the photo between insert and update, so insert one
246 insertPhoto(values, rawContactDataUri, false);
247 } else {
248 throw new IllegalStateException("Problem inserting photo raw_contacts/data", e);
249 }
250 }
251 }
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800252}