1/*
2 * Copyright (C) 2016 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.shortcuts;
18
19import android.annotation.TargetApi;
20import android.app.job.JobParameters;
21import android.app.job.JobService;
22import android.os.AsyncTask;
23import android.os.Build.VERSION_CODES;
24import android.support.annotation.MainThread;
25import android.support.annotation.NonNull;
26import android.support.annotation.WorkerThread;
27import com.android.dialer.common.Assert;
28import com.android.dialer.common.LogUtil;
29
30/** {@link AsyncTask} used by the periodic job service to refresh dynamic and pinned shortcuts. */
31@TargetApi(VERSION_CODES.N_MR1) // Shortcuts introduced in N MR1
32final class RefreshShortcutsTask extends AsyncTask<JobParameters, Void, JobParameters> {
33
34  private final JobService jobService;
35
36  RefreshShortcutsTask(@NonNull JobService jobService) {
37    this.jobService = jobService;
38  }
39
40  /** @param params array with length 1, provided from PeriodicJobService */
41  @Override
42  @NonNull
43  @WorkerThread
44  protected JobParameters doInBackground(JobParameters... params) {
45    Assert.isWorkerThread();
46    LogUtil.enterBlock("RefreshShortcutsTask.doInBackground");
47
48    // Dynamic shortcuts are refreshed from the UI but icons can become stale, so update them
49    // periodically using the job service.
50    //
51    // The reason that icons can become is stale is that there is no last updated timestamp for
52    // pictures; there is only a last updated timestamp for the entire contact row, which changes
53    // frequently (for example, when they are called their "times_contacted" is incremented).
54    // Relying on such a spuriously updated timestamp would result in too frequent shortcut updates,
55    // so instead we just allow the icon to become stale in the case that the contact's photo is
56    // updated, and then rely on the job service to periodically force update it.
57    new DynamicShortcuts(jobService, new IconFactory(jobService)).updateIcons(); // Blocking
58    new PinnedShortcuts(jobService).refresh(); // Blocking
59
60    return params[0];
61  }
62
63  @Override
64  @MainThread
65  protected void onPostExecute(JobParameters params) {
66    Assert.isMainThread();
67    LogUtil.enterBlock("RefreshShortcutsTask.onPostExecute");
68
69    jobService.jobFinished(params, false /* needsReschedule */);
70  }
71}
72