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