1 /* 2 * Copyright (C) 2018 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.car.dialer.ui.favorite; 18 19 import android.view.LayoutInflater; 20 import android.view.View; 21 import android.view.ViewGroup; 22 23 import androidx.recyclerview.widget.RecyclerView; 24 25 import com.android.car.dialer.R; 26 import com.android.car.dialer.log.L; 27 import com.android.car.dialer.ui.common.OnItemClickedListener; 28 import com.android.car.dialer.ui.common.entity.Header; 29 import com.android.car.telephony.common.Contact; 30 31 import java.util.Collections; 32 import java.util.List; 33 34 /** 35 * Adapter class for binding favorite contacts. 36 */ 37 public class FavoriteAdapter extends RecyclerView.Adapter<FavoriteContactViewHolder> { 38 private static final String TAG = "CD.FavoriteAdapter"; 39 static final int TYPE_CONTACT = 0; 40 static final int TYPE_HEADER = 1; 41 static final int TYPE_ADD_FAVORITE = 2; 42 43 /** 44 * Listener interface for when the add favorite button is clicked 45 */ 46 public interface OnAddFavoriteClickedListener { 47 /** 48 * Called when the add favorite button is clicked 49 */ onAddFavoriteClicked()50 void onAddFavoriteClicked(); 51 } 52 53 private List<Object> mFavoriteContacts = Collections.emptyList(); 54 private OnItemClickedListener<Contact> mListener; 55 private OnAddFavoriteClickedListener mAddFavoriteListener; 56 57 /** 58 * Sets the favorite contact list. 59 */ setFavoriteContacts(List<Object> favoriteContacts)60 public void setFavoriteContacts(List<Object> favoriteContacts) { 61 L.d(TAG, "setFavoriteContacts %s", favoriteContacts); 62 mFavoriteContacts = (favoriteContacts != null) ? favoriteContacts : Collections.emptyList(); 63 notifyDataSetChanged(); 64 } 65 66 @Override getItemCount()67 public int getItemCount() { 68 return mFavoriteContacts.size(); 69 } 70 71 @Override getItemViewType(int position)72 public int getItemViewType(int position) { 73 Object item = mFavoriteContacts.get(position); 74 if (item instanceof Contact) { 75 return TYPE_CONTACT; 76 } else if (item instanceof Header) { 77 return TYPE_HEADER; 78 } else { 79 return TYPE_ADD_FAVORITE; 80 } 81 } 82 83 @Override onCreateViewHolder(ViewGroup parent, int viewType)84 public FavoriteContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 85 int layoutId; 86 if (viewType == TYPE_CONTACT) { 87 layoutId = R.layout.favorite_contact_list_item; 88 } else if (viewType == TYPE_HEADER) { 89 layoutId = R.layout.header_item; 90 } else { 91 layoutId = R.layout.add_favorite_list_item; 92 } 93 94 View view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false); 95 return new FavoriteContactViewHolder(view); 96 } 97 98 @Override onBindViewHolder(FavoriteContactViewHolder viewHolder, int position)99 public void onBindViewHolder(FavoriteContactViewHolder viewHolder, int position) { 100 int itemViewType = getItemViewType(position); 101 switch (itemViewType) { 102 case TYPE_CONTACT: 103 Contact contact = (Contact) mFavoriteContacts.get(position); 104 viewHolder.onBind(contact); 105 viewHolder.itemView.setOnClickListener(v -> onItemViewClicked(contact)); 106 break; 107 case TYPE_HEADER: 108 Header header = (Header) mFavoriteContacts.get(position); 109 viewHolder.onBind(header); 110 viewHolder.itemView.setOnClickListener(null); 111 break; 112 case TYPE_ADD_FAVORITE: 113 viewHolder.itemView.setOnClickListener(v -> { 114 if (mAddFavoriteListener != null) { 115 mAddFavoriteListener.onAddFavoriteClicked(); 116 } 117 }); 118 break; 119 } 120 } 121 onItemViewClicked(Contact contact)122 private void onItemViewClicked(Contact contact) { 123 if (mListener != null) { 124 mListener.onItemClicked(contact); 125 } 126 } 127 128 /** 129 * Sets a {@link OnItemClickedListener listener} which will be called when an item is clicked. 130 */ setOnListItemClickedListener(OnItemClickedListener<Contact> listener)131 public void setOnListItemClickedListener(OnItemClickedListener<Contact> listener) { 132 mListener = listener; 133 } 134 135 /** 136 * Sets a {@link OnAddFavoriteClickedListener listener} which will be called when the "Add 137 * favorite" button is clicked. 138 */ setOnAddFavoriteClickedListener(OnAddFavoriteClickedListener listener)139 public void setOnAddFavoriteClickedListener(OnAddFavoriteClickedListener listener) { 140 mAddFavoriteListener = listener; 141 } 142 } 143