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.contacts.calllog;
18
19import android.content.res.Resources;
20import android.telephony.PhoneNumberUtils;
21import android.text.TextUtils;
22
23import com.android.contacts.R;
24import com.android.internal.telephony.CallerInfo;
25
26/**
27 * Helper for formatting and managing phone numbers.
28 */
29public class PhoneNumberHelper {
30    private final Resources mResources;
31
32    public PhoneNumberHelper(Resources resources) {
33        mResources = resources;
34    }
35
36    /** Returns true if it is possible to place a call to the given number. */
37    public boolean canPlaceCallsTo(CharSequence number) {
38        return !(TextUtils.isEmpty(number)
39                || number.equals(CallerInfo.UNKNOWN_NUMBER)
40                || number.equals(CallerInfo.PRIVATE_NUMBER)
41                || number.equals(CallerInfo.PAYPHONE_NUMBER));
42    }
43
44    /** Returns true if it is possible to send an SMS to the given number. */
45    public boolean canSendSmsTo(CharSequence number) {
46        return canPlaceCallsTo(number) && !isVoicemailNumber(number) && !isSipNumber(number);
47    }
48
49    /**
50     * Returns the string to display for the given phone number.
51     *
52     * @param number the number to display
53     * @param formattedNumber the formatted number if available, may be null
54     */
55    public CharSequence getDisplayNumber(CharSequence number, CharSequence formattedNumber) {
56        if (TextUtils.isEmpty(number)) {
57            return "";
58        }
59        if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
60            return mResources.getString(R.string.unknown);
61        }
62        if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
63            return mResources.getString(R.string.private_num);
64        }
65        if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
66            return mResources.getString(R.string.payphone);
67        }
68        if (isVoicemailNumber(number)) {
69            return mResources.getString(R.string.voicemail);
70        }
71        if (TextUtils.isEmpty(formattedNumber)) {
72            return number;
73        } else {
74            return formattedNumber;
75        }
76    }
77
78    /**
79     * Returns true if the given number is the number of the configured voicemail.
80     * To be able to mock-out this, it is not a static method.
81     */
82    public boolean isVoicemailNumber(CharSequence number) {
83        return PhoneNumberUtils.isVoiceMailNumber(number.toString());
84    }
85
86    /**
87     * Returns true if the given number is a SIP address.
88     * To be able to mock-out this, it is not a static method.
89     */
90    public boolean isSipNumber(CharSequence number) {
91        return PhoneNumberUtils.isUriNumber(number.toString());
92    }
93}
94