Wenyi Wang | 1fc3ef4 | 2016-01-14 18:21:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.contacts.callblocking; |
| 18 | |
| 19 | import android.app.FragmentManager; |
| 20 | import android.content.Context; |
| 21 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 22 | import android.text.BidiFormatter; |
| 23 | import android.text.TextDirectionHeuristics; |
| 24 | import android.text.TextUtils; |
| 25 | import android.view.View; |
| 26 | import android.widget.QuickContactBadge; |
| 27 | import android.widget.SimpleCursorAdapter; |
| 28 | import android.widget.TextView; |
| 29 | |
| 30 | import com.android.contacts.R; |
| 31 | import com.android.contacts.callblocking.ContactInfo; |
| 32 | import com.android.contacts.callblocking.ContactInfoHelper; |
| 33 | import com.android.contacts.common.ContactPhotoManager; |
| 34 | import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest; |
| 35 | import com.android.contacts.common.compat.CompatUtils; |
| 36 | import com.android.contacts.common.util.UriUtils; |
| 37 | |
| 38 | import java.util.Locale; |
| 39 | |
| 40 | public class NumbersAdapter extends SimpleCursorAdapter { |
| 41 | |
| 42 | private Context mContext; |
| 43 | private FragmentManager mFragmentManager; |
| 44 | private ContactInfoHelper mContactInfoHelper; |
| 45 | private BidiFormatter mBidiFormatter = BidiFormatter.getInstance(); |
| 46 | private ContactPhotoManager mContactPhotoManager; |
| 47 | |
| 48 | public NumbersAdapter( |
| 49 | Context context, |
| 50 | FragmentManager fragmentManager, |
| 51 | ContactInfoHelper contactInfoHelper, |
| 52 | ContactPhotoManager contactPhotoManager) { |
| 53 | super(context, R.layout.blocked_number_item, null, new String[]{}, new int[]{}, 0); |
| 54 | mContext = context; |
| 55 | mFragmentManager = fragmentManager; |
| 56 | mContactInfoHelper = contactInfoHelper; |
| 57 | mContactPhotoManager = contactPhotoManager; |
| 58 | } |
| 59 | |
| 60 | public void updateView(View view, String number, String countryIso) { |
| 61 | // TODO: add touch feedback on list item. |
| 62 | final TextView callerName = (TextView) view.findViewById(R.id.caller_name); |
| 63 | final TextView callerNumber = (TextView) view.findViewById(R.id.caller_number); |
| 64 | final QuickContactBadge quickContactBadge = |
| 65 | (QuickContactBadge) view.findViewById(R.id.quick_contact_photo); |
| 66 | quickContactBadge.setOverlay(null); |
| 67 | if (CompatUtils.hasPrioritizedMimeType()) { |
| 68 | quickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE); |
| 69 | } |
| 70 | |
| 71 | ContactInfo info = mContactInfoHelper.lookupNumber(number, countryIso); |
| 72 | if (info == null) { |
| 73 | info = new ContactInfo(); |
| 74 | info.number = number; |
| 75 | } |
| 76 | final CharSequence locationOrType = getNumberTypeOrLocation(info); |
| 77 | final String displayNumber = getDisplayNumber(info); |
| 78 | final String displayNumberStr = mBidiFormatter.unicodeWrap(displayNumber, |
| 79 | TextDirectionHeuristics.LTR); |
| 80 | |
| 81 | String nameForDefaultImage; |
| 82 | if (!TextUtils.isEmpty(info.name)) { |
| 83 | nameForDefaultImage = info.name; |
| 84 | callerName.setText(info.name); |
| 85 | callerNumber.setText(locationOrType + " " + displayNumberStr); |
| 86 | } else { |
| 87 | nameForDefaultImage = displayNumber; |
| 88 | callerName.setText(displayNumberStr); |
| 89 | if (!TextUtils.isEmpty(locationOrType)) { |
| 90 | callerNumber.setText(locationOrType); |
| 91 | callerNumber.setVisibility(View.VISIBLE); |
| 92 | } else { |
| 93 | callerNumber.setVisibility(View.GONE); |
| 94 | } |
| 95 | } |
| 96 | loadContactPhoto(info, nameForDefaultImage, quickContactBadge); |
| 97 | } |
| 98 | |
| 99 | private void loadContactPhoto(ContactInfo info, String displayName, QuickContactBadge badge) { |
| 100 | final String lookupKey = info.lookupUri == null |
| 101 | ? null : UriUtils.getLookupKeyFromUri(info.lookupUri); |
| 102 | final DefaultImageRequest request = new DefaultImageRequest(displayName, lookupKey, |
| 103 | ContactPhotoManager.TYPE_DEFAULT, /* isCircular */ true); |
| 104 | badge.assignContactUri(info.lookupUri); |
| 105 | badge.setContentDescription( |
| 106 | mContext.getResources().getString(R.string.description_contact_details, displayName)); |
| 107 | mContactPhotoManager.loadDirectoryPhoto(badge, info.photoUri, |
| 108 | /* darkTheme */ false, /* isCircular */ true, request); |
| 109 | } |
| 110 | |
| 111 | private String getDisplayNumber(ContactInfo info) { |
| 112 | if (!TextUtils.isEmpty(info.formattedNumber)) { |
| 113 | return info.formattedNumber; |
| 114 | } else if (!TextUtils.isEmpty(info.number)) { |
| 115 | return info.number; |
| 116 | } else { |
| 117 | return ""; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private CharSequence getNumberTypeOrLocation(ContactInfo info) { |
| 122 | if (!TextUtils.isEmpty(info.name)) { |
| 123 | return Phone.getTypeLabel( |
| 124 | mContext.getResources(), info.type, info.label); |
| 125 | } else { |
| 126 | return FilteredNumbersUtil.getGeoDescription(mContext, info.number); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | protected Context getContext() { |
| 131 | return mContext; |
| 132 | } |
| 133 | |
| 134 | protected FragmentManager getFragmentManager() { |
| 135 | return mFragmentManager; |
| 136 | } |
| 137 | } |