OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.components.background_task_scheduler; | 5 package org.chromium.components.background_task_scheduler; |
6 | 6 |
7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
8 import android.os.Build; | 8 import android.os.Build; |
9 | 9 |
10 import org.chromium.base.ThreadUtils; | 10 import org.chromium.base.ThreadUtils; |
| 11 import org.chromium.base.VisibleForTesting; |
11 | 12 |
12 /** | 13 /** |
13 * A factory for {@link BackgroundTaskScheduler} that ensures there is only ever
a single instance. | 14 * A factory for {@link BackgroundTaskScheduler} that ensures there is only ever
a single instance. |
14 */ | 15 */ |
15 public final class BackgroundTaskSchedulerFactory { | 16 public final class BackgroundTaskSchedulerFactory { |
16 private static BackgroundTaskScheduler sInstance; | 17 private static BackgroundTaskScheduler sInstance; |
17 | 18 |
18 @TargetApi(Build.VERSION_CODES.M) | 19 @TargetApi(Build.VERSION_CODES.M) |
19 private static BackgroundTaskSchedulerDelegate getSchedulerDelegate() { | 20 private static BackgroundTaskSchedulerDelegate getSchedulerDelegate() { |
20 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | 21 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
21 return new BackgroundTaskSchedulerJobService(); | 22 return new BackgroundTaskSchedulerJobService(); |
22 } else { | 23 } else { |
23 return new BackgroundTaskSchedulerGcmNetworkManager(); | 24 return new BackgroundTaskSchedulerGcmNetworkManager(); |
24 } | 25 } |
25 } | 26 } |
26 | 27 |
27 /** | 28 /** |
28 * @return the current instance of the {@link BackgroundTaskScheduler}. Crea
tes one if none | 29 * @return the current instance of the {@link BackgroundTaskScheduler}. Crea
tes one if none |
29 * exist. | 30 * exist. |
30 */ | 31 */ |
31 public static BackgroundTaskScheduler getScheduler() { | 32 public static BackgroundTaskScheduler getScheduler() { |
32 ThreadUtils.assertOnUiThread(); | 33 ThreadUtils.assertOnUiThread(); |
33 if (sInstance == null) sInstance = new BackgroundTaskScheduler(getSchedu
lerDelegate()); | 34 if (sInstance == null) sInstance = new BackgroundTaskScheduler(getSchedu
lerDelegate()); |
34 return sInstance; | 35 return sInstance; |
35 } | 36 } |
36 | 37 |
| 38 @VisibleForTesting |
| 39 static void setSchedulerForTesting(BackgroundTaskScheduler scheduler) { |
| 40 sInstance = scheduler; |
| 41 } |
| 42 |
37 // Do not instantiate. | 43 // Do not instantiate. |
38 private BackgroundTaskSchedulerFactory() {} | 44 private BackgroundTaskSchedulerFactory() {} |
39 } | 45 } |
OLD | NEW |