• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
isBlocked(Context context, String phoneNumber)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      */
isBlocked(Context context, String phoneNumber, Bundle extras)43     public static boolean isBlocked(Context context, String phoneNumber, Bundle extras) {
44         return getBlockStatus(context, phoneNumber, extras)
45                 != BlockedNumberContract.STATUS_NOT_BLOCKED;
46     }
47 
48     /**
49      * Returns the call blocking status for the {@code phoneNumber}.
50      * <p>
51      * This method catches all underlying exceptions to ensure that this method never throws any
52      * exception.
53      *
54      * @param context the context of the caller.
55      * @param phoneNumber the number to check.
56      * @param extras the extra attribute of the number.
57      * @return result code indicating if the number should be blocked, and if so why.
58      *         Valid values are: {@link BlockedNumberContract#STATUS_NOT_BLOCKED},
59      *         {@link BlockedNumberContract#STATUS_BLOCKED_IN_LIST},
60      *         {@link BlockedNumberContract#STATUS_BLOCKED_NOT_IN_CONTACTS},
61      *         {@link BlockedNumberContract#STATUS_BLOCKED_PAYPHONE},
62      *         {@link BlockedNumberContract#STATUS_BLOCKED_RESTRICTED},
63      *         {@link BlockedNumberContract#STATUS_BLOCKED_UNKNOWN_NUMBER}.
64      */
getBlockStatus(Context context, String phoneNumber, Bundle extras)65     public static int getBlockStatus(Context context, String phoneNumber, Bundle extras) {
66         int blockStatus = BlockedNumberContract.STATUS_NOT_BLOCKED;
67         long startTimeNano = System.nanoTime();
68 
69         try {
70             blockStatus = BlockedNumberContract.SystemContract.shouldSystemBlockNumber(
71                     context, phoneNumber, extras);
72             if (blockStatus != BlockedNumberContract.STATUS_NOT_BLOCKED) {
73                 Rlog.d(TAG, phoneNumber + " is blocked.");
74             }
75         } catch (Exception e) {
76             Rlog.e(TAG, "Exception checking for blocked number: " + e);
77         }
78 
79         int durationMillis = (int) ((System.nanoTime() - startTimeNano) / 1000000);
80         if (durationMillis > 500 || VDBG) {
81             Rlog.d(TAG, "Blocked number lookup took: " + durationMillis + " ms.");
82         }
83         return blockStatus;
84     }
85 }
86