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