Bug 1454686 - MmaLeanPlumImp().stop() will now stop LP, stop showing messages and allow restart in same app session; r?mcomella draft
authorPetru Lingurar <petru.lingurar@softvision.ro>
Thu, 17 May 2018 18:45:01 +0300
changeset 796361 4387d5e3e55cb85be5aac735ceabb80f7aedc7cf
parent 796360 a5e61e421ead96184b64af53d70a2ade90e5991d
child 796362 636c57538894de8191c23f305c33daf7a0cf7d5b
push id110233
push userplingurar@mozilla.com
push dateThu, 17 May 2018 16:13:02 +0000
reviewersmcomella
bugs1454686
milestone62.0a1
Bug 1454686 - MmaLeanPlumImp().stop() will now stop LP, stop showing messages and allow restart in same app session; r?mcomella According to current LP documentation there are no SDK APIs to allow users to fully stop LP: events reporting and message displaying there. After extensive testing and investigations I think I found the least intrusive way to offer that. We will use internal methods but which are public so I hope they will be supported in the future also. Nevertheless we will need to maintain this in regards to future SDK updates. MozReview-Commit-ID: Ke3HGAyCqVA ***
mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
@@ -15,32 +15,39 @@ import android.support.annotation.Drawab
 import android.support.annotation.NonNull;
 import android.support.v4.app.NotificationCompat;
 
 import com.leanplum.Leanplum;
 import com.leanplum.LeanplumActivityHelper;
 import com.leanplum.LeanplumPushNotificationCustomizer;
 import com.leanplum.LeanplumPushService;
 import com.leanplum.internal.Constants;
+import com.leanplum.internal.LeanplumInternal;
+import com.leanplum.internal.VarCache;
 
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.MmaConstants;
 
 import java.util.Map;
 import java.util.UUID;
 
 
 public class MmaLeanplumImp implements MmaInterface {
 
 
     @Override
     public void init(final Activity activity, Map<String, ?> attributes) {
         if (activity == null) {
             return;
         }
+
+        // Need to call this in the eventuality that in this app session stop() has been called.
+        // It will allow LeanPlum to communicate again with the servers.
+        Leanplum.setIsTestModeEnabled(false);
+
         Leanplum.setApplicationContext(activity.getApplicationContext());
 
         LeanplumActivityHelper.enableLifecycleCallbacks(activity.getApplication());
 
         if (AppConstants.MOZILLA_OFFICIAL) {
             Leanplum.setAppIdForProductionMode(MmaConstants.MOZ_LEANPLUM_SDK_CLIENTID, MmaConstants.MOZ_LEANPLUM_SDK_KEY);
         } else {
             Leanplum.setAppIdForDevelopmentMode(MmaConstants.MOZ_LEANPLUM_SDK_CLIENTID, MmaConstants.MOZ_LEANPLUM_SDK_KEY);
@@ -95,19 +102,38 @@ public class MmaLeanplumImp implements M
     }
 
     @Override
     public void event(String leanplumEvent, double value) {
         Leanplum.track(leanplumEvent, value);
 
     }
 
+    // This method digs deep into LeanPlum internals.
+    // It's currently the only way to achieve what we needed:
+    //      - fully stop LeanPlum: tracking events and displaying messages
+    //      - allow LP to be restarted in the same app session
+    // Did extensive testing to ensure all will work as intended
+    // but although this method uses LP public methods it should be maintained in accordance with LP SDK updates.
     @Override
     public void stop() {
+        // This will just inform LeanPlum server that we stopped current LeanPlum session
         Leanplum.stop();
+
+        // As written in LeanPlum SDK documentation, "This prevents Leanplum from communicating with the server."
+        // as this "isTestMode" flag is checked before LeanPlum SDK does anything.
+        // Also has the benefit effect of blocking the display of already downloaded messages.
+        //
+        // The reverse of this - setIsTestModeEnabled(false) must be called before trying to init LP in the same session.
+        Leanplum.setIsTestModeEnabled(true);
+
+        // This is just to allow restarting LP and it's functionality in the same app session
+        // as LP stores it's state internally and check against it.
+        LeanplumInternal.setCalledStart(false);
+        LeanplumInternal.setHasStarted(false);
     }
 
     @Override
     public boolean handleGcmMessage(Context context, String from, Bundle bundle) {
         if (from != null && from.equals(MmaConstants.MOZ_MMA_SENDER_ID) && bundle.containsKey(Constants.Keys.PUSH_MESSAGE_TEXT)) {
             LeanplumPushService.handleNotification(context, bundle);
             return true;
         }