• 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.calllog;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import com.android.dialer.common.LogUtil;
22 import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
23 import com.android.dialer.configprovider.ConfigProviderBindings;
24 import com.android.dialer.inject.ApplicationContext;
25 import com.android.dialer.storage.Unencrypted;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import com.google.common.util.concurrent.ListeningExecutorService;
29 import com.google.common.util.concurrent.MoreExecutors;
30 import javax.inject.Inject;
31 
32 /**
33  * Builds the annotated call log on application create once after the feature is enabled to reduce
34  * the latency the first time call log is shown.
35  */
36 public final class AnnotatedCallLogMigrator {
37 
38   private static final String PREF_MIGRATED = "annotatedCallLogMigratorMigrated";
39 
40   private final Context appContext;
41   private final SharedPreferences sharedPreferences;
42   private final RefreshAnnotatedCallLogWorker refreshAnnotatedCallLogWorker;
43   private final ListeningExecutorService backgroundExecutor;
44 
45   @Inject
AnnotatedCallLogMigrator( @pplicationContext Context appContext, @Unencrypted SharedPreferences sharedPreferences, @BackgroundExecutor ListeningExecutorService backgroundExecutor, RefreshAnnotatedCallLogWorker refreshAnnotatedCallLogWorker)46   AnnotatedCallLogMigrator(
47       @ApplicationContext Context appContext,
48       @Unencrypted SharedPreferences sharedPreferences,
49       @BackgroundExecutor ListeningExecutorService backgroundExecutor,
50       RefreshAnnotatedCallLogWorker refreshAnnotatedCallLogWorker) {
51     this.appContext = appContext;
52     this.sharedPreferences = sharedPreferences;
53     this.backgroundExecutor = backgroundExecutor;
54     this.refreshAnnotatedCallLogWorker = refreshAnnotatedCallLogWorker;
55   }
56 
57   /**
58    * Builds the annotated call log on application create once after the feature is enabled to reduce
59    * the latency the first time call log is shown.
60    */
migrate()61   public ListenableFuture<Void> migrate() {
62     return Futures.transformAsync(
63         shouldMigrate(),
64         (shouldMigrate) -> {
65           if (!shouldMigrate) {
66             return Futures.immediateFuture(null);
67           }
68           LogUtil.i("AnnotatedCallLogMigrator.migrate", "migrating annotated call log");
69           return Futures.transform(
70               refreshAnnotatedCallLogWorker.refreshWithoutDirtyCheck(),
71               (unused) -> {
72                 sharedPreferences.edit().putBoolean(PREF_MIGRATED, true).apply();
73                 return null;
74               },
75               MoreExecutors.directExecutor());
76         },
77         MoreExecutors.directExecutor());
78   }
79 
80   private ListenableFuture<Boolean> shouldMigrate() {
81     return backgroundExecutor.submit(
82         () -> {
83           if (!(ConfigProviderBindings.get(appContext)
84               .getBoolean("is_nui_shortcut_enabled", false))) {
85             return false;
86           }
87           if (sharedPreferences.getBoolean(PREF_MIGRATED, false)) {
88             return false;
89           }
90           return true;
91         });
92   }
93 }
94