Home | History | Annotate | Download | only in util
      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.util;
     18 
     19 import android.content.Context;
     20 import android.provider.CallLog;
     21 import android.telecom.PhoneAccountHandle;
     22 import android.telecom.TelecomManager;
     23 import android.text.TextUtils;
     24 import android.util.Log;
     25 import android.util.Pair;
     26 
     27 import com.android.contacts.common.util.PhoneNumberHelper;
     28 import com.google.common.collect.Sets;
     29 
     30 import java.util.HashMap;
     31 import java.util.Map;
     32 import java.util.Set;
     33 
     34 public class PhoneNumberUtil {
     35     private static final Set<String> LEGACY_UNKNOWN_NUMBERS = Sets.newHashSet("-1", "-2", "-3");
     36 
     37     /** Returns true if it is possible to place a call to the given number. */
     38     public static boolean canPlaceCallsTo(CharSequence number, int presentation) {
     39         return presentation == CallLog.Calls.PRESENTATION_ALLOWED
     40             && !TextUtils.isEmpty(number) && !isLegacyUnknownNumbers(number);
     41     }
     42 
     43     /**
     44      * Returns true if the given number is the number of the configured voicemail. To be able to
     45      * mock-out this, it is not a static method.
     46      */
     47     public static boolean isVoicemailNumber(
     48             Context context, PhoneAccountHandle accountHandle, CharSequence number) {
     49         if (TextUtils.isEmpty(number)) {
     50             return false;
     51         }
     52 
     53         final TelecomManager telecomManager =
     54                 (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
     55         return telecomManager.isVoiceMailNumber(accountHandle, 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 static boolean isSipNumber(CharSequence number) {
     63         return number != null && PhoneNumberHelper.isUriNumber(number.toString());
     64     }
     65 
     66     public static boolean isUnknownNumberThatCanBeLookedUp(
     67             Context context,
     68             PhoneAccountHandle accountHandle,
     69             CharSequence number,
     70             int presentation) {
     71         if (presentation == CallLog.Calls.PRESENTATION_UNKNOWN) {
     72             return false;
     73         }
     74         if (presentation == CallLog.Calls.PRESENTATION_RESTRICTED) {
     75             return false;
     76         }
     77         if (presentation == CallLog.Calls.PRESENTATION_PAYPHONE) {
     78             return false;
     79         }
     80         if (TextUtils.isEmpty(number)) {
     81             return false;
     82         }
     83         if (isVoicemailNumber(context, accountHandle, number)) {
     84             return false;
     85         }
     86         if (isLegacyUnknownNumbers(number)) {
     87             return false;
     88         }
     89         return true;
     90     }
     91 
     92     public static boolean isLegacyUnknownNumbers(CharSequence number) {
     93         return number != null && LEGACY_UNKNOWN_NUMBERS.contains(number.toString());
     94     }
     95 }
     96