1/*
2 * Copyright (C) 2013 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.calllog;
18
19import android.provider.CallLog;
20import android.telephony.PhoneNumberUtils;
21import android.text.TextUtils;
22
23import com.android.contacts.common.util.PhoneNumberHelper;
24
25import com.google.common.collect.Sets;
26
27import java.util.Set;
28
29/**
30 *
31 */
32public class PhoneNumberUtilsWrapper {
33    public static final PhoneNumberUtilsWrapper INSTANCE = new PhoneNumberUtilsWrapper();
34    private static final Set<String> LEGACY_UNKNOWN_NUMBERS = Sets.newHashSet("-1", "-2", "-3");
35
36    /** Returns true if it is possible to place a call to the given number. */
37    public static boolean canPlaceCallsTo(CharSequence number, int presentation) {
38        return presentation == CallLog.Calls.PRESENTATION_ALLOWED
39            && !TextUtils.isEmpty(number) && !isLegacyUnknownNumbers(number);
40    }
41
42    /**
43     * Returns true if it is possible to send an SMS to the given number.
44     */
45    public boolean canSendSmsTo(CharSequence number, int presentation) {
46        return canPlaceCallsTo(number, presentation) && !isVoicemailNumber(number) && !isSipNumber(
47                number);
48    }
49
50    /**
51     * Returns true if the given number is the number of the configured voicemail. To be able to
52     * mock-out this, it is not a static method.
53     */
54    public boolean isVoicemailNumber(CharSequence number) {
55        return number!= null && PhoneNumberUtils.isVoiceMailNumber(number.toString());
56    }
57
58    /**
59     * Returns true if the given number is a SIP address. To be able to mock-out this, it is not a
60     * static method.
61     */
62    public boolean isSipNumber(CharSequence number) {
63        return number != null && PhoneNumberHelper.isUriNumber(number.toString());
64    }
65
66    public static boolean isUnknownNumberThatCanBeLookedUp(CharSequence number, int presentation) {
67        if (presentation == CallLog.Calls.PRESENTATION_UNKNOWN) {
68            return false;
69        }
70        if (presentation == CallLog.Calls.PRESENTATION_RESTRICTED) {
71            return false;
72        }
73        if (presentation == CallLog.Calls.PRESENTATION_PAYPHONE) {
74            return false;
75        }
76        if (TextUtils.isEmpty(number)) {
77            return false;
78        }
79        if (INSTANCE.isVoicemailNumber(number)) {
80            return false;
81        }
82        if (isLegacyUnknownNumbers(number)) {
83            return false;
84        }
85        return true;
86    }
87
88    public static boolean isLegacyUnknownNumbers(CharSequence number) {
89        return number != null && LEGACY_UNKNOWN_NUMBERS.contains(number.toString());
90    }
91}
92