• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.speeddial;
18 
19 import android.content.Context;
20 import android.support.v7.widget.RecyclerView;
21 import android.text.TextUtils;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.widget.TextView;
25 import com.android.dialer.common.Assert;
26 import com.android.dialer.historyitemactions.HistoryItemBottomSheetHeaderInfo;
27 import com.android.dialer.location.GeoUtil;
28 import com.android.dialer.phonenumberutil.PhoneNumberHelper;
29 import com.android.dialer.speeddial.database.SpeedDialEntry.Channel;
30 import com.android.dialer.speeddial.loader.SpeedDialUiItem;
31 import com.android.dialer.widget.ContactPhotoView;
32 
33 /** ViewHolder for displaying suggested contacts in {@link SpeedDialFragment}. */
34 public class SuggestionViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
35 
36   private final SuggestedContactsListener listener;
37 
38   private final ContactPhotoView photoView;
39   private final TextView nameOrNumberView;
40   private final TextView numberView;
41 
42   private SpeedDialUiItem speedDialUiItem;
43 
SuggestionViewHolder(View view, SuggestedContactsListener listener)44   SuggestionViewHolder(View view, SuggestedContactsListener listener) {
45     super(view);
46     photoView = view.findViewById(R.id.avatar);
47     nameOrNumberView = view.findViewById(R.id.name);
48     numberView = view.findViewById(R.id.number);
49     itemView.setOnClickListener(this);
50     view.findViewById(R.id.overflow).setOnClickListener(this);
51     this.listener = listener;
52   }
53 
bind(Context context, SpeedDialUiItem speedDialUiItem)54   public void bind(Context context, SpeedDialUiItem speedDialUiItem) {
55     Assert.isNotNull(speedDialUiItem.defaultChannel());
56     this.speedDialUiItem = speedDialUiItem;
57     String number =
58         PhoneNumberHelper.formatNumber(
59             context,
60             speedDialUiItem.defaultChannel().number(),
61             GeoUtil.getCurrentCountryIso(context));
62 
63     String label = speedDialUiItem.defaultChannel().label();
64     String secondaryInfo =
65         TextUtils.isEmpty(label)
66             ? number
67             : context.getString(R.string.call_subject_type_and_number, label, number);
68 
69     nameOrNumberView.setText(speedDialUiItem.name());
70     numberView.setText(secondaryInfo);
71 
72     photoView.setPhoto(speedDialUiItem.getPhotoInfo());
73   }
74 
75   @Override
onClick(View v)76   public void onClick(View v) {
77     if (v.getId() == R.id.overflow) {
78       listener.onOverFlowMenuClicked(speedDialUiItem, getHeaderInfo());
79     } else {
80       listener.onRowClicked(speedDialUiItem.defaultChannel());
81     }
82   }
83 
getHeaderInfo()84   private HistoryItemBottomSheetHeaderInfo getHeaderInfo() {
85     return HistoryItemBottomSheetHeaderInfo.newBuilder()
86         .setPhotoInfo(speedDialUiItem.getPhotoInfo())
87         .setPrimaryText(nameOrNumberView.getText().toString())
88         .setSecondaryText(numberView.getText().toString())
89         .build();
90   }
91 
92   /** Listener/Callback for {@link SuggestionViewHolder} parents. */
93   public interface SuggestedContactsListener {
94 
onOverFlowMenuClicked( SpeedDialUiItem speedDialUiItem, HistoryItemBottomSheetHeaderInfo headerInfo)95     void onOverFlowMenuClicked(
96         SpeedDialUiItem speedDialUiItem, HistoryItemBottomSheetHeaderInfo headerInfo);
97 
98     /** Called when a suggested contact is clicked. */
onRowClicked(Channel channel)99     void onRowClicked(Channel channel);
100   }
101 }
102