| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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.chrome.browser.ntp.snippets; | 5 package org.chromium.chrome.browser.ntp.snippets; |
| 6 | 6 |
| 7 import android.content.Context; |
| 8 |
| 7 import com.google.android.gms.gcm.GcmNetworkManager; | 9 import com.google.android.gms.gcm.GcmNetworkManager; |
| 8 import com.google.android.gms.gcm.PeriodicTask; | 10 import com.google.android.gms.gcm.PeriodicTask; |
| 9 import com.google.android.gms.gcm.Task; | 11 import com.google.android.gms.gcm.Task; |
| 10 | 12 |
| 11 import org.chromium.base.ContextUtils; | 13 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
| 13 import org.chromium.base.VisibleForTesting; | 15 import org.chromium.base.VisibleForTesting; |
| 14 import org.chromium.base.annotations.CalledByNative; | 16 import org.chromium.base.annotations.CalledByNative; |
| 15 import org.chromium.base.annotations.SuppressFBWarnings; | 17 import org.chromium.base.annotations.SuppressFBWarnings; |
| 16 import org.chromium.chrome.browser.ChromeBackgroundService; | 18 import org.chromium.chrome.browser.ChromeBackgroundService; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 43 // The instance of SnippetsLauncher currently owned by a C++ SnippetsLaunche
rAndroid, if any. | 45 // The instance of SnippetsLauncher currently owned by a C++ SnippetsLaunche
rAndroid, if any. |
| 44 // If it is non-null then the browser is running. | 46 // If it is non-null then the browser is running. |
| 45 private static SnippetsLauncher sInstance; | 47 private static SnippetsLauncher sInstance; |
| 46 | 48 |
| 47 private GcmNetworkManager mScheduler; | 49 private GcmNetworkManager mScheduler; |
| 48 | 50 |
| 49 private boolean mGCMEnabled = true; | 51 private boolean mGCMEnabled = true; |
| 50 | 52 |
| 51 /** | 53 /** |
| 52 * Create a SnippetsLauncher object, which is owned by C++. | 54 * Create a SnippetsLauncher object, which is owned by C++. |
| 55 * @param context The app context. |
| 53 */ | 56 */ |
| 54 @VisibleForTesting | 57 @VisibleForTesting |
| 55 @CalledByNative | 58 @CalledByNative |
| 56 public static SnippetsLauncher create() { | 59 public static SnippetsLauncher create(Context context) { |
| 57 if (sInstance != null) { | 60 if (sInstance != null) { |
| 58 throw new IllegalStateException("Already instantiated"); | 61 throw new IllegalStateException("Already instantiated"); |
| 59 } | 62 } |
| 60 | 63 |
| 61 sInstance = new SnippetsLauncher(); | 64 sInstance = new SnippetsLauncher(context); |
| 62 return sInstance; | 65 return sInstance; |
| 63 } | 66 } |
| 64 | 67 |
| 65 /** | 68 /** |
| 66 * Called when the C++ counterpart is deleted. | 69 * Called when the C++ counterpart is deleted. |
| 67 */ | 70 */ |
| 68 @VisibleForTesting | 71 @VisibleForTesting |
| 69 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") | 72 @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") |
| 70 @CalledByNative | 73 @CalledByNative |
| 71 public void destroy() { | 74 public void destroy() { |
| 72 assert sInstance == this; | 75 assert sInstance == this; |
| 73 sInstance = null; | 76 sInstance = null; |
| 74 } | 77 } |
| 75 | 78 |
| 76 /** | 79 /** |
| 77 * Returns true if the native browser has started and created an instance of
{@link | 80 * Returns true if the native browser has started and created an instance of
{@link |
| 78 * SnippetsLauncher}. | 81 * SnippetsLauncher}. |
| 79 */ | 82 */ |
| 80 public static boolean hasInstance() { | 83 public static boolean hasInstance() { |
| 81 return sInstance != null; | 84 return sInstance != null; |
| 82 } | 85 } |
| 83 | 86 |
| 84 protected SnippetsLauncher() { | 87 protected SnippetsLauncher(Context context) { |
| 85 checkGCM(); | 88 checkGCM(context); |
| 86 mScheduler = GcmNetworkManager.getInstance(ContextUtils.getApplicationCo
ntext()); | 89 mScheduler = GcmNetworkManager.getInstance(context); |
| 87 } | 90 } |
| 88 | 91 |
| 89 private boolean canUseGooglePlayServices() { | 92 private boolean canUseGooglePlayServices(Context context) { |
| 90 return ExternalAuthUtils.getInstance().canUseGooglePlayServices( | 93 return ExternalAuthUtils.getInstance().canUseGooglePlayServices( |
| 91 ContextUtils.getApplicationContext(), new UserRecoverableErrorHa
ndler.Silent()); | 94 context, new UserRecoverableErrorHandler.Silent()); |
| 92 } | 95 } |
| 93 | 96 |
| 94 private void checkGCM() { | 97 private void checkGCM(Context context) { |
| 95 // Check to see if Play Services is up to date, and disable GCM if not. | 98 // Check to see if Play Services is up to date, and disable GCM if not. |
| 96 if (!canUseGooglePlayServices()) { | 99 if (!canUseGooglePlayServices(context)) { |
| 97 mGCMEnabled = false; | 100 mGCMEnabled = false; |
| 98 Log.i(TAG, "Disabling SnippetsLauncher because Play Services is not
up to date."); | 101 Log.i(TAG, "Disabling SnippetsLauncher because Play Services is not
up to date."); |
| 99 } | 102 } |
| 100 } | 103 } |
| 101 | 104 |
| 102 private static PeriodicTask buildFetchTask( | 105 private static PeriodicTask buildFetchTask( |
| 103 String tag, long periodSeconds, int requiredNetwork) { | 106 String tag, long periodSeconds, int requiredNetwork) { |
| 104 // Add a bit of "flex" around the target period. This achieves the follo
wing: | 107 // Add a bit of "flex" around the target period. This achieves the follo
wing: |
| 105 // - It makes sure the task doesn't run (significantly) before its initi
al period has | 108 // - It makes sure the task doesn't run (significantly) before its initi
al period has |
| 106 // elapsed. In practice, the scheduler seems to behave like that anywa
y, but it doesn't | 109 // elapsed. In practice, the scheduler seems to behave like that anywa
y, but it doesn't |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 Log.i(TAG, "Unscheduling"); | 167 Log.i(TAG, "Unscheduling"); |
| 165 return schedule(0, 0); | 168 return schedule(0, 0); |
| 166 } | 169 } |
| 167 | 170 |
| 168 public static boolean shouldRescheduleTasksOnUpgrade() { | 171 public static boolean shouldRescheduleTasksOnUpgrade() { |
| 169 // Reschedule the periodic tasks if they were enabled previously. | 172 // Reschedule the periodic tasks if they were enabled previously. |
| 170 return ContextUtils.getAppSharedPreferences().getBoolean(PREF_IS_SCHEDUL
ED, false); | 173 return ContextUtils.getAppSharedPreferences().getBoolean(PREF_IS_SCHEDUL
ED, false); |
| 171 } | 174 } |
| 172 } | 175 } |
| 173 | 176 |
| OLD | NEW |