Skip to content

Commit 55771ba

Browse files
committed
feat(manifest): adding component check when setting up gcm
1 parent 0f45a4d commit 55771ba

File tree

6 files changed

+239
-63
lines changed

6 files changed

+239
-63
lines changed

AndroidSDK/src/com/leanplum/LeanplumCloudMessagingProvider.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,49 @@
3535
abstract class LeanplumCloudMessagingProvider {
3636
private static String registrationId;
3737

38+
static String getCurrentRegistrationId() {
39+
return registrationId;
40+
}
41+
42+
/**
43+
* Sends the registration ID to the server over HTTP.
44+
*/
45+
private static void sendRegistrationIdToBackend(String registrationId) {
46+
Leanplum.setRegistrationId(registrationId);
47+
}
48+
3849
/**
3950
* Registration app for Cloud Messaging.
4051
*
4152
* @return String - registration id for app.
4253
*/
4354
public abstract String getRegistrationId();
4455

56+
/**
57+
* Whether Messaging Provider is initialized correctly.
58+
*
59+
* @return True if provider is initialized, false otherwise.
60+
*/
4561
public abstract boolean isInitialized();
4662

63+
/**
64+
* Whether app manifest is setup correctly. This will be checked only in DEBUG builds.
65+
*
66+
* @return True if manifest is setup, false otherwise.
67+
*/
68+
public abstract boolean isManifestSetup();
69+
4770
/**
4871
* Unregister from cloud messaging.
4972
*/
5073
public abstract void unregister();
5174

52-
static String getCurrentRegistrationId() {
53-
return registrationId;
54-
}
55-
75+
/**
76+
* Callback should be invoked when Registration ID is received from provider.
77+
*
78+
* @param context The application context.
79+
* @param registrationId Registration Id.
80+
*/
5681
void onRegistrationIdReceived(Context context, String registrationId) {
5782
if (registrationId == null) {
5883
Log.w("Registration ID is undefined.");
@@ -69,17 +94,10 @@ void onRegistrationIdReceived(Context context, String registrationId) {
6994
}
7095
}
7196

72-
/**
73-
* Sends the registration ID to the server over HTTP.
74-
*/
75-
private static void sendRegistrationIdToBackend(String registrationId) {
76-
Leanplum.setRegistrationId(registrationId);
77-
}
78-
7997
/**
8098
* Stores the registration ID in the application's {@code SharedPreferences}.
8199
*
82-
* @param context application's context.
100+
* @param context The application context.
83101
*/
84102
public void storePreferences(Context context) {
85103
Log.v("Saving the registration ID in the shared preferences.");

AndroidSDK/src/com/leanplum/LeanplumFcmProvider.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ public boolean isInitialized() {
3939
return true;
4040
}
4141

42-
/**
43-
* Unregister from FCM.
44-
*/
42+
@Override
43+
public boolean isManifestSetup() {
44+
// Firebase can only be setup through gradle, so we don't have to check manually
45+
// whether manifest is properly setup.
46+
return true;
47+
}
48+
4549
public void unregister() {
4650
try {
4751
FirebaseInstanceId.getInstance().deleteInstanceId();

AndroidSDK/src/com/leanplum/LeanplumGcmProvider.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,60 @@ public boolean isInitialized() {
111111
return senderIds != null || getCurrentRegistrationId() != null;
112112
}
113113

114-
/**
115-
* Unregister from GCM.
116-
*/
114+
@Override
115+
public boolean isManifestSetup() {
116+
Context context = Leanplum.getContext();
117+
if (context == null) {
118+
return false;
119+
}
120+
// We don't want to check if manifest is setup in Release build.
121+
if (!BuildConfig.DEBUG) {
122+
return true;
123+
}
124+
try {
125+
boolean hasPermissions = LeanplumManifestHelper.checkPermission(LeanplumManifestHelper.RECEIVE_PERMISSION, false, true)
126+
&& (LeanplumManifestHelper.checkPermission(context.getPackageName() + ".gcm.permission.C2D_MESSAGE", true, false)
127+
|| LeanplumManifestHelper.checkPermission(context.getPackageName() + ".permission.C2D_MESSAGE", true, true));
128+
129+
boolean hasGcmReceiver = LeanplumManifestHelper.checkComponent(
130+
LeanplumManifestHelper.ApplicationComponent.RECEIVER, LeanplumManifestHelper.GCM_RECEIVER,
131+
true, LeanplumManifestHelper.SEND_PERMISSION, Arrays.asList(LeanplumManifestHelper.RECEIVE_ACTION,
132+
LeanplumManifestHelper.REGISTRATION_ACTION), context.getPackageName());
133+
boolean hasPushReceiver = LeanplumManifestHelper.checkComponent(LeanplumManifestHelper.ApplicationComponent.RECEIVER,
134+
LeanplumPushReceiver.class.getName(), false, null,
135+
Collections.singletonList(LeanplumManifestHelper.PUSH_LISTENER_SERVICE_FILTER), null);
136+
137+
boolean hasReceivers = hasGcmReceiver && hasPushReceiver;
138+
139+
boolean hasPushListenerService = LeanplumManifestHelper.checkComponent(
140+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
141+
LeanplumPushListenerService.class.getName(), false, null,
142+
Collections.singletonList(LeanplumManifestHelper.RECEIVE_ACTION), null);
143+
boolean hasInstanceIdService = LeanplumManifestHelper.checkComponent(
144+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
145+
LeanplumPushInstanceIDService.class.getName(), false, null,
146+
Collections.singletonList(LeanplumManifestHelper.INSTANCE_ID_ACTION), null);
147+
boolean hasRegistrationService = LeanplumManifestHelper.checkComponent(
148+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
149+
LeanplumPushRegistrationService.class.getName(), false, null, null, null);
150+
151+
boolean hasServices = hasPushListenerService && hasInstanceIdService && hasRegistrationService;
152+
153+
if (hasPermissions && hasReceivers && hasServices) {
154+
Log.i("Google Cloud Messaging is setup correctly.");
155+
return true;
156+
}
157+
} catch (Throwable t) {
158+
Util.handleException(t);
159+
}
160+
Log.i("Failed to setup Google Cloud Messaging, check your manifest configuration.");
161+
return false;
162+
}
163+
117164
public void unregister() {
118165
try {
119166
InstanceID.getInstance(Leanplum.getContext()).deleteInstanceID();
120-
Log.i("Application was unregistred from GCM.");
167+
Log.i("Application was unregistered from GCM.");
121168
} catch (Exception e) {
122169
Log.e("Failed to unregister from GCM.");
123170
}

AndroidSDK/src/com/leanplum/LeanplumManualProvider.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ public boolean isInitialized() {
4141
return true;
4242
}
4343

44-
public void unregister() {
44+
@Override
45+
public boolean isManifestSetup() {
46+
return true;
47+
}
4548

49+
public void unregister() {
4650
}
4751
}

AndroidSDK/src/com/leanplum/LeanplumPushService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ private static void initPushService() {
675675
}
676676
provider = new LeanplumGcmProvider();
677677
}
678-
if (!provider.isInitialized()) {
678+
if (!provider.isInitialized() || !provider.isManifestSetup()) {
679679
return;
680680
}
681681
if (hasAppIDChanged(Request.appId())) {

0 commit comments

Comments
 (0)