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.contact; 18 19 import android.content.Context; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 24 import androidx.annotation.NonNull; 25 import androidx.annotation.Nullable; 26 import androidx.recyclerview.widget.RecyclerView; 27 28 import com.android.car.dialer.R; 29 import com.android.car.dialer.log.L; 30 import com.android.car.telephony.common.Contact; 31 import com.android.car.telephony.common.PhoneNumber; 32 import com.android.car.telephony.common.PostalAddress; 33 34 import java.util.ArrayList; 35 36 import dagger.assisted.Assisted; 37 import dagger.assisted.AssistedFactory; 38 import dagger.assisted.AssistedInject; 39 import dagger.hilt.android.qualifiers.ActivityContext; 40 41 class ContactDetailsAdapter extends RecyclerView.Adapter<ContactDetailsViewHolder> { 42 43 private static final String TAG = "CD.ContactDetailsAdapter"; 44 45 private static final int ID_HEADER = 1; 46 private static final int ID_NUMBER = 2; 47 private static final int ID_ADDRESS = 3; 48 49 interface PhoneNumberPresenter { onClick(Contact contact, PhoneNumber phoneNumber)50 void onClick(Contact contact, PhoneNumber phoneNumber); 51 } 52 53 private final Context mContext; 54 private final ContactDetailsViewHolder.Factory mViewHolderFactory; 55 private final PhoneNumberPresenter mPhoneNumberPresenter; 56 private final ArrayList<Object> mItems = new ArrayList<>(); 57 private Contact mContact; 58 59 @AssistedInject ContactDetailsAdapter( @ctivityContext @onNull Context context, ContactDetailsViewHolder.Factory viewHolderFactory, @Assisted @Nullable Contact contact, @Assisted @NonNull PhoneNumberPresenter phoneNumberPresenter)60 ContactDetailsAdapter( 61 @ActivityContext @NonNull Context context, 62 ContactDetailsViewHolder.Factory viewHolderFactory, 63 @Assisted @Nullable Contact contact, 64 @Assisted @NonNull PhoneNumberPresenter phoneNumberPresenter) { 65 super(); 66 mContext = context; 67 mViewHolderFactory = viewHolderFactory; 68 mPhoneNumberPresenter = phoneNumberPresenter; 69 setContact(contact); 70 } 71 setContact(Contact contact)72 void setContact(Contact contact) { 73 L.d(TAG, "setContact %s", contact); 74 mContact = contact; 75 mItems.clear(); 76 77 boolean showHeader = mContext.getResources() 78 .getBoolean(R.bool.config_show_contact_details_header); 79 80 if (showHeader || contact == null) { 81 // We add null contacts to display an error message 82 mItems.add(contact); 83 } 84 if (contact != null) { 85 mItems.addAll(contact.getNumbers()); 86 if (mContext.getResources().getBoolean(R.bool.config_show_postal_address)) { 87 mItems.addAll(contact.getPostalAddresses()); 88 } 89 } 90 notifyDataSetChanged(); 91 } 92 93 @Override getItemViewType(int position)94 public int getItemViewType(int position) { 95 Object obj = mItems.get(position); 96 if (obj == null || obj instanceof Contact) { 97 return ID_HEADER; 98 } else if (obj instanceof PhoneNumber) { 99 return ID_NUMBER; 100 } else if (obj instanceof PostalAddress) { 101 return ID_ADDRESS; 102 } else { 103 L.w(TAG, "Unknown view type"); 104 return -1; 105 } 106 } 107 108 @Override getItemCount()109 public int getItemCount() { 110 return mItems.size(); 111 } 112 113 @Override onCreateViewHolder(ViewGroup parent, int viewType)114 public ContactDetailsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 115 int layoutResId; 116 switch (viewType) { 117 case ID_HEADER: 118 layoutResId = R.layout.contact_details_name_image; 119 break; 120 case ID_NUMBER: 121 layoutResId = R.layout.contact_details_number; 122 break; 123 case ID_ADDRESS: 124 layoutResId = R.layout.contact_details_address; 125 break; 126 default: 127 L.w(TAG, "Unknown view type: %d", viewType); 128 return null; 129 } 130 131 View view = LayoutInflater.from(parent.getContext()).inflate(layoutResId, parent, 132 false); 133 return mViewHolderFactory.create(view, mPhoneNumberPresenter); 134 } 135 136 @Override onBindViewHolder(ContactDetailsViewHolder viewHolder, int position)137 public void onBindViewHolder(ContactDetailsViewHolder viewHolder, int position) { 138 switch (viewHolder.getItemViewType()) { 139 case ID_HEADER: 140 viewHolder.bind(mContext, (Contact) mItems.get(position)); 141 break; 142 case ID_NUMBER: 143 viewHolder.bind(mContext, mContact, (PhoneNumber) mItems.get(position)); 144 break; 145 case ID_ADDRESS: 146 viewHolder.bind(mContext, (PostalAddress) mItems.get(position)); 147 break; 148 default: 149 L.w(TAG, "Unknown view type %d ", viewHolder.getItemViewType()); 150 return; 151 } 152 } 153 154 /** 155 * Factory to create {@link ContactDetailsAdapter} instances via the {@link AssistedInject} 156 * constructor. 157 */ 158 @AssistedFactory 159 interface Factory { create(@ullable Contact contact, @NonNull PhoneNumberPresenter phoneNumberPresenter)160 ContactDetailsAdapter create(@Nullable Contact contact, 161 @NonNull PhoneNumberPresenter phoneNumberPresenter); 162 } 163 } 164