Home | History | Annotate | Download | only in listitem
      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.graphics.drawable.ScaleDrawable;
     24 import android.view.ViewGroup;
     25 import android.widget.ImageView;
     26 
     27 import androidx.car.widget.TextListItem;
     28 
     29 import com.android.car.apps.common.LetterTileDrawable;
     30 import com.android.car.dialer.telecom.ContactBitmapWorker;
     31 import com.android.car.dialer.ui.CallHistoryListItemProvider;
     32 import com.android.car.dialer.ui.CallLogListingTask;
     33 import com.android.car.dialer.ui.CircleBitmapDrawable;
     34 import com.android.car.dialer.R;
     35 
     36 /**
     37  * List item which is created by {@link CallHistoryListItemProvider} binds a call list item to a
     38  * list view item.
     39  */
     40 public class CallLogListItem extends TextListItem {
     41     private final CallLogListingTask.CallLogItem mCallLogItem;
     42     private final Context mContext;
     43 
     44     public CallLogListItem(Context context, CallLogListingTask.CallLogItem callLog) {
     45         super(context);
     46         mCallLogItem = callLog;
     47         mContext = context;
     48     }
     49 
     50     @Override
     51     public void onBind(ViewHolder viewHolder) {
     52         super.onBind(viewHolder);
     53         ContactBitmapWorker.loadBitmap(mContext.getContentResolver(), viewHolder.getPrimaryIcon(),
     54                 mCallLogItem.mNumber,
     55                 bitmap -> {
     56                     Resources r = mContext.getResources();
     57                     viewHolder.getPrimaryIcon().setScaleType(ImageView.ScaleType.CENTER);
     58                     Drawable avatarDrawable;
     59                     if (bitmap != null) {
     60                         avatarDrawable = new CircleBitmapDrawable(r, bitmap);
     61                         setPrimaryActionIcon(new CircleBitmapDrawable(r, bitmap), true);
     62                     } else {
     63                         LetterTileDrawable letterTileDrawable = new LetterTileDrawable(r);
     64                         letterTileDrawable.setContactDetails(mCallLogItem.mTitle,
     65                                 mCallLogItem.mNumber);
     66                         letterTileDrawable.setIsCircular(true);
     67                         avatarDrawable = letterTileDrawable;
     68                     }
     69 
     70                     int iconSize = mContext.getResources().getDimensionPixelSize(
     71                             R.dimen.avatar_icon_size);
     72                     setPrimaryActionIcon(scaleDrawable(avatarDrawable, iconSize), true);
     73                     super.onBind(viewHolder);
     74                 });
     75 
     76         viewHolder.getContainerLayout().setBackgroundColor(
     77                 mContext.getColor(R.color.call_history_list_item_color));
     78     }
     79 
     80     private Drawable scaleDrawable(Drawable targetDrawable, int sizeInPixel) {
     81         Bitmap bitmap = null;
     82         if (targetDrawable instanceof CircleBitmapDrawable) {
     83             bitmap = ((CircleBitmapDrawable) targetDrawable).toBitmap(sizeInPixel);
     84         } else if (targetDrawable instanceof LetterTileDrawable){
     85             bitmap = ((LetterTileDrawable) targetDrawable).toBitmap(sizeInPixel);
     86         }
     87         return new BitmapDrawable(mContext.getResources(), bitmap);
     88     }
     89 }
     90