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