Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..656996dd1a0e5923e15749f85de12151942c0505 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/snippets/SnippetsLauncher.java |
| @@ -0,0 +1,124 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.ntp.snippets; |
| + |
| +import android.content.Context; |
| + |
| +import com.google.android.gms.gcm.GcmNetworkManager; |
| +import com.google.android.gms.gcm.PeriodicTask; |
| +import com.google.android.gms.gcm.Task; |
| + |
| +import org.chromium.base.Log; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.chrome.browser.externalauth.ExternalAuthUtils; |
| +import org.chromium.chrome.browser.externalauth.UserRecoverableErrorHandler; |
| + |
| +/** |
| + * The {@link SnippetsLauncher} singleton is created and owned by the C++ browser. |
| + * |
| + * Thread model: This class is to be run on the UI thread only. |
| + */ |
| +public class SnippetsLauncher { |
| + private static final String TAG = "SnippetsLauncher"; |
| + |
| + // The instance of SnippetsLauncher currently owned by a C++ SnippetsLauncherAndroid, if any. |
| + // If it is non-null then the browser is running. |
| + private static SnippetsLauncher sInstance; |
| + |
| + private GcmNetworkManager mScheduler; |
| + |
| + private boolean mGCMEnabled = true; |
| + |
| + /** |
| + * Create a SnippetsLauncher object, which is owned by C++. |
| + * @param context The app context. |
| + */ |
| + @CalledByNative |
| + protected static SnippetsLauncher create(Context context) { |
| + if (sInstance != null) { |
| + throw new IllegalStateException("Already instantiated"); |
| + } |
| + |
| + sInstance = new SnippetsLauncher(context); |
| + return sInstance; |
| + } |
| + |
| + /** |
| + * Called when the C++ counterpart is deleted. |
| + */ |
| + @CalledByNative |
| + protected void destroy() { |
| + assert sInstance == this; |
| + sInstance = null; |
| + } |
| + |
| + /** |
| + * Returns true if the native browser has started and created an instance of {@link |
| + * SnippetsLauncher}. |
| + */ |
| + protected static boolean hasInstance() { |
| + return sInstance != null; |
| + } |
| + |
| + protected SnippetsLauncher(Context context) { |
| + checkGCM(context); |
| + mScheduler = GcmNetworkManager.getInstance(context); |
| + } |
| + |
| + private boolean canUseGooglePlayServices(Context context) { |
| + return ExternalAuthUtils.getInstance().canUseGooglePlayServices( |
| + context, new UserRecoverableErrorHandler.Silent()); |
| + } |
| + |
| + private void checkGCM(Context context) { |
| + // Check to see if Play Services is up to date, and disable GCM if not. |
| + if (!canUseGooglePlayServices(context)) { |
| + mGCMEnabled = false; |
| + Log.i(TAG, "Disabling SnippetsLauncher because Play Services is not up to date."); |
| + } |
| + } |
| + |
| + @CalledByNative |
| + private boolean schedule(int periodSeconds) { |
| + if (!mGCMEnabled) return false; |
| + Log.i(TAG, "Scheduling: " + periodSeconds); |
| + // Google Play Services may not be up to date, if the application was not installed through |
| + // the Play Store. In this case, scheduling the task will fail silently. |
| + PeriodicTask task = new PeriodicTask.Builder() |
| + .setService(SnippetsLauncherService.class) |
| + .setTag("SnippetsLauncher") |
| + .setPeriod(periodSeconds) |
| + .setRequiredNetwork(Task.NETWORK_STATE_UNMETERED) |
|
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.
|
| + .setPersisted(true) |
| + .setUpdateCurrent(true) |
| + .build(); |
| + try { |
| + mScheduler.schedule(task); |
| + } catch (IllegalArgumentException e) { |
| + // Disable GCM for the remainder of this session. |
| + mGCMEnabled = false; |
| + // Return false so that the failure will be logged. |
| + return false; |
| + } |
| + return true; |
| + } |
| + |
| + @CalledByNative |
| + private boolean unschedule() { |
| + if (!mGCMEnabled) return false; |
| + Log.i(TAG, "Unscheduling"); |
| + try { |
| + mScheduler.cancelAllTasks(SnippetsLauncherService.class); |
| + } catch (IllegalArgumentException e) { |
| + // This occurs when SnippetsLauncherService is not found in the application |
| + // manifest. Disable GCM for the remainder of this session. |
| + mGCMEnabled = false; |
| + // Return false so that the failure will be logged. |
| + return false; |
| + } |
| + return true; |
| + } |
| +} |
| + |