1/*
2 * Copyright (C) 2012 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.detail;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.Bitmap;
22import android.graphics.Rect;
23import android.net.Uri;
24import android.view.View;
25import android.view.View.OnClickListener;
26import android.widget.ImageView;
27
28import com.android.contacts.ContactPhotoManager;
29import com.android.contacts.activities.PhotoSelectionActivity;
30import com.android.contacts.model.Contact;
31import com.android.contacts.model.RawContactDeltaList;
32import com.android.contacts.util.ImageViewDrawableSetter;
33
34/**
35 * Extends superclass with methods specifically for setting the contact-detail
36 * photo.
37 */
38public class ContactDetailPhotoSetter extends ImageViewDrawableSetter {
39    public OnClickListener setupContactPhotoForClick(Context context, Contact contactData,
40            ImageView photoView, boolean expandPhotoOnClick) {
41        setTarget(photoView);
42        Bitmap bitmap = setCompressedImage(contactData.getPhotoBinaryData());
43        return setupClickListener(context, contactData, bitmap, expandPhotoOnClick);
44    }
45
46    private static final class PhotoClickListener implements OnClickListener {
47
48        private final Context mContext;
49        private final Contact mContactData;
50        private final Bitmap mPhotoBitmap;
51        private final byte[] mPhotoBytes;
52        private final boolean mExpandPhotoOnClick;
53
54        public PhotoClickListener(Context context, Contact contactData, Bitmap photoBitmap,
55                byte[] photoBytes, boolean expandPhotoOnClick) {
56            mContext = context;
57            mContactData = contactData;
58            mPhotoBitmap = photoBitmap;
59            mPhotoBytes = photoBytes;
60            mExpandPhotoOnClick = expandPhotoOnClick;
61        }
62
63        @Override
64        public void onClick(View v) {
65            // Assemble the intent.
66            RawContactDeltaList delta = mContactData.createRawContactDeltaList();
67
68            // Find location and bounds of target view, adjusting based on the
69            // assumed local density.
70            final float appScale =
71                    mContext.getResources().getCompatibilityInfo().applicationScale;
72            final int[] pos = new int[2];
73            v.getLocationOnScreen(pos);
74
75            // rect is the bounds (in pixels) of the photo view in screen coordinates
76            final Rect rect = new Rect();
77            rect.left = (int) (pos[0] * appScale + 0.5f);
78            rect.top = (int) (pos[1] * appScale + 0.5f);
79            rect.right = (int) ((pos[0] + v.getWidth()) * appScale + 0.5f);
80            rect.bottom = (int) ((pos[1] + v.getHeight()) * appScale + 0.5f);
81
82            Uri photoUri = null;
83            if (mContactData.getPhotoUri() != null) {
84                photoUri = Uri.parse(mContactData.getPhotoUri());
85            }
86            Intent photoSelectionIntent = PhotoSelectionActivity.buildIntent(mContext,
87                    photoUri, mPhotoBitmap, mPhotoBytes, rect, delta, mContactData.isUserProfile(),
88                    mContactData.isDirectoryEntry(), mExpandPhotoOnClick);
89            // Cache the bitmap directly, so the activity can pull it from the
90            // photo manager.
91            if (mPhotoBitmap != null) {
92                ContactPhotoManager.getInstance(mContext).cacheBitmap(
93                        photoUri, mPhotoBitmap, mPhotoBytes);
94            }
95            mContext.startActivity(photoSelectionIntent);
96        }
97    }
98
99    private OnClickListener setupClickListener(Context context, Contact contactData, Bitmap bitmap,
100            boolean expandPhotoOnClick) {
101        final ImageView target = getTarget();
102        if (target == null) return null;
103
104        return new PhotoClickListener(
105                context, contactData, bitmap, getCompressedImage(), expandPhotoOnClick);
106    }
107}
108