Home | History | Annotate | Download | only in dialer
      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.car.dialer;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.graphics.Bitmap;
     22 import android.graphics.BitmapFactory;
     23 import android.net.Uri;
     24 import android.support.annotation.Nullable;
     25 import android.support.v7.widget.RecyclerView;
     26 import android.view.View;
     27 import android.widget.ImageView;
     28 import android.widget.TextView;
     29 
     30 import androidx.car.utils.ListItemBackgroundResolver;
     31 
     32 import com.android.car.apps.common.LetterTileDrawable;
     33 import com.android.car.dialer.ui.CircleBitmapDrawable;
     34 
     35 import java.io.FileNotFoundException;
     36 import java.io.InputStream;
     37 
     38 /**
     39  * A {@link android.support.v7.widget.RecyclerView.ViewHolder} that will parse relevant
     40  * views out of a {@code contact_result} layout.
     41  */
     42 public class ContactResultViewHolder extends RecyclerView.ViewHolder {
     43     private final Context mContext;
     44     private final View mContactCard;
     45     private final TextView mContactName;
     46     private final ImageView mContactPicture;
     47 
     48     public ContactResultViewHolder(View view) {
     49         super(view);
     50         mContext = view.getContext();
     51         mContactCard = view.findViewById(R.id.contact_result_card);
     52         mContactName = view.findViewById(R.id.contact_name);
     53         mContactPicture = view.findViewById(R.id.contact_picture);
     54     }
     55 
     56     /**
     57      * Populates the view that is represented by this ViewHolder with the information in the
     58      * provided {@link ContactDetails}.
     59      */
     60     public void bind(ContactDetails details, int itemCount) {
     61         ListItemBackgroundResolver.setBackground(mContactCard, getAdapterPosition(), itemCount);
     62 
     63         mContactCard.setOnClickListener(v -> {
     64             Intent intent = new Intent();
     65             intent.setAction(TelecomIntents.ACTION_SHOW_CONTACT_DETAILS);
     66             intent.putExtra(TelecomIntents.CONTACT_LOOKUP_URI_EXTRA, details.lookupUri.toString());
     67             mContext.startActivity(intent);
     68         });
     69 
     70         mContactName.setText(details.displayName);
     71 
     72         if (details.photoUri == null) {
     73             setLetterDrawableForContact(details);
     74             return;
     75         }
     76 
     77         Bitmap bitmap = getContactBitmapFromUri(mContext, details.photoUri);
     78         if (bitmap == null) {
     79             setLetterDrawableForContact(details);
     80         } else {
     81             mContactPicture.setScaleType(ImageView.ScaleType.CENTER_CROP);
     82             mContactPicture.setImageDrawable(
     83                     new CircleBitmapDrawable(mContext.getResources(), bitmap));
     84         }
     85     }
     86 
     87     /**
     88      * Sets the contact picture to be a rounded, colored circle that has the first letter of the
     89      * contact's name in it.
     90      */
     91     private void setLetterDrawableForContact(ContactDetails details) {
     92         mContactPicture.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
     93         LetterTileDrawable letterTileDrawable = new LetterTileDrawable(mContext.getResources());
     94         letterTileDrawable.setContactDetails(details.displayName, details.displayName);
     95         letterTileDrawable.setIsCircular(true);
     96         mContactPicture.setImageDrawable(letterTileDrawable);
     97     }
     98 
     99     /**
    100      * Retrieves the picture that is specified by the given {@link Uri}.
    101      */
    102     @Nullable
    103     private static Bitmap getContactBitmapFromUri(Context context, Uri uri) {
    104         try {
    105             InputStream input = context.getContentResolver().openInputStream(uri);
    106             return input == null ? null : BitmapFactory.decodeStream(input);
    107         } catch (FileNotFoundException e) {
    108             return null;
    109         }
    110     }
    111 
    112     /**
    113      * A struct that holds the details for a contact row.
    114      */
    115     public static class ContactDetails {
    116         public final String displayName;
    117         public final Uri photoUri;
    118         public final Uri lookupUri;
    119 
    120         public ContactDetails(String displayName, String photoUri, Uri lookupUri) {
    121             this.displayName = displayName;
    122             this.photoUri = photoUri == null ? null : Uri.parse(photoUri);
    123             this.lookupUri = lookupUri;
    124         }
    125     }
    126 }
    127