Home | History | Annotate | Download | only in calldetails
      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.calllogutils.CallTypeHelper;
     27 import com.android.dialer.common.Assert;
     28 import com.android.dialer.dialercontact.DialerContact;
     29 import java.util.List;
     30 
     31 /** Adapter for RecyclerView in {@link CallDetailsActivity}. */
     32 final class CallDetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
     33 
     34   private static final int HEADER_VIEW_TYPE = 1;
     35   private static final int CALL_ENTRY_VIEW_TYPE = 2;
     36   private static final int FOOTER_VIEW_TYPE = 3;
     37 
     38   private final DialerContact contact;
     39   private final List<CallDetailsEntry> callDetailsEntries;
     40   private final CallDetailsFooterViewHolder.ReportCallIdListener listener;
     41   private final CallTypeHelper callTypeHelper;
     42 
     43   CallDetailsAdapter(
     44       Context context,
     45       @NonNull DialerContact contact,
     46       @NonNull List<CallDetailsEntry> callDetailsEntries,
     47       CallDetailsFooterViewHolder.ReportCallIdListener listener) {
     48     this.contact = Assert.isNotNull(contact);
     49     this.callDetailsEntries = callDetailsEntries;
     50     this.listener = listener;
     51     callTypeHelper = new CallTypeHelper(context.getResources());
     52   }
     53 
     54   @Override
     55   public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     56     LayoutInflater inflater = LayoutInflater.from(parent.getContext());
     57     switch (viewType) {
     58       case HEADER_VIEW_TYPE:
     59         return new CallDetailsHeaderViewHolder(
     60             inflater.inflate(R.layout.contact_container, parent, false));
     61       case CALL_ENTRY_VIEW_TYPE:
     62         return new CallDetailsEntryViewHolder(
     63             inflater.inflate(R.layout.call_details_entry, parent, false));
     64       case FOOTER_VIEW_TYPE:
     65         return new CallDetailsFooterViewHolder(
     66             inflater.inflate(R.layout.call_details_footer, parent, false), listener);
     67       default:
     68         throw Assert.createIllegalStateFailException(
     69             "No ViewHolder available for viewType: " + viewType);
     70     }
     71   }
     72 
     73   @Override
     74   public void onBindViewHolder(ViewHolder holder, int position) {
     75     if (position == 0) { // Header
     76       ((CallDetailsHeaderViewHolder) holder).updateContactInfo(contact);
     77     } else if (position == getItemCount() - 1) {
     78       ((CallDetailsFooterViewHolder) holder).setPhoneNumber(contact.getNumber());
     79     } else {
     80       CallDetailsEntryViewHolder viewHolder = (CallDetailsEntryViewHolder) holder;
     81       CallDetailsEntry entry = callDetailsEntries.get(position - 1);
     82       viewHolder.setCallDetails(
     83           contact.getNumber(),
     84           entry,
     85           callTypeHelper,
     86           !entry.getHistoryResultsList().isEmpty() && position != getItemCount() - 2);
     87     }
     88   }
     89 
     90   @Override
     91   public int getItemViewType(int position) {
     92     if (position == 0) { // Header
     93       return HEADER_VIEW_TYPE;
     94     } else if (position == getItemCount() - 1) {
     95       return FOOTER_VIEW_TYPE;
     96     } else {
     97       return CALL_ENTRY_VIEW_TYPE;
     98     }
     99   }
    100 
    101   @Override
    102   public int getItemCount() {
    103     return callDetailsEntries.size() + 2; // Header + footer
    104   }
    105 }
    106