BlockedNumberContract.java revision e3e686cff8fa43a54d72e162cf3e93b05c517c86
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 */ 16package android.provider; 17 18import android.content.Context; 19import android.net.Uri; 20import android.os.Bundle; 21 22/** 23 * <p> 24 * The contract between the blockednumber provider and applications. Contains definitions for 25 * the supported URIs and columns. 26 * </p> 27 * 28 * <h3> Overview </h3> 29 * <p> 30 * The content provider exposes a table containing blocked numbers. The columns and URIs for 31 * accessing this table are defined by the {@link BlockedNumbers} class. Messages, and calls from 32 * blocked numbers are discarded by the platform. Notifications upon provider changes can be 33 * received using a {@link android.database.ContentObserver}. 34 * </p> 35 * <p> 36 * The platform will not block messages, and calls from emergency numbers as defined by 37 * {@link android.telephony.PhoneNumberUtils#isEmergencyNumber(String)}. If the user contacts 38 * emergency services, number blocking is disabled by the platform for a duration defined by 39 * {@link android.telephony.CarrierConfigManager#KEY_DURATION_BLOCKING_DISABLED_AFTER_EMERGENCY_INT}. 40 * </p> 41 * 42 * <h3> Permissions </h3> 43 * <p> 44 * Only the system, the default SMS application, and the default phone app 45 * (See {@link android.telecom.TelecomManager#getDefaultDialerPackage()}), and carrier apps 46 * (See {@link android.service.carrier.CarrierService}) can read, and write to the blockednumber 47 * provider. 48 * </p> 49 * 50 * <h3> Data </h3> 51 * <p> 52 * Other than regular phone numbers, the blocked number provider can also store addresses (such 53 * as email) from which a user can receive messages, and calls. The blocked numbers are stored 54 * in the {@link BlockedNumbers#COLUMN_ORIGINAL_NUMBER} column. A normalized version of phone 55 * numbers (if normalization is possible) is stored in {@link BlockedNumbers#COLUMN_E164_NUMBER} 56 * column. The platform blocks calls, and messages from an address if it is present in in the 57 * {@link BlockedNumbers#COLUMN_ORIGINAL_NUMBER} column or if the E164 version of the address 58 * matches the {@link BlockedNumbers#COLUMN_E164_NUMBER} column. 59 * </p> 60 * 61 * <h3> Operations </h3> 62 * <dl> 63 * <dt><b>Insert</b></dt> 64 * <dd> 65 * <p> 66 * {@link BlockedNumbers#COLUMN_ORIGINAL_NUMBER} is a required column that needs to be populated. 67 * Apps can optionally provide the {@link BlockedNumbers#COLUMN_E164_NUMBER} which is the phone 68 * number's E164 representation. The provider automatically populates this column if the app does 69 * not provide it. Note that this column is not populated if normalization fails or if the address 70 * is not a phone number (eg: email). The provider enforces uniqueness constraint on this column. 71 * Examples: 72 * <pre> 73 * ContentValues values = new ContentValues(); 74 * values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890"); 75 * Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values); 76 * </pre> 77 * <pre> 78 * ContentValues values = new ContentValues(); 79 * values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890"); 80 * values.put(BlockedNumbers.COLUMN_E164_NUMBER, "+11234567890"); 81 * Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values); 82 * </pre> 83 * <pre> 84 * ContentValues values = new ContentValues(); 85 * values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "12345@abdcde.com"); 86 * Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values); 87 * </pre> 88 * </p> 89 * </dd> 90 * <dt><b>Update</b></dt> 91 * <dd> 92 * <p> 93 * Updates are not supported. Use Delete, and Insert instead. 94 * </p> 95 * </dd> 96 * <dt><b>Delete</b></dt> 97 * <dd> 98 * <p> 99 * Deletions can be performed as follows: 100 * <pre> 101 * ContentValues values = new ContentValues(); 102 * values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890"); 103 * Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values); 104 * getContentResolver().delete(uri, null, null); 105 * </pre> 106 * </p> 107 * </dd> 108 * <dt><b>Query</b></dt> 109 * <dd> 110 * <p> 111 * All blocked numbers can be enumerated as follows: 112 * <pre> 113 * Cursor c = getContentResolver().query(BlockedNumbers.CONTENT_URI, 114 * new String[]{BlockedNumbers.COLUMN_ID, BlockedNumbers.COLUMN_ORIGINAL_NUMBER, 115 * BlockedNumbers.COLUMN_E164_NUMBER}, null, null, null); 116 * </pre> 117 * To check if a particular number is blocked, use the method 118 * {@link #isBlocked(Context, String)}. 119 * </p> 120 * </dd> 121 * 122 * <h3> Multi-user </h3> 123 * <p> 124 * Apps must use the method {@link #canCurrentUserBlockNumbers(Context)} before performing any 125 * operation on the blocked number provider. If {@link #canCurrentUserBlockNumbers(Context)} returns 126 * {@code false}, all operations on the provider will fail with an 127 * {@link UnsupportedOperationException}. The platform will block calls, and messages from numbers 128 * in the provider independent of the current user. 129 * </p> 130 */ 131public class BlockedNumberContract { 132 private BlockedNumberContract() { 133 } 134 135 /** The authority for the blocked number provider */ 136 public static final String AUTHORITY = "com.android.blockednumber"; 137 138 /** A content:// style uri to the authority for the blocked number provider */ 139 public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY); 140 141 /** 142 * Constants to interact with the blocked numbers list. 143 */ 144 public static class BlockedNumbers { 145 private BlockedNumbers() { 146 } 147 148 /** 149 * Content URI for the blocked numbers. 150 * <h3> Supported operations </h3> 151 * <p> blocked 152 * <ul> 153 * <li> query 154 * <li> delete 155 * <li> insert 156 * </ul> 157 * <p> blocked/ID 158 * <ul> 159 * <li> query (selection is not supported) 160 * <li> delete (selection is not supported) 161 * </ul> 162 */ 163 public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "blocked"); 164 165 /** 166 * The MIME type of {@link #CONTENT_URI} itself providing a directory of blocked phone 167 * numbers. 168 */ 169 public static final String CONTENT_TYPE = "vnd.android.cursor.dir/blocked_number"; 170 171 /** 172 * The MIME type of a blocked phone number under {@link #CONTENT_URI}. 173 */ 174 public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/blocked_number"; 175 176 /** 177 * Auto-generated ID field which monotonically increases. 178 * <p>TYPE: long</p> 179 */ 180 public static final String COLUMN_ID = "_id"; 181 182 /** 183 * Phone number to block. 184 * <p>Must be specified in {@code insert}. 185 * <p>TYPE: String</p> 186 */ 187 public static final String COLUMN_ORIGINAL_NUMBER = "original_number"; 188 189 /** 190 * Phone number to block. The system generates it from {@link #COLUMN_ORIGINAL_NUMBER} 191 * by removing all formatting characters. 192 * <p>Optional in {@code insert}. When not specified, the system tries to generate it 193 * assuming the current country. (Which will still be null if the number is not valid.) 194 * <p>TYPE: String</p> 195 */ 196 public static final String COLUMN_E164_NUMBER = "e164_number"; 197 } 198 199 /** @hide */ 200 public static final String METHOD_IS_BLOCKED = "is_blocked"; 201 202 /** @hide */ 203 public static final String RES_NUMBER_IS_BLOCKED = "blocked"; 204 205 /** @hide */ 206 public static final String METHOD_CAN_CURRENT_USER_BLOCK_NUMBERS = 207 "can_current_user_block_numbers"; 208 209 /** @hide */ 210 public static final String RES_CAN_BLOCK_NUMBERS = "can_block"; 211 212 /** 213 * Returns whether a given number is in the blocked list. 214 * <p> Note that if the {@link #canCurrentUserBlockNumbers} is {@code false} for the user 215 * context {@code context}, this method will throw an {@link UnsupportedOperationException}. 216 */ 217 public static boolean isBlocked(Context context, String phoneNumber) { 218 final Bundle res = context.getContentResolver().call( 219 AUTHORITY_URI, METHOD_IS_BLOCKED, phoneNumber, null); 220 return res != null && res.getBoolean(RES_NUMBER_IS_BLOCKED, false); 221 } 222 223 /** 224 * Returns {@code true} if blocking numbers is supported for the current user. 225 * <p> Typically, blocking numbers is only supported for one user at a time. 226 */ 227 public static boolean canCurrentUserBlockNumbers(Context context) { 228 final Bundle res = context.getContentResolver().call( 229 AUTHORITY_URI, METHOD_CAN_CURRENT_USER_BLOCK_NUMBERS, null, null); 230 return res != null && res.getBoolean(RES_CAN_BLOCK_NUMBERS, false); 231 } 232 233 /** 234 * <p> 235 * The contract between the blockednumber provider and the system. 236 * </p> 237 * <p>This is a wrapper over {@link BlockedNumberContract} that also manages the blocking 238 * behavior when the user contacts emergency services. See 239 * {@link #notifyEmergencyContact(Context)} for details. All methods are protected by 240 * {@link android.Manifest.permission#READ_BLOCKED_NUMBERS} and 241 * {@link android.Manifest.permission#WRITE_BLOCKED_NUMBERS} appropriately which ensure that 242 * only system can access the methods defined here. 243 * </p> 244 * @hide 245 */ 246 public static class SystemContract { 247 /** 248 * A protected broadcast intent action for letting components with 249 * {@link android.Manifest.permission#READ_BLOCKED_NUMBERS} know that the block suppression 250 * status as returned by {@link #getBlockSuppressionStatus(Context)} has been updated. 251 */ 252 public static final String ACTION_BLOCK_SUPPRESSION_STATE_CHANGED = 253 "android.provider.action.BLOCK_SUPPRESSION_STATE_CHANGED"; 254 255 public static final String METHOD_NOTIFY_EMERGENCY_CONTACT = "notify_emergency_contact"; 256 257 public static final String METHOD_END_BLOCK_SUPPRESSION = "end_block_suppression"; 258 259 public static final String METHOD_SHOULD_SYSTEM_BLOCK_NUMBER = "should_system_block_number"; 260 261 public static final String METHOD_GET_BLOCK_SUPPRESSION_STATUS = 262 "get_block_suppression_status"; 263 264 public static final String RES_IS_BLOCKING_SUPPRESSED = "blocking_suppressed"; 265 266 public static final String RES_BLOCKING_SUPPRESSED_UNTIL_TIMESTAMP = 267 "blocking_suppressed_until_timestamp"; 268 269 /** 270 * Notifies the provider that emergency services were contacted by the user. 271 * <p> This results in {@link #shouldSystemBlockNumber} returning {@code false} independent 272 * of the contents of the provider for a duration defined by 273 * {@link android.telephony.CarrierConfigManager#KEY_DURATION_BLOCKING_DISABLED_AFTER_EMERGENCY_INT} 274 * the provider unless {@link #endBlockSuppression(Context)} is called. 275 */ 276 public static void notifyEmergencyContact(Context context) { 277 context.getContentResolver().call( 278 AUTHORITY_URI, METHOD_NOTIFY_EMERGENCY_CONTACT, null, null); 279 } 280 281 /** 282 * Notifies the provider to disable suppressing blocking. If emergency services were not 283 * contacted recently at all, calling this method is a no-op. 284 */ 285 public static void endBlockSuppression(Context context) { 286 context.getContentResolver().call( 287 AUTHORITY_URI, METHOD_END_BLOCK_SUPPRESSION, null, null); 288 } 289 290 /** 291 * Returns {@code true} if {@code phoneNumber} is blocked taking 292 * {@link #notifyEmergencyContact(Context)} into consideration. If emergency services have 293 * not been contacted recently, this method is equivalent to 294 * {@link #isBlocked(Context, String)}. 295 */ 296 public static boolean shouldSystemBlockNumber(Context context, String phoneNumber) { 297 final Bundle res = context.getContentResolver().call( 298 AUTHORITY_URI, METHOD_SHOULD_SYSTEM_BLOCK_NUMBER, phoneNumber, null); 299 return res != null && res.getBoolean(RES_NUMBER_IS_BLOCKED, false); 300 } 301 302 /** 303 * Returns the current status of block suppression. 304 */ 305 public static BlockSuppressionStatus getBlockSuppressionStatus(Context context) { 306 final Bundle res = context.getContentResolver().call( 307 AUTHORITY_URI, METHOD_GET_BLOCK_SUPPRESSION_STATUS, null, null); 308 return new BlockSuppressionStatus(res.getBoolean(RES_IS_BLOCKING_SUPPRESSED, false), 309 res.getLong(RES_BLOCKING_SUPPRESSED_UNTIL_TIMESTAMP, 0)); 310 } 311 312 /** 313 * Represents the current status of {@link #shouldSystemBlockNumber(Context, String)}. If 314 * emergency services have been contacted recently, {@link #isSuppressed} is {@code true}, 315 * and blocking is disabled until the timestamp {@link #untilTimestampMillis}. 316 */ 317 public static class BlockSuppressionStatus { 318 public final boolean isSuppressed; 319 /** 320 * Timestamp in milliseconds from epoch. 321 */ 322 public final long untilTimestampMillis; 323 324 public BlockSuppressionStatus(boolean isSuppressed, long untilTimestampMillis) { 325 this.isSuppressed = isSuppressed; 326 this.untilTimestampMillis = untilTimestampMillis; 327 } 328 } 329 } 330} 331