1 /* 2 * Copyright (C) 2018 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.car.dialer.ui.listitem; 17 18 import android.content.Context; 19 import android.content.res.Resources; 20 import android.graphics.Bitmap; 21 import android.graphics.drawable.BitmapDrawable; 22 import android.graphics.drawable.Drawable; 23 import android.widget.ImageView; 24 25 import androidx.car.widget.TextListItem; 26 27 import com.android.car.apps.common.LetterTileDrawable; 28 import com.android.car.dialer.R; 29 import com.android.car.dialer.telecom.ContactBitmapWorker; 30 import com.android.car.dialer.ui.CircleBitmapDrawable; 31 import com.android.car.dialer.ui.ContactListFragment; 32 33 /** 34 * ListItem for contact. 35 */ 36 public class ContactListItem extends TextListItem { 37 private Context mContext; 38 private ContactListFragment.ContactItem mContactItem; 39 40 public ContactListItem(Context context, ContactListFragment.ContactItem contactItem) { 41 super(context); 42 mContext = context; 43 mContactItem = contactItem; 44 } 45 46 @Override 47 public void onBind(ViewHolder viewHolder) { 48 super.onBind(viewHolder); 49 ContactBitmapWorker.loadBitmap(mContext.getContentResolver(), viewHolder.getPrimaryIcon(), 50 mContactItem.mNumber, 51 bitmap -> { 52 Resources r = mContext.getResources(); 53 viewHolder.getPrimaryIcon().setScaleType(ImageView.ScaleType.CENTER); 54 Drawable avatarDrawable; 55 if (bitmap != null) { 56 avatarDrawable = new CircleBitmapDrawable(r, bitmap); 57 } else { 58 LetterTileDrawable letterTileDrawable = new LetterTileDrawable(r); 59 letterTileDrawable.setContactDetails(mContactItem.mDisplayName, 60 mContactItem.mNumber); 61 letterTileDrawable.setIsCircular(true); 62 avatarDrawable = letterTileDrawable; 63 } 64 65 int iconSize = mContext.getResources().getDimensionPixelSize( 66 R.dimen.avatar_icon_size); 67 setPrimaryActionIcon(scaleDrawable(avatarDrawable, iconSize), true); 68 super.onBind(viewHolder); 69 70 // force rebind the view. 71 super.onBind(viewHolder); 72 }); 73 viewHolder.getContainerLayout().setBackgroundColor( 74 mContext.getColor(R.color.contact_list_item_color)); 75 } 76 77 private Drawable scaleDrawable(Drawable targetDrawable, int sizeInPixel) { 78 Bitmap bitmap = null; 79 if (targetDrawable instanceof CircleBitmapDrawable) { 80 bitmap = ((CircleBitmapDrawable) targetDrawable).toBitmap(sizeInPixel); 81 } else if (targetDrawable instanceof LetterTileDrawable){ 82 bitmap = ((LetterTileDrawable) targetDrawable).toBitmap(sizeInPixel); 83 } 84 return new BitmapDrawable(mContext.getResources(), bitmap); 85 } 86 } 87