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.contactsfragment;
18 
19 import android.content.Context;
20 import android.net.Uri;
21 import android.support.v7.widget.RecyclerView;
22 import android.text.TextUtils;
23 import android.view.View;
24 import android.view.View.OnClickListener;
25 import android.widget.QuickContactBadge;
26 import android.widget.TextView;
27 import com.android.dialer.common.Assert;
28 import com.android.dialer.contactsfragment.ContactsFragment.OnContactSelectedListener;
29 import com.android.dialer.logging.InteractionEvent;
30 import com.android.dialer.logging.Logger;
31 import com.android.dialer.widget.BidiTextView;
32 
33 /** View holder for a contact. */
34 final class ContactViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
35 
36   private final TextView header;
37   private final BidiTextView name;
38   private final QuickContactBadge photo;
39   private final Context context;
40   private final OnContactSelectedListener onContactSelectedListener;
41 
42   private String headerText;
43   private Uri contactUri;
44   private long contactId;
45 
ContactViewHolder(View itemView, OnContactSelectedListener onContactSelectedListener)46   ContactViewHolder(View itemView, OnContactSelectedListener onContactSelectedListener) {
47     super(itemView);
48     this.onContactSelectedListener = Assert.isNotNull(onContactSelectedListener);
49     context = itemView.getContext();
50     itemView.findViewById(R.id.click_target).setOnClickListener(this);
51     header = itemView.findViewById(R.id.header);
52     name = itemView.findViewById(R.id.contact_name);
53     photo = itemView.findViewById(R.id.photo);
54   }
55 
56   /**
57    * Binds the ViewHolder with relevant data.
58    *
59    * @param headerText populates the header view.
60    * @param displayName populates the name view.
61    * @param contactUri to be shown by the contact card on photo click.
62    * @param showHeader if header view should be shown {@code True}, {@code False} otherwise.
63    */
bind( String headerText, String displayName, Uri contactUri, long contactId, boolean showHeader)64   public void bind(
65       String headerText, String displayName, Uri contactUri, long contactId, boolean showHeader) {
66     Assert.checkArgument(!TextUtils.isEmpty(displayName));
67     this.contactUri = contactUri;
68     this.contactId = contactId;
69     this.headerText = headerText;
70 
71     name.setText(displayName);
72     header.setText(headerText);
73     header.setVisibility(showHeader ? View.VISIBLE : View.INVISIBLE);
74 
75     Logger.get(context)
76         .logQuickContactOnTouch(
77             photo, InteractionEvent.Type.OPEN_QUICK_CONTACT_FROM_CONTACTS_FRAGMENT_BADGE, true);
78   }
79 
getPhoto()80   public QuickContactBadge getPhoto() {
81     return photo;
82   }
83 
getHeader()84   public String getHeader() {
85     return headerText;
86   }
87 
getHeaderView()88   public TextView getHeaderView() {
89     return header;
90   }
91 
92   @Override
onClick(View v)93   public void onClick(View v) {
94     onContactSelectedListener.onContactSelected(photo, contactUri, contactId);
95   }
96 }
97