• 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.provider.ContactsContract.Contacts;
21 import android.support.v7.widget.RecyclerView;
22 import android.support.v7.widget.helper.ItemTouchHelper;
23 import android.view.View;
24 import android.view.View.OnClickListener;
25 import android.view.View.OnLongClickListener;
26 import android.view.ViewConfiguration;
27 import android.widget.FrameLayout;
28 import android.widget.QuickContactBadge;
29 import android.widget.TextView;
30 import com.android.dialer.common.Assert;
31 import com.android.dialer.glidephotomanager.GlidePhotoManagerComponent;
32 import com.android.dialer.glidephotomanager.PhotoInfo;
33 import com.android.dialer.speeddial.database.SpeedDialEntry.Channel;
34 import com.android.dialer.speeddial.draghelper.SpeedDialFavoritesViewHolderOnTouchListener;
35 import com.android.dialer.speeddial.draghelper.SpeedDialFavoritesViewHolderOnTouchListener.OnTouchFinishCallback;
36 import com.android.dialer.speeddial.loader.SpeedDialUiItem;
37 
38 /** ViewHolder for starred/favorite contacts in {@link SpeedDialFragment}. */
39 public class FavoritesViewHolder extends RecyclerView.ViewHolder
40     implements OnClickListener, OnLongClickListener, OnTouchFinishCallback {
41 
42   private final FavoriteContactsListener listener;
43 
44   private final QuickContactBadge photoView;
45   private final TextView nameView;
46   private final TextView phoneType;
47   private final FrameLayout videoCallIcon;
48 
49   private final FrameLayout avatarContainer;
50 
51   private SpeedDialUiItem speedDialUiItem;
52 
FavoritesViewHolder(View view, ItemTouchHelper helper, FavoriteContactsListener listener)53   public FavoritesViewHolder(View view, ItemTouchHelper helper, 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     avatarContainer = view.findViewById(R.id.avatar_container);
60     view.setOnClickListener(this);
61     view.setOnLongClickListener(this);
62     view.setOnTouchListener(
63         new SpeedDialFavoritesViewHolderOnTouchListener(
64             ViewConfiguration.get(view.getContext()), helper, this, this));
65     photoView.setClickable(false);
66     this.listener = listener;
67   }
68 
bind(Context context, SpeedDialUiItem speedDialUiItem)69   public void bind(Context context, SpeedDialUiItem speedDialUiItem) {
70     this.speedDialUiItem = Assert.isNotNull(speedDialUiItem);
71     Assert.checkArgument(speedDialUiItem.isStarred());
72 
73     nameView.setText(speedDialUiItem.name());
74 
75     Channel channel = speedDialUiItem.defaultChannel();
76     if (channel == null) {
77       channel = speedDialUiItem.getDefaultVoiceChannel();
78     }
79 
80     if (channel != null) {
81       phoneType.setText(channel.label());
82       videoCallIcon.setVisibility(channel.isVideoTechnology() ? View.VISIBLE : View.GONE);
83     } else {
84       phoneType.setText("");
85       videoCallIcon.setVisibility(View.GONE);
86     }
87 
88     GlidePhotoManagerComponent.get(context)
89         .glidePhotoManager()
90         .loadQuickContactBadge(
91             photoView,
92             PhotoInfo.newBuilder()
93                 .setPhotoId(speedDialUiItem.photoId())
94                 .setPhotoUri(speedDialUiItem.photoUri())
95                 .setName(speedDialUiItem.name())
96                 .setLookupUri(
97                     Contacts.getLookupUri(speedDialUiItem.contactId(), speedDialUiItem.lookupKey())
98                         .toString())
99                 .build());
100   }
101 
102   @Override
onClick(View v)103   public void onClick(View v) {
104     if (speedDialUiItem.defaultChannel() != null) {
105       listener.onClick(speedDialUiItem.defaultChannel());
106     } else {
107       listener.onAmbiguousContactClicked(speedDialUiItem);
108     }
109   }
110 
111   @Override
onLongClick(View view)112   public boolean onLongClick(View view) {
113     // TODO(calderwoodra): add bounce/sin wave scale animation
114     listener.showContextMenu(photoView, speedDialUiItem);
115     return true;
116   }
117 
118   @Override
onTouchFinished(boolean closeContextMenu)119   public void onTouchFinished(boolean closeContextMenu) {
120     listener.onTouchFinished(closeContextMenu);
121   }
122 
getAvatarContainer()123   FrameLayout getAvatarContainer() {
124     return avatarContainer;
125   }
126 
onSelectedChanged(boolean selected)127   void onSelectedChanged(boolean selected) {
128     nameView.setVisibility(selected ? View.GONE : View.VISIBLE);
129     phoneType.setVisibility(selected ? View.GONE : View.VISIBLE);
130   }
131 
132   /** Listener/callback for {@link FavoritesViewHolder} actions. */
133   public interface FavoriteContactsListener {
134 
135     /** Called when the user clicks on a favorite contact that doesn't have a default number. */
onAmbiguousContactClicked(SpeedDialUiItem speedDialUiItem)136     void onAmbiguousContactClicked(SpeedDialUiItem speedDialUiItem);
137 
138     /** Called when the user clicks on a favorite contact. */
onClick(Channel channel)139     void onClick(Channel channel);
140 
141     /** Called when the user long clicks on a favorite contact. */
showContextMenu(View view, SpeedDialUiItem speedDialUiItem)142     void showContextMenu(View view, SpeedDialUiItem speedDialUiItem);
143 
144     /** Called when the user is no longer touching the favorite contact. */
onTouchFinished(boolean closeContextMenu)145     void onTouchFinished(boolean closeContextMenu);
146 
147     /** Called when the user drag the favorite to remove. */
onRequestRemove(SpeedDialUiItem speedDialUiItem)148     void onRequestRemove(SpeedDialUiItem speedDialUiItem);
149   }
150 }
151