OLD | NEW |
(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 import java.util.concurrent.TimeUnit; |
| 16 |
| 17 /** |
| 18 * The background job scheduler class used for scheduling tasks using JobSchedul
er. |
| 19 */ |
| 20 @TargetApi(Build.VERSION_CODES.N) |
| 21 public class BackgroundJobScheduler extends BackgroundScheduler { |
| 22 public static final int JOB_ID = 774322033; |
| 23 |
| 24 public BackgroundJobScheduler(Context context) { |
| 25 super(context); |
| 26 } |
| 27 |
| 28 @Override |
| 29 public void cancel() { |
| 30 getJobScheduler().cancel(JOB_ID); |
| 31 } |
| 32 |
| 33 @Override |
| 34 protected void scheduleImpl(TriggerConditions triggerConditions, long delayS
tartSeconds, |
| 35 long executionDeadlineSeconds, boolean overwrite) { |
| 36 if (!overwrite) { |
| 37 JobInfo existingJob = getJobScheduler().getPendingJob(JOB_ID); |
| 38 if (existingJob != null) return; |
| 39 } |
| 40 |
| 41 PersistableBundle taskExtras = new PersistableBundle(); |
| 42 TaskExtrasPacker.packTimeInBundle(taskExtras); |
| 43 TaskExtrasPacker.packTriggerConditionsInBundle(taskExtras, triggerCondit
ions); |
| 44 |
| 45 JobInfo jobInfo = |
| 46 new JobInfo |
| 47 .Builder(JOB_ID, new ComponentName( |
| 48 getContext(), BackgroundSchedul
erJobService.class)) |
| 49 .setMinimumLatency(TimeUnit.SECONDS.toMillis(delayStartS
econds)) |
| 50 .setOverrideDeadline(TimeUnit.SECONDS.toMillis(execution
DeadlineSeconds)) |
| 51 .setPersisted(true) // Across device resets. |
| 52 .setRequiredNetworkType(triggerConditions.requireUnmeter
edNetwork() |
| 53 ? JobInfo.NETWORK_TYPE_UNMETERED |
| 54 : JobInfo.NETWORK_TYPE_ANY) |
| 55 .setRequiresCharging(triggerConditions.requirePowerConne
cted()) |
| 56 .setExtras(taskExtras) |
| 57 .build(); |
| 58 |
| 59 getJobScheduler().schedule(jobInfo); |
| 60 } |
| 61 |
| 62 private JobScheduler getJobScheduler() { |
| 63 return (JobScheduler) getContext().getSystemService(Context.JOB_SCHEDULE
R_SERVICE); |
| 64 } |
| 65 } |
OLD | NEW |