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