Bug 1429386 Added missing functionalities from a newer LP SDK version draft
authorVlad Baicu <vlad.baicu@softvision.ro>
Thu, 05 Apr 2018 19:09:45 +0300
changeset 777948 218f86b40078e5412ace98f15b3f6e6ba60ae446
parent 777361 cf3b1b492844acc87a856c8ee09bce7e775cd06a
child 782952 428cda3c9f75a1df25857ed2bd1099c58fe1ee40
push id105346
push uservbaicu@mozilla.com
push dateThu, 05 Apr 2018 16:12:37 +0000
bugs1429386
milestone61.0a1
Bug 1429386 Added missing functionalities from a newer LP SDK version MozReview-Commit-ID: EWGABsN3Diu
mobile/android/thirdparty/com/leanplum/messagetemplates/HTMLOptions.java
mobile/android/thirdparty/com/leanplum/messagetemplates/HTMLTemplate.java
mobile/android/thirdparty/com/leanplum/messagetemplates/MessageTemplates.java
mobile/android/thirdparty/com/leanplum/utils/SizeUtil.java
--- a/mobile/android/thirdparty/com/leanplum/messagetemplates/HTMLOptions.java
+++ b/mobile/android/thirdparty/com/leanplum/messagetemplates/HTMLOptions.java
@@ -16,25 +16,25 @@
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 package com.leanplum.messagetemplates;
 
+import android.app.Activity;
+import android.graphics.Point;
 import android.text.TextUtils;
 import android.util.Log;
-
 import com.leanplum.ActionArgs;
 import com.leanplum.ActionContext;
 import com.leanplum.Leanplum;
-
+import com.leanplum.utils.SizeUtil;
 import org.json.JSONException;
-
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.Map;
 
 /**
@@ -47,27 +47,32 @@ class HTMLOptions {
   private String openUrl;
   private String trackUrl;
   private String actionUrl;
   private String trackActionUrl;
   private String htmlTemplate;
   private ActionContext actionContext;
   private String htmlAlign;
   private int htmlHeight;
+  private Size htmlYOffset;
+  private boolean htmlTabOutsideToClose;
 
   HTMLOptions(ActionContext context) {
     this.setActionContext(context);
     this.setHtmlTemplate(getTemplate(context));
     this.setCloseUrl(context.stringNamed(MessageTemplates.Args.CLOSE_URL));
     this.setOpenUrl(context.stringNamed(MessageTemplates.Args.OPEN_URL));
     this.setTrackUrl(context.stringNamed(MessageTemplates.Args.TRACK_URL));
     this.setActionUrl(context.stringNamed(MessageTemplates.Args.ACTION_URL));
     this.setTrackActionUrl(context.stringNamed(MessageTemplates.Args.TRACK_ACTION_URL));
     this.setHtmlAlign(context.stringNamed(MessageTemplates.Args.HTML_ALIGN));
     this.setHtmlHeight(context.numberNamed(MessageTemplates.Args.HTML_HEIGHT).intValue());
+    this.setHtmlYOffset(context.stringNamed(MessageTemplates.Args.HTML_Y_OFFSET));
+    this.setHtmlTabOutsideToClose(context.booleanNamed(
+            MessageTemplates.Args.HTML_TAP_OUTSIDE_TO_CLOSE));
   }
 
   /**
    * Read data from file as String.
    *
    * @param context ActionContext.
    * @param name Name of file.
    * @return String String with data of file.
@@ -138,16 +143,21 @@ class HTMLOptions {
               localPath.replace(" ", "%20"));
         }
         map.remove(key);
       }
     }
     return map;
   }
 
+  static class Size {
+    int value;
+    String type;
+  }
+
   /**
    * Get HTML template file.
    *
    * @param context ActionContext.
    * @return String String with data of HTML template file.
    */
   private static String getTemplate(ActionContext context) {
     if (context == null) {
@@ -194,16 +204,69 @@ class HTMLOptions {
   String getHtmlAlign() {
     return htmlAlign;
   }
 
   private void setHtmlAlign(String htmlAlign) {
     this.htmlAlign = htmlAlign;
   }
 
+  //Gets html y offset in pixels.
+  int getHtmlYOffset(Activity context) {
+    int yOffset = 0;
+    if (context == null) {
+      return yOffset;
+    }
+
+    if (htmlYOffset != null && !TextUtils.isEmpty(htmlYOffset.type)) {
+      yOffset = htmlYOffset.value;
+      if ("%".equals(htmlYOffset.type)) {
+        Point size = SizeUtil.getDisplaySize(context);
+        yOffset = (size.y - SizeUtil.getStatusBarHeight(context)) * yOffset / 100;
+      } else {
+        yOffset = SizeUtil.dpToPx(context, yOffset);
+      }
+    }
+    return yOffset;
+  }
+
+  private void setHtmlYOffset(String htmlYOffset) {
+    this.htmlYOffset = getSizeValueAndType(htmlYOffset);
+  }
+
+  private Size getSizeValueAndType(String stringValue) {
+    if (TextUtils.isEmpty(stringValue)) {
+      return null;
+    }
+
+    Size out = new Size();
+    if (stringValue.contains("px")) {
+      String[] sizeValue = stringValue.split("px");
+      if (sizeValue.length != 0) {
+        out.value = Integer.parseInt(sizeValue[0]);
+      }
+      out.type = "px";
+    } else if (stringValue.contains("%")) {
+      String[] sizeValue = stringValue.split("%");
+      if (sizeValue.length != 0) {
+        out.value = Integer.parseInt(sizeValue[0]);
+      }
+      out.type = "%";
+    }
+    return out;
+  }
+
+  boolean isHtmlTabOutsideToClose() {
+    return htmlTabOutsideToClose;
+  }
+
+  private void setHtmlTabOutsideToClose(boolean htmlTabOutsideToClose) {
+    this.htmlTabOutsideToClose = htmlTabOutsideToClose;
+  }
+
   ActionContext getActionContext() {
     return actionContext;
   }
 
   private void setActionContext(ActionContext actionContext) {
     //noinspection AccessStaticViaInstance
     this.actionContext = actionContext;
   }
--- a/mobile/android/thirdparty/com/leanplum/messagetemplates/HTMLTemplate.java
+++ b/mobile/android/thirdparty/com/leanplum/messagetemplates/HTMLTemplate.java
@@ -17,20 +17,20 @@
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  */
 
 package com.leanplum.messagetemplates;
 
 import android.app.Activity;
+import android.graphics.Point;
 import android.support.annotation.NonNull;
 import android.util.Log;
 import android.view.MotionEvent;
-
 import com.leanplum.ActionContext;
 import com.leanplum.Leanplum;
 import com.leanplum.LeanplumActivityHelper;
 import com.leanplum.callbacks.ActionCallback;
 import com.leanplum.callbacks.PostponableAction;
 import com.leanplum.callbacks.VariablesChangedCallback;
 import com.leanplum.utils.SizeUtil;
 
@@ -45,27 +45,42 @@ public class HTMLTemplate extends BaseMe
 
   public HTMLTemplate(Activity activity, HTMLOptions htmlOptions) {
     super(activity, htmlOptions.isFullScreen(), null, null, htmlOptions);
     this.htmlOptions = htmlOptions;
   }
 
   @Override
   public boolean dispatchTouchEvent(@NonNull MotionEvent ev) {
-    if (!htmlOptions.isFullScreen()) {
-      int height = SizeUtil.dpToPx(Leanplum.getContext(), htmlOptions.getHtmlHeight());
-      int statusBarHeight = SizeUtil.getStatusBarHeight(Leanplum.getContext());
-      if (htmlOptions.getHtmlAlign().equals(MessageTemplates.Args.HTML_ALIGN_TOP) && ev.getY()
-          > height + statusBarHeight ||
-          htmlOptions.getHtmlAlign().equals(MessageTemplates.Args.HTML_ALIGN_BOTTOM) && ev.getY()
-              < dialogView.getHeight() + statusBarHeight - height) {
-        activity.dispatchTouchEvent(ev);
+      if (!htmlOptions.isFullScreen()) {
+          Point size = SizeUtil.getDisplaySize(activity);
+          int dialogWidth = webView.getWidth();
+          int left = (size.x - dialogWidth) / 2;
+          int right = (size.x + dialogWidth) / 2;
+          int height = SizeUtil.dpToPx(Leanplum.getContext(), htmlOptions.getHtmlHeight());
+          int statusBarHeight = SizeUtil.getStatusBarHeight(Leanplum.getContext());
+          int htmlYOffset = htmlOptions.getHtmlYOffset(activity);
+          int top;
+          int bottom;
+          if (MessageTemplates.Args.HTML_ALIGN_BOTTOM.equals(htmlOptions.getHtmlAlign())) {
+              top = size.y - height - statusBarHeight - htmlYOffset;
+              bottom = size.y - htmlYOffset - statusBarHeight;
+          } else {
+              top = htmlYOffset + statusBarHeight;
+              bottom = height + statusBarHeight + htmlYOffset;
+          }
+
+          if (ev.getY() < top || ev.getY() > bottom || ev.getX() < left || ev.getX() > right) {
+              if (htmlOptions.isHtmlTabOutsideToClose()) {
+                  cancel();
+              }
+              activity.dispatchTouchEvent(ev);
+          }
       }
-    }
-    return super.dispatchTouchEvent(ev);
+      return super.dispatchTouchEvent(ev);
   }
 
   public static void register() {
     Leanplum.defineAction(NAME, Leanplum.ACTION_KIND_MESSAGE | Leanplum.ACTION_KIND_ACTION,
         HTMLOptions.toArgs(), new ActionCallback() {
           @Override
           public boolean onResponse(final ActionContext context) {
             Leanplum.addOnceVariablesChangedAndNoDownloadsPendingHandler(
--- a/mobile/android/thirdparty/com/leanplum/messagetemplates/MessageTemplates.java
+++ b/mobile/android/thirdparty/com/leanplum/messagetemplates/MessageTemplates.java
@@ -50,16 +50,18 @@ public class MessageTemplates {
     static final String MESSAGE_COLOR = "Message.Color";
     static final String ACCEPT_BUTTON_TEXT = "Accept button.Text";
     static final String ACCEPT_BUTTON_BACKGROUND_COLOR = "Accept button.Background color";
     static final String ACCEPT_BUTTON_TEXT_COLOR = "Accept button.Text color";
     static final String BACKGROUND_IMAGE = "Background image";
     static final String BACKGROUND_COLOR = "Background color";
     static final String LAYOUT_WIDTH = "Layout.Width";
     static final String LAYOUT_HEIGHT = "Layout.Height";
+    static final String HTML_Y_OFFSET = "HTML Y Offset";
+    static final String HTML_TAP_OUTSIDE_TO_CLOSE = "Tap Outside to Close";
     static final String HTML_HEIGHT = "HTML Height";
     static final String HTML_ALIGN = "HTML Align";
     static final String HTML_ALIGN_TOP = "Top";
     static final String HTML_ALIGN_BOTTOM = "Bottom";
 
     // Web interstitial arguments.
     static final String CLOSE_URL = "Close URL";
     static final String HAS_DISMISS_BUTTON = "Has dismiss button";
--- a/mobile/android/thirdparty/com/leanplum/utils/SizeUtil.java
+++ b/mobile/android/thirdparty/com/leanplum/utils/SizeUtil.java
@@ -18,17 +18,19 @@
  * specific language governing permissions and limitations
  * under the License.
  */
 
 package com.leanplum.utils;
 
 import android.app.Activity;
 import android.content.Context;
+import android.graphics.Point;
 import android.util.DisplayMetrics;
+import android.view.Display;
 import android.view.WindowManager;
 
 /**
  * Utilities for converting between different size units.
  *
  * @author Martin Yanakiev
  */
 public class SizeUtil {
@@ -122,9 +124,28 @@ public class SizeUtil {
       int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
       if (resourceId > 0) {
         result = context.getResources().getDimensionPixelSize(resourceId);
       }
     } catch (Throwable ignored) {
     }
     return result;
   }
+
+  /**
+   * Gets the size of display in pixels.
+   *
+   * @param context Current activity.
+   * @return A Point object with display size information.
+   */
+  public static Point getDisplaySize(Activity context) {
+    Point size = new Point();
+    if (context == null) {
+      return size;
+    }
+    try {
+      Display display = context.getWindowManager().getDefaultDisplay();
+      display.getSize(size);
+    } catch (Throwable ignored) {
+    }
+    return size;
+  }
 }