1 /*
2  * Copyright (C) 2011 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.dialer.calllog;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.provider.CallLog.Calls;
22 import android.telecom.PhoneAccountHandle;
23 import android.text.TextUtils;
24 import android.util.Log;
25 
26 import com.android.dialer.R;
27 
28 /**
29  * Helper for formatting and managing the display of phone numbers.
30  */
31 public class PhoneNumberDisplayHelper {
32     private final Context mContext;
33     private final Resources mResources;
34     private final PhoneNumberUtilsWrapper mPhoneNumberUtilsWrapper;
35 
PhoneNumberDisplayHelper(Context context, Resources resources)36     public PhoneNumberDisplayHelper(Context context, Resources resources) {
37         mContext = context;
38         mResources = resources;
39         mPhoneNumberUtilsWrapper = new PhoneNumberUtilsWrapper(context);
40     }
41 
PhoneNumberDisplayHelper(Context context, Resources resources, PhoneNumberUtilsWrapper phoneNumberUtils)42     public PhoneNumberDisplayHelper(Context context, Resources resources,
43             PhoneNumberUtilsWrapper phoneNumberUtils) {
44         mContext = context;
45         mResources = resources;
46         mPhoneNumberUtilsWrapper = phoneNumberUtils;
47     }
48 
getDisplayName(PhoneAccountHandle accountHandle, CharSequence number, int presentation)49     /* package */ CharSequence getDisplayName(PhoneAccountHandle accountHandle, CharSequence number,
50             int presentation) {
51         if (presentation == Calls.PRESENTATION_UNKNOWN) {
52             return mResources.getString(R.string.unknown);
53         }
54         if (presentation == Calls.PRESENTATION_RESTRICTED) {
55             return mResources.getString(R.string.private_num);
56         }
57         if (presentation == Calls.PRESENTATION_PAYPHONE) {
58             return mResources.getString(R.string.payphone);
59         }
60         if (mPhoneNumberUtilsWrapper.isVoicemailNumber(accountHandle, number)) {
61             return mResources.getString(R.string.voicemail);
62         }
63         if (PhoneNumberUtilsWrapper.isLegacyUnknownNumbers(number)) {
64             return mResources.getString(R.string.unknown);
65         }
66         return "";
67     }
68 
69     /**
70      * Returns the string to display for the given phone number.
71      *
72      * @param accountHandle The handle for the account corresponding to the call
73      * @param number the number to display
74      * @param formattedNumber the formatted number if available, may be null
75      */
getDisplayNumber(PhoneAccountHandle accountHandle, CharSequence number, int presentation, CharSequence formattedNumber)76     public CharSequence getDisplayNumber(PhoneAccountHandle accountHandle, CharSequence number,
77             int presentation, CharSequence formattedNumber) {
78 
79         final CharSequence displayName = getDisplayName(accountHandle, number, presentation);
80         if (!TextUtils.isEmpty(displayName)) {
81             return displayName;
82         }
83 
84         if (TextUtils.isEmpty(number)) {
85             return "";
86         }
87 
88         if (TextUtils.isEmpty(formattedNumber)) {
89             return number;
90         } else {
91             return formattedNumber;
92         }
93     }
94 }
95