Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskJobService.java

Issue 2714463002: [android] Add JobScheduler-based BackgroundTaskScheduler. (Closed)
Patch Set: FindBugs wants the real Pi, but I won't give it. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.components.background_task_scheduler;
6
7 import android.annotation.TargetApi;
8 import android.app.job.JobParameters;
9 import android.app.job.JobService;
10 import android.os.Build;
11
12 import org.chromium.base.Log;
13 import org.chromium.base.ThreadUtils;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 /** Delegates calls out to various tasks that need to run in the background. */
19 @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
20 public class BackgroundTaskJobService extends JobService {
21 private static final String TAG = "BkgrdTaskJS";
22
23 private static class TaskFinishedCallbackJobService
24 implements BackgroundTask.TaskFinishedCallback {
25 private final BackgroundTaskJobService mJobService;
26 private final BackgroundTask mBackgroundTask;
27 private final JobParameters mParams;
28
29 TaskFinishedCallbackJobService(BackgroundTaskJobService jobService,
30 BackgroundTask backgroundTask, JobParameters params) {
31 mJobService = jobService;
32 mBackgroundTask = backgroundTask;
33 mParams = params;
34 }
35
36 @Override
37 public void taskFinished(final boolean needsReschedule) {
38 // Need to remove the current task from the currently running tasks. All other access
39 // happens on the main thread, so do this removal also on the main t hread.
40 // To ensure that a new job is not immediately scheduled in between removing the task
41 // from being a current task and before calling jobFinished, leading to us finishing
42 // something with the same ID, call
43 // {@link JobService#jobFinished(JobParameters, boolean} also on the main thread.
44 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
45 @Override
46 public void run() {
47 if (!isCurrentBackgroundTaskForJobId()) {
48 Log.e(TAG, "Tried finishing non-current BackgroundTask." );
49 return;
50 }
51
52 mJobService.mCurrentTasks.remove(mParams.getJobId());
53 mJobService.jobFinished(mParams, needsReschedule);
54 }
55 });
56 }
57
58 private boolean isCurrentBackgroundTaskForJobId() {
59 return mJobService.mCurrentTasks.get(mParams.getJobId()) == mBackgro undTask;
60 }
61 }
62
63 private final Map<Integer, BackgroundTask> mCurrentTasks = new HashMap<>();
64
65 @Override
66 public boolean onStartJob(JobParameters params) {
67 ThreadUtils.assertOnUiThread();
68 BackgroundTask backgroundTask =
69 BackgroundTaskSchedulerJobService.getBackgroundTaskFromJobParame ters(params);
70 if (backgroundTask == null) {
71 Log.w(TAG, "Failed to start task. Could not instantiate class.");
72 return false;
73 }
74
75 mCurrentTasks.put(params.getJobId(), backgroundTask);
76
77 TaskParameters taskParams =
78 BackgroundTaskSchedulerJobService.getTaskParametersFromJobParame ters(params);
79 boolean taskNeedsBackgroundProcessing = backgroundTask.onStartTask(getAp plicationContext(),
80 taskParams, new TaskFinishedCallbackJobService(this, backgroundT ask, params));
81
82 if (!taskNeedsBackgroundProcessing) mCurrentTasks.remove(params.getJobId ());
83 return taskNeedsBackgroundProcessing;
84 }
85
86 @Override
87 public boolean onStopJob(JobParameters params) {
88 ThreadUtils.assertOnUiThread();
89 if (!mCurrentTasks.containsKey(params.getJobId())) {
90 Log.w(TAG, "Failed to stop job, because job with job id " + params.g etJobId()
91 + " does not exist.");
92 return false;
93 }
94
95 BackgroundTask backgroundTask = mCurrentTasks.get(params.getJobId());
96
97 TaskParameters taskParams =
98 BackgroundTaskSchedulerJobService.getTaskParametersFromJobParame ters(params);
99 boolean taskNeedsReschedule =
100 backgroundTask.onStopTask(getApplicationContext(), taskParams);
101 mCurrentTasks.remove(params.getJobId());
102 return taskNeedsReschedule;
103 }
104 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698