Home | History | Annotate | Download | only in filterednumber
      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  */
     16 package com.android.dialer.app.filterednumber;
     17 
     18 import android.app.FragmentManager;
     19 import android.content.Context;
     20 import android.provider.ContactsContract;
     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 import com.android.dialer.app.R;
     30 import com.android.dialer.compat.CompatUtils;
     31 import com.android.dialer.contactphoto.ContactPhotoManager;
     32 import com.android.dialer.contactphoto.ContactPhotoManager.DefaultImageRequest;
     33 import com.android.dialer.lettertile.LetterTileDrawable;
     34 import com.android.dialer.phonenumbercache.ContactInfo;
     35 import com.android.dialer.phonenumbercache.ContactInfoHelper;
     36 import com.android.dialer.phonenumberutil.PhoneNumberHelper;
     37 import com.android.dialer.util.UriUtils;
     38 
     39 /** TODO(calderwoodra): documentation */
     40 public 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