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 class ContactDetailsAdapter extends RecyclerView.Adapter<ContactDetailsViewHolder> { 37 38 private static final String TAG = "CD.ContactDetailsAdapter"; 39 40 private static final int ID_HEADER = 1; 41 private static final int ID_NUMBER = 2; 42 private static final int ID_ADDRESS = 3; 43 44 interface PhoneNumberPresenter { onClick(Contact contact, PhoneNumber phoneNumber)45 void onClick(Contact contact, PhoneNumber phoneNumber); 46 } 47 48 private final Context mContext; 49 private final PhoneNumberPresenter mPhoneNumberPresenter; 50 private final ArrayList<Object> mItems = new ArrayList<>(); 51 private Contact mContact; 52 ContactDetailsAdapter( @onNull Context context, @Nullable Contact contact, @NonNull PhoneNumberPresenter phoneNumberPresenter)53 ContactDetailsAdapter( 54 @NonNull Context context, 55 @Nullable Contact contact, 56 @NonNull PhoneNumberPresenter phoneNumberPresenter) { 57 super(); 58 mContext = context; 59 mPhoneNumberPresenter = phoneNumberPresenter; 60 setContact(contact); 61 } 62 setContact(Contact contact)63 void setContact(Contact contact) { 64 L.d(TAG, "setContact %s", contact); 65 mContact = contact; 66 mItems.clear(); 67 mItems.add(contact); 68 if (contact != null) { 69 mItems.addAll(contact.getNumbers()); 70 if (mContext.getResources().getBoolean(R.bool.config_show_postal_address)) { 71 mItems.addAll(contact.getPostalAddresses()); 72 } 73 } 74 notifyDataSetChanged(); 75 } 76 77 @Override getItemViewType(int position)78 public int getItemViewType(int position) { 79 Object obj = mItems.get(position); 80 if (obj == null || obj instanceof Contact) { 81 return ID_HEADER; 82 } else if (obj instanceof PhoneNumber) { 83 return ID_NUMBER; 84 } else if (obj instanceof PostalAddress) { 85 return ID_ADDRESS; 86 } else { 87 L.w(TAG, "Unknown view type"); 88 return -1; 89 } 90 } 91 92 @Override getItemCount()93 public int getItemCount() { 94 return mItems.size(); 95 } 96 97 @Override onCreateViewHolder(ViewGroup parent, int viewType)98 public ContactDetailsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 99 int layoutResId; 100 switch (viewType) { 101 case ID_HEADER: 102 layoutResId = R.layout.contact_details_name_image; 103 break; 104 case ID_NUMBER: 105 layoutResId = R.layout.contact_details_number; 106 break; 107 case ID_ADDRESS: 108 layoutResId = R.layout.contact_details_address; 109 break; 110 default: 111 L.w(TAG, "Unknown view type: %d", viewType); 112 return null; 113 } 114 115 View view = LayoutInflater.from(parent.getContext()).inflate(layoutResId, parent, 116 false); 117 return new ContactDetailsViewHolder(view, mPhoneNumberPresenter); 118 } 119 120 @Override onBindViewHolder(ContactDetailsViewHolder viewHolder, int position)121 public void onBindViewHolder(ContactDetailsViewHolder viewHolder, int position) { 122 switch (viewHolder.getItemViewType()) { 123 case ID_HEADER: 124 viewHolder.bind(mContext, (Contact) mItems.get(position)); 125 break; 126 case ID_NUMBER: 127 viewHolder.bind(mContext, mContact, (PhoneNumber) mItems.get(position)); 128 break; 129 case ID_ADDRESS: 130 viewHolder.bind(mContext, (PostalAddress) mItems.get(position)); 131 break; 132 default: 133 L.w(TAG, "Unknown view type %d ", viewHolder.getItemViewType()); 134 return; 135 } 136 } 137 } 138