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
17package com.android.dialer.calllog;
18
19import android.content.SharedPreferences;
20import android.support.annotation.AnyThread;
21import android.support.annotation.VisibleForTesting;
22import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor;
23import com.android.dialer.storage.Unencrypted;
24import com.google.common.util.concurrent.ListenableFuture;
25import com.google.common.util.concurrent.ListeningExecutorService;
26import javax.annotation.concurrent.ThreadSafe;
27import javax.inject.Inject;
28
29/** Provides information about the state of the annotated call log. */
30@ThreadSafe
31public final class CallLogState {
32
33  private static final String ANNOTATED_CALL_LOG_BUILT_PREF = "annotated_call_log_built";
34
35  private final SharedPreferences sharedPreferences;
36  private final ListeningExecutorService backgroundExecutor;
37
38  @VisibleForTesting
39  @Inject
40  public CallLogState(
41      @Unencrypted SharedPreferences sharedPreferences,
42      @BackgroundExecutor ListeningExecutorService backgroundExecutor) {
43    this.sharedPreferences = sharedPreferences;
44    this.backgroundExecutor = backgroundExecutor;
45  }
46
47  /**
48   * Mark the call log as having been built. This is written to disk the first time the annotated
49   * call log has been built and shouldn't ever be reset unless the user clears data.
50   */
51  @AnyThread
52  public void markBuilt() {
53    sharedPreferences.edit().putBoolean(ANNOTATED_CALL_LOG_BUILT_PREF, true).apply();
54  }
55
56  /**
57   * Returns true if the annotated call log has been built at least once.
58   *
59   * <p>It may not yet have been built if the user was just upgraded to the new call log, or they
60   * just cleared data.
61   */
62  @AnyThread
63  public ListenableFuture<Boolean> isBuilt() {
64    return backgroundExecutor.submit(
65        () -> sharedPreferences.getBoolean(ANNOTATED_CALL_LOG_BUILT_PREF, false));
66  }
67}
68