1/*
2 * Copyright (C) 2017 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.Context;
20import android.content.SharedPreferences;
21import android.preference.PreferenceManager;
22import android.support.annotation.MainThread;
23import android.support.annotation.Nullable;
24import com.android.dialer.calllog.datasources.CallLogDataSource;
25import com.android.dialer.common.Assert;
26import com.android.dialer.common.ConfigProviderBindings;
27import com.android.dialer.common.LogUtil;
28import javax.inject.Inject;
29import javax.inject.Singleton;
30
31/**
32 * Coordinates work across CallLog data sources to detect if the annotated call log is out of date
33 * ("dirty") and update it if necessary.
34 *
35 * <p>All methods should be called on the main thread.
36 */
37@Singleton
38public final class CallLogFramework implements CallLogDataSource.ContentObserverCallbacks {
39
40  static final String PREF_FORCE_REBUILD = "callLogFrameworkForceRebuild";
41  static final String PREF_LAST_REBUILD_TIMESTAMP_MILLIS = "callLogFrameworkLastRebuild";
42
43  private final DataSources dataSources;
44
45  @Nullable private CallLogUi ui;
46
47  @Inject
48  CallLogFramework(DataSources dataSources) {
49    this.dataSources = dataSources;
50  }
51
52  public boolean isNewCallLogEnabled(Context context) {
53    return ConfigProviderBindings.get(context).getBoolean("enable_new_call_log_tab", false);
54  }
55
56  /** Registers the content observers for all data sources. */
57  public void registerContentObservers(Context appContext) {
58    LogUtil.enterBlock("CallLogFramework.registerContentObservers");
59
60    if (!isNewCallLogEnabled(appContext)) {
61      return;
62    }
63
64    for (CallLogDataSource dataSource : dataSources.getDataSourcesIncludingSystemCallLog()) {
65      dataSource.registerContentObservers(appContext, this);
66    }
67  }
68
69  /**
70   * Attach a UI component to the framework so that it may be notified of changes to the annotated
71   * call log.
72   */
73  public void attachUi(CallLogUi ui) {
74    LogUtil.enterBlock("CallLogFramework.attachUi");
75    this.ui = ui;
76  }
77
78  /**
79   * Detaches the UI from the framework. This should be called when the UI is hidden or destroyed
80   * and no longer needs to be notified of changes to the annotated call log.
81   */
82  public void detachUi() {
83    LogUtil.enterBlock("CallLogFramework.detachUi");
84    this.ui = null;
85  }
86
87  /**
88   * Marks the call log as dirty and notifies any attached UI components. If there are no UI
89   * components currently attached, this is an efficient operation since it is just writing a shared
90   * pref.
91   *
92   * <p>We don't want to actually force a rebuild when there is no UI running because we don't want
93   * to be constantly rebuilding the database when the device is sitting on a desk and receiving a
94   * lot of calls, for example.
95   */
96  @Override
97  @MainThread
98  public void markDirtyAndNotify(Context appContext) {
99    Assert.isMainThread();
100    LogUtil.enterBlock("CallLogFramework.markDirtyAndNotify");
101
102    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
103    sharedPreferences.edit().putBoolean(PREF_FORCE_REBUILD, true).apply();
104
105    if (ui != null) {
106      ui.invalidateUi();
107    }
108  }
109
110  /** Callbacks invoked on listening UI components. */
111  public interface CallLogUi {
112
113    /** Notifies the call log UI that the annotated call log is out of date. */
114    @MainThread
115    void invalidateUi();
116  }
117}
118