summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/calllog/RefreshAnnotatedCallLogWorker.java
blob: d25ec5e65ce34b7fce7542379cab17e91dc6ad2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.dialer.calllog;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.OperationApplicationException;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.support.annotation.WorkerThread;
import com.android.dialer.calllog.database.CallLogDatabaseComponent;
import com.android.dialer.calllog.datasources.CallLogDataSource;
import com.android.dialer.calllog.datasources.CallLogMutations;
import com.android.dialer.calllog.datasources.DataSources;
import com.android.dialer.common.Assert;
import com.android.dialer.common.LogUtil;
import com.android.dialer.common.concurrent.DialerExecutor.Worker;
import javax.inject.Inject;

/**
 * Worker which brings the annotated call log up to date, if necessary.
 *
 * <p>Accepts a boolean which indicates if the dirty check should be skipped.
 */
public class RefreshAnnotatedCallLogWorker implements Worker<Boolean, Void> {

  private final Context appContext;
  private final DataSources dataSources;

  @Inject
  RefreshAnnotatedCallLogWorker(Context appContext, DataSources dataSources) {
    this.appContext = appContext;
    this.dataSources = dataSources;
  }

  @Override
  public Void doInBackground(Boolean skipDirtyCheck)
      throws RemoteException, OperationApplicationException {
    LogUtil.enterBlock("RefreshAnnotatedCallLogWorker.doInBackground");

    long startTime = System.currentTimeMillis();
    checkDirtyAndRebuildIfNecessary(appContext, skipDirtyCheck);
    LogUtil.i(
        "RefreshAnnotatedCallLogWorker.doInBackground",
        "took %dms",
        System.currentTimeMillis() - startTime);
    return null;
  }

  @WorkerThread
  private void checkDirtyAndRebuildIfNecessary(Context appContext, boolean skipDirtyCheck)
      throws RemoteException, OperationApplicationException {
    Assert.isWorkerThread();

    long startTime = System.currentTimeMillis();

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
    // Default to true. If the pref doesn't exist, the annotated call log hasn't been created and
    // we just skip isDirty checks and force a rebuild.
    boolean forceRebuildPrefValue =
        sharedPreferences.getBoolean(CallLogFramework.PREF_FORCE_REBUILD, true);
    if (forceRebuildPrefValue) {
      LogUtil.i(
          "RefreshAnnotatedCallLogWorker.checkDirtyAndRebuildIfNecessary",
          "annotated call log has been marked dirty or does not exist");
    }

    boolean isDirty = skipDirtyCheck || forceRebuildPrefValue || isDirty(appContext);

    LogUtil.i(
        "RefreshAnnotatedCallLogWorker.checkDirtyAndRebuildIfNecessary",
        "isDirty took: %dms",
        System.currentTimeMillis() - startTime);
    if (isDirty) {
      startTime = System.currentTimeMillis();
      rebuild(appContext);
      LogUtil.i(
          "RefreshAnnotatedCallLogWorker.checkDirtyAndRebuildIfNecessary",
          "rebuild took: %dms",
          System.currentTimeMillis() - startTime);
    }
  }

  @WorkerThread
  private boolean isDirty(Context appContext) {
    Assert.isWorkerThread();

    for (CallLogDataSource dataSource : dataSources.getDataSourcesIncludingSystemCallLog()) {
      String dataSourceName = getName(dataSource);
      long startTime = System.currentTimeMillis();
      LogUtil.i("RefreshAnnotatedCallLogWorker.isDirty", "running isDirty for %s", dataSourceName);
      boolean isDirty = dataSource.isDirty(appContext);
      LogUtil.i(
          "RefreshAnnotatedCallLogWorker.isDirty",
          "%s.isDirty returned %b in %dms",
          dataSourceName,
          isDirty,
          System.currentTimeMillis() - startTime);
      if (isDirty) {
        return true;
      }
    }
    return false;
  }

  @TargetApi(Build.VERSION_CODES.M) // Uses try-with-resources
  @WorkerThread
  private void rebuild(Context appContext) throws RemoteException, OperationApplicationException {
    Assert.isWorkerThread();

    CallLogMutations mutations = new CallLogMutations();

    // System call log data source must go first!
    CallLogDataSource systemCallLogDataSource = dataSources.getSystemCallLogDataSource();
    String dataSourceName = getName(systemCallLogDataSource);
    LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "filling %s", dataSourceName);
    long startTime = System.currentTimeMillis();
    systemCallLogDataSource.fill(appContext, mutations);
    LogUtil.i(
        "RefreshAnnotatedCallLogWorker.rebuild",
        "%s.fill took: %dms",
        dataSourceName,
        System.currentTimeMillis() - startTime);

    for (CallLogDataSource dataSource : dataSources.getDataSourcesExcludingSystemCallLog()) {
      dataSourceName = getName(dataSource);
      LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "filling %s", dataSourceName);
      startTime = System.currentTimeMillis();
      dataSource.fill(appContext, mutations);
      LogUtil.i(
          "CallLogFramework.rebuild",
          "%s.fill took: %dms",
          dataSourceName,
          System.currentTimeMillis() - startTime);
    }
    LogUtil.i("RefreshAnnotatedCallLogWorker.rebuild", "applying mutations to database");
    startTime = System.currentTimeMillis();
    CallLogDatabaseComponent.get(appContext)
        .mutationApplier()
        .applyToDatabase(mutations, appContext);
    LogUtil.i(
        "RefreshAnnotatedCallLogWorker.rebuild",
        "applyToDatabase took: %dms",
        System.currentTimeMillis() - startTime);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
    sharedPreferences
        .edit()
        .putBoolean(CallLogFramework.PREF_FORCE_REBUILD, false)
        .commit();
  }

  private static String getName(CallLogDataSource dataSource) {
    return dataSource.getClass().getSimpleName();
  }
}