1fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng/*
2fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * Copyright (C) 2013 The Android Open Source Project
3fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng *
4fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
5fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * you may not use this file except in compliance with the License.
6fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * You may obtain a copy of the License at
7fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng *
8fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
9fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng *
10fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * Unless required by applicable law or agreed to in writing, software
11fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
12fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * See the License for the specific language governing permissions and
14fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng * limitations under the License
15fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng */
16fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng
17fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Chengpackage com.android.dialer.calllog;
18fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng
19fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Chengimport android.provider.CallLog;
20fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Chengimport android.telephony.PhoneNumberUtils;
21fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Chengimport android.text.TextUtils;
22fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng
23a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chenimport com.google.android.collect.Sets;
24a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen
25a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chenimport java.util.Set;
26a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen
27fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng/**
28fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng *
29fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng */
30fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Chengpublic class PhoneNumberUtilsWrapper {
31fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng
32a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen    private static final Set<String> LEGACY_UNKNOWN_NUMBERS = Sets.newHashSet("-1", "-2", "-3");
33a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen
34fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    /** Returns true if it is possible to place a call to the given number. */
35fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    public static boolean canPlaceCallsTo(CharSequence number, int presentation) {
36fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        return presentation == CallLog.Calls.PRESENTATION_ALLOWED
37a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen            && !TextUtils.isEmpty(number) && !isLegacyUnknownNumbers(number);
38fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    }
39fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng
40fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    /**
41fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng     * Returns true if it is possible to send an SMS to the given number.
42fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng     */
43fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    public boolean canSendSmsTo(CharSequence number, int presentation) {
44fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        return canPlaceCallsTo(number, presentation) && !isVoicemailNumber(number) && !isSipNumber(
45fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng                number);
46fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    }
47fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng
48fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    /**
49fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng     * Returns true if the given number is the number of the configured voicemail. To be able to
50fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng     * mock-out this, it is not a static method.
51fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng     */
52fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    public boolean isVoicemailNumber(CharSequence number) {
53fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        return PhoneNumberUtils.isVoiceMailNumber(number.toString());
54fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    }
55fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng
56fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    /**
57fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng     * Returns true if the given number is a SIP address. To be able to mock-out this, it is not a
58fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng     * static method.
59fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng     */
60fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    public boolean isSipNumber(CharSequence number) {
61fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        return PhoneNumberUtils.isUriNumber(number.toString());
62fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    }
63fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng
64fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    public static boolean isUnknownNumberThatCanBeLookedUp(CharSequence number, int presentation) {
65fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        if (presentation == CallLog.Calls.PRESENTATION_UNKNOWN) {
66fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng            return false;
67fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        }
68fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        if (presentation == CallLog.Calls.PRESENTATION_RESTRICTED) {
69fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng            return false;
70fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        }
71fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        if (presentation == CallLog.Calls.PRESENTATION_PAYPHONE) {
72fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng            return false;
73fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        }
74fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        if (TextUtils.isEmpty(number)) {
75fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng            return false;
76fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        }
77fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        if (new PhoneNumberUtilsWrapper().isVoicemailNumber(number)) {
78fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng            return false;
79fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        }
80a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen        if (isLegacyUnknownNumbers(number.toString())) {
81a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen            return false;
82a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen        }
83fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng        return true;
84fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng    }
85a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen
86a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen    public static boolean isLegacyUnknownNumbers(CharSequence number) {
87a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen        return LEGACY_UNKNOWN_NUMBERS.contains(number.toString());
88a35059abbec801b20d8f75199d8b30cba981e4ceChristine Chen    }
89fb0a934ad90f1855787563eb80f2c8fff7f640acChiao Cheng}
90