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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/offlinepages/BackgroundJobScheduler.java

Issue 2686203002: [Offline pages] Creating BackgroundJobScheduler, which uses JobScheduler (Closed)
Patch Set: Adding null checks 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.chrome.browser.offlinepages;
6
7 import android.annotation.TargetApi;
8 import android.app.job.JobInfo;
9 import android.app.job.JobScheduler;
10 import android.content.ComponentName;
11 import android.content.Context;
12 import android.os.Build;
13 import android.os.PersistableBundle;
14
15 /**
16 * The background job scheduler class used for scheduling tasks using JobSchedul er.
17 */
18 @TargetApi(Build.VERSION_CODES.N)
19 public class BackgroundJobScheduler extends BackgroundScheduler {
20 public static final int JOB_ID = 774322033;
21 private static final long MILLISECONDS_IN_SECOND = 1000;
nyquist 2017/02/16 23:49:32 TimeUnit.SECONDS.toMillis(1)?
fgorski 2017/02/17 22:31:15 Done.
22
23 public BackgroundJobScheduler(Context context) {
24 super(context);
25 }
26
27 @Override
28 public void cancel() {
29 JobScheduler jobScheduler = getJobScheduler();
30 if (jobScheduler == null) return;
nyquist 2017/02/16 23:49:32 When will this be null? And if it is, would you wa
fgorski 2017/02/17 22:31:15 This piece of code was added, as I was trying to s
31 jobScheduler.cancel(JOB_ID);
32 }
33
34 @Override
35 protected void scheduleImpl(TriggerConditions triggerConditions, long delayS tartSeconds,
36 long executionDeadlineSeconds, boolean overwrite) {
37 JobScheduler jobScheduler = getJobScheduler();
38 if (jobScheduler == null) return;
39
40 if (!overwrite) {
41 JobInfo existingJob = jobScheduler.getPendingJob(JOB_ID);
42 if (existingJob != null) return;
43 }
44
45 PersistableBundle taskExtras = new PersistableBundle();
46 TaskExtrasPacker.packTimeInBundle(taskExtras);
47 TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerCondit ions);
48
49 JobInfo jobInfo =
50 new JobInfo
51 .Builder(JOB_ID, new ComponentName(
52 getContext(), BackgroundSchedul erJobService.class))
53 .setMinimumLatency(delayStartSeconds * MILLISECONDS_IN_S ECOND)
nyquist 2017/02/16 23:49:32 How about something like: TimeUnit.SECONDS.toMilli
fgorski 2017/02/17 22:31:15 Done.
54 .setOverrideDeadline(executionDeadlineSeconds * MILLISEC ONDS_IN_SECOND)
55 .setPersisted(true) // across device resets
nyquist 2017/02/16 23:49:32 Nit: Double space before // and period at end of s
fgorski 2017/02/17 22:31:15 Done.
56 .setRequiredNetworkType(triggerConditions.requireUnmeter edNetwork()
57 ? JobInfo.NETWORK_TYPE_UNMETERED
58 : JobInfo.NETWORK_TYPE_ANY)
59 .setRequiresCharging(triggerConditions.requirePowerConne cted())
60 .setExtras(taskExtras)
61 .build();
62
63 jobScheduler.schedule(jobInfo);
64 }
65
66 private JobScheduler getJobScheduler() {
67 return (JobScheduler) getContext().getSystemService(Context.JOB_SCHEDULE R_SERVICE);
68 }
69 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698