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

Side by Side Diff: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerJobService.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.JobInfo;
9 import android.app.job.JobParameters;
10 import android.app.job.JobScheduler;
11 import android.content.ComponentName;
12 import android.content.Context;
13 import android.os.Build;
14 import android.os.Bundle;
15 import android.os.PersistableBundle;
16
17 import org.chromium.base.Log;
18 import org.chromium.base.ThreadUtils;
19 import org.chromium.base.VisibleForTesting;
20
21 /**
22 * An implementation of {@link BackgroundTaskSchedulerDelegate} that uses the sy stem
23 * {@link JobScheduler} to schedule jobs.
24 */
25 @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
26 class BackgroundTaskSchedulerJobService implements BackgroundTaskSchedulerDelega te {
27 private static final String TAG = "BkgrdTaskSchedulerJS";
28
29 private static final String BACKGROUND_TASK_CLASS_KEY = "_background_task_cl ass";
30 @VisibleForTesting
31 static final String BACKGROUND_TASK_EXTRAS_KEY = "_background_task_extras";
32
33 static BackgroundTask getBackgroundTaskFromJobParameters(JobParameters jobPa rameters) {
34 String backgroundTaskClassName = getBackgroundTaskClassFromJobParameters (jobParameters);
35 return BackgroundTaskScheduler.getBackgroundTaskFromClassName(background TaskClassName);
36 }
37
38 private static String getBackgroundTaskClassFromJobParameters(JobParameters jobParameters) {
39 PersistableBundle extras = jobParameters.getExtras();
40 if (extras == null) return null;
41 return extras.getString(BACKGROUND_TASK_CLASS_KEY);
42 }
43
44 /**
45 * Retrieves the {@link TaskParameters} from the {@link JobParameters}, whic h are passed as
46 * one of the keys. Only values valid for {@link android.os.BaseBundle} are supported, and other
47 * values are stripped at the time when the task is scheduled.
48 *
49 * @param jobParameters the {@link JobParameters} to extract the {@link Task Parameters} from.
50 * @return the {@link TaskParameters} for the current job.
51 */
52 static TaskParameters getTaskParametersFromJobParameters(JobParameters jobPa rameters) {
53 TaskParameters.Builder builder = TaskParameters.create(jobParameters.get JobId());
54
55 PersistableBundle jobExtras = jobParameters.getExtras();
56 PersistableBundle persistableTaskExtras =
57 jobExtras.getPersistableBundle(BACKGROUND_TASK_EXTRAS_KEY);
58
59 Bundle taskExtras = new Bundle();
60 taskExtras.putAll(persistableTaskExtras);
61 builder.addExtras(taskExtras);
62
63 return builder.build();
64 }
65
66 @VisibleForTesting
67 static JobInfo createJobInfoFromTaskInfo(Context context, TaskInfo taskInfo) {
68 PersistableBundle jobExtras = new PersistableBundle();
69 jobExtras.putString(BACKGROUND_TASK_CLASS_KEY, taskInfo.getBackgroundTas kClass().getName());
70
71 PersistableBundle persistableBundle = getTaskExtrasAsPersistableBundle(t askInfo);
72 jobExtras.putPersistableBundle(BACKGROUND_TASK_EXTRAS_KEY, persistableBu ndle);
73
74 JobInfo.Builder builder =
75 new JobInfo
76 .Builder(taskInfo.getTaskId(),
77 new ComponentName(context, BackgroundTaskJobServ ice.class))
78 .setExtras(jobExtras)
79 .setPersisted(taskInfo.isPersisted())
80 .setRequiresCharging(taskInfo.requiresCharging())
81 .setRequiredNetworkType(getJobInfoNetworkTypeFromTaskNet workType(
82 taskInfo.getRequiredNetworkType()));
83
84 if (taskInfo.isPeriodic()) {
85 builder = getPeriodicJobInfo(builder, taskInfo);
86 } else {
87 builder = getOneOffJobInfo(builder, taskInfo);
88 }
89
90 return builder.build();
91 }
92
93 private static JobInfo.Builder getOneOffJobInfo(JobInfo.Builder builder, Tas kInfo taskInfo) {
94 TaskInfo.OneOffInfo oneOffInfo = taskInfo.getOneOffInfo();
95 if (oneOffInfo.hasWindowStartTimeConstraint()) {
96 builder = builder.setMinimumLatency(oneOffInfo.getWindowStartTimeMs( ));
97 }
98 return builder.setOverrideDeadline(oneOffInfo.getWindowEndTimeMs());
99 }
100
101 private static JobInfo.Builder getPeriodicJobInfo(JobInfo.Builder builder, T askInfo taskInfo) {
102 TaskInfo.PeriodicInfo periodicInfo = taskInfo.getPeriodicInfo();
103 if (periodicInfo.hasFlex()) {
104 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
105 return builder.setPeriodic(periodicInfo.getIntervalMs(), periodi cInfo.getFlexMs());
106 }
107 return builder.setPeriodic(periodicInfo.getIntervalMs());
108 }
109 return builder.setPeriodic(periodicInfo.getIntervalMs());
110 }
111
112 private static int getJobInfoNetworkTypeFromTaskNetworkType(
113 @TaskInfo.NetworkType int networkType) {
114 // The values are hard coded to represent the same as the network type f rom JobService.
115 return networkType;
116 }
117
118 private static PersistableBundle getTaskExtrasAsPersistableBundle(TaskInfo t askInfo) {
119 Bundle taskExtras = taskInfo.getExtras();
120 BundleToPersistableBundleConverter.Result convertedData =
121 BundleToPersistableBundleConverter.convert(taskExtras);
122 if (convertedData.hasErrors()) {
123 Log.w(TAG, "Failed converting extras to PersistableBundle: "
124 + convertedData.getFailedKeysErrorString());
125 }
126 return convertedData.getPersistableBundle();
127 }
128
129 @Override
130 public boolean schedule(Context context, TaskInfo taskInfo) {
131 ThreadUtils.assertOnUiThread();
132 if (!BackgroundTaskScheduler.hasParameterlessPublicConstructor(
133 taskInfo.getBackgroundTaskClass())) {
134 Log.e(TAG, "BackgroundTask " + taskInfo.getBackgroundTaskClass()
135 + " has no parameterless public constructor.");
136 return false;
137 }
138
139 JobInfo jobInfo = createJobInfoFromTaskInfo(context, taskInfo);
140
141 JobScheduler jobScheduler =
142 (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SE RVICE);
143
144 if (taskInfo.shouldUpdateCurrent()) {
145 jobScheduler.cancel(taskInfo.getTaskId());
146 }
147
148 int result = jobScheduler.schedule(jobInfo);
149 return result == JobScheduler.RESULT_SUCCESS;
150 }
151
152 @Override
153 public void cancel(Context context, int taskId) {
154 ThreadUtils.assertOnUiThread();
155 JobScheduler jobScheduler =
156 (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SE RVICE);
157 jobScheduler.cancel(taskId);
158 }
159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698