Home | History | Annotate | Download | only in editor
      1 /*
      2  * Copyright (C) 2010 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.editor;
     18 
     19 import android.content.Context;
     20 import android.graphics.BitmapFactory;
     21 import android.net.Uri;
     22 import android.provider.ContactsContract.Contacts;
     23 import android.text.TextUtils;
     24 import android.util.AttributeSet;
     25 import android.view.View;
     26 import android.widget.ImageView;
     27 import android.widget.LinearLayout;
     28 import android.widget.TextView;
     29 
     30 import com.android.contacts.R;
     31 import com.android.contacts.editor.AggregationSuggestionEngine.RawContact;
     32 import com.android.contacts.editor.AggregationSuggestionEngine.Suggestion;
     33 import com.android.contacts.common.ContactPhotoManager;
     34 import com.android.contacts.common.model.AccountTypeManager;
     35 import com.android.contacts.common.model.account.AccountType;
     36 
     37 import com.google.common.collect.Lists;
     38 
     39 import java.util.ArrayList;
     40 import java.util.List;
     41 
     42 /**
     43  * A view that contains a name, picture and other data for a contact aggregation suggestion.
     44  */
     45 public class AggregationSuggestionView extends LinearLayout {
     46 
     47     public interface Listener {
     48 
     49         /**
     50          * Callback that passes the contact ID to join with and, for convenience,
     51          * also the list of constituent raw contact IDs to avoid a separate query
     52          * for those.
     53          */
     54         public void onJoinAction(long contactId, List<Long> rawContacIds);
     55 
     56         /**
     57          * Callback that passes the contact ID to edit instead of the current contact.
     58          */
     59         public void onEditAction(Uri contactLookupUri);
     60     }
     61 
     62     private Listener mListener;
     63     private long mContactId;
     64     private String mLookupKey;
     65     private List<RawContact> mRawContacts = Lists.newArrayList();
     66     private boolean mNewContact;
     67 
     68     public AggregationSuggestionView(Context context) {
     69         super(context);
     70     }
     71 
     72     public AggregationSuggestionView(Context context, AttributeSet attrs) {
     73         super(context, attrs);
     74     }
     75 
     76     public AggregationSuggestionView(Context context, AttributeSet attrs, int defStyle) {
     77         super(context, attrs, defStyle);
     78     }
     79 
     80     public void setNewContact(boolean flag) {
     81         mNewContact = flag;
     82     }
     83 
     84     public void bindSuggestion(Suggestion suggestion) {
     85         mContactId = suggestion.contactId;
     86         mLookupKey = suggestion.lookupKey;
     87         mRawContacts = suggestion.rawContacts;
     88         ImageView photo = (ImageView) findViewById(R.id.aggregation_suggestion_photo);
     89         if (suggestion.photo != null) {
     90             photo.setImageBitmap(BitmapFactory.decodeByteArray(
     91                     suggestion.photo, 0, suggestion.photo.length));
     92         } else {
     93             photo.setImageDrawable(ContactPhotoManager.getDefaultAvatarDrawableForContact(
     94                     getResources(), false, null));
     95         }
     96 
     97         TextView name = (TextView) findViewById(R.id.aggregation_suggestion_name);
     98         name.setText(suggestion.name);
     99 
    100         TextView data = (TextView) findViewById(R.id.aggregation_suggestion_data);
    101         String dataText = null;
    102         if (suggestion.nickname != null) {
    103             dataText = suggestion.nickname;
    104         } else if (suggestion.emailAddress != null) {
    105             dataText = suggestion.emailAddress;
    106         } else if (suggestion.phoneNumber != null) {
    107             dataText = suggestion.phoneNumber;
    108             // Phone numbers should always be in LTR mode.
    109             data.setTextDirection(View.TEXT_DIRECTION_LTR);
    110         }
    111         data.setText(dataText);
    112     }
    113 
    114     /**
    115      * Returns true if the suggested contact can be edited.
    116      */
    117     private boolean canEditSuggestedContact() {
    118         if (!mNewContact) {
    119             return false;
    120         }
    121 
    122         AccountTypeManager accountTypes = AccountTypeManager.getInstance(getContext());
    123         for (RawContact rawContact : mRawContacts) {
    124             String accountType = rawContact.accountType;
    125             String dataSet = rawContact.dataSet;
    126             if (accountType == null) {
    127                 return true;
    128             }
    129             AccountType type = accountTypes.getAccountType(accountType, dataSet);
    130             if (type.areContactsWritable()) {
    131                 return true;
    132             }
    133         }
    134 
    135         return false;
    136     }
    137 
    138     public void setListener(Listener listener) {
    139         mListener = listener;
    140     }
    141 
    142     public boolean handleItemClickEvent() {
    143         if (mListener != null && isEnabled()) {
    144             if (canEditSuggestedContact()) {
    145                 if (TextUtils.isEmpty(mLookupKey)) {
    146                     return false;
    147                 }
    148                 mListener.onEditAction(Contacts.getLookupUri(mContactId, mLookupKey));
    149             } else {
    150                 ArrayList<Long> rawContactIds = Lists.newArrayList();
    151                 for (RawContact rawContact : mRawContacts) {
    152                     rawContactIds.add(rawContact.rawContactId);
    153                 }
    154                 mListener.onJoinAction(mContactId, rawContactIds);
    155             }
    156             return true;
    157         }
    158         return false;
    159     }
    160 }
    161