• 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.content.res.Resources;
21 import android.database.Cursor;
22 import android.net.Uri;
23 import android.provider.ContactsContract.CommonDataKinds.Phone;
24 import android.provider.ContactsContract.Contacts;
25 import android.support.v7.widget.RecyclerView;
26 import android.text.TextUtils;
27 import android.view.View;
28 import android.view.View.OnClickListener;
29 import android.view.View.OnLongClickListener;
30 import android.widget.FrameLayout;
31 import android.widget.QuickContactBadge;
32 import android.widget.TextView;
33 import com.android.dialer.common.Assert;
34 import com.android.dialer.contactphoto.ContactPhotoManager;
35 import com.android.dialer.lettertile.LetterTileDrawable;
36 
37 /** ViewHolder for starred/favorite contacts in {@link SpeedDialFragment}. */
38 public class FavoritesViewHolder extends RecyclerView.ViewHolder
39     implements OnClickListener, OnLongClickListener {
40 
41   private final FavoriteContactsListener listener;
42 
43   private final QuickContactBadge photoView;
44   private final TextView nameView;
45   private final TextView phoneType;
46   private final FrameLayout videoCallIcon;
47 
48   private boolean hasDefaultNumber;
49   private boolean isVideoCall;
50   private String number;
51   private String lookupKey;
52 
FavoritesViewHolder(View view, FavoriteContactsListener listener)53   public FavoritesViewHolder(View view, FavoriteContactsListener listener) {
54     super(view);
55     photoView = view.findViewById(R.id.avatar);
56     nameView = view.findViewById(R.id.name);
57     phoneType = view.findViewById(R.id.phone_type);
58     videoCallIcon = view.findViewById(R.id.video_call_container);
59     view.setOnClickListener(this);
60     view.setOnLongClickListener(this);
61     photoView.setClickable(false);
62     this.listener = listener;
63   }
64 
bind(Context context, Cursor cursor)65   public void bind(Context context, Cursor cursor) {
66     Assert.checkArgument(cursor.getInt(StrequentContactsCursorLoader.PHONE_STARRED) == 1);
67     isVideoCall = false; // TODO(calderwoodra): get from disambig data
68     number = cursor.getString(StrequentContactsCursorLoader.PHONE_NUMBER);
69 
70     String name = cursor.getString(StrequentContactsCursorLoader.PHONE_DISPLAY_NAME);
71     long contactId = cursor.getLong(StrequentContactsCursorLoader.PHONE_ID);
72     lookupKey = cursor.getString(StrequentContactsCursorLoader.PHONE_LOOKUP_KEY);
73     Uri contactUri = Contacts.getLookupUri(contactId, lookupKey);
74 
75     String photoUri = cursor.getString(StrequentContactsCursorLoader.PHONE_PHOTO_URI);
76     ContactPhotoManager.getInstance(context)
77         .loadDialerThumbnailOrPhoto(
78             photoView,
79             contactUri,
80             cursor.getLong(StrequentContactsCursorLoader.PHONE_PHOTO_ID),
81             photoUri == null ? null : Uri.parse(photoUri),
82             name,
83             LetterTileDrawable.TYPE_DEFAULT);
84     nameView.setText(name);
85     phoneType.setText(getLabel(context.getResources(), cursor));
86     videoCallIcon.setVisibility(isVideoCall ? View.VISIBLE : View.GONE);
87 
88     // TODO(calderwoodra): Update this to include communication avenues also
89     hasDefaultNumber = cursor.getInt(StrequentContactsCursorLoader.PHONE_IS_SUPER_PRIMARY) != 0;
90   }
91 
92   // TODO(calderwoodra): handle CNAP and cequint types.
93   // TODO(calderwoodra): unify this into a utility method with CallLogAdapter#getNumberType
getLabel(Resources resources, Cursor cursor)94   private static String getLabel(Resources resources, Cursor cursor) {
95     int numberType = cursor.getInt(StrequentContactsCursorLoader.PHONE_TYPE);
96     String numberLabel = cursor.getString(StrequentContactsCursorLoader.PHONE_LABEL);
97 
98     // Returns empty label instead of "custom" if the custom label is empty.
99     if (numberType == Phone.TYPE_CUSTOM && TextUtils.isEmpty(numberLabel)) {
100       return "";
101     }
102     return (String) Phone.getTypeLabel(resources, numberType, numberLabel);
103   }
104 
105   @Override
onClick(View v)106   public void onClick(View v) {
107     if (hasDefaultNumber) {
108       listener.onClick(number, isVideoCall);
109     } else {
110       listener.onAmbiguousContactClicked(lookupKey);
111     }
112   }
113 
114   @Override
onLongClick(View v)115   public boolean onLongClick(View v) {
116     // TODO(calderwoodra): implement drag and drop logic
117     listener.onLongClick(number);
118     return true;
119   }
120 
121   /** Listener/callback for {@link FavoritesViewHolder} actions. */
122   public interface FavoriteContactsListener {
123 
124     /** Called when the user clicks on a favorite contact that doesn't have a default number. */
onAmbiguousContactClicked(String contactId)125     void onAmbiguousContactClicked(String contactId);
126 
127     /** Called when the user clicks on a favorite contact. */
onClick(String number, boolean isVideoCall)128     void onClick(String number, boolean isVideoCall);
129 
130     /** Called when the user long clicks on a favorite contact. */
onLongClick(String number)131     void onLongClick(String number);
132   }
133 }
134