Skip to content

Commit 5f46a3c

Browse files
authored
Merge pull request #74 from Leanplum/feature/manifest-parser
feat(manifest): removing manifest parser
2 parents c918783 + 08dfc81 commit 5f46a3c

File tree

8 files changed

+390
-898
lines changed

8 files changed

+390
-898
lines changed

AndroidSDK/src/com/leanplum/LeanplumCloudMessagingProvider.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,24 @@
3333
* @author Anna Orlova
3434
*/
3535
abstract class LeanplumCloudMessagingProvider {
36-
static final String PUSH_REGISTRATION_SERVICE = "com.leanplum.LeanplumPushRegistrationService";
37-
static final String PUSH_RECEIVER = "com.leanplum.LeanplumPushReceiver";
38-
3936
private static String registrationId;
4037

38+
/**
39+
* Gets the registration Id associated with current messaging provider.
40+
*
41+
* @return Registration Id.
42+
*/
43+
static String getCurrentRegistrationId() {
44+
return registrationId;
45+
}
46+
47+
/**
48+
* Sends the registration ID to the server over HTTP.
49+
*/
50+
private static void sendRegistrationIdToBackend(String registrationId) {
51+
Leanplum.setRegistrationId(registrationId);
52+
}
53+
4154
/**
4255
* Registration app for Cloud Messaging.
4356
*
@@ -46,23 +59,30 @@ abstract class LeanplumCloudMessagingProvider {
4659
public abstract String getRegistrationId();
4760

4861
/**
49-
* Verifies that Android Manifest is set up correctly.
62+
* Whether Messaging Provider is initialized correctly.
5063
*
51-
* @return true If Android Manifest is set up correctly.
64+
* @return True if provider is initialized, false otherwise.
5265
*/
53-
public abstract boolean isManifestSetUp();
54-
5566
public abstract boolean isInitialized();
5667

68+
/**
69+
* Whether app manifest is setup correctly.
70+
*
71+
* @return True if manifest is setup, false otherwise.
72+
*/
73+
public abstract boolean isManifestSetup();
74+
5775
/**
5876
* Unregister from cloud messaging.
5977
*/
6078
public abstract void unregister();
6179

62-
static String getCurrentRegistrationId() {
63-
return registrationId;
64-
}
65-
80+
/**
81+
* Callback should be invoked when Registration ID is received from provider.
82+
*
83+
* @param context The application context.
84+
* @param registrationId Registration Id.
85+
*/
6686
void onRegistrationIdReceived(Context context, String registrationId) {
6787
if (registrationId == null) {
6888
Log.w("Registration ID is undefined.");
@@ -79,17 +99,10 @@ void onRegistrationIdReceived(Context context, String registrationId) {
7999
}
80100
}
81101

82-
/**
83-
* Sends the registration ID to the server over HTTP.
84-
*/
85-
private static void sendRegistrationIdToBackend(String registrationId) {
86-
Leanplum.setRegistrationId(registrationId);
87-
}
88-
89102
/**
90103
* Stores the registration ID in the application's {@code SharedPreferences}.
91104
*
92-
* @param context application's context.
105+
* @param context The application context.
93106
*/
94107
public void storePreferences(Context context) {
95108
Log.v("Saving the registration ID in the shared preferences.");

AndroidSDK/src/com/leanplum/LeanplumFcmProvider.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121

2222
package com.leanplum;
2323

24+
import android.content.Context;
25+
2426
import com.google.firebase.iid.FirebaseInstanceId;
2527
import com.leanplum.internal.LeanplumManifestHelper;
2628
import com.leanplum.internal.Log;
29+
import com.leanplum.internal.Util;
2730

2831
import java.util.Collections;
2932

@@ -33,49 +36,62 @@
3336
* @author Anna Orlova
3437
*/
3538
class LeanplumFcmProvider extends LeanplumCloudMessagingProvider {
36-
private static final String INSTANCE_ID_EVENT = "com.google.firebase.INSTANCE_ID_EVENT";
37-
private static final String MESSAGING_EVENT = "com.google.firebase.MESSAGING_EVENT";
38-
private static final String PUSH_FCM_LISTENER_SERVICE =
39-
"com.leanplum.LeanplumPushFcmListenerService";
40-
private static final String PUSH_FIREBASE_MESSAGING_SERVICE =
41-
"com.leanplum.LeanplumPushFirebaseMessagingService";
4239

40+
@Override
4341
public String getRegistrationId() {
4442
return FirebaseInstanceId.getInstance().getToken();
4543
}
4644

45+
@Override
4746
public boolean isInitialized() {
4847
return true;
4948
}
5049

51-
public boolean isManifestSetUp() {
52-
boolean hasReceivers =
53-
LeanplumManifestHelper.checkComponent(LeanplumManifestHelper.getReceivers(),
54-
PUSH_RECEIVER, false, null,
55-
Collections.singletonList(PUSH_FIREBASE_MESSAGING_SERVICE), null);
50+
@Override
51+
public boolean isManifestSetup() {
52+
Context context = Leanplum.getContext();
53+
if (context == null) {
54+
return false;
55+
}
56+
57+
try {
58+
boolean hasPushReceiver = LeanplumManifestHelper.checkComponent(LeanplumManifestHelper.ApplicationComponent.RECEIVER,
59+
LeanplumManifestHelper.LP_PUSH_RECEIVER, false, null,
60+
Collections.singletonList(LeanplumManifestHelper.LP_PUSH_FCM_LISTENER_SERVICE), context.getPackageName());
5661

57-
boolean hasPushFirebaseMessagingService = LeanplumManifestHelper.checkComponent(
58-
LeanplumManifestHelper.getServices(), PUSH_FIREBASE_MESSAGING_SERVICE, false, null,
59-
Collections.singletonList(MESSAGING_EVENT), null);
60-
boolean hasPushFcmListenerService = LeanplumManifestHelper.checkComponent(
61-
LeanplumManifestHelper.getServices(), PUSH_FCM_LISTENER_SERVICE, false, null,
62-
Collections.singletonList(INSTANCE_ID_EVENT), null);
63-
boolean hasPushRegistrationService = LeanplumManifestHelper.checkComponent(
64-
LeanplumManifestHelper.getServices(), PUSH_REGISTRATION_SERVICE, false, null, null, null);
62+
boolean hasPushFirebaseMessagingService = LeanplumManifestHelper.checkComponent(
63+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
64+
LeanplumManifestHelper.LP_PUSH_FCM_MESSAGING_SERVICE, false, null,
65+
Collections.singletonList(LeanplumManifestHelper.FCM_MESSAGING_EVENT), context.getPackageName());
6566

66-
boolean hasServices = hasPushFirebaseMessagingService && hasPushFcmListenerService
67-
&& hasPushRegistrationService;
67+
boolean hasPushFirebaseListenerService = LeanplumManifestHelper.checkComponent(
68+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
69+
LeanplumManifestHelper.LP_PUSH_FCM_LISTENER_SERVICE, false, null,
70+
Collections.singletonList(LeanplumManifestHelper.FCM_INSTANCE_ID_EVENT), context.getPackageName());
6871

69-
return hasReceivers && hasServices;
72+
boolean hasRegistrationService = LeanplumManifestHelper.checkComponent(
73+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
74+
LeanplumPushRegistrationService.class.getName(), false, null, null, context.getPackageName());
75+
76+
boolean hasServices = hasPushFirebaseMessagingService && hasPushFirebaseListenerService &&
77+
hasRegistrationService;
78+
79+
if (hasPushReceiver && hasServices) {
80+
Log.i("Firebase Messaging is setup correctly.");
81+
return true;
82+
}
83+
} catch (Throwable t) {
84+
Util.handleException(t);
85+
}
86+
Log.e("Failed to setup Firebase Messaging, check your manifest configuration.");
87+
return false;
7088
}
7189

72-
/**
73-
* Unregister from FCM.
74-
*/
90+
@Override
7591
public void unregister() {
7692
try {
7793
FirebaseInstanceId.getInstance().deleteInstanceId();
78-
Log.i("Application was unregistred from FCM.");
94+
Log.i("Application was unregistered from FCM.");
7995
} catch (Exception e) {
8096
Log.e("Failed to unregister from FCM.");
8197
}

AndroidSDK/src/com/leanplum/LeanplumGcmProvider.java

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,17 @@ class LeanplumGcmProvider extends LeanplumCloudMessagingProvider {
4747
private static final String ERROR_PHONE_REGISTRATION_ERROR = "PHONE_REGISTRATION_ERROR";
4848
private static final String ERROR_TOO_MANY_REGISTRATIONS = "TOO_MANY_REGISTRATIONS";
4949

50-
private static final String SEND_PERMISSION = "com.google.android.c2dm.permission.SEND";
51-
private static final String RECEIVE_PERMISSION = "com.google.android.c2dm.permission.RECEIVE";
52-
private static final String RECEIVE_ACTION = "com.google.android.c2dm.intent.RECEIVE";
53-
private static final String REGISTRATION_ACTION = "com.google.android.c2dm.intent.REGISTRATION";
54-
private static final String INSTANCE_ID_ACTION = "com.google.android.gms.iid.InstanceID";
55-
private static final String PUSH_LISTENER_SERVICE = "com.leanplum.LeanplumPushListenerService";
56-
private static final String GCM_RECEIVER = "com.google.android.gms.gcm.GcmReceiver";
57-
private static final String PUSH_INSTANCE_ID_SERVICE =
58-
"com.leanplum.LeanplumPushInstanceIDService";
59-
6050
private static String senderIds;
6151

52+
/**
53+
* Sets GCM sender id.
54+
*
55+
* @param senderId Sender id.
56+
*/
6257
static void setSenderId(String senderId) {
6358
senderIds = senderId;
6459
}
6560

66-
/**
67-
* Stores the GCM sender ID in the application's {@code SharedPreferences}.
68-
*
69-
* @param context application's context.
70-
*/
7161
@Override
7262
public void storePreferences(Context context) {
7363
super.storePreferences(context);
@@ -76,6 +66,7 @@ public void storePreferences(Context context) {
7666
Constants.Defaults.PROPERTY_SENDER_IDS, senderIds);
7767
}
7868

69+
@Override
7970
public String getRegistrationId() {
8071
String registrationId = null;
8172
try {
@@ -117,52 +108,62 @@ public String getRegistrationId() {
117108
return registrationId;
118109
}
119110

111+
@Override
120112
public boolean isInitialized() {
121113
return senderIds != null || getCurrentRegistrationId() != null;
122114
}
123115

124-
public boolean isManifestSetUp() {
116+
@Override
117+
public boolean isManifestSetup() {
125118
Context context = Leanplum.getContext();
126119
if (context == null) {
127120
return false;
128121
}
129-
130-
boolean hasPermissions = LeanplumManifestHelper.checkPermission(RECEIVE_PERMISSION, false, true)
131-
&& (LeanplumManifestHelper.checkPermission(context.getPackageName() +
132-
".gcm.permission.C2D_MESSAGE", true, false) || LeanplumManifestHelper.checkPermission(
133-
context.getPackageName() + ".permission.C2D_MESSAGE", true, true));
134-
135-
boolean hasGcmReceiver = LeanplumManifestHelper.checkComponent(
136-
LeanplumManifestHelper.getReceivers(), GCM_RECEIVER, true, SEND_PERMISSION,
137-
Arrays.asList(RECEIVE_ACTION, REGISTRATION_ACTION), context.getPackageName());
138-
boolean hasPushReceiver = LeanplumManifestHelper.checkComponent(
139-
LeanplumManifestHelper.getReceivers(), PUSH_RECEIVER, false, null,
140-
Collections.singletonList(PUSH_LISTENER_SERVICE), null);
141-
142-
boolean hasReceivers = hasGcmReceiver && hasPushReceiver;
143-
144-
boolean hasPushListenerService = LeanplumManifestHelper.checkComponent(
145-
LeanplumManifestHelper.getServices(), PUSH_LISTENER_SERVICE, false, null,
146-
Collections.singletonList(RECEIVE_ACTION), null);
147-
boolean hasPushInstanceIDService = LeanplumManifestHelper.checkComponent(
148-
LeanplumManifestHelper.getServices(), PUSH_INSTANCE_ID_SERVICE, false, null,
149-
Collections.singletonList(INSTANCE_ID_ACTION), null);
150-
boolean hasPushRegistrationService = LeanplumManifestHelper.checkComponent(
151-
LeanplumManifestHelper.getServices(), PUSH_REGISTRATION_SERVICE, false, null, null, null);
152-
153-
boolean hasServices = hasPushListenerService && hasPushInstanceIDService
154-
&& hasPushRegistrationService;
155-
156-
return hasPermissions && hasReceivers && hasServices;
122+
try {
123+
boolean hasPermissions = LeanplumManifestHelper.checkPermission(LeanplumManifestHelper.GCM_RECEIVE_PERMISSION, false, true)
124+
&& (LeanplumManifestHelper.checkPermission(context.getPackageName() + ".gcm.permission.C2D_MESSAGE", true, false)
125+
|| LeanplumManifestHelper.checkPermission(context.getPackageName() + ".permission.C2D_MESSAGE", true, true));
126+
127+
boolean hasGcmReceiver = LeanplumManifestHelper.checkComponent(
128+
LeanplumManifestHelper.ApplicationComponent.RECEIVER, LeanplumManifestHelper.GCM_RECEIVER,
129+
true, LeanplumManifestHelper.GCM_SEND_PERMISSION, Arrays.asList(LeanplumManifestHelper.GCM_RECEIVE_ACTION,
130+
LeanplumManifestHelper.GCM_REGISTRATION_ACTION), context.getPackageName());
131+
boolean hasPushReceiver = LeanplumManifestHelper.checkComponent(LeanplumManifestHelper.ApplicationComponent.RECEIVER,
132+
LeanplumManifestHelper.LP_PUSH_RECEIVER, false, null,
133+
Collections.singletonList(LeanplumManifestHelper.LP_PUSH_LISTENER_SERVICE), context.getPackageName());
134+
135+
boolean hasReceivers = hasGcmReceiver && hasPushReceiver;
136+
137+
boolean hasPushListenerService = LeanplumManifestHelper.checkComponent(
138+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
139+
LeanplumManifestHelper.LP_PUSH_LISTENER_SERVICE, false, null,
140+
Collections.singletonList(LeanplumManifestHelper.GCM_RECEIVE_ACTION), context.getPackageName());
141+
boolean hasInstanceIdService = LeanplumManifestHelper.checkComponent(
142+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
143+
LeanplumManifestHelper.LP_PUSH_INSTANCE_ID_SERVICE, false, null,
144+
Collections.singletonList(LeanplumManifestHelper.GCM_INSTANCE_ID_ACTION), context.getPackageName());
145+
boolean hasRegistrationService = LeanplumManifestHelper.checkComponent(
146+
LeanplumManifestHelper.ApplicationComponent.SERVICE,
147+
LeanplumManifestHelper.LP_PUSH_REGISTRATION_SERVICE, false, null, null, context.getPackageName());
148+
149+
boolean hasServices = hasPushListenerService && hasInstanceIdService && hasRegistrationService;
150+
151+
if (hasPermissions && hasReceivers && hasServices) {
152+
Log.i("Google Cloud Messaging is setup correctly.");
153+
return true;
154+
}
155+
} catch (Throwable t) {
156+
Util.handleException(t);
157+
}
158+
Log.e("Failed to setup Google Cloud Messaging, check your manifest configuration.");
159+
return false;
157160
}
158161

159-
/**
160-
* Unregister from GCM.
161-
*/
162+
@Override
162163
public void unregister() {
163164
try {
164165
InstanceID.getInstance(Leanplum.getContext()).deleteInstanceID();
165-
Log.i("Application was unregistred from GCM.");
166+
Log.i("Application was unregistered from GCM.");
166167
} catch (Exception e) {
167168
Log.e("Failed to unregister from GCM.");
168169
}

AndroidSDK/src/com/leanplum/LeanplumManualProvider.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ public class LeanplumManualProvider extends LeanplumCloudMessagingProvider {
3333
onRegistrationIdReceived(context, registrationId);
3434
}
3535

36+
@Override
3637
public String getRegistrationId() {
3738
return getCurrentRegistrationId();
3839
}
3940

41+
@Override
4042
public boolean isInitialized() {
4143
return true;
4244
}
4345

44-
public boolean isManifestSetUp() {
46+
@Override
47+
public boolean isManifestSetup() {
4548
return true;
4649
}
4750

51+
@Override
4852
public void unregister() {
49-
5053
}
5154
}

0 commit comments

Comments
 (0)