public class BlockedNumberContract extends Object
The contract between the blockednumber provider and applications. Contains definitions for the supported URIs and columns.
The content provider exposes a table containing blocked numbers. The columns and URIs for
accessing this table are defined by the BlockedNumberContract.BlockedNumbers
class. Messages, and calls from
blocked numbers are discarded by the platform. Notifications upon provider changes can be
received using a ContentObserver
.
The platform will not block messages, and calls from emergency numbers as defined by
PhoneNumberUtils.isEmergencyNumber(String)
. If the user contacts
emergency services, number blocking is disabled by the platform for a duration defined by
CarrierConfigManager.KEY_DURATION_BLOCKING_DISABLED_AFTER_EMERGENCY_INT
.
Only the system, the default SMS application, and the default phone app
(See TelecomManager.getDefaultDialerPackage()
), and carrier apps
(See CarrierService
) can read, and write to the blockednumber
provider. However, canCurrentUserBlockNumbers(Context)
can be accessed by any
application.
Other than regular phone numbers, the blocked number provider can also store addresses (such
as email) from which a user can receive messages, and calls. The blocked numbers are stored
in the BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER
column. A normalized version of phone
numbers (if normalization is possible) is stored in BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER
column. The platform blocks calls, and messages from an address if it is present in in the
BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER
column or if the E164 version of the address
matches the BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER
column.
BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER
is a required column that needs to be populated.
Apps can optionally provide the BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER
which is the phone
number's E164 representation. The provider automatically populates this column if the app does
not provide it. Note that this column is not populated if normalization fails or if the address
is not a phone number (eg: email).
Attempting to insert an existing blocked number (same
BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER
column) will result in replacing the existing
blocked number.
Examples:
ContentValues values = new ContentValues(); values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890"); Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values);
ContentValues values = new ContentValues(); values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890"); values.put(BlockedNumbers.COLUMN_E164_NUMBER, "+11234567890"); Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values);
ContentValues values = new ContentValues(); values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "12345@abdcde.com"); Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values);
Updates are not supported. Use Delete, and Insert instead.
Deletions can be performed as follows:
ContentValues values = new ContentValues(); values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890"); Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values); getContentResolver().delete(uri, null, null);To check if a particular number is blocked, use the method
isBlocked(Context, String)
.
All blocked numbers can be enumerated as follows:
Cursor c = getContentResolver().query(BlockedNumbers.CONTENT_URI, new String[]{BlockedNumbers.COLUMN_ID, BlockedNumbers.COLUMN_ORIGINAL_NUMBER, BlockedNumbers.COLUMN_E164_NUMBER}, null, null, null);
Use the method unblock(Context, String)
to unblock numbers.
Apps must use the method canCurrentUserBlockNumbers(Context)
before performing any
operation on the blocked number provider. If canCurrentUserBlockNumbers(Context)
returns
false
, all operations on the provider will fail with a SecurityException
. The
platform will block calls, and messages from numbers in the provider independent of the current
user.
Modifier and Type | Class and Description |
---|---|
static class |
BlockedNumberContract.BlockedNumbers
Constants to interact with the blocked numbers list.
|
static class |
BlockedNumberContract.SystemContract
The contract between the blockednumber provider and the system.
|
Modifier and Type | Field and Description |
---|---|
static String |
AUTHORITY
The authority for the blocked number provider
|
static Uri |
AUTHORITY_URI
A content:// style uri to the authority for the blocked number provider
|
static String |
METHOD_CAN_CURRENT_USER_BLOCK_NUMBERS |
static String |
METHOD_IS_BLOCKED |
static String |
METHOD_UNBLOCK |
static String |
RES_CAN_BLOCK_NUMBERS |
static String |
RES_NUM_ROWS_DELETED |
static String |
RES_NUMBER_IS_BLOCKED |
Modifier and Type | Method and Description |
---|---|
static boolean |
canCurrentUserBlockNumbers(Context context)
Checks if blocking numbers is supported for the current user.
|
static boolean |
isBlocked(Context context,
String phoneNumber)
Returns whether a given number is in the blocked list.
|
static int |
unblock(Context context,
String phoneNumber)
Unblocks the
phoneNumber if it is blocked. |
public static final String AUTHORITY
public static final Uri AUTHORITY_URI
public static final String METHOD_IS_BLOCKED
public static final String METHOD_UNBLOCK
public static final String RES_NUMBER_IS_BLOCKED
public static final String RES_NUM_ROWS_DELETED
public static final String METHOD_CAN_CURRENT_USER_BLOCK_NUMBERS
public static final String RES_CAN_BLOCK_NUMBERS
public static boolean isBlocked(Context context, String phoneNumber)
This matches the phoneNumber
against the
BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER
column, and the E164 representation of the
phoneNumber
with the BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER
column.
Note that if the canCurrentUserBlockNumbers(android.content.Context)
is false
for the user
context context
, this method will throw a SecurityException
.
true
if the phoneNumber
is blocked.public static int unblock(Context context, String phoneNumber)
phoneNumber
if it is blocked.
This deletes all rows where the phoneNumber
matches the
BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER
column or the E164 representation of the
phoneNumber
matches the BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER
column.
To delete rows based on exact match with specific columns such as
BlockedNumberContract.BlockedNumbers.COLUMN_ID
use
ContentProvider.delete(Uri, String, String[])
with
BlockedNumberContract.BlockedNumbers.CONTENT_URI
URI.
Note that if the canCurrentUserBlockNumbers(android.content.Context)
is false
for the user
context context
, this method will throw a SecurityException
.
public static boolean canCurrentUserBlockNumbers(Context context)
Typically, blocking numbers is only supported for one user at a time.
true
if the current user can block numbers.