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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/omaha/OmahaService.java

Issue 2664253005: [Omaha] Move most functionality to OmahaBase, add JobService (Closed)
Patch Set: [Omaha] Move most functionality to OmahaBase, add JobService 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.chrome.browser.omaha;
6
7 import android.annotation.TargetApi;
8 import android.app.IntentService;
9 import android.app.job.JobService;
10 import android.content.Context;
11 import android.os.AsyncTask;
12 import android.os.Build;
13 import android.support.annotation.Nullable;
14
15 import org.chromium.base.ContextUtils;
16 import org.chromium.base.Log;
17 import org.chromium.base.ThreadUtils;
18 import org.chromium.components.background_task_scheduler.BackgroundTask;
19 import org.chromium.components.background_task_scheduler.BackgroundTaskScheduler Factory;
20 import org.chromium.components.background_task_scheduler.TaskIds;
21 import org.chromium.components.background_task_scheduler.TaskInfo;
22 import org.chromium.components.background_task_scheduler.TaskParameters;
23
24 /**
25 * Manages scheduling and running of the Omaha client code.
26 * Delegates out to either an {@link IntentService} or {@link JobService}, as ne cessary.
27 */
28 public class OmahaService extends OmahaBase implements BackgroundTask {
29 private static final String TAG = "omaha";
30
31 private static class OmahaClientDelegate extends OmahaDelegateBase {
32 public OmahaClientDelegate(Context context) {
33 super(context);
34 }
35
36 @Override
37 public void scheduleService(long currentTimestampMs, long nextTimestampM s) {
38 if (Build.VERSION.SDK_INT < OmahaBase.MIN_API_JOB_SCHEDULER) {
39 getScheduler().createAlarm(OmahaClient.createIntent(getContext() ), nextTimestampMs);
40 Log.i(TAG, "Scheduled using AlarmManager and IntentService");
41 } else {
42 final long delay = nextTimestampMs - currentTimestampMs;
43 ThreadUtils.runOnUiThread(new Runnable() {
44 @Override
45 public void run() {
46 if (scheduleJobService(getContext(), delay)) {
47 Log.i(TAG, "Scheduled using JobService");
48 } else {
49 Log.e(TAG, "Failed to schedule job");
50 }
51 }
52 });
53 }
54 }
55 }
56
57 private static final Object DELEGATE_LOCK = new Object();
58 private static OmahaService sInstance;
59
60 @Nullable
61 public static OmahaService getInstance(Context context) {
62 synchronized (DELEGATE_LOCK) {
63 if (sInstance == null) sInstance = new OmahaService(context);
64 return sInstance;
65 }
66 }
67
68 private AsyncTask<Void, Void, Void> mJobServiceTask;
69
70 /** Used only by {@link BackgroundTaskScheduler}. */
71 public OmahaService() {
72 this(ContextUtils.getApplicationContext());
73 }
74
75 private OmahaService(Context context) {
76 super(new OmahaClientDelegate(context));
77 }
78
79 /**
80 * Trigger the {@link BackgroundTaskScheduler} immediately.
81 * Must only be called by {@link OmahaBase#onForegroundSessionStart}.
82 */
83 static void startServiceImmediately(Context context) {
84 if (Build.VERSION.SDK_INT < OmahaBase.MIN_API_JOB_SCHEDULER) {
85 context.startService(OmahaClient.createIntent(context));
86 } else {
87 scheduleJobService(context, 0);
88 }
89 }
90
91 @Override
92 @TargetApi(Build.VERSION_CODES.M)
93 public boolean onStartTask(
94 Context context, TaskParameters parameters, final TaskFinishedCallba ck callback) {
95 mJobServiceTask = new AsyncTask<Void, Void, Void>() {
96 @Override
97 public Void doInBackground(Void... params) {
98 run();
99 return null;
100 }
101
102 @Override
103 public void onPostExecute(Void result) {
104 callback.taskFinished(false);
105 }
106 }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
107 return false;
108 }
109
110 @Override
111 @TargetApi(Build.VERSION_CODES.M)
112 public boolean onStopTask(Context context, TaskParameters taskParameters) {
113 if (mJobServiceTask != null) {
114 mJobServiceTask.cancel(false);
115 mJobServiceTask = null;
116 }
117 return false;
118 }
119
120 /**
121 * Schedules the Omaha code to run at the given time.
122 * @param context Context to use.
123 * @param delayMs How long to wait until the job should be triggered.
124 */
125 @TargetApi(Build.VERSION_CODES.M)
126 static boolean scheduleJobService(Context context, long delayMs) {
127 long latency = Math.max(0, delayMs);
128
129 TaskInfo taskInfo = TaskInfo.createOneOffTask(TaskIds.OMAHA_JOB_ID, Omah aService.class,
130 latency, latency)
131 .build();
132 return BackgroundTaskSchedulerFactory.getScheduler().schedule(context, t askInfo);
133 }
134 }
OLDNEW
« no previous file with comments | « chrome/android/java/src/org/chromium/chrome/browser/omaha/OmahaDelegateImpl.java ('k') | chrome/android/java_sources.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698