1187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus/*
2187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * Copyright (C) 2012 The Android Open Source Project
3187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus *
4187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * Licensed under the Apache License, Version 2.0 (the "License");
5187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * you may not use this file except in compliance with the License.
6187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * You may obtain a copy of the License at
7187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus *
8187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus *      http://www.apache.org/licenses/LICENSE-2.0
9187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus *
10187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * Unless required by applicable law or agreed to in writing, software
11187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * distributed under the License is distributed on an "AS IS" BASIS,
12187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * See the License for the specific language governing permissions and
14187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * limitations under the License.
15187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus */
16187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
17187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Garguspackage com.android.contacts.detail;
18187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
19187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport android.content.Context;
20187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport android.content.Intent;
21187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport android.graphics.Bitmap;
22187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport android.graphics.Rect;
23187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport android.net.Uri;
24187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport android.view.View;
25187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport android.view.View.OnClickListener;
26187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport android.widget.ImageView;
27187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
28e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.ContactPhotoManager;
29187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport com.android.contacts.activities.PhotoSelectionActivity;
30851222a96b5d68602fb361ea3527101e893f67e3Maurice Chuimport com.android.contacts.model.Contact;
31851222a96b5d68602fb361ea3527101e893f67e3Maurice Chuimport com.android.contacts.model.RawContactDeltaList;
32187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport com.android.contacts.util.ImageViewDrawableSetter;
33187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
34187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus/**
35187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * Extends superclass with methods specifically for setting the contact-detail
36187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus * photo.
37187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus */
38187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Garguspublic class ContactDetailPhotoSetter extends ImageViewDrawableSetter {
39851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu    public OnClickListener setupContactPhotoForClick(Context context, Contact contactData,
40187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            ImageView photoView, boolean expandPhotoOnClick) {
41187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        setTarget(photoView);
42187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        Bitmap bitmap = setCompressedImage(contactData.getPhotoBinaryData());
43187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        return setupClickListener(context, contactData, bitmap, expandPhotoOnClick);
44187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    }
45187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
46187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    private static final class PhotoClickListener implements OnClickListener {
47187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
48187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        private final Context mContext;
49851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        private final Contact mContactData;
50187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        private final Bitmap mPhotoBitmap;
51187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        private final byte[] mPhotoBytes;
52187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        private final boolean mExpandPhotoOnClick;
53187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
54851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public PhotoClickListener(Context context, Contact contactData, Bitmap photoBitmap,
55187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                byte[] photoBytes, boolean expandPhotoOnClick) {
56187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mContext = context;
57187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mContactData = contactData;
58187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mPhotoBitmap = photoBitmap;
59187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mPhotoBytes = photoBytes;
60187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mExpandPhotoOnClick = expandPhotoOnClick;
61187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        }
62187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
63187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        @Override
64187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        public void onClick(View v) {
65187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // Assemble the intent.
66851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            RawContactDeltaList delta = mContactData.createRawContactDeltaList();
67187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
68187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // Find location and bounds of target view, adjusting based on the
69187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // assumed local density.
70187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            final float appScale =
71187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                    mContext.getResources().getCompatibilityInfo().applicationScale;
72187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            final int[] pos = new int[2];
73187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            v.getLocationOnScreen(pos);
74187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
752d4632c2eb7d2dc3bb94bf64c14c471aafa67b0fMaurice Chu            // rect is the bounds (in pixels) of the photo view in screen coordinates
76187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            final Rect rect = new Rect();
77187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            rect.left = (int) (pos[0] * appScale + 0.5f);
78187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            rect.top = (int) (pos[1] * appScale + 0.5f);
79187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            rect.right = (int) ((pos[0] + v.getWidth()) * appScale + 0.5f);
80187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            rect.bottom = (int) ((pos[1] + v.getHeight()) * appScale + 0.5f);
81187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
82187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            Uri photoUri = null;
83187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            if (mContactData.getPhotoUri() != null) {
84187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                photoUri = Uri.parse(mContactData.getPhotoUri());
85187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            }
86187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            Intent photoSelectionIntent = PhotoSelectionActivity.buildIntent(mContext,
87187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                    photoUri, mPhotoBitmap, mPhotoBytes, rect, delta, mContactData.isUserProfile(),
88187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                    mContactData.isDirectoryEntry(), mExpandPhotoOnClick);
89187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // Cache the bitmap directly, so the activity can pull it from the
90187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // photo manager.
91187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            if (mPhotoBitmap != null) {
92187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                ContactPhotoManager.getInstance(mContext).cacheBitmap(
93187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                        photoUri, mPhotoBitmap, mPhotoBytes);
94187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            }
95187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mContext.startActivity(photoSelectionIntent);
96187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        }
97187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    }
98187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
99851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu    private OnClickListener setupClickListener(Context context, Contact contactData, Bitmap bitmap,
100187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            boolean expandPhotoOnClick) {
101187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        final ImageView target = getTarget();
102187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        if (target == null) return null;
103187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
10484edfd9a76657a653491faac53b5976adf9fd2cbJosh Gargus        return new PhotoClickListener(
105187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                context, contactData, bitmap, getCompressedImage(), expandPhotoOnClick);
106187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    }
107187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus}
108