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 
17 package com.android.dialer.shortcuts;
18 
19 import android.annotation.TargetApi;
20 import android.app.job.JobParameters;
21 import android.app.job.JobService;
22 import android.os.AsyncTask;
23 import android.os.Build.VERSION_CODES;
24 import android.support.annotation.MainThread;
25 import android.support.annotation.NonNull;
26 import android.support.annotation.WorkerThread;
27 import com.android.dialer.common.Assert;
28 import 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
32 final class RefreshShortcutsTask extends AsyncTask<JobParameters, Void, JobParameters> {
33 
34   private final JobService jobService;
35 
RefreshShortcutsTask(@onNull JobService jobService)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
doInBackground(JobParameters... params)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
onPostExecute(JobParameters params)65   protected void onPostExecute(JobParameters params) {
66     Assert.isMainThread();
67     LogUtil.enterBlock("RefreshShortcutsTask.onPostExecute");
68 
69     jobService.jobFinished(params, false /* needsReschedule */);
70   }
71 }
72