From: Wengling Chen Date: Sat, 2 Nov 2019 07:56:44 +0100 Subject: Remove dependency on com.google.android.gms.gcm It also disalbes Snippets, auto-update of offline pages and launching browser from background service. The snippets are already disabled in native codes, this will disable it on Java level. --- chrome/android/BUILD.gn | 3 - .../browser/ChromeBackgroundService.java | 4 - .../ntp/snippets/SnippetsLauncher.java | 2 +- .../gcm/ChromeGcmListenerService.java | 58 +----------- components/background_task_scheduler/BUILD.gn | 3 - .../BackgroundTaskGcmTaskService.java | 63 +------------ .../BackgroundTaskSchedulerFactory.java | 6 +- ...kgroundTaskSchedulerGcmNetworkManager.java | 14 +-- third_party/android_deps/BUILD.gn | 15 ---- third_party/cacheinvalidation/BUILD.gn | 1 - .../android2/AndroidManifestUpdatedGcm.xml | 20 ----- .../channel/AndroidGcmController.java | 42 +-------- .../AndroidInstanceIDListenerService.java | 13 +-- .../channel/GcmRegistrationTaskService.java | 89 +------------------ 14 files changed, 11 insertions(+), 322 deletions(-) diff --git a/chrome/android/BUILD.gn b/chrome/android/BUILD.gn --- a/chrome/android/BUILD.gn +++ b/chrome/android/BUILD.gn @@ -227,7 +227,6 @@ android_library("chrome_java") { "$google_play_services_package:google_play_services_cast_framework_java", "$google_play_services_package:google_play_services_cast_java", "$google_play_services_package:google_play_services_fido_java", - "$google_play_services_package:google_play_services_gcm_java", "$google_play_services_package:google_play_services_iid_java", "$google_play_services_package:google_play_services_tasks_java", "//base:base_java", @@ -744,7 +743,6 @@ android_library("chrome_test_java") { "$google_play_services_package:google_play_services_cast_framework_java", "$google_play_services_package:google_play_services_cast_java", "$google_play_services_package:google_play_services_fido_java", - "$google_play_services_package:google_play_services_gcm_java", "$google_play_services_package:google_play_services_iid_java", "$google_play_services_package:google_play_services_tasks_java", "//base:base_java", @@ -1526,7 +1524,6 @@ android_library("browser_java_test_support") { ] deps = [ ":chrome_java", - "$google_play_services_package:google_play_services_gcm_java", "//base:base_java", "//base:base_java_test_support", "//chrome/android/public/profiles:java", diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundService.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundService.java --- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundService.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeBackgroundService.java @@ -8,10 +8,6 @@ import android.content.Context; import androidx.annotation.VisibleForTesting; -import com.google.android.gms.gcm.GcmNetworkManager; -import com.google.android.gms.gcm.GcmTaskService; -import com.google.android.gms.gcm.TaskParams; - import org.chromium.base.Log; import org.chromium.base.task.PostTask; import org.chromium.chrome.browser.background_sync.BackgroundSyncBackgroundTaskScheduler; 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 --- 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 @@ -35,7 +35,7 @@ public class SnippetsLauncher { // If it is non-null then the browser is running. private static SnippetsLauncher sInstance; - private boolean mGCMEnabled = true; + private boolean mGCMEnabled; /** * Create a SnippetsLauncher object, which is owned by C++. diff --git a/chrome/android/java/src/org/chromium/chrome/browser/services/gcm/ChromeGcmListenerService.java b/chrome/android/java/src/org/chromium/chrome/browser/services/gcm/ChromeGcmListenerService.java --- a/chrome/android/java/src/org/chromium/chrome/browser/services/gcm/ChromeGcmListenerService.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/services/gcm/ChromeGcmListenerService.java @@ -10,7 +10,6 @@ import android.os.Build; import android.os.Bundle; import android.text.TextUtils; -import com.google.android.gms.gcm.GcmListenerService; import com.google.ipc.invalidation.ticl.android2.channel.AndroidGcmController; import org.chromium.base.ContextUtils; @@ -33,62 +32,9 @@ import org.chromium.content_public.browser.UiThreadTaskTraits; /** * Receives Downstream messages and status of upstream messages from GCM. */ -public class ChromeGcmListenerService extends GcmListenerService { +public class ChromeGcmListenerService { private static final String TAG = "ChromeGcmListener"; - @Override - public void onCreate() { - ProcessInitializationHandler.getInstance().initializePreNative(); - super.onCreate(); - } - - @Override - public void onMessageReceived(final String from, final Bundle data) { - boolean hasCollapseKey = !TextUtils.isEmpty(data.getString("collapse_key")); - GcmUma.recordDataMessageReceived(ContextUtils.getApplicationContext(), hasCollapseKey); - - String invalidationSenderId = AndroidGcmController.get(this).getSenderId(); - if (from.equals(invalidationSenderId)) { - AndroidGcmController.get(this).onMessageReceived(data); - return; - } - - // Dispatch the message to the GCM Driver for native features. - PostTask.runOrPostTask(UiThreadTaskTraits.DEFAULT, () -> { - GCMMessage message = null; - try { - message = new GCMMessage(from, data); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Received an invalid GCM Message", e); - return; - } - - scheduleOrDispatchMessageToDriver(message); - }); - } - - @Override - public void onMessageSent(String msgId) { - Log.d(TAG, "Message sent successfully. Message id: " + msgId); - GcmUma.recordGcmUpstreamHistogram( - ContextUtils.getApplicationContext(), GcmUma.UMA_UPSTREAM_SUCCESS); - } - - @Override - public void onSendError(String msgId, String error) { - Log.w(TAG, "Error in sending message. Message id: " + msgId + " Error: " + error); - GcmUma.recordGcmUpstreamHistogram( - ContextUtils.getApplicationContext(), GcmUma.UMA_UPSTREAM_SEND_FAILED); - } - - @Override - public void onDeletedMessages() { - // TODO(johnme): Ask GCM to include the subtype in this event. - Log.w(TAG, "Push messages were deleted, but we can't tell the Service Worker as we don't" - + "know what subtype (app ID) it occurred for."); - GcmUma.recordDeletedMessages(ContextUtils.getApplicationContext()); - } - /** * Returns if we deliver the GCMMessage with a background service by calling * Context#startService. This will only work if Android has put us in a whitelist to allow @@ -218,7 +164,7 @@ public class ChromeGcmListenerService extends GcmListenerService { static void dispatchMessageToDriver(GCMMessage message) { ThreadUtils.assertOnUiThread(); ChromeBrowserInitializer.getInstance().handleSynchronousStartup(); - GCMDriver.dispatchMessage(message); + //GCMDriver.dispatchMessage(message); } private static boolean isFullBrowserLoaded() { diff --git a/components/background_task_scheduler/BUILD.gn b/components/background_task_scheduler/BUILD.gn --- a/components/background_task_scheduler/BUILD.gn +++ b/components/background_task_scheduler/BUILD.gn @@ -54,7 +54,6 @@ if (is_android) { ":scheduled_task_java", "$google_play_services_package:google_play_services_base_java", "$google_play_services_package:google_play_services_basement_java", - "$google_play_services_package:google_play_services_gcm_java", "$google_play_services_package:google_play_services_tasks_java", "//base:base_java", "//content/public/android:content_java", @@ -78,7 +77,6 @@ if (is_android) { ":background_task_scheduler_task_ids_java", "$google_play_services_package:google_play_services_base_java", "$google_play_services_package:google_play_services_basement_java", - "$google_play_services_package:google_play_services_gcm_java", "$google_play_services_package:google_play_services_tasks_java", "//base:base_java", "//base:base_java_test_support", @@ -110,7 +108,6 @@ if (is_android) { "$google_play_services_package:google_play_services_auth_base_java", "$google_play_services_package:google_play_services_base_java", "$google_play_services_package:google_play_services_basement_java", - "$google_play_services_package:google_play_services_gcm_java", "$google_play_services_package:google_play_services_tasks_java", "//base:base_java", "//base:base_java_test_support", diff --git a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskGcmTaskService.java b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskGcmTaskService.java --- a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskGcmTaskService.java +++ b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskGcmTaskService.java @@ -8,10 +8,6 @@ import android.os.Build; import androidx.annotation.VisibleForTesting; -import com.google.android.gms.gcm.GcmNetworkManager; -import com.google.android.gms.gcm.GcmTaskService; -import com.google.android.gms.gcm.TaskParams; - import org.chromium.base.ContextUtils; import org.chromium.base.Log; import org.chromium.base.ThreadUtils; @@ -21,7 +17,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; /** Delegates calls out to various tasks that need to run in the background. */ -public class BackgroundTaskGcmTaskService extends GcmTaskService { +public class BackgroundTaskGcmTaskService { private static final String TAG = "BkgrdTaskGcmTS"; private BackgroundTaskSchedulerGcmNetworkManager.Clock mClock = System::currentTimeMillis; @@ -90,63 +86,6 @@ public class BackgroundTaskGcmTaskService extends GcmTaskService { } } - @Override - public int onRunTask(TaskParams params) { - final TaskParameters taskParams = - BackgroundTaskSchedulerGcmNetworkManager.getTaskParametersFromTaskParams(params); - - final BackgroundTask backgroundTask = - BackgroundTaskSchedulerFactory.getBackgroundTaskFromTaskId(taskParams.getTaskId()); - if (backgroundTask == null) { - Log.w(TAG, "Failed to start task. Could not instantiate BackgroundTask class."); - // Cancel task if the BackgroundTask class is not found anymore. We assume this means - // that the task has been deprecated. - BackgroundTaskSchedulerFactory.getScheduler().cancel( - ContextUtils.getApplicationContext(), taskParams.getTaskId()); - return GcmNetworkManager.RESULT_FAILURE; - } - - if (BackgroundTaskSchedulerGcmNetworkManager.didTaskExpire( - params, mClock.currentTimeMillis())) { - BackgroundTaskSchedulerUma.getInstance().reportTaskExpired(taskParams.getTaskId()); - return GcmNetworkManager.RESULT_FAILURE; - } - - final Waiter waiter = new Waiter(Waiter.MAX_TIMEOUT_SECONDS); - - final AtomicBoolean taskNeedsBackgroundProcessing = new AtomicBoolean(); - ThreadUtils.runOnUiThreadBlocking(new Runnable() { - @Override - public void run() { - BackgroundTaskSchedulerUma.getInstance().reportTaskStarted(taskParams.getTaskId()); - taskNeedsBackgroundProcessing.set( - backgroundTask.onStartTask(ContextUtils.getApplicationContext(), taskParams, - new TaskFinishedCallbackGcmTaskService(waiter))); - } - }); - - if (!taskNeedsBackgroundProcessing.get()) return GcmNetworkManager.RESULT_SUCCESS; - - waiter.startWaiting(); - - if (waiter.isRescheduleNeeded()) return GcmNetworkManager.RESULT_RESCHEDULE; - if (!waiter.hasTaskTimedOut()) return GcmNetworkManager.RESULT_SUCCESS; - - final AtomicBoolean taskNeedsRescheduling = new AtomicBoolean(); - ThreadUtils.runOnUiThreadBlocking(new Runnable() { - @Override - public void run() { - BackgroundTaskSchedulerUma.getInstance().reportTaskStopped(taskParams.getTaskId()); - taskNeedsRescheduling.set(backgroundTask.onStopTask( - ContextUtils.getApplicationContext(), taskParams)); - } - }); - - if (taskNeedsRescheduling.get()) return GcmNetworkManager.RESULT_RESCHEDULE; - - return GcmNetworkManager.RESULT_SUCCESS; - } - @Override public void onInitializeTasks() { // Ignore the event on OSs supporting JobScheduler. diff --git a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerFactory.java b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerFactory.java --- a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerFactory.java +++ b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerFactory.java @@ -18,11 +18,7 @@ public final class BackgroundTaskSchedulerFactory { private static BackgroundTaskFactory sBackgroundTaskFactory; static BackgroundTaskSchedulerDelegate getSchedulerDelegateForSdk(int sdkInt) { - if (sdkInt >= Build.VERSION_CODES.M) { - return new BackgroundTaskSchedulerJobService(); - } else { - return new BackgroundTaskSchedulerGcmNetworkManager(); - } + return new BackgroundTaskSchedulerJobService(); /* minSDK is 24 */ } /** diff --git a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerGcmNetworkManager.java b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerGcmNetworkManager.java --- a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerGcmNetworkManager.java +++ b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerGcmNetworkManager.java @@ -19,7 +19,7 @@ import java.util.concurrent.TimeUnit; * An implementation of {@link BackgroundTaskSchedulerDelegate} that uses the Play Services * {@link GcmNetworkManager} to schedule jobs. */ -class BackgroundTaskSchedulerGcmNetworkManager implements BackgroundTaskSchedulerDelegate { +class BackgroundTaskSchedulerGcmNetworkManager { private static final String TAG = "BkgrdTaskSchedGcmNM"; /** Delta time for expiration checks, after the end time. */ @@ -35,18 +35,6 @@ class BackgroundTaskSchedulerGcmNetworkManager implements BackgroundTaskSchedule sClock = clock; } - @Override - public boolean schedule(Context context, @NonNull TaskInfo taskInfo) { - ThreadUtils.assertOnUiThread(); - - return false; - } - - @Override - public void cancel(Context context, int taskId) { - ThreadUtils.assertOnUiThread(); - } - private static String taskIdToTaskTag(int taskId) { return Integer.toString(taskId); } diff --git a/third_party/android_deps/BUILD.gn b/third_party/android_deps/BUILD.gn --- a/third_party/android_deps/BUILD.gn +++ b/third_party/android_deps/BUILD.gn @@ -921,21 +921,6 @@ android_aar_prebuilt("google_play_services_fido_java") { strip_drawables = true } -# This is generated, do not edit. Update BuildConfigGenerator.groovy instead. -android_aar_prebuilt("google_play_services_gcm_java") { - aar_path = "libs/com_google_android_gms_play_services_gcm/play-services-gcm-15.0.1.aar" - info_path = "libs/com_google_android_gms_play_services_gcm/com_google_android_gms_play_services_gcm.info" - deps = [ - ":google_play_services_base_java", - ":google_play_services_basement_java", - ":google_play_services_iid_java", - ":google_play_services_stats_java", - ] - - # Removing drawables from GMS .aars as they are unused bloat. - strip_drawables = true -} - # This is generated, do not edit. Update BuildConfigGenerator.groovy instead. android_aar_prebuilt("google_play_services_iid_java") { aar_path = "libs/com_google_android_gms_play_services_iid/play-services-iid-15.0.1.aar" diff --git a/third_party/cacheinvalidation/BUILD.gn b/third_party/cacheinvalidation/BUILD.gn --- a/third_party/cacheinvalidation/BUILD.gn +++ b/third_party/cacheinvalidation/BUILD.gn @@ -133,7 +133,6 @@ if (is_android) { jacoco_never_instrument = true deps = [ ":cacheinvalidation_proto_java", - "$google_play_services_package:google_play_services_gcm_java", "$google_play_services_package:google_play_services_iid_java", "$google_play_services_package:google_play_services_tasks_java", "//base:base_java", diff --git a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/external/client/android2/AndroidManifestUpdatedGcm.xml b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/external/client/android2/AndroidManifestUpdatedGcm.xml --- a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/external/client/android2/AndroidManifestUpdatedGcm.xml +++ b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/external/client/android2/AndroidManifestUpdatedGcm.xml @@ -24,17 +24,6 @@ - - - - - - - - - @@ -42,14 +31,5 @@ - - - - - - - diff --git a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/AndroidGcmController.java b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/AndroidGcmController.java --- a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/AndroidGcmController.java +++ b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/AndroidGcmController.java @@ -15,8 +15,6 @@ */ package com.google.ipc.invalidation.ticl.android2.channel; -import com.google.android.gms.gcm.GcmNetworkManager; -import com.google.android.gms.gcm.OneoffTask; import com.google.ipc.invalidation.common.GcmSharedConstants; import com.google.ipc.invalidation.external.client.SystemResources.Logger; import com.google.ipc.invalidation.external.client.android.service.AndroidLogger; @@ -50,7 +48,7 @@ public class AndroidGcmController { private static AndroidGcmController androidGcmController; - private GcmNetworkManager gcmNetworkManager; +// private GcmNetworkManager gcmNetworkManager; private Context context; @@ -64,32 +62,12 @@ public class AndroidGcmController { public static AndroidGcmController get(Context context) { synchronized (lock) { if (androidGcmController == null) { - androidGcmController = - new AndroidGcmController(context, GcmNetworkManager.getInstance(context)); + androidGcmController = null; } } return androidGcmController; } - /** - * Override AndroidGcmController with a custom GcmNetworkManager in tests. This overrides the - * existing instance of AndroidGcmController if any. - * - * @param context the application context. - * @param gcmNetworkManager the custom GcmNetworkManager to use. - */ - public static void overrideAndroidGcmControllerForTests( - Context context, GcmNetworkManager gcmNetworkManager) { - synchronized (lock) { - androidGcmController = new AndroidGcmController(context, gcmNetworkManager); - } - } - - private AndroidGcmController(Context context, GcmNetworkManager gcmNetworkManager) { - this.context = context; - this.gcmNetworkManager = gcmNetworkManager; - } - /** * Returns true if no registration token is stored or the current application version is higher * than the version for the token stored. @@ -142,21 +120,7 @@ public class AndroidGcmController { return; } - OneoffTask registrationTask = - new OneoffTask.Builder() - .setExecutionWindow(0, 1) - .setTag(AndroidChannelConstants.GCM_REGISTRATION_TASK_SERVICE_TAG) - .setService(GcmRegistrationTaskService.class) - .build(); - - try { - gcmNetworkManager.schedule(registrationTask); - } catch (IllegalArgumentException exception) { - // Scheduling the service can throw an exception due to a framework error on Android when - // the the look up for the GCMTaskService being scheduled to be run fails. - // See crbug/548314. - logger.warning("Failed to schedule GCM registration task. Exception: %s", exception); - } + logger.warning("Failed to schedule GCM registration task."); } /** diff --git a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/AndroidInstanceIDListenerService.java b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/AndroidInstanceIDListenerService.java --- a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/AndroidInstanceIDListenerService.java +++ b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/AndroidInstanceIDListenerService.java @@ -15,7 +15,6 @@ */ package com.google.ipc.invalidation.ticl.android2.channel; -import com.google.android.gms.iid.InstanceIDListenerService; import com.google.ipc.invalidation.external.client.SystemResources.Logger; import com.google.ipc.invalidation.external.client.android.service.AndroidLogger; @@ -23,16 +22,6 @@ import com.google.ipc.invalidation.external.client.android.service.AndroidLogger * Implementation of {@link InstanceIDListenerService} to receive notifications from GCM to * update the registration token. */ -public class AndroidInstanceIDListenerService extends InstanceIDListenerService { +public class AndroidInstanceIDListenerService { private static final Logger logger = AndroidLogger.forTag("InstanceIDListener"); - - /** - * Called when the token needs to updated. {@link AndroidGcmController#fetchToken} clears the - * current token and schedules a task to fetch a new token. - */ - @Override - public void onTokenRefresh() { - logger.info("Received token refresh request"); - AndroidGcmController.get(this).fetchToken(); - } } diff --git a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/GcmRegistrationTaskService.java b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/GcmRegistrationTaskService.java --- a/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/GcmRegistrationTaskService.java +++ b/third_party/cacheinvalidation/src/java/com/google/ipc/invalidation/ticl/android2/channel/GcmRegistrationTaskService.java @@ -15,11 +15,6 @@ */ package com.google.ipc.invalidation.ticl.android2.channel; -import com.google.android.gms.gcm.GcmNetworkManager; -import com.google.android.gms.gcm.GcmTaskService; -import com.google.android.gms.gcm.GoogleCloudMessaging; -import com.google.android.gms.gcm.TaskParams; -import com.google.android.gms.iid.InstanceID; import com.google.ipc.invalidation.common.GcmSharedConstants; import com.google.ipc.invalidation.external.client.SystemResources.Logger; import com.google.ipc.invalidation.external.client.android.service.AndroidLogger; @@ -42,88 +37,6 @@ import java.io.IOException; * failure to fetch the token, the task is rescheduled using the GcmNetworkManager which uses * exponential back-offs to control when the task is executed. */ -public class GcmRegistrationTaskService extends GcmTaskService { +public class GcmRegistrationTaskService { private static final Logger logger = AndroidLogger.forTag("RegistrationTaskService"); - - public InstanceID getInstanceID(Context context) { - return InstanceID.getInstance(context); - } - - /** - * Called when the task is ready to be executed. Registers with GCM using - * {@link InstanceID#getToken} and stores the registration token. - * - *

Returns {@link GcmNetworkManager#RESULT_SUCCESS} when the token is successfully retrieved. - * On failure {@link GcmNetworkManager#RESULT_RESCHEDULE} is used which reschedules the service - * to be executed again using exponential back-off. - */ - @Override - public int onRunTask(TaskParams params) { - if (!AndroidChannelConstants.GCM_REGISTRATION_TASK_SERVICE_TAG.equals(params.getTag())) { - logger.warning("Unknown task received with tag: %s", params.getTag()); - return GcmNetworkManager.RESULT_FAILURE; - } - - String senderId = GcmSharedConstants.GCM_UPDATED_SENDER_ID; - try { - String token = getInstanceID(this).getToken( - senderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); - storeToken(token); - return GcmNetworkManager.RESULT_SUCCESS; - } catch (IOException exception) { - logger.warning("Failed to get token for sender: %s. Exception : %s", senderId, exception); - return GcmNetworkManager.RESULT_RESCHEDULE; - } catch (SecurityException exception) { - // InstanceID#getToken occasionally throws a security exception when trying send the - // registration intent to GMSCore. Catching the exception here to prevent crashes. - logger.warning("Security exception when fetching token: %s", exception); - return GcmNetworkManager.RESULT_RESCHEDULE; - } - } - - /** Stores the registration token and the current application version in Shared Preferences. */ - private void storeToken(String token) { - AndroidChannelPreferences.setRegistrationToken(token); - AndroidChannelPreferences.setAppVersion( - CommonUtils.getPackageVersion(this, getPackageName())); - // Send the updated token to the server. - updateServer(); - } - - /** Sends a message to the server to update the GCM registration token. */ - private void updateServer() { - // Inform the sender service that the registration token has changed. If the sender service - // had buffered a message because no registration token was previously available, this intent - // will cause it to send that message. - Intent sendBuffered = new Intent(); - final String ignoredData = ""; - sendBuffered.putExtra(AndroidChannelConstants.MESSAGE_SENDER_SVC_GCM_REGID_CHANGE, ignoredData); - - // Select the sender service to use for upstream message. - if (AndroidChannelPreferences.getGcmChannelType() == GcmChannelType.GCM_UPSTREAM) { - String upstreamServiceClass = new AndroidTiclManifest(this).getGcmUpstreamServiceClass(); - if (upstreamServiceClass == null) { - logger.warning("GcmUpstreamSenderService class not found."); - return; - } - sendBuffered.setClassName(this, upstreamServiceClass); - } else { - sendBuffered.setClass(this, AndroidMessageSenderService.class); - } - try { - startService(sendBuffered); - } catch (IllegalStateException exception) { - logger.warning("Unable to send buffered message(s): %s", exception); - } - - // Inform the Ticl service that the registration id has changed. This will cause it to send - // a message to the data center and update the GCM registration id stored at the data center. - Intent updateServer = ProtocolIntents.InternalDowncalls.newNetworkAddrChangeIntent(); - updateServer.setClassName(this, new AndroidTiclManifest(this).getTiclServiceClass()); - try { - startService(updateServer); - } catch (IllegalStateException exception) { - logger.warning("Unable to inform server about new registration id: %s", exception); - } - } } -- 2.17.1