@@ -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 }
0 commit comments