Bug 1386192 - add custom template. WIP draft
authorNevin Chen <cnevinchen@gmail.com>
Fri, 28 Jul 2017 16:39:01 +0800
changeset 618990 9924a2e3eea481c0141688acd5e715d16a3884ec
parent 616205 f1693d664f8e8ee4c79801630c181c28095cad56
child 640251 cfae2d622615e49941d762e4daf3a7c2258a9587
push id71525
push userbmo:cnevinchen@gmail.com
push dateTue, 01 Aug 2017 10:04:01 +0000
bugs1386192
milestone56.0a1
Bug 1386192 - add custom template. WIP MozReview-Commit-ID: FKkRP8FkwAJ
mobile/android/app/src/photon/res/layout/ad.xml
mobile/android/app/src/photon/res/layout/ad_pager_item.xml
mobile/android/base/java/org/mozilla/gecko/mma/CustomPagerAdapter.java
mobile/android/base/java/org/mozilla/gecko/mma/LeanplumOnboarding.java
mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
mobile/android/base/java/org/mozilla/gecko/mma/MmaMessageTemplates.java
mobile/android/bouncer/java/org/mozilla/gecko/BrowserApp.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/app/src/photon/res/layout/ad.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+                xmlns:tools="http://schemas.android.com/tools"
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+
+                android:id="@+id/relativeLayout">
+
+
+    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
+                                       android:id="@+id/pager"
+                                       android:layout_width="match_parent"
+                                       android:layout_height="match_parent">
+    </android.support.v4.view.ViewPager>
+
+</RelativeLayout>
new file mode 100644
--- /dev/null
+++ b/mobile/android/app/src/photon/res/layout/ad_pager_item.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="utf-8"?>
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+              xmlns:tools="http://schemas.android.com/tools"
+              android:layout_width="match_parent"
+              android:layout_height="match_parent"
+              android:background="#A4C069"
+              android:orientation="vertical">
+
+    <ImageView
+        android:id="@+id/imageView"
+        android:layout_gravity="center"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        tools:src="@drawable/firstrun_account"/>
+
+    <TextView
+        android:id="@+id/ad_text"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center"
+        android:text="@string/firstrun_account_message"
+        android:textSize="20sp"/>
+
+    <Button
+        android:id="@+id/ad_bt"
+        android:layout_gravity="center"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:text="Go"
+        />
+</LinearLayout>
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/CustomPagerAdapter.java
@@ -0,0 +1,81 @@
+package org.mozilla.gecko.mma;
+
+import android.content.Context;
+import android.support.v4.view.PagerAdapter;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.Button;
+import android.widget.ImageView;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import org.mozilla.gecko.R;
+
+
+class CustomPagerAdapter extends PagerAdapter {
+
+    Context mContext;
+    LayoutInflater mLayoutInflater;
+    int[] mResources = {
+            R.drawable.firstrun_tabqueue_on,
+            R.drawable.firstrun_signin,
+            R.drawable.firstrun_readerview,
+            R.drawable.firstrun_data_off,
+            R.drawable.firstrun_bookmarks,
+            R.drawable.firstrun_sync
+    };
+
+    int[] mStr = {
+            R.string.firstrun_account_message,
+            R.string.firstrun_account_title,
+            R.string.firstrun_bookmarks_message,
+            R.string.firstrun_data_message,
+            R.string.firstrun_readerview_message,
+            R.string.firstrun_panel_title_welcome
+    };
+
+    public CustomPagerAdapter(Context context) {
+        mContext = context;
+        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+    }
+
+    @Override
+    public int getCount() {
+        return mResources.length;
+    }
+
+
+    @Override
+    public boolean isViewFromObject(View view, Object object) {
+        return view == ((LinearLayout) object);
+    }
+
+    @Override
+    public Object instantiateItem(ViewGroup container, int position) {
+        View itemView = mLayoutInflater.inflate(R.layout.ad_pager_item, container, false);
+
+        ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
+        imageView.setImageResource(mResources[position]);
+
+        TextView tv = (TextView) itemView.findViewById(R.id.ad_text);
+        tv.setText(mStr[position]);
+
+        Button bt = (Button) itemView.findViewById(R.id.ad_bt);
+        bt.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                ((View)v.getParent().getParent()).setVisibility(View.GONE);
+            }
+        });
+
+        container.addView(itemView);
+
+        return itemView;
+    }
+
+    @Override
+    public void destroyItem(ViewGroup container, int position, Object object) {
+        container.removeView((LinearLayout) object);
+    }
+}
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/LeanplumOnboarding.java
@@ -0,0 +1,102 @@
+package org.mozilla.gecko.mma;
+
+import android.app.Activity;
+import android.content.Context;
+import android.support.v4.view.ViewPager;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+import com.leanplum.ActionArgs;
+import com.leanplum.ActionContext;
+import com.leanplum.Leanplum;
+import com.leanplum.LeanplumActivityHelper;
+import com.leanplum.callbacks.ActionCallback;
+import com.leanplum.callbacks.VariablesChangedCallback;
+
+import org.mozilla.gecko.R;
+
+import static org.mozilla.gecko.mma.MmaMessageTemplates.getApplicationName;
+
+// Creating a new class for the 3-buttons Confirm Message
+// Class name will be the name displayed in the Message dropdown on the Dashboard
+
+// In this case we basically got the code from the Confirm class and added code for the third 'Maybe' button
+
+public class LeanplumOnboarding {
+
+    private static final String NAME = "Post 57 On Boarding";
+
+    public static void register(Context currentContext) {
+        Leanplum.defineAction(
+                NAME,
+                Leanplum.ACTION_KIND_MESSAGE | Leanplum.ACTION_KIND_ACTION,
+                new ActionArgs().with(MmaMessageTemplates.Args.TITLE, getApplicationName(currentContext))
+                        .with(MmaMessageTemplates.Args.MESSAGE, MmaMessageTemplates.Values.CONFIRM_MESSAGE)
+                        .with(MmaMessageTemplates.Args.ACCEPT_TEXT, MmaMessageTemplates.Values.YES_TEXT)
+                        .with(MmaMessageTemplates.Args.CANCEL_TEXT, MmaMessageTemplates.Values.NO_TEXT)
+                        // #### example: adding Text and Values for the Maybe button options
+                        .with(MmaMessageTemplates.Args.MAYBE_TEXT, MmaMessageTemplates.Values.MAYBE_TEXT)
+                        .withAction(MmaMessageTemplates.Args.ACCEPT_ACTION, null)
+                        .withAction(MmaMessageTemplates.Args.CANCEL_ACTION, null)
+                        // #### example: adding the Action for the Maybe button
+                        .withAction(MmaMessageTemplates.Args.MAYBE_ACTION, null), new ActionCallback() {
+
+                    @Override
+                    public boolean onResponse(final ActionContext context) {
+                        LeanplumActivityHelper.queueActionUponActive(new VariablesChangedCallback() {
+                            @Override
+                            public void variablesChanged() {
+                                Activity activity = LeanplumActivityHelper.getCurrentActivity();
+                                final ViewGroup main = (ViewGroup) activity.findViewById(R.id.main_layout);
+                                final View content = LayoutInflater.from(activity).inflate(R.layout.ad, main);
+                                content.setOnClickListener(new View.OnClickListener() {
+                                    @Override
+                                    public void onClick(View view) {
+                                        view.setVisibility(View.GONE);
+                                    }
+                                });
+                                CustomPagerAdapter mCustomPagerAdapter = new CustomPagerAdapter(activity);
+
+                                ViewPager mViewPager = (ViewPager) activity.findViewById(R.id.pager);
+                                mViewPager.setAdapter(mCustomPagerAdapter);
+
+//                                Toast.makeText(activity,"Just a test",Toast.LENGTH_LONG).show();
+//
+//                                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
+//                                        activity);
+//                                alertDialogBuilder
+//                                        .setTitle(context.stringNamed(MessageTemplates.Args.TITLE))
+//                                        .setMessage(context.stringNamed(MessageTemplates.Args.MESSAGE))
+//                                        .setCancelable(false)
+//                                        .setPositiveButton(context.stringNamed(MessageTemplates.Args.ACCEPT_TEXT),
+//                                                new DialogInterface.OnClickListener() {
+//                                                    public void onClick(DialogInterface dialog, int id) {
+//                                                        context.runTrackedActionNamed(MessageTemplates.Args.ACCEPT_ACTION);
+//                                                    }
+//                                                })
+//                                        .setNegativeButton(context.stringNamed(MessageTemplates.Args.CANCEL_TEXT),
+//                                                new DialogInterface.OnClickListener() {
+//                                                    public void onClick(DialogInterface dialog, int id) {
+//                                                        context.runActionNamed(MessageTemplates.Args.CANCEL_ACTION);
+//                                                    }
+//                                                })
+//
+//                                        // #### example: adding the Maybe button to the Alert dialog
+//                                        .setNeutralButton(context.stringNamed(MessageTemplates.Args.MAYBE_TEXT),
+//                                                new DialogInterface.OnClickListener() {
+//                                                    public void onClick(DialogInterface dialog, int id) {
+//                                                        context.runActionNamed(MessageTemplates.Args.MAYBE_ACTION);
+//                                                    }
+//                                                });
+//                                AlertDialog alertDialog = alertDialogBuilder.create();
+//                                alertDialog.show();
+                            }
+                        });
+                        return true;
+                    }
+                });
+    }
+
+
+}
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaDelegate.java
@@ -111,16 +111,18 @@ public class MmaDelegate {
         return attributes;
     }
 
 
     public static void track(String event) {
         if (isMmaEnabled()) {
             mmaHelper.event(event);
         }
+
+
     }
 
     public static void track(String event, long value) {
         if (isMmaEnabled()) {
             mmaHelper.event(event, value);
         }
     }
 
--- a/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaLeanplumImp.java
@@ -7,16 +7,17 @@
 package org.mozilla.gecko.mma;
 
 import android.app.Activity;
 import android.content.Context;
 import android.content.SharedPreferences;
 
 import com.leanplum.Leanplum;
 import com.leanplum.LeanplumActivityHelper;
+import com.leanplum.messagetemplates.MessageTemplates;
 
 import org.mozilla.gecko.AppConstants;
 import org.mozilla.gecko.MmaConstants;
 
 import java.util.Map;
 import java.util.UUID;
 
 
@@ -42,16 +43,18 @@ public class MmaLeanplumImp implements M
         final SharedPreferences sharedPreferences = activity.getPreferences(0);
         String deviceId = sharedPreferences.getString(KEY_ANDROID_PREF_STRING_LEANPLUM_DEVICE_ID, null);
         if (deviceId == null) {
             deviceId = UUID.randomUUID().toString();
             sharedPreferences.edit().putString(KEY_ANDROID_PREF_STRING_LEANPLUM_DEVICE_ID, deviceId).apply();
         }
         Leanplum.setDeviceId(deviceId);
 
+        MmaMessageTemplates.register(activity.getApplicationContext());
+
         if (attributes != null) {
             Leanplum.start(activity, attributes);
         } else {
             Leanplum.start(activity);
         }
 
         // this is special to Leanplum. Since we defer LeanplumActivityHelper's onResume call till
         // switchboard completes loading. We miss the call to LeanplumActivityHelper.onResume.
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/mma/MmaMessageTemplates.java
@@ -0,0 +1,88 @@
+// Copyright 2014, Leanplum, Inc.
+
+package org.mozilla.gecko.mma;
+
+import android.content.Context;
+
+/**
+ * Registers all of the built-in message templates.
+ *
+ * @author Andrew First
+ */
+public class MmaMessageTemplates {
+    static class Args {
+        // Open URL
+        static final String URL = "URL";
+
+        // Alert/confirm arguments.
+        static final String TITLE = "Title";
+        static final String MESSAGE = "Message";
+        static final String ACCEPT_TEXT = "Accept text";
+        static final String CANCEL_TEXT = "Cancel text";
+
+        // #### example:
+        static final String MAYBE_TEXT = "Maybe text";
+
+
+        static final String DISMISS_TEXT = "Dismiss text";
+        static final String ACCEPT_ACTION = "Accept action";
+        static final String CANCEL_ACTION = "Cancel action";
+        static final String DISMISS_ACTION = "Dismiss action";
+        static final String MAYBE_ACTION = "Maybe action";
+
+        // Center popup/interstitial arguments.
+        static final String TITLE_TEXT = "Title.Text";
+        static final String TITLE_COLOR = "Title.Color";
+        static final String MESSAGE_TEXT = "Message.Text";
+        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";
+
+        // Web interstitial arguments.
+        static final String CLOSE_URL = "Close URL";
+        static final String HAS_DISMISS_BUTTON = "Has dismiss button";
+    }
+
+    static class Values {
+        static final String ALERT_MESSAGE = "Alert message goes here.";
+        static final String CONFIRM_MESSAGE = "Confirmation message goes here.";
+        static final String POPUP_MESSAGE = "Popup message goes here.";
+        static final String INTERSTITIAL_MESSAGE = "Interstitial message goes here.";
+        static final String OK_TEXT = "OK";
+        static final String YES_TEXT = "Yes";
+        static final String NO_TEXT = "No";
+        static final String MAYBE_TEXT = "Maybe";
+        static final int CENTER_POPUP_WIDTH = 300;
+        static final int CENTER_POPUP_HEIGHT = 250;
+
+        // Open URL
+        static final String DEFAULT_URL = "http://www.example.com";
+
+        // Web interstitial values
+        static final String DEFAULT_CLOSE_URL = "http://leanplum:close";
+        static final boolean DEFAULT_HAS_DISMISS_BUTTON = true;
+    }
+
+    private static Boolean registered = false;
+
+    static String getApplicationName(Context context) {
+        int stringId = context.getApplicationInfo().labelRes;
+        if (stringId == 0) {
+            return context.getApplicationInfo().loadLabel(context.getPackageManager()).toString();
+        }
+        return context.getString(stringId);
+    }
+
+    public synchronized static void register(Context currentContext) {
+        if (registered) {
+            return;
+        }
+        registered = true;
+        LeanplumOnboarding.register(currentContext);
+    }
+}
deleted file mode 100644
--- a/mobile/android/bouncer/java/org/mozilla/gecko/BrowserApp.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-
-package org.mozilla.gecko;
-
-import android.app.Activity;
-import android.content.Intent;
-import android.net.Uri;
-import android.os.Bundle;
-import android.util.Log;
-
-import org.mozilla.bouncer.BouncerService;
-
-/**
- * Bouncer activity version of BrowserApp.
- *
- * This class has the same name as org.mozilla.gecko.BrowserApp to preserve
- * shortcuts created by the bouncer app.
- */
-public class BrowserApp extends Activity {
-    private static final String LOGTAG = "GeckoBouncerActivity";
-
-    @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
-        // This races distribution installation against the Play Store killing our process to
-        // install the update.  We'll live with it.  To do better, consider using an Intent to
-        // notify when the service has completed.
-        startService(new Intent(this, BouncerService.class));
-
-        final String appPackageName = Uri.encode(getPackageName());
-        final Uri uri = Uri.parse("market://details?id=" + appPackageName);
-        Log.i(LOGTAG, "Lanching activity with URL: " + uri.toString());
-
-        // It might be more correct to catch failure in case the Play Store isn't installed.  The
-        // fallback action is to open the Play Store website... but doing so may offer Firefox as
-        // browser (since even the bouncer offers to view URLs), which will be very confusing.
-        // Therefore, we don't try to be fancy here, and we just fail (silently).
-        startActivity(new Intent(Intent.ACTION_VIEW, uri));
-
-        finish();
-    }
-}