1 package com.android.internal.telephony;
2 
3 import android.content.Context;
4 import android.os.Bundle;
5 import android.provider.BlockedNumberContract;
6 
7 import com.android.telephony.Rlog;
8 
9 /**
10  * {@hide} Checks for blocked phone numbers against {@link BlockedNumberContract}
11  */
12 public class BlockChecker {
13     private static final String TAG = "BlockChecker";
14     private static final boolean VDBG = false; // STOPSHIP if true.
15 
16     /**
17      * Returns {@code true} if {@code phoneNumber} is blocked according to {@code extras}.
18      * <p>
19      * This method catches all underlying exceptions to ensure that this method never throws any
20      * exception.
21      * <p>
22      * @deprecated use {@link #isBlocked(Context, String, Bundle)} instead.
23      *
24      * @param context the context of the caller.
25      * @param phoneNumber the number to check.
26      * @return {@code true} if the number is blocked. {@code false} otherwise.
27      */
28     @Deprecated
isBlocked(Context context, String phoneNumber)29     public static boolean isBlocked(Context context, String phoneNumber) {
30         return isBlocked(context, phoneNumber, null /* extras */);
31     }
32 
33     /**
34      * Returns {@code true} if {@code phoneNumber} is blocked according to {@code extras}.
35      * <p>
36      * This method catches all underlying exceptions to ensure that this method never throws any
37      * exception.
38      *
39      * @param context the context of the caller.
40      * @param phoneNumber the number to check.
41      * @param extras the extra attribute of the number.
42      * @return {@code true} if the number is blocked. {@code false} otherwise.
43      */
isBlocked(Context context, String phoneNumber, Bundle extras)44     public static boolean isBlocked(Context context, String phoneNumber, Bundle extras) {
45         return getBlockStatus(context, phoneNumber, extras)
46                 != BlockedNumberContract.STATUS_NOT_BLOCKED;
47     }
48 
49     /**
50      * Returns the call blocking status for the {@code phoneNumber}.
51      * <p>
52      * This method catches all underlying exceptions to ensure that this method never throws any
53      * exception.
54      *
55      * @param context the context of the caller.
56      * @param phoneNumber the number to check.
57      * @param extras the extra attribute of the number.
58      * @return result code indicating if the number should be blocked, and if so why.
59      *         Valid values are: {@link BlockedNumberContract#STATUS_NOT_BLOCKED},
60      *         {@link BlockedNumberContract#STATUS_BLOCKED_IN_LIST},
61      *         {@link BlockedNumberContract#STATUS_BLOCKED_NOT_IN_CONTACTS},
62      *         {@link BlockedNumberContract#STATUS_BLOCKED_PAYPHONE},
63      *         {@link BlockedNumberContract#STATUS_BLOCKED_RESTRICTED},
64      *         {@link BlockedNumberContract#STATUS_BLOCKED_UNKNOWN_NUMBER}.
65      */
getBlockStatus(Context context, String phoneNumber, Bundle extras)66     public static int getBlockStatus(Context context, String phoneNumber, Bundle extras) {
67         int blockStatus = BlockedNumberContract.STATUS_NOT_BLOCKED;
68         long startTimeNano = System.nanoTime();
69 
70         try {
71             blockStatus = BlockedNumberContract.SystemContract.shouldSystemBlockNumber(
72                     context, phoneNumber, extras);
73             if (blockStatus != BlockedNumberContract.STATUS_NOT_BLOCKED) {
74                 Rlog.d(TAG, phoneNumber + " is blocked.");
75             }
76         } catch (Exception e) {
77             Rlog.e(TAG, "Exception checking for blocked number: " + e);
78         }
79 
80         int durationMillis = (int) ((System.nanoTime() - startTimeNano) / 1000000);
81         if (durationMillis > 500 || VDBG) {
82             Rlog.d(TAG, "Blocked number lookup took: " + durationMillis + " ms.");
83         }
84         return blockStatus;
85     }
86 }
87