Home | History | Annotate | Download | only in list
      1 /*
      2  * Copyright (C) 2011 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 package com.android.contacts.common.list;
     17 
     18 import android.content.Context;
     19 import android.graphics.Rect;
     20 import android.net.Uri;
     21 import android.util.AttributeSet;
     22 import android.util.Log;
     23 import android.view.View;
     24 import android.widget.FrameLayout;
     25 import android.widget.ImageView;
     26 import android.widget.QuickContactBadge;
     27 import android.widget.TextView;
     28 
     29 import com.android.contacts.common.ContactPhotoManager;
     30 import com.android.contacts.common.MoreContactUtils;
     31 import com.android.contacts.common.R;
     32 
     33 /**
     34  * A ContactTile displays a contact's picture and name
     35  */
     36 public abstract class ContactTileView extends FrameLayout {
     37     private final static String TAG = ContactTileView.class.getSimpleName();
     38 
     39     private Uri mLookupUri;
     40     private ImageView mPhoto;
     41     private QuickContactBadge mQuickContact;
     42     private TextView mName;
     43     private TextView mStatus;
     44     private TextView mPhoneLabel;
     45     private TextView mPhoneNumber;
     46     private ContactPhotoManager mPhotoManager = null;
     47     private View mPushState;
     48     private View mHorizontalDivider;
     49     protected Listener mListener;
     50 
     51     public ContactTileView(Context context, AttributeSet attrs) {
     52         super(context, attrs);
     53     }
     54 
     55     @Override
     56     protected void onFinishInflate() {
     57         super.onFinishInflate();
     58         mName = (TextView) findViewById(R.id.contact_tile_name);
     59 
     60         mQuickContact = (QuickContactBadge) findViewById(R.id.contact_tile_quick);
     61         mPhoto = (ImageView) findViewById(R.id.contact_tile_image);
     62         mStatus = (TextView) findViewById(R.id.contact_tile_status);
     63         mPhoneLabel = (TextView) findViewById(R.id.contact_tile_phone_type);
     64         mPhoneNumber = (TextView) findViewById(R.id.contact_tile_phone_number);
     65         mPushState = findViewById(R.id.contact_tile_push_state);
     66         mHorizontalDivider = findViewById(R.id.contact_tile_horizontal_divider);
     67 
     68         OnClickListener listener = createClickListener();
     69 
     70         if(mPushState != null) {
     71             mPushState.setOnClickListener(listener);
     72         } else {
     73             setOnClickListener(listener);
     74         }
     75     }
     76 
     77     protected OnClickListener createClickListener() {
     78         return new OnClickListener() {
     79             @Override
     80             public void onClick(View v) {
     81                 if (mListener == null) return;
     82                 mListener.onContactSelected(
     83                         getLookupUri(),
     84                         MoreContactUtils.getTargetRectFromView(mContext, ContactTileView.this));
     85             }
     86         };
     87     }
     88 
     89     public void setPhotoManager(ContactPhotoManager photoManager) {
     90         mPhotoManager = photoManager;
     91     }
     92 
     93     /**
     94      * Populates the data members to be displayed from the
     95      * fields in {@link com.android.contacts.common.list.ContactTileAdapter.ContactEntry}
     96      */
     97     public void loadFromContact(ContactTileAdapter.ContactEntry entry) {
     98 
     99         if (entry != null) {
    100             mName.setText(entry.name);
    101             mLookupUri = entry.lookupKey;
    102 
    103             if (mStatus != null) {
    104                 if (entry.status == null) {
    105                     mStatus.setVisibility(View.GONE);
    106                 } else {
    107                     mStatus.setText(entry.status);
    108                     mStatus.setCompoundDrawablesWithIntrinsicBounds(entry.presenceIcon,
    109                             null, null, null);
    110                     mStatus.setVisibility(View.VISIBLE);
    111                 }
    112             }
    113 
    114             if (mPhoneLabel != null) {
    115                 mPhoneLabel.setText(entry.phoneLabel);
    116             }
    117 
    118             if (mPhoneNumber != null) {
    119                 // TODO: Format number correctly
    120                 mPhoneNumber.setText(entry.phoneNumber);
    121             }
    122 
    123             setVisibility(View.VISIBLE);
    124 
    125             if (mPhotoManager != null) {
    126                 if (mPhoto != null) {
    127                     mPhotoManager.loadPhoto(mPhoto, entry.photoUri, getApproximateImageSize(),
    128                             isDarkTheme());
    129 
    130                     if (mQuickContact != null) {
    131                         mQuickContact.assignContactUri(mLookupUri);
    132                     }
    133                 } else if (mQuickContact != null) {
    134                     mQuickContact.assignContactUri(mLookupUri);
    135                     mPhotoManager.loadPhoto(mQuickContact, entry.photoUri,
    136                             getApproximateImageSize(), isDarkTheme());
    137                 }
    138             } else {
    139                 Log.w(TAG, "contactPhotoManager not set");
    140             }
    141 
    142             if (mPushState != null) {
    143                 mPushState.setContentDescription(entry.name);
    144             } else if (mQuickContact != null) {
    145                 mQuickContact.setContentDescription(entry.name);
    146             }
    147         } else {
    148             setVisibility(View.INVISIBLE);
    149         }
    150     }
    151 
    152     public void setListener(Listener listener) {
    153         mListener = listener;
    154     }
    155 
    156     public void setHorizontalDividerVisibility(int visibility) {
    157         if (mHorizontalDivider != null) mHorizontalDivider.setVisibility(visibility);
    158     }
    159 
    160     public Uri getLookupUri() {
    161         return mLookupUri;
    162     }
    163 
    164     protected QuickContactBadge getQuickContact() {
    165         return mQuickContact;
    166     }
    167 
    168     /**
    169      * Implemented by subclasses to estimate the size of the picture. This can return -1 if only
    170      * a thumbnail is shown anyway
    171      */
    172     protected abstract int getApproximateImageSize();
    173 
    174     protected abstract boolean isDarkTheme();
    175 
    176     public interface Listener {
    177         /**
    178          * Notification that the contact was selected; no specific action is dictated.
    179          */
    180         void onContactSelected(Uri contactLookupUri, Rect viewRect);
    181         /**
    182          * Notification that the specified number is to be called.
    183          */
    184         void onCallNumberDirectly(String phoneNumber);
    185         /**
    186          * @return The width of each tile. This doesn't have to be a precise number (e.g. paddings
    187          *         can be ignored), but is used to load the correct picture size from the database
    188          */
    189         int getApproximateTileWidth();
    190     }
    191 }
    192