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
17package com.android.dialer.filterednumber;
18
19import com.google.common.base.Preconditions;
20
21import android.content.SharedPreferences;
22import android.util.Log;
23
24import com.android.dialer.compat.FilteredNumberCompat;
25import com.android.dialer.database.FilteredNumberAsyncQueryHandler;
26import com.android.dialer.database.FilteredNumberAsyncQueryHandler.OnHasBlockedNumbersListener;
27
28/**
29 * Class responsible for checking if the user can be auto-migrated to {@link
30 * android.provider.BlockedNumberContract} blocking. In order for this to happen, the user cannot
31 * have any numbers that are blocked in the Dialer solution.
32 */
33public class BlockedNumbersAutoMigrator {
34
35    private static final String TAG = "BlockedNumbersAuto";
36
37    private static final String HAS_CHECKED_AUTO_MIGRATE_KEY = "checkedAutoMigrate";
38
39    private final SharedPreferences sharedPreferences;
40    private final FilteredNumberAsyncQueryHandler queryHandler;
41
42    /**
43     * Constructs the BlockedNumbersAutoMigrator with the given {@link SharedPreferences} and {@link
44     * FilteredNumberAsyncQueryHandler}.
45     *
46     * @param sharedPreferences The SharedPreferences used to persist information.
47     * @param queryHandler The FilteredNumberAsyncQueryHandler used to determine if there are
48     * blocked numbers.
49     * @throws NullPointerException if sharedPreferences or queryHandler are null.
50     */
51    public BlockedNumbersAutoMigrator(SharedPreferences sharedPreferences,
52            FilteredNumberAsyncQueryHandler queryHandler) {
53        this.sharedPreferences = Preconditions.checkNotNull(sharedPreferences);
54        this.queryHandler = Preconditions.checkNotNull(queryHandler);
55    }
56
57    /**
58     * Attempts to perform the auto-migration. Auto-migration will only be attempted once and can be
59     * performed only when the user has no blocked numbers. As a result of this method, the user
60     * will be migrated to the framework blocking solution, as determined by {@link
61     * FilteredNumberCompat#hasMigratedToNewBlocking()}.
62     */
63    public void autoMigrate() {
64        if (!shouldAttemptAutoMigrate()) {
65            return;
66        }
67
68        Log.i(TAG, "Attempting to auto-migrate.");
69        queryHandler.hasBlockedNumbers(new OnHasBlockedNumbersListener() {
70            @Override
71            public void onHasBlockedNumbers(boolean hasBlockedNumbers) {
72                if (hasBlockedNumbers) {
73                    Log.i(TAG, "Not auto-migrating: blocked numbers exist.");
74                    return;
75                }
76                Log.i(TAG, "Auto-migrating: no blocked numbers.");
77                FilteredNumberCompat.setHasMigratedToNewBlocking(true);
78            }
79        });
80    }
81
82    private boolean shouldAttemptAutoMigrate() {
83        if (sharedPreferences.contains(HAS_CHECKED_AUTO_MIGRATE_KEY)) {
84            Log.d(TAG, "Not attempting auto-migrate: already checked once.");
85            return false;
86        }
87        Log.i(TAG, "Updating state as already checked for auto-migrate.");
88        sharedPreferences.edit().putBoolean(HAS_CHECKED_AUTO_MIGRATE_KEY, true).apply();
89
90        if (!FilteredNumberCompat.canUseNewFiltering()) {
91            Log.i(TAG, "Not attempting auto-migrate: not available.");
92            return false;
93        }
94
95        if (FilteredNumberCompat.hasMigratedToNewBlocking()) {
96            Log.i(TAG, "Not attempting auto-migrate: already migrated.");
97            return false;
98        }
99        return true;
100    }
101}
102