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

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

Powered by Google App Engine
This is Rietveld 408576698