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