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 
17 package com.android.dialer.blocking;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import android.preference.PreferenceManager;
22 import android.support.annotation.NonNull;
23 import android.support.annotation.Nullable;
24 import android.support.v4.os.UserManagerCompat;
25 import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler.OnHasBlockedNumbersListener;
26 import com.android.dialer.common.Assert;
27 import com.android.dialer.common.LogUtil;
28 import com.android.dialer.common.concurrent.DialerExecutor.Worker;
29 import com.android.dialer.common.concurrent.DialerExecutorFactory;
30 
31 /**
32  * Class responsible for checking if the user can be auto-migrated to {@link
33  * android.provider.BlockedNumberContract} blocking. In order for this to happen, the user cannot
34  * have any numbers that are blocked in the Dialer solution.
35  */
36 @Deprecated
37 public class BlockedNumbersAutoMigrator {
38 
39   static final String HAS_CHECKED_AUTO_MIGRATE_KEY = "checkedAutoMigrate";
40 
41   @NonNull private final Context appContext;
42   @NonNull private final FilteredNumberAsyncQueryHandler queryHandler;
43   @NonNull private final DialerExecutorFactory dialerExecutorFactory;
44 
45   /**
46    * Constructs the BlockedNumbersAutoMigrator with the given {@link
47    * FilteredNumberAsyncQueryHandler}.
48    *
49    * @param queryHandler The FilteredNumberAsyncQueryHandler used to determine if there are blocked
50    *     numbers.
51    * @throws NullPointerException if sharedPreferences or queryHandler are null.
52    */
BlockedNumbersAutoMigrator( @onNull Context appContext, @NonNull FilteredNumberAsyncQueryHandler queryHandler, @NonNull DialerExecutorFactory dialerExecutorFactory)53   public BlockedNumbersAutoMigrator(
54       @NonNull Context appContext,
55       @NonNull FilteredNumberAsyncQueryHandler queryHandler,
56       @NonNull DialerExecutorFactory dialerExecutorFactory) {
57     this.appContext = Assert.isNotNull(appContext);
58     this.queryHandler = Assert.isNotNull(queryHandler);
59     this.dialerExecutorFactory = Assert.isNotNull(dialerExecutorFactory);
60   }
61 
asyncAutoMigrate()62   public void asyncAutoMigrate() {
63     dialerExecutorFactory
64         .createNonUiTaskBuilder(new ShouldAttemptAutoMigrate(appContext))
65         .onSuccess(this::autoMigrate)
66         .build()
67         .executeParallel(null);
68   }
69 
70   /**
71    * Attempts to perform the auto-migration. Auto-migration will only be attempted once and can be
72    * performed only when the user has no blocked numbers. As a result of this method, the user will
73    * be migrated to the framework blocking solution if blocked numbers don't exist.
74    */
autoMigrate(boolean shouldAttemptAutoMigrate)75   private void autoMigrate(boolean shouldAttemptAutoMigrate) {
76     if (!shouldAttemptAutoMigrate) {
77       return;
78     }
79 
80     LogUtil.i("BlockedNumbersAutoMigrator", "attempting to auto-migrate.");
81     queryHandler.hasBlockedNumbers(
82         new OnHasBlockedNumbersListener() {
83           @Override
84           public void onHasBlockedNumbers(boolean hasBlockedNumbers) {
85             if (hasBlockedNumbers) {
86               LogUtil.i("BlockedNumbersAutoMigrator", "not auto-migrating: blocked numbers exist.");
87               return;
88             }
89             LogUtil.i("BlockedNumbersAutoMigrator", "auto-migrating: no blocked numbers.");
90             FilteredNumberCompat.setHasMigratedToNewBlocking(appContext, true);
91           }
92         });
93   }
94 
95   private static class ShouldAttemptAutoMigrate implements Worker<Void, Boolean> {
96     private final Context appContext;
97 
ShouldAttemptAutoMigrate(Context appContext)98     ShouldAttemptAutoMigrate(Context appContext) {
99       this.appContext = appContext;
100     }
101 
102     @Nullable
103     @Override
doInBackground(@ullable Void input)104     public Boolean doInBackground(@Nullable Void input) {
105       if (!UserManagerCompat.isUserUnlocked(appContext)) {
106         LogUtil.i("BlockedNumbersAutoMigrator", "not attempting auto-migrate: device is locked");
107         return false;
108       }
109       SharedPreferences sharedPreferences =
110           PreferenceManager.getDefaultSharedPreferences(appContext);
111 
112       if (sharedPreferences.contains(HAS_CHECKED_AUTO_MIGRATE_KEY)) {
113         LogUtil.v(
114             "BlockedNumbersAutoMigrator", "not attempting auto-migrate: already checked once.");
115         return false;
116       }
117 
118       if (!FilteredNumberCompat.canAttemptBlockOperations(appContext)) {
119         // This may be the case where the user is on the lock screen, so we shouldn't record that
120         // the migration status was checked.
121         LogUtil.i(
122             "BlockedNumbersAutoMigrator", "not attempting auto-migrate: current user can't block");
123         return false;
124       }
125       LogUtil.i(
126           "BlockedNumbersAutoMigrator", "updating state as already checked for auto-migrate.");
127       sharedPreferences.edit().putBoolean(HAS_CHECKED_AUTO_MIGRATE_KEY, true).apply();
128 
129       if (!FilteredNumberCompat.canUseNewFiltering()) {
130         LogUtil.i("BlockedNumbersAutoMigrator", "not attempting auto-migrate: not available.");
131         return false;
132       }
133 
134       if (FilteredNumberCompat.hasMigratedToNewBlocking(appContext)) {
135         LogUtil.i("BlockedNumbersAutoMigrator", "not attempting auto-migrate: already migrated.");
136         return false;
137       }
138       return true;
139     }
140   }
141 }
142