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.calldetails; 18 19 import android.content.Context; 20 import android.support.annotation.NonNull; 21 import android.support.v7.widget.RecyclerView; 22 import android.support.v7.widget.RecyclerView.ViewHolder; 23 import android.view.LayoutInflater; 24 import android.view.ViewGroup; 25 import com.android.dialer.calldetails.CallDetailsEntries.CallDetailsEntry; 26 import com.android.dialer.calldetails.CallDetailsFooterViewHolder.DeleteCallDetailsListener; 27 import com.android.dialer.calldetails.CallDetailsHeaderViewHolder.CallDetailsHeaderListener; 28 import com.android.dialer.calllogutils.CallTypeHelper; 29 import com.android.dialer.calllogutils.CallbackActionHelper; 30 import com.android.dialer.calllogutils.CallbackActionHelper.CallbackAction; 31 import com.android.dialer.common.Assert; 32 import com.android.dialer.dialercontact.DialerContact; 33 import com.android.dialer.duo.DuoComponent; 34 import java.util.List; 35 36 /** Adapter for RecyclerView in {@link CallDetailsActivity}. */ 37 final class CallDetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 38 39 private static final int HEADER_VIEW_TYPE = 1; 40 private static final int CALL_ENTRY_VIEW_TYPE = 2; 41 private static final int FOOTER_VIEW_TYPE = 3; 42 43 private final DialerContact contact; 44 private final CallDetailsHeaderListener callDetailsHeaderListener; 45 private final CallDetailsFooterViewHolder.ReportCallIdListener reportCallIdListener; 46 private final DeleteCallDetailsListener deleteCallDetailsListener; 47 private final CallTypeHelper callTypeHelper; 48 private List<CallDetailsEntry> callDetailsEntries; 49 CallDetailsAdapter( Context context, @NonNull DialerContact contact, @NonNull List<CallDetailsEntry> callDetailsEntries, CallDetailsHeaderListener callDetailsHeaderListener, CallDetailsFooterViewHolder.ReportCallIdListener reportCallIdListener, DeleteCallDetailsListener deleteCallDetailsListener)50 CallDetailsAdapter( 51 Context context, 52 @NonNull DialerContact contact, 53 @NonNull List<CallDetailsEntry> callDetailsEntries, 54 CallDetailsHeaderListener callDetailsHeaderListener, 55 CallDetailsFooterViewHolder.ReportCallIdListener reportCallIdListener, 56 DeleteCallDetailsListener deleteCallDetailsListener) { 57 this.contact = Assert.isNotNull(contact); 58 this.callDetailsEntries = callDetailsEntries; 59 this.callDetailsHeaderListener = callDetailsHeaderListener; 60 this.reportCallIdListener = reportCallIdListener; 61 this.deleteCallDetailsListener = deleteCallDetailsListener; 62 callTypeHelper = new CallTypeHelper(context.getResources(), DuoComponent.get(context).getDuo()); 63 } 64 65 @Override onCreateViewHolder(ViewGroup parent, int viewType)66 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 67 LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 68 switch (viewType) { 69 case HEADER_VIEW_TYPE: 70 return new CallDetailsHeaderViewHolder( 71 inflater.inflate(R.layout.contact_container, parent, false), callDetailsHeaderListener); 72 case CALL_ENTRY_VIEW_TYPE: 73 return new CallDetailsEntryViewHolder( 74 inflater.inflate(R.layout.call_details_entry, parent, false)); 75 case FOOTER_VIEW_TYPE: 76 return new CallDetailsFooterViewHolder( 77 inflater.inflate(R.layout.call_details_footer, parent, false), 78 reportCallIdListener, 79 deleteCallDetailsListener); 80 default: 81 throw Assert.createIllegalStateFailException( 82 "No ViewHolder available for viewType: " + viewType); 83 } 84 } 85 86 @Override onBindViewHolder(ViewHolder holder, int position)87 public void onBindViewHolder(ViewHolder holder, int position) { 88 if (position == 0) { // Header 89 ((CallDetailsHeaderViewHolder) holder).updateContactInfo(contact, getCallbackAction()); 90 ((CallDetailsHeaderViewHolder) holder) 91 .updateAssistedDialingInfo(callDetailsEntries.get(position)); 92 } else if (position == getItemCount() - 1) { 93 ((CallDetailsFooterViewHolder) holder).setPhoneNumber(contact.getNumber()); 94 } else { 95 CallDetailsEntryViewHolder viewHolder = (CallDetailsEntryViewHolder) holder; 96 CallDetailsEntry entry = callDetailsEntries.get(position - 1); 97 viewHolder.setCallDetails( 98 contact.getNumber(), 99 entry, 100 callTypeHelper, 101 !entry.getHistoryResultsList().isEmpty() && position != getItemCount() - 2); 102 } 103 } 104 105 @Override getItemViewType(int position)106 public int getItemViewType(int position) { 107 if (position == 0) { // Header 108 return HEADER_VIEW_TYPE; 109 } else if (position == getItemCount() - 1) { 110 return FOOTER_VIEW_TYPE; 111 } else { 112 return CALL_ENTRY_VIEW_TYPE; 113 } 114 } 115 116 @Override getItemCount()117 public int getItemCount() { 118 return callDetailsEntries.isEmpty() 119 ? 0 120 : callDetailsEntries.size() + 2; // plus header and footer 121 } 122 updateCallDetailsEntries(List<CallDetailsEntry> entries)123 void updateCallDetailsEntries(List<CallDetailsEntry> entries) { 124 callDetailsEntries = entries; 125 notifyDataSetChanged(); 126 } 127 getCallbackAction()128 private @CallbackAction int getCallbackAction() { 129 Assert.checkState(!callDetailsEntries.isEmpty()); 130 131 CallDetailsEntry entry = callDetailsEntries.get(0); 132 return CallbackActionHelper.getCallbackAction( 133 contact.getNumber(), entry.getFeatures(), entry.getIsDuoCall()); 134 } 135 } 136