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.calllog; 17 18 import android.content.Context; 19 import android.view.LayoutInflater; 20 import android.view.View; 21 import android.view.ViewGroup; 22 23 import androidx.annotation.IntDef; 24 import androidx.annotation.NonNull; 25 import androidx.recyclerview.widget.RecyclerView; 26 27 import com.android.car.dialer.R; 28 import com.android.car.dialer.log.L; 29 import com.android.car.dialer.ui.common.entity.HeaderViewHolder; 30 import com.android.car.dialer.ui.common.entity.UiCallLog; 31 import com.android.car.telephony.common.Contact; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 /** Adapter for call history list. */ 37 public class CallLogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 38 39 private static final String TAG = "CD.CallLogAdapter"; 40 41 /** IntDef for the different groups of calllog lists separated by time periods. */ 42 @IntDef({ 43 EntryType.TYPE_HEADER, 44 EntryType.TYPE_CALLLOG, 45 }) 46 private @interface EntryType { 47 /** Entry typre is header. */ 48 int TYPE_HEADER = 1; 49 50 /** Entry type is calllog. */ 51 int TYPE_CALLLOG = 2; 52 } 53 54 public interface OnShowContactDetailListener { onShowContactDetail(Contact contact)55 void onShowContactDetail(Contact contact); 56 } 57 58 private List<Object> mUiCallLogs = new ArrayList<>(); 59 private Context mContext; 60 private CallLogAdapter.OnShowContactDetailListener mOnShowContactDetailListener; 61 CallLogAdapter(Context context, CallLogAdapter.OnShowContactDetailListener onShowContactDetailListener)62 public CallLogAdapter(Context context, 63 CallLogAdapter.OnShowContactDetailListener onShowContactDetailListener) { 64 mContext = context; 65 mOnShowContactDetailListener = onShowContactDetailListener; 66 } 67 68 /** 69 * Sets calllogs. 70 */ setUiCallLogs(@onNull List<Object> uiCallLogs)71 public void setUiCallLogs(@NonNull List<Object> uiCallLogs) { 72 L.d(TAG, "setUiCallLogs: %d", uiCallLogs.size()); 73 mUiCallLogs.clear(); 74 mUiCallLogs.addAll(uiCallLogs); 75 notifyDataSetChanged(); 76 } 77 78 @NonNull 79 @Override onCreateViewHolder(@onNull ViewGroup parent, int viewType)80 public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 81 if (viewType == EntryType.TYPE_CALLLOG) { 82 View rootView = LayoutInflater.from(mContext) 83 .inflate(R.layout.call_history_list_item, parent, false); 84 return new CallLogViewHolder(rootView, mOnShowContactDetailListener); 85 } 86 87 View rootView = LayoutInflater.from(mContext) 88 .inflate(R.layout.header_item, parent, false); 89 return new HeaderViewHolder(rootView); 90 } 91 92 @Override onBindViewHolder(@onNull RecyclerView.ViewHolder holder, int position)93 public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { 94 if (holder instanceof CallLogViewHolder) { 95 ((CallLogViewHolder) holder).bind((UiCallLog) mUiCallLogs.get(position)); 96 } else { 97 ((HeaderViewHolder) holder).setHeaderTitle((String) mUiCallLogs.get(position)); 98 } 99 } 100 101 @Override 102 @EntryType getItemViewType(int position)103 public int getItemViewType(int position) { 104 if (mUiCallLogs.get(position) instanceof UiCallLog) { 105 return EntryType.TYPE_CALLLOG; 106 } else { 107 return EntryType.TYPE_HEADER; 108 } 109 } 110 111 @Override onViewRecycled(@onNull RecyclerView.ViewHolder holder)112 public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { 113 if (holder instanceof CallLogViewHolder) { 114 ((CallLogViewHolder) holder).recycle(); 115 } 116 } 117 118 @Override getItemCount()119 public int getItemCount() { 120 return mUiCallLogs.size(); 121 } 122 } 123