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.SharedPreferences; 20 import android.support.annotation.AnyThread; 21 import android.support.annotation.VisibleForTesting; 22 import com.android.dialer.common.concurrent.Annotations.BackgroundExecutor; 23 import com.android.dialer.storage.Unencrypted; 24 import com.google.common.util.concurrent.ListenableFuture; 25 import com.google.common.util.concurrent.ListeningExecutorService; 26 import javax.annotation.concurrent.ThreadSafe; 27 import javax.inject.Inject; 28 29 /** Provides information about the state of the annotated call log. */ 30 @ThreadSafe 31 public 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 CallLogState( @nencrypted SharedPreferences sharedPreferences, @BackgroundExecutor ListeningExecutorService backgroundExecutor)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 markBuilt()52 public void markBuilt() { 53 sharedPreferences.edit().putBoolean(ANNOTATED_CALL_LOG_BUILT_PREF, true).apply(); 54 } 55 56 /** 57 * Clear the call log state. This is useful for example if the annotated call log needs to be 58 * disabled because there was a problem. 59 */ 60 @AnyThread clearData()61 public void clearData() { 62 sharedPreferences.edit().remove(ANNOTATED_CALL_LOG_BUILT_PREF).apply(); 63 } 64 65 /** 66 * Returns true if the annotated call log has been built at least once. 67 * 68 * <p>It may not yet have been built if the user was just upgraded to the new call log, or they 69 * just cleared data. 70 */ 71 @AnyThread isBuilt()72 public ListenableFuture<Boolean> isBuilt() { 73 return backgroundExecutor.submit( 74 () -> sharedPreferences.getBoolean(ANNOTATED_CALL_LOG_BUILT_PREF, false)); 75 } 76 } 77