• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.dialer.calllog;
18 
19 import android.provider.CallLog;
20 import android.telephony.PhoneNumberUtils;
21 import android.text.TextUtils;
22 
23 import com.google.common.collect.Sets;
24 
25 import java.util.Set;
26 
27 /**
28  *
29  */
30 public class PhoneNumberUtilsWrapper {
31     public static final PhoneNumberUtilsWrapper INSTANCE = new PhoneNumberUtilsWrapper();
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. */
canPlaceCallsTo(CharSequence number, int presentation)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      */
canSendSmsTo(CharSequence number, int presentation)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      */
isVoicemailNumber(CharSequence number)52     public boolean isVoicemailNumber(CharSequence number) {
53         return number!= null && 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      */
isSipNumber(CharSequence number)60     public boolean isSipNumber(CharSequence number) {
61         return number != null && PhoneNumberUtils.isUriNumber(number.toString());
62     }
63 
isUnknownNumberThatCanBeLookedUp(CharSequence number, int presentation)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 (INSTANCE.isVoicemailNumber(number)) {
78             return false;
79         }
80         if (isLegacyUnknownNumbers(number)) {
81             return false;
82         }
83         return true;
84     }
85 
isLegacyUnknownNumbers(CharSequence number)86     public static boolean isLegacyUnknownNumbers(CharSequence number) {
87         return number != null && LEGACY_UNKNOWN_NUMBERS.contains(number.toString());
88     }
89 }
90