CallDetailsAdapter.java revision 1019500220518fb5fb023fcb7d370ab3cbf12307
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
17package com.android.dialer.calldetails;
18
19import android.content.Context;
20import android.support.v7.widget.RecyclerView;
21import android.support.v7.widget.RecyclerView.ViewHolder;
22import android.view.LayoutInflater;
23import android.view.ViewGroup;
24import com.android.dialer.callcomposer.nano.CallComposerContact;
25import com.android.dialer.calldetails.nano.CallDetailsEntries.CallDetailsEntry;
26import com.android.dialer.calllogutils.CallTypeHelper;
27import com.android.dialer.common.Assert;
28
29/** Adapter for RecyclerView in {@link CallDetailsActivity}. */
30public class CallDetailsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
31
32  private static final int HEADER_VIEW_TYPE = 1;
33  private static final int CALL_ENTRY_VIEW_TYPE = 2;
34  private static final int FOOTER_VIEW_TYPE = 3;
35
36  private final CallComposerContact contact;
37  private final CallDetailsEntry[] callDetailsEntries;
38  private final CallTypeHelper callTypeHelper;
39
40  public CallDetailsAdapter(
41      Context context, CallComposerContact contact, CallDetailsEntry[] callDetailsEntries) {
42    this.contact = Assert.isNotNull(contact);
43    this.callDetailsEntries = Assert.isNotNull(callDetailsEntries);
44    callTypeHelper = new CallTypeHelper(context.getResources());
45  }
46
47  @Override
48  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
49    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
50    switch (viewType) {
51      case HEADER_VIEW_TYPE:
52        return new CallDetailsHeaderViewHolder(
53            inflater.inflate(R.layout.contact_container, parent, false));
54      case CALL_ENTRY_VIEW_TYPE:
55        return new CallDetailsEntryViewHolder(
56            inflater.inflate(R.layout.call_details_entry, parent, false));
57      case FOOTER_VIEW_TYPE:
58        return new CallDetailsFooterViewHolder(
59            inflater.inflate(R.layout.call_details_footer, parent, false));
60      default:
61        Assert.fail("No ViewHolder available for viewType: " + viewType);
62        return null;
63    }
64  }
65
66  @Override
67  public void onBindViewHolder(ViewHolder holder, int position) {
68    if (position == 0) { // Header
69      ((CallDetailsHeaderViewHolder) holder).updateContactInfo(contact);
70    } else if (position == getItemCount() - 1) {
71      ((CallDetailsFooterViewHolder) holder).setPhoneNumber(contact.number);
72    } else {
73      CallDetailsEntryViewHolder viewHolder = (CallDetailsEntryViewHolder) holder;
74      CallDetailsEntry entry = callDetailsEntries[position - 1];
75      viewHolder.setCallDetails(
76          contact.number,
77          entry,
78          callTypeHelper,
79          entry.historyResults.length > 0 && position != getItemCount() - 2);
80    }
81  }
82
83  @Override
84  public int getItemViewType(int position) {
85    if (position == 0) { // Header
86      return HEADER_VIEW_TYPE;
87    } else if (position == getItemCount() - 1) {
88      return FOOTER_VIEW_TYPE;
89    } else {
90      return CALL_ENTRY_VIEW_TYPE;
91    }
92  }
93
94  @Override
95  public int getItemCount() {
96    return callDetailsEntries.length + 2; // Header + footer
97  }
98}
99