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
2879a6b5d3a885bc508b4b98c92a946fe12643c444Chiao Chengimport com.android.contacts.common.ContactPhotoManager;
29187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargusimport com.android.contacts.activities.PhotoSelectionActivity;
30cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.model.Contact;
31cd321f65f1e50409812976380ad1f0fdb3fa35cbYorke Leeimport com.android.contacts.common.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) {
41f677450d82e7b961fda2a18fea2ad51e88437e8bYorke Lee        Bitmap bitmap = setupContactPhoto(contactData, photoView);
42187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        return setupClickListener(context, contactData, bitmap, expandPhotoOnClick);
43187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    }
44187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
45187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    private static final class PhotoClickListener implements OnClickListener {
46187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
47187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        private final Context mContext;
48851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        private final Contact mContactData;
49187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        private final Bitmap mPhotoBitmap;
50187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        private final byte[] mPhotoBytes;
51187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        private final boolean mExpandPhotoOnClick;
52187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
53851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu        public PhotoClickListener(Context context, Contact contactData, Bitmap photoBitmap,
54187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                byte[] photoBytes, boolean expandPhotoOnClick) {
55187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mContext = context;
56187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mContactData = contactData;
57187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mPhotoBitmap = photoBitmap;
58187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mPhotoBytes = photoBytes;
59187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mExpandPhotoOnClick = expandPhotoOnClick;
60187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        }
61187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
62187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        @Override
63187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        public void onClick(View v) {
64187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // Assemble the intent.
65851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu            RawContactDeltaList delta = mContactData.createRawContactDeltaList();
66187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
67187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // Find location and bounds of target view, adjusting based on the
68187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // assumed local density.
69187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            final float appScale =
70187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                    mContext.getResources().getCompatibilityInfo().applicationScale;
71187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            final int[] pos = new int[2];
72187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            v.getLocationOnScreen(pos);
73187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
742d4632c2eb7d2dc3bb94bf64c14c471aafa67b0fMaurice Chu            // rect is the bounds (in pixels) of the photo view in screen coordinates
75187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            final Rect rect = new Rect();
76187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            rect.left = (int) (pos[0] * appScale + 0.5f);
77187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            rect.top = (int) (pos[1] * appScale + 0.5f);
78187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            rect.right = (int) ((pos[0] + v.getWidth()) * appScale + 0.5f);
79187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            rect.bottom = (int) ((pos[1] + v.getHeight()) * appScale + 0.5f);
80187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
81187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            Uri photoUri = null;
82187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            if (mContactData.getPhotoUri() != null) {
83187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                photoUri = Uri.parse(mContactData.getPhotoUri());
84187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            }
85187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            Intent photoSelectionIntent = PhotoSelectionActivity.buildIntent(mContext,
86187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                    photoUri, mPhotoBitmap, mPhotoBytes, rect, delta, mContactData.isUserProfile(),
87187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                    mContactData.isDirectoryEntry(), mExpandPhotoOnClick);
88187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // Cache the bitmap directly, so the activity can pull it from the
89187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            // photo manager.
90187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            if (mPhotoBitmap != null) {
91187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                ContactPhotoManager.getInstance(mContext).cacheBitmap(
92187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                        photoUri, mPhotoBitmap, mPhotoBytes);
93187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            }
94187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            mContext.startActivity(photoSelectionIntent);
95187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        }
96187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    }
97187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
98851222a96b5d68602fb361ea3527101e893f67e3Maurice Chu    private OnClickListener setupClickListener(Context context, Contact contactData, Bitmap bitmap,
99187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus            boolean expandPhotoOnClick) {
100187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        final ImageView target = getTarget();
101187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus        if (target == null) return null;
102187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus
10384edfd9a76657a653491faac53b5976adf9fd2cbJosh Gargus        return new PhotoClickListener(
104187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus                context, contactData, bitmap, getCompressedImage(), expandPhotoOnClick);
105187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus    }
106187c8167d77687fbc04237c9ac1e87564b2d5f33Josh Gargus}
107