1/*
2 * Copyright (C) 2015 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 */
16package com.android.dialer.app.filterednumber;
17
18import android.app.FragmentManager;
19import android.content.Context;
20import android.provider.ContactsContract;
21import android.provider.ContactsContract.CommonDataKinds.Phone;
22import android.text.BidiFormatter;
23import android.text.TextDirectionHeuristics;
24import android.text.TextUtils;
25import android.view.View;
26import android.widget.QuickContactBadge;
27import android.widget.SimpleCursorAdapter;
28import android.widget.TextView;
29import com.android.dialer.app.R;
30import com.android.dialer.compat.CompatUtils;
31import com.android.dialer.contactphoto.ContactPhotoManager;
32import com.android.dialer.contactphoto.ContactPhotoManager.DefaultImageRequest;
33import com.android.dialer.lettertile.LetterTileDrawable;
34import com.android.dialer.phonenumbercache.ContactInfo;
35import com.android.dialer.phonenumbercache.ContactInfoHelper;
36import com.android.dialer.phonenumberutil.PhoneNumberHelper;
37import com.android.dialer.util.UriUtils;
38
39/** TODO(calderwoodra): documentation */
40public class NumbersAdapter extends SimpleCursorAdapter {
41
42  private final Context context;
43  private final FragmentManager fragmentManager;
44  private final ContactInfoHelper contactInfoHelper;
45  private final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
46  private final ContactPhotoManager contactPhotoManager;
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    this.context = context;
55    this.fragmentManager = fragmentManager;
56    this.contactInfoHelper = contactInfoHelper;
57    this.contactPhotoManager = contactPhotoManager;
58  }
59
60  public void updateView(View view, String number, String countryIso) {
61    final TextView callerName = (TextView) view.findViewById(R.id.caller_name);
62    final TextView callerNumber = (TextView) view.findViewById(R.id.caller_number);
63    final QuickContactBadge quickContactBadge =
64        (QuickContactBadge) view.findViewById(R.id.quick_contact_photo);
65    quickContactBadge.setOverlay(null);
66    if (CompatUtils.hasPrioritizedMimeType()) {
67      quickContactBadge.setPrioritizedMimeType(Phone.CONTENT_ITEM_TYPE);
68    }
69
70    ContactInfo info = contactInfoHelper.lookupNumber(number, countryIso);
71    if (info == null) {
72      info = new ContactInfo();
73      info.number = number;
74    }
75    final CharSequence locationOrType = getNumberTypeOrLocation(info, countryIso);
76    final String displayNumber = getDisplayNumber(info);
77    final String displayNumberStr =
78        bidiFormatter.unicodeWrap(displayNumber, TextDirectionHeuristics.LTR);
79
80    String nameForDefaultImage;
81    if (!TextUtils.isEmpty(info.name)) {
82      nameForDefaultImage = info.name;
83      callerName.setText(info.name);
84      callerNumber.setText(locationOrType + " " + displayNumberStr);
85    } else {
86      nameForDefaultImage = displayNumber;
87      callerName.setText(displayNumberStr);
88      if (!TextUtils.isEmpty(locationOrType)) {
89        callerNumber.setText(locationOrType);
90        callerNumber.setVisibility(View.VISIBLE);
91      } else {
92        callerNumber.setVisibility(View.GONE);
93      }
94    }
95    loadContactPhoto(info, nameForDefaultImage, quickContactBadge);
96  }
97
98  private void loadContactPhoto(ContactInfo info, String displayName, QuickContactBadge badge) {
99    final String lookupKey =
100        info.lookupUri == null ? null : UriUtils.getLookupKeyFromUri(info.lookupUri);
101    final int contactType =
102        contactInfoHelper.isBusiness(info.sourceType)
103            ? LetterTileDrawable.TYPE_BUSINESS
104            : LetterTileDrawable.TYPE_DEFAULT;
105    final DefaultImageRequest request =
106        new DefaultImageRequest(displayName, lookupKey, contactType, true /* isCircular */);
107    badge.assignContactUri(info.lookupUri);
108    badge.setContentDescription(
109        context.getResources().getString(R.string.description_contact_details, displayName));
110    contactPhotoManager.loadDirectoryPhoto(
111        badge, info.photoUri, false /* darkTheme */, true /* isCircular */, request);
112  }
113
114  private String getDisplayNumber(ContactInfo info) {
115    if (!TextUtils.isEmpty(info.formattedNumber)) {
116      return info.formattedNumber;
117    } else if (!TextUtils.isEmpty(info.number)) {
118      return info.number;
119    } else {
120      return "";
121    }
122  }
123
124  private CharSequence getNumberTypeOrLocation(ContactInfo info, String countryIso) {
125    if (!TextUtils.isEmpty(info.name)) {
126      return ContactsContract.CommonDataKinds.Phone.getTypeLabel(
127          context.getResources(), info.type, info.label);
128    } else {
129      return PhoneNumberHelper.getGeoDescription(context, info.number, countryIso);
130    }
131  }
132
133  protected Context getContext() {
134    return context;
135  }
136
137  protected FragmentManager getFragmentManager() {
138    return fragmentManager;
139  }
140}
141