Bug 1429386 Added missing functionalities from a newer LP SDK version r=nechen
authorVlad Baicu <vlad.baicu@softvision.ro>
Thu, 05 Apr 2018 19:09:45 +0300
changeset 412361 4f4e8888611f380f8fb4f8bc7cd888aef1bae3df
parent 412360 9f436684f4f7425e7af421dec2ae39464ec34b2d
child 412362 f20cbdbdc3ac3d2b2c178d84d33c66d46aa7f336
push id33800
push usernerli@mozilla.com
push dateMon, 09 Apr 2018 16:45:06 +0000
treeherdermozilla-central@e37784665179 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnechen
bugs1429386
milestone61.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1429386 Added missing functionalities from a newer LP SDK version r=nechen 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;
+  }
 }