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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java

Issue 1699143002: [NTP Snippets] Schedule periodic fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_feature
Patch Set: Created 4 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 2016 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.ntp.snippets;
6
7 import android.content.Context;
8
9 import com.google.android.gms.gcm.GcmNetworkManager;
10 import com.google.android.gms.gcm.PeriodicTask;
11 import com.google.android.gms.gcm.Task;
12
13 import org.chromium.base.Log;
14 import org.chromium.base.annotations.CalledByNative;
15 import org.chromium.chrome.browser.externalauth.ExternalAuthUtils;
16 import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler;
17
18 /**
19 * The {@link SnippetsLauncher} singleton is created and owned by the C++ browse r.
20 *
21 * Thread model: This class is to be run on the UI thread only.
22 */
23 public class SnippetsLauncher {
24 private static final String TAG = "SnippetsLauncher";
25
26 // The instance of SnippetsLauncher currently owned by a C++ SnippetsLaunche rAndroid, if any.
27 // If it is non-null then the browser is running.
28 private static SnippetsLauncher sInstance;
29
30 private GcmNetworkManager mScheduler;
31
32 private boolean mGCMEnabled = true;
33
34 /**
35 * Create a SnippetsLauncher object, which is owned by C++.
36 * @param context The app context.
37 */
38 @CalledByNative
39 protected static SnippetsLauncher create(Context context) {
40 if (sInstance != null) {
41 throw new IllegalStateException("Already instantiated");
42 }
43
44 sInstance = new SnippetsLauncher(context);
45 return sInstance;
46 }
47
48 /**
49 * Called when the C++ counterpart is deleted.
50 */
51 @CalledByNative
52 protected void destroy() {
53 assert sInstance == this;
54 sInstance = null;
55 }
56
57 /**
58 * Returns true if the native browser has started and created an instance of {@link
59 * SnippetsLauncher}.
60 */
61 protected static boolean hasInstance() {
62 return sInstance != null;
63 }
64
65 protected SnippetsLauncher(Context context) {
66 checkGCM(context);
67 mScheduler = GcmNetworkManager.getInstance(context);
68 }
69
70 private boolean canUseGooglePlayServices(Context context) {
71 return ExternalAuthUtils.getInstance().canUseGooglePlayServices(
72 context, new UserRecoverableErrorHandler.Silent());
73 }
74
75 private void checkGCM(Context context) {
76 // Check to see if Play Services is up to date, and disable GCM if not.
77 if (!canUseGooglePlayServices(context)) {
78 mGCMEnabled = false;
79 Log.i(TAG, "Disabling SnippetsLauncher because Play Services is not up to date.");
80 }
81 }
82
83 @CalledByNative
84 private boolean schedule(int periodSeconds) {
85 if (!mGCMEnabled) return false;
86 Log.i(TAG, "Scheduling: " + periodSeconds);
87 // Google Play Services may not be up to date, if the application was no t installed through
88 // the Play Store. In this case, scheduling the task will fail silently.
89 PeriodicTask task = new PeriodicTask.Builder()
90 .setService(SnippetsLauncherService.class)
91 .setTag("SnippetsLauncher")
92 .setPeriod(periodSeconds)
93 .setRequiredNetwork(Task.NETWORK_STATE_UNMET ERED)
Bernhard Bauer 2016/02/16 16:19:08 Do you want to also require being on a charger?
Marc Treib 2016/02/16 16:54:27 I don't think so, at least for now? But since this
Bernhard Bauer 2016/02/17 16:53:00 Acknowledged.
94 .setPersisted(true)
95 .setUpdateCurrent(true)
96 .build();
97 try {
98 mScheduler.schedule(task);
99 } catch (IllegalArgumentException e) {
100 // Disable GCM for the remainder of this session.
101 mGCMEnabled = false;
102 // Return false so that the failure will be logged.
103 return false;
104 }
105 return true;
106 }
107
108 @CalledByNative
109 private boolean unschedule() {
110 if (!mGCMEnabled) return false;
111 Log.i(TAG, "Unscheduling");
112 try {
113 mScheduler.cancelAllTasks(SnippetsLauncherService.class);
114 } catch (IllegalArgumentException e) {
115 // This occurs when SnippetsLauncherService is not found in the appl ication
116 // manifest. Disable GCM for the remainder of this session.
117 mGCMEnabled = false;
118 // Return false so that the failure will be logged.
119 return false;
120 }
121 return true;
122 }
123 }
124
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698