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