• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.server.telecom.callfiltering;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.provider.BlockedNumberContract;
22 import android.provider.BlockedNumbersManager;
23 import android.telecom.Log;
24 
25 import com.android.server.telecom.flags.FeatureFlags;
26 
27 public class BlockCheckerAdapter {
28     private static final String TAG = BlockCheckerAdapter.class.getSimpleName();
29 
30     private FeatureFlags mFeatureFlags;
31 
BlockCheckerAdapter(FeatureFlags featureFlags)32     public BlockCheckerAdapter(FeatureFlags featureFlags) {
33         mFeatureFlags = featureFlags;
34     }
35 
36     /**
37      * Returns the call blocking status for the {@code phoneNumber}.
38      * <p>
39      * This method catches all underlying exceptions to ensure that this method never throws any
40      * exception.
41      *
42      * @param phoneNumber the number to check.
43      * @param numberPresentation the presentation code associated with the call.
44      * @param isNumberInContacts indicates if the provided number exists as a contact.
45      * @return result code indicating if the number should be blocked, and if so why.
46      *         Valid values are: {@link BlockCheckerFilter#STATUS_NOT_BLOCKED},
47      *         {@link BlockCheckerFilter#STATUS_BLOCKED_IN_LIST},
48      *         {@link BlockCheckerFilter#STATUS_BLOCKED_NOT_IN_CONTACTS},
49      *         {@link BlockCheckerFilter#STATUS_BLOCKED_PAYPHONE},
50      *         {@link BlockCheckerFilter#STATUS_BLOCKED_RESTRICTED},
51      *         {@link BlockCheckerFilter#STATUS_BLOCKED_UNKNOWN_NUMBER}.
52      */
getBlockStatus(Context context, String phoneNumber, int numberPresentation, boolean isNumberInContacts)53     public int getBlockStatus(Context context, String phoneNumber,
54             int numberPresentation, boolean isNumberInContacts) {
55         int blockStatus = BlockedNumberContract.STATUS_NOT_BLOCKED;
56         long startTimeNano = System.nanoTime();
57         BlockedNumbersManager blockedNumbersManager = mFeatureFlags
58                 .telecomMainlineBlockedNumbersManager()
59                 ? context.getSystemService(BlockedNumbersManager.class)
60                 : null;
61 
62         try {
63             Bundle extras = new Bundle();
64             extras.putInt(BlockedNumberContract.EXTRA_CALL_PRESENTATION, numberPresentation);
65             extras.putBoolean(BlockedNumberContract.EXTRA_CONTACT_EXIST, isNumberInContacts);
66             blockStatus = blockedNumbersManager != null
67                     ? blockedNumbersManager.shouldSystemBlockNumber(phoneNumber,
68                     numberPresentation, isNumberInContacts)
69                     : BlockedNumberContract.SystemContract.shouldSystemBlockNumber(context,
70                             phoneNumber, extras);
71             if (blockStatus != BlockedNumberContract.STATUS_NOT_BLOCKED) {
72                 Log.d(TAG, phoneNumber + " is blocked.");
73             }
74         } catch (Exception e) {
75             Log.e(TAG, e, "Exception checking for blocked number");
76         }
77 
78         int durationMillis = (int) ((System.nanoTime() - startTimeNano) / 1000000);
79         if (durationMillis > 500 || Log.isLoggable(android.util.Log.DEBUG)) {
80             Log.d(TAG, "Blocked number lookup took: " + durationMillis + " ms.");
81         }
82         return blockStatus;
83     }
84 }
85