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
17package com.android.dialer.calllogutils;
18
19import android.content.Context;
20import android.provider.CallLog.Calls;
21import android.text.BidiFormatter;
22import android.text.TextDirectionHeuristics;
23import android.text.TextUtils;
24import com.android.contacts.common.compat.PhoneNumberUtilsCompat;
25import com.android.dialer.phonenumberutil.PhoneNumberHelper;
26
27/** Helper for formatting and managing the display of phone numbers. */
28public class PhoneNumberDisplayUtil {
29
30  /** Returns the string to display for the given phone number if there is no matching contact. */
31  public static CharSequence getDisplayName(
32      Context context, CharSequence number, int presentation, boolean isVoicemail) {
33    if (presentation == Calls.PRESENTATION_UNKNOWN) {
34      return context.getResources().getString(R.string.unknown);
35    }
36    if (presentation == Calls.PRESENTATION_RESTRICTED) {
37      return PhoneNumberHelper.getDisplayNameForRestrictedNumber(context);
38    }
39    if (presentation == Calls.PRESENTATION_PAYPHONE) {
40      return context.getResources().getString(R.string.payphone);
41    }
42    if (isVoicemail) {
43      return context.getResources().getString(R.string.voicemail_string);
44    }
45    if (PhoneNumberHelper.isLegacyUnknownNumbers(number)) {
46      return context.getResources().getString(R.string.unknown);
47    }
48    return "";
49  }
50
51  /**
52   * Returns the string to display for the given phone number.
53   *
54   * @param number the number to display
55   * @param formattedNumber the formatted number if available, may be null
56   */
57  public static CharSequence getDisplayNumber(
58      Context context,
59      CharSequence number,
60      int presentation,
61      CharSequence formattedNumber,
62      CharSequence postDialDigits,
63      boolean isVoicemail) {
64    final CharSequence displayName = getDisplayName(context, number, presentation, isVoicemail);
65    if (!TextUtils.isEmpty(displayName)) {
66      return getTtsSpannableLtrNumber(displayName);
67    }
68
69    if (!TextUtils.isEmpty(formattedNumber)) {
70      return getTtsSpannableLtrNumber(formattedNumber);
71    } else if (!TextUtils.isEmpty(number)) {
72      return getTtsSpannableLtrNumber(number.toString() + postDialDigits);
73    } else {
74      return context.getResources().getString(R.string.unknown);
75    }
76  }
77
78  /** Returns number annotated as phone number in LTR direction. */
79  public static CharSequence getTtsSpannableLtrNumber(CharSequence number) {
80    return PhoneNumberUtilsCompat.createTtsSpannable(
81        BidiFormatter.getInstance().unicodeWrap(number.toString(), TextDirectionHeuristics.LTR));
82  }
83}
84