Home | History | Annotate | Download | only in speeddial
      1 /*
      2  * Copyright (C) 2017 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.dialer.speeddial;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.database.Cursor;
     22 import android.net.Uri;
     23 import android.provider.ContactsContract.CommonDataKinds.Phone;
     24 import android.provider.ContactsContract.Contacts;
     25 import android.support.v7.widget.RecyclerView;
     26 import android.text.TextUtils;
     27 import android.view.View;
     28 import android.view.View.OnClickListener;
     29 import android.widget.QuickContactBadge;
     30 import android.widget.TextView;
     31 import com.android.dialer.contactphoto.ContactPhotoManager;
     32 import com.android.dialer.lettertile.LetterTileDrawable;
     33 import com.android.dialer.location.GeoUtil;
     34 import com.android.dialer.phonenumberutil.PhoneNumberHelper;
     35 
     36 /** ViewHolder for displaying suggested contacts in {@link SpeedDialFragment}. */
     37 public class SuggestionViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
     38 
     39   private final SuggestedContactsListener listener;
     40 
     41   private final QuickContactBadge photoView;
     42   private final TextView nameOrNumberView;
     43   private final TextView numberView;
     44 
     45   private String number;
     46 
     47   SuggestionViewHolder(View view, SuggestedContactsListener listener) {
     48     super(view);
     49     photoView = view.findViewById(R.id.avatar);
     50     nameOrNumberView = view.findViewById(R.id.name);
     51     numberView = view.findViewById(R.id.number);
     52     itemView.setOnClickListener(this);
     53     view.findViewById(R.id.overflow).setOnClickListener(this);
     54     this.listener = listener;
     55   }
     56 
     57   public void bind(Context context, Cursor cursor) {
     58     number = cursor.getString(StrequentContactsCursorLoader.PHONE_NUMBER);
     59     number = PhoneNumberHelper.formatNumber(context, number, GeoUtil.getCurrentCountryIso(context));
     60 
     61     String name = cursor.getString(StrequentContactsCursorLoader.PHONE_DISPLAY_NAME);
     62     String label = getLabel(context.getResources(), cursor);
     63     String secondaryInfo =
     64         TextUtils.isEmpty(label)
     65             ? number
     66             : context.getString(
     67                 com.android.contacts.common.R.string.call_subject_type_and_number, label, number);
     68 
     69     nameOrNumberView.setText(name);
     70     numberView.setText(secondaryInfo);
     71 
     72     long contactId = cursor.getLong(StrequentContactsCursorLoader.PHONE_ID);
     73     String lookupKey = cursor.getString(StrequentContactsCursorLoader.PHONE_LOOKUP_KEY);
     74     Uri contactUri = Contacts.getLookupUri(contactId, lookupKey);
     75 
     76     String photoUri = cursor.getString(StrequentContactsCursorLoader.PHONE_PHOTO_URI);
     77     ContactPhotoManager.getInstance(context)
     78         .loadDialerThumbnailOrPhoto(
     79             photoView,
     80             contactUri,
     81             cursor.getLong(StrequentContactsCursorLoader.PHONE_PHOTO_ID),
     82             photoUri == null ? null : Uri.parse(photoUri),
     83             name,
     84             LetterTileDrawable.TYPE_DEFAULT);
     85   }
     86 
     87   // TODO(calderwoodra): handle CNAP and cequint types.
     88   // TODO(calderwoodra): unify this into a utility method with CallLogAdapter#getNumberType
     89   private static String getLabel(Resources resources, Cursor cursor) {
     90     int numberType = cursor.getInt(StrequentContactsCursorLoader.PHONE_TYPE);
     91     String numberLabel = cursor.getString(StrequentContactsCursorLoader.PHONE_LABEL);
     92 
     93     // Returns empty label instead of "custom" if the custom label is empty.
     94     if (numberType == Phone.TYPE_CUSTOM && TextUtils.isEmpty(numberLabel)) {
     95       return "";
     96     }
     97     return (String) Phone.getTypeLabel(resources, numberType, numberLabel);
     98   }
     99 
    100   @Override
    101   public void onClick(View v) {
    102     if (v.getId() == R.id.overflow) {
    103       listener.onOverFlowMenuClicked(number);
    104     } else {
    105       listener.onRowClicked(number);
    106     }
    107   }
    108 
    109   /** Listener/Callback for {@link SuggestionViewHolder} parents. */
    110   public interface SuggestedContactsListener {
    111 
    112     void onOverFlowMenuClicked(String number);
    113 
    114     /** Called when a suggested contact is clicked. */
    115     void onRowClicked(String number);
    116   }
    117 }
    118