From e698616e84132caa1c84290ab5a73caeb3c7471f Mon Sep 17 00:00:00 2001 From: Elvis Hew Date: Fri, 11 Mar 2016 23:54:12 +0800 Subject: [PATCH 1/3] Fix the bug that wrongly use the GCM when GSF is not fully available. --- Parse/src/main/java/com/parse/ManifestInfo.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Parse/src/main/java/com/parse/ManifestInfo.java b/Parse/src/main/java/com/parse/ManifestInfo.java index 96451570e..46ed24109 100644 --- a/Parse/src/main/java/com/parse/ManifestInfo.java +++ b/Parse/src/main/java/com/parse/ManifestInfo.java @@ -198,6 +198,7 @@ public static PushType getPushType() { synchronized (lock) { if (pushType == null) { boolean isGooglePlayServicesAvailable = isGooglePlayServicesAvailable(); + boolean isGcmRegisterServiceAvailable = isGcmRegisterServiceAvailable(); boolean isPPNSAvailable = PPNSUtil.isPPNSAvailable(); boolean hasAnyGcmSpecificDeclaration = hasAnyGcmSpecificDeclaration(); ManifestCheckResult gcmSupportLevel = gcmSupportLevel(); @@ -211,12 +212,13 @@ public static PushType getPushType() { if (hasPushBroadcastReceiver && isGooglePlayServicesAvailable + && isGcmRegisterServiceAvailable && hasRequiredGcmDeclarations) { pushType = PushType.GCM; } else if (hasPushBroadcastReceiver && isPPNSAvailable && hasRequiredPpnsDeclarations - && (!hasAnyGcmSpecificDeclaration || !isGooglePlayServicesAvailable)) { + && !(isGcmRegisterServiceAvailable && isGooglePlayServicesAvailable && hasAnyGcmSpecificDeclaration)) { pushType = PushType.PPNS; if (isGooglePlayServicesAvailable) { @@ -466,6 +468,13 @@ private static boolean isGooglePlayServicesAvailable() { return Build.VERSION.SDK_INT >= 8 && getPackageInfo("com.google.android.gsf") != null; } + private static boolean isGcmRegisterServiceAvailable() { + Intent intent = new Intent(GcmRegistrar.REGISTER_ACTION); + intent.setPackage("com.google.android.gsf"); + List services = getContext().getPackageManager().queryIntentServices(intent, 0); + return services != null && services.size() > 0; + } + private static ManifestCheckResult gcmSupportLevel() { Context context = getContext(); if (getServiceInfo(PushService.class) == null) { From 141fba6c1773264184563b565b3e7d6e42cce4b9 Mon Sep 17 00:00:00 2001 From: Elvis Hew Date: Thu, 24 Mar 2016 15:30:05 +0800 Subject: [PATCH 2/3] Revision for review: remove isGooglePlayServicesAvailable and move --- .../src/main/java/com/parse/GcmRegistrar.java | 13 ++++++++- .../src/main/java/com/parse/ManifestInfo.java | 28 +++++-------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Parse/src/main/java/com/parse/GcmRegistrar.java b/Parse/src/main/java/com/parse/GcmRegistrar.java index d4234a0b1..4ea3e6320 100644 --- a/Parse/src/main/java/com/parse/GcmRegistrar.java +++ b/Parse/src/main/java/com/parse/GcmRegistrar.java @@ -15,6 +15,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.pm.ResolveInfo; import android.os.Bundle; import android.os.SystemClock; @@ -44,6 +45,8 @@ public static final String REGISTER_ACTION = "com.google.android.c2dm.intent.REGISTER"; + private static final String GSF_PACKAGE_NAME = "com.google.android.gsf"; + private static final String FILENAME_DEVICE_TOKEN_LAST_MODIFIED = "deviceTokenLastModified"; private long localDeviceTokenLastModified; private final Object localDeviceTokenLastModifiedMutex = new Object(); @@ -69,6 +72,14 @@ private static String actualSenderIDFromExtra(Object senderIDExtra) { return senderID.substring(3); } + public static boolean isGcmRegisterServiceAvailable() { + Intent intent = new Intent(REGISTER_ACTION); + intent.setPackage(GSF_PACKAGE_NAME); + List services = Parse.getApplicationContext().getPackageManager(). + queryIntentServices(intent, 0); + return services != null && services.size() > 0; + } + private final Object lock = new Object(); private Request request = null; private Context context = null; @@ -337,7 +348,7 @@ public Task getTask() { private void send() { Intent intent = new Intent(REGISTER_ACTION); - intent.setPackage("com.google.android.gsf"); + intent.setPackage(GSF_PACKAGE_NAME); intent.putExtra("sender", senderId); intent.putExtra("app", appIntent); diff --git a/Parse/src/main/java/com/parse/ManifestInfo.java b/Parse/src/main/java/com/parse/ManifestInfo.java index 46ed24109..29ad2e088 100644 --- a/Parse/src/main/java/com/parse/ManifestInfo.java +++ b/Parse/src/main/java/com/parse/ManifestInfo.java @@ -20,7 +20,6 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.content.pm.ServiceInfo; -import android.os.Build; import android.os.Bundle; import android.util.Log; @@ -197,8 +196,7 @@ static void setPushType(PushType newPushType) { public static PushType getPushType() { synchronized (lock) { if (pushType == null) { - boolean isGooglePlayServicesAvailable = isGooglePlayServicesAvailable(); - boolean isGcmRegisterServiceAvailable = isGcmRegisterServiceAvailable(); + boolean isGcmRegisterServiceAvailable = GcmRegistrar.isGcmRegisterServiceAvailable(); boolean isPPNSAvailable = PPNSUtil.isPPNSAvailable(); boolean hasAnyGcmSpecificDeclaration = hasAnyGcmSpecificDeclaration(); ManifestCheckResult gcmSupportLevel = gcmSupportLevel(); @@ -211,18 +209,17 @@ public static PushType getPushType() { (ppnsSupportLevel != ManifestCheckResult.MISSING_REQUIRED_DECLARATIONS); if (hasPushBroadcastReceiver - && isGooglePlayServicesAvailable && isGcmRegisterServiceAvailable && hasRequiredGcmDeclarations) { pushType = PushType.GCM; } else if (hasPushBroadcastReceiver && isPPNSAvailable && hasRequiredPpnsDeclarations - && !(isGcmRegisterServiceAvailable && isGooglePlayServicesAvailable && hasAnyGcmSpecificDeclaration)) { + && !(isGcmRegisterServiceAvailable && hasAnyGcmSpecificDeclaration)) { pushType = PushType.PPNS; - if (isGooglePlayServicesAvailable) { - Log.w(TAG, "Using PPNS for push even though Google Play Services is available." + + if (isGcmRegisterServiceAvailable) { + Log.w(TAG, "Using PPNS for push even though GCM service is available." + " Please " + getGcmManifestMessage()); } } else { @@ -248,9 +245,9 @@ public static PushType getPushType() { " \n" + " "); } - if (!isGooglePlayServicesAvailable) { - PLog.e(TAG, "Cannot use GCM for push on this device because Google Play " + - "Services is not available. Install Google Play Services from the Play Store."); + if (!isGcmRegisterServiceAvailable) { + PLog.e(TAG, "Cannot use GCM for push on this device because GCM " + + "Service is not available. Install Google Play Services from the Play Store."); } // Emit warnings if the client doesn't get push due to misconfiguration of the manifest. if (!hasRequiredGcmDeclarations) { @@ -464,17 +461,6 @@ private static boolean hasAnyGcmSpecificDeclaration() { return false; } - private static boolean isGooglePlayServicesAvailable() { - return Build.VERSION.SDK_INT >= 8 && getPackageInfo("com.google.android.gsf") != null; - } - - private static boolean isGcmRegisterServiceAvailable() { - Intent intent = new Intent(GcmRegistrar.REGISTER_ACTION); - intent.setPackage("com.google.android.gsf"); - List services = getContext().getPackageManager().queryIntentServices(intent, 0); - return services != null && services.size() > 0; - } - private static ManifestCheckResult gcmSupportLevel() { Context context = getContext(); if (getServiceInfo(PushService.class) == null) { From 53bb81f961ac07151dfa889e26410f215c757e12 Mon Sep 17 00:00:00 2001 From: Elvis Hew Date: Wed, 3 Aug 2016 10:54:02 +0800 Subject: [PATCH 3/3] Revision for review: simplify the change --- .../src/main/java/com/parse/GcmRegistrar.java | 13 +--------- .../src/main/java/com/parse/ManifestInfo.java | 25 +++++++++++++------ 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Parse/src/main/java/com/parse/GcmRegistrar.java b/Parse/src/main/java/com/parse/GcmRegistrar.java index 4ea3e6320..d4234a0b1 100644 --- a/Parse/src/main/java/com/parse/GcmRegistrar.java +++ b/Parse/src/main/java/com/parse/GcmRegistrar.java @@ -15,7 +15,6 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.pm.ResolveInfo; import android.os.Bundle; import android.os.SystemClock; @@ -45,8 +44,6 @@ public static final String REGISTER_ACTION = "com.google.android.c2dm.intent.REGISTER"; - private static final String GSF_PACKAGE_NAME = "com.google.android.gsf"; - private static final String FILENAME_DEVICE_TOKEN_LAST_MODIFIED = "deviceTokenLastModified"; private long localDeviceTokenLastModified; private final Object localDeviceTokenLastModifiedMutex = new Object(); @@ -72,14 +69,6 @@ private static String actualSenderIDFromExtra(Object senderIDExtra) { return senderID.substring(3); } - public static boolean isGcmRegisterServiceAvailable() { - Intent intent = new Intent(REGISTER_ACTION); - intent.setPackage(GSF_PACKAGE_NAME); - List services = Parse.getApplicationContext().getPackageManager(). - queryIntentServices(intent, 0); - return services != null && services.size() > 0; - } - private final Object lock = new Object(); private Request request = null; private Context context = null; @@ -348,7 +337,7 @@ public Task getTask() { private void send() { Intent intent = new Intent(REGISTER_ACTION); - intent.setPackage(GSF_PACKAGE_NAME); + intent.setPackage("com.google.android.gsf"); intent.putExtra("sender", senderId); intent.putExtra("app", appIntent); diff --git a/Parse/src/main/java/com/parse/ManifestInfo.java b/Parse/src/main/java/com/parse/ManifestInfo.java index 29ad2e088..f2ca09d62 100644 --- a/Parse/src/main/java/com/parse/ManifestInfo.java +++ b/Parse/src/main/java/com/parse/ManifestInfo.java @@ -20,6 +20,7 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.content.pm.ServiceInfo; +import android.os.Build; import android.os.Bundle; import android.util.Log; @@ -196,7 +197,7 @@ static void setPushType(PushType newPushType) { public static PushType getPushType() { synchronized (lock) { if (pushType == null) { - boolean isGcmRegisterServiceAvailable = GcmRegistrar.isGcmRegisterServiceAvailable(); + boolean isGooglePlayServicesAvailable = isGooglePlayServicesAvailable(); boolean isPPNSAvailable = PPNSUtil.isPPNSAvailable(); boolean hasAnyGcmSpecificDeclaration = hasAnyGcmSpecificDeclaration(); ManifestCheckResult gcmSupportLevel = gcmSupportLevel(); @@ -209,17 +210,17 @@ public static PushType getPushType() { (ppnsSupportLevel != ManifestCheckResult.MISSING_REQUIRED_DECLARATIONS); if (hasPushBroadcastReceiver - && isGcmRegisterServiceAvailable + && isGooglePlayServicesAvailable && hasRequiredGcmDeclarations) { pushType = PushType.GCM; } else if (hasPushBroadcastReceiver && isPPNSAvailable && hasRequiredPpnsDeclarations - && !(isGcmRegisterServiceAvailable && hasAnyGcmSpecificDeclaration)) { + && (!hasAnyGcmSpecificDeclaration || !isGooglePlayServicesAvailable)) { pushType = PushType.PPNS; - if (isGcmRegisterServiceAvailable) { - Log.w(TAG, "Using PPNS for push even though GCM service is available." + + if (isGooglePlayServicesAvailable) { + Log.w(TAG, "Using PPNS for push even though Google Play Services is available." + " Please " + getGcmManifestMessage()); } } else { @@ -245,9 +246,9 @@ public static PushType getPushType() { " \n" + " "); } - if (!isGcmRegisterServiceAvailable) { - PLog.e(TAG, "Cannot use GCM for push on this device because GCM " + - "Service is not available. Install Google Play Services from the Play Store."); + if (!isGooglePlayServicesAvailable) { + PLog.e(TAG, "Cannot use GCM for push on this device because Google Play " + + "Services is not available. Install Google Play Services from the Play Store."); } // Emit warnings if the client doesn't get push due to misconfiguration of the manifest. if (!hasRequiredGcmDeclarations) { @@ -461,6 +462,14 @@ private static boolean hasAnyGcmSpecificDeclaration() { return false; } + private static boolean isGooglePlayServicesAvailable() { + Intent intent = new Intent(GcmRegistrar.REGISTER_ACTION); + intent.setPackage("com.google.android.gsf"); + List services = Parse.getApplicationContext().getPackageManager(). + queryIntentServices(intent, 0); + return services != null && services.size() > 0; + } + private static ManifestCheckResult gcmSupportLevel() { Context context = getContext(); if (getServiceInfo(PushService.class) == null) {