• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.dialer.databasepopulator;
18 
19 import android.annotation.TargetApi;
20 import android.content.ContentProviderOperation;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.content.OperationApplicationException;
24 import android.os.Build.VERSION_CODES;
25 import android.os.RemoteException;
26 import android.provider.BlockedNumberContract;
27 import android.provider.BlockedNumberContract.BlockedNumbers;
28 import android.support.annotation.NonNull;
29 import com.android.dialer.common.Assert;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.List;
33 
34 /** Populates the device database with blocked number entries. */
35 public class BlockedBumberPopulator {
36 
37   private static final List<ContentValues> values =
38       Arrays.asList(
39           createContentValuesWithNumber("123456789"), createContentValuesWithNumber("987654321"));
40 
41   @TargetApi(VERSION_CODES.N)
populateBlockedNumber(@onNull Context context)42   public static void populateBlockedNumber(@NonNull Context context) {
43     ArrayList<ContentProviderOperation> operations = new ArrayList<>();
44     for (ContentValues value : values) {
45       operations.add(
46           ContentProviderOperation.newInsert(BlockedNumbers.CONTENT_URI)
47               .withValues(value)
48               .withYieldAllowed(true)
49               .build());
50     }
51     try {
52       context.getContentResolver().applyBatch(BlockedNumberContract.AUTHORITY, operations);
53     } catch (RemoteException | OperationApplicationException e) {
54       Assert.fail("error adding block number entries: " + e);
55     }
56   }
57 
58   @TargetApi(VERSION_CODES.N)
deleteBlockedNumbers(@onNull Context context)59   public static void deleteBlockedNumbers(@NonNull Context context) {
60     // clean BlockedNumbers db
61     context.getContentResolver().delete(BlockedNumbers.CONTENT_URI, null, null);
62   }
63 
createContentValuesWithNumber(String number)64   private static ContentValues createContentValuesWithNumber(String number) {
65     ContentValues contentValues = new ContentValues();
66     contentValues.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, number);
67     return contentValues;
68   }
69 }
70