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

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: Clean up background section of documentation Created 3 years, 10 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 class BackgroundTaskJobService extends JobService {
Peter Beverloo 2017/02/24 19:43:49 This must be a `public` class so that Android can
21 private static final String TAG = "BkgrdTaskJS";
Peter Beverloo 2017/02/24 18:07:09 Why not use the class' name, which is way more com
nyquist 2017/02/24 23:41:11 Nope. It's hidden in //PRESUBMIT.py:_CheckAndroidC
22
23 private static class TaskFinishedCallbackJobService
24 implements BackgroundTask.TaskFinishedCallback {
25 private final BackgroundTaskJobService mJobService;
26 private final JobParameters mParams;
27
28 TaskFinishedCallbackJobService(BackgroundTaskJobService jobService, JobP arameters params) {
29 mJobService = jobService;
30 mParams = params;
31 }
32
33 @Override
34 public void taskFinished(final boolean needsReSchedule) {
35 // Need to remove the current task from the currently running tasks. All other access
36 // happens on the main thread, so do this removal also on the main t hread.
37 // To ensure that a new job is not immediately scheduled in between removing the task
38 // from being a current task and before calling jobFinished, leading to us finishing
39 // something with the same ID, call
40 // {@link JobService#jobFinished(JobParameters, boolean} also on the main thread.
41 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
42 @Override
43 public void run() {
44 // FIXME what if this happens way later?
nyquist 2017/02/22 18:24:12 Self-review: Need to look into this. The thought i
45 mJobService.mCurrentTasks.remove(mParams.getJobId());
46 mJobService.jobFinished(mParams, needsReSchedule);
47 }
48 });
49 }
50 }
51
52 private final Map<Integer, BackgroundTask> mCurrentTasks = new HashMap<>();
53
54 @Override
55 public boolean onStartJob(JobParameters params) {
56 ThreadUtils.assertOnUiThread();
57 BackgroundTask backgroundTask =
58 BackgroundTaskSchedulerJobService.getBackgroundTaskFromJobParame ters(params);
59 if (backgroundTask == null) {
60 Log.w(TAG, "Failed to start job, because class does not exist.");
David Trainor- moved to gerrit 2017/02/24 07:19:48 can't instantiate class?
nyquist 2017/02/24 23:41:11 Done.
61 return false;
62 }
63
64 mCurrentTasks.put(params.getJobId(), backgroundTask);
65
66 TaskParameters taskParams =
67 BackgroundTaskSchedulerJobService.getTaskParametersFromJobParame ters(params);
68 boolean taskNeedsBackgroundProcessing = backgroundTask.onStartTask(getAp plicationContext(),
69 taskParams, new TaskFinishedCallbackJobService(this, params));
70
71 if (!taskNeedsBackgroundProcessing) mCurrentTasks.remove(params.getJobId ());
72 return taskNeedsBackgroundProcessing;
73 }
74
75 @Override
76 public boolean onStopJob(JobParameters params) {
77 ThreadUtils.assertOnUiThread();
78 if (!mCurrentTasks.containsKey(params.getJobId())) {
79 Log.w(TAG, "Failed to stop job, because job with job id " + params.g etJobId()
80 + " does not exist.");
81 return false;
82 }
83
84 BackgroundTask backgroundTask = mCurrentTasks.get(params.getJobId());
85
86 TaskParameters taskParams =
87 BackgroundTaskSchedulerJobService.getTaskParametersFromJobParame ters(params);
88 boolean taskNeedsReSchedule =
89 backgroundTask.onStopTask(getApplicationContext(), taskParams);
90 mCurrentTasks.remove(params.getJobId());
91 return taskNeedsReSchedule;
92 }
93 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698