Home | History | Annotate | Download | only in telephony
      1 package com.android.internal.telephony;
      2 
      3 import android.content.Context;
      4 import android.os.Bundle;
      5 import android.provider.BlockedNumberContract;
      6 import android.telephony.Rlog;
      7 
      8 /**
      9  * {@hide} Checks for blocked phone numbers against {@link BlockedNumberContract}
     10  */
     11 public class BlockChecker {
     12     private static final String TAG = "BlockChecker";
     13     private static final boolean VDBG = false; // STOPSHIP if true.
     14 
     15     /**
     16      * Returns {@code true} if {@code phoneNumber} is blocked according to {@code extras}.
     17      * <p>
     18      * This method catches all underlying exceptions to ensure that this method never throws any
     19      * exception.
     20      * <p>
     21      * @deprecated use {@link #isBlocked(Context, String, Bundle)} instead.
     22      *
     23      * @param context the context of the caller.
     24      * @param phoneNumber the number to check.
     25      * @return {@code true} if the number is blocked. {@code false} otherwise.
     26      */
     27     @Deprecated
     28     public static boolean isBlocked(Context context, String phoneNumber) {
     29         return isBlocked(context, phoneNumber, null /* extras */);
     30     }
     31 
     32     /**
     33      * Returns {@code true} if {@code phoneNumber} is blocked according to {@code extras}.
     34      * <p>
     35      * This method catches all underlying exceptions to ensure that this method never throws any
     36      * exception.
     37      *
     38      * @param context the context of the caller.
     39      * @param phoneNumber the number to check.
     40      * @param extras the extra attribute of the number.
     41      * @return {@code true} if the number is blocked. {@code false} otherwise.
     42      */
     43     public static boolean isBlocked(Context context, String phoneNumber, Bundle extras) {
     44         boolean isBlocked = false;
     45         long startTimeNano = System.nanoTime();
     46 
     47         try {
     48             if (BlockedNumberContract.SystemContract.shouldSystemBlockNumber(
     49                     context, phoneNumber, extras)) {
     50                 Rlog.d(TAG, phoneNumber + " is blocked.");
     51                 isBlocked = true;
     52             }
     53         } catch (Exception e) {
     54             Rlog.e(TAG, "Exception checking for blocked number: " + e);
     55         }
     56 
     57         int durationMillis = (int) ((System.nanoTime() - startTimeNano) / 1000000);
     58         if (durationMillis > 500 || VDBG) {
     59             Rlog.d(TAG, "Blocked number lookup took: " + durationMillis + " ms.");
     60         }
     61         return isBlocked;
     62     }
     63 }
     64