Bug 1393672 - Show PWA confirm prompt. r?jwu draft
authorNevin Chen(:nechen) <cnevinchen@gmail.com>
Tue, 26 Sep 2017 17:40:05 +0800
changeset 678460 3cb3e55698dfb46ef4634fe2c71bc184f7925d6f
parent 678459 e48cb5e921234205692a6229ab3c11d456f202e1
child 678461 d1b3738b7ee93aa0d023242a6726b7744fbbc8ff
push id83926
push userbmo:cnevinchen@gmail.com
push dateWed, 11 Oct 2017 10:32:10 +0000
reviewersjwu
bugs1393672
milestone58.0a1
Bug 1393672 - Show PWA confirm prompt. r?jwu MozReview-Commit-ID: 2IlxzN6eFRo
mobile/android/app/src/main/res/drawable/pwa_gradient.xml
mobile/android/app/src/main/res/layout/pwa_confirm.xml
mobile/android/base/java/org/mozilla/gecko/toolbar/PageActionLayout.java
mobile/android/base/java/org/mozilla/gecko/toolbar/PwaConfirm.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/app/src/main/res/drawable/pwa_gradient.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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/. -->
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="rectangle">
+    <!--  Gradient Bg for listrow -->
+    <gradient
+        android:startColor="@color/photon_loading_indicator_light"
+        android:centerColor="@color/photon_loading_indicator_dark"
+        android:endColor="@color/photon_highlight"
+        android:angle="315" />
+</shape>
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/mobile/android/app/src/main/res/layout/pwa_confirm.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="utf-8"?>
+<org.mozilla.gecko.toolbar.PwaConfirm xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/pwa_confirm_root"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:background="@color/photon_browser_toolbar_bg"
+    android:clickable="true"
+    android:elevation="15dp">
+
+
+    <ImageView
+        android:id="@+id/pwa_confirm_icon"
+        android:layout_width="40dp"
+        android:layout_height="40dp"
+        android:layout_margin="20dp"
+        tools:src="@drawable/favicon_globe" />
+
+
+    <TextView
+        android:id="@+id/pwa_confirm_title"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_alignTop="@+id/pwa_confirm_icon"
+        android:layout_toEndOf="@+id/pwa_confirm_icon"
+        android:layout_toStartOf="@+id/pwa_confirm_cancel"
+        android:maxLines="2"
+        android:textSize="18sp"
+        android:textStyle="bold"
+        tools:text="Mozilla title can grow to two lines... do" />
+
+    <TextView
+        android:id="@+id/pwa_confirm_url"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_alignEnd="@+id/pwa_confirm_title"
+        android:layout_alignStart="@+id/pwa_confirm_title"
+        android:layout_below="@+id/pwa_confirm_title"
+        android:maxLines="1"
+        android:textSize="13sp"
+        tools:text="http://www.mozilla.org/12345678911123" />
+
+
+    <ImageButton
+        android:id="@+id/pwa_confirm_cancel"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:layout_alignParentEnd="true"
+        android:layout_alignParentTop="true"
+        android:background="@null"
+        android:src="@drawable/ic_cancel_nm" />
+
+    <Button
+        android:id="@+id/pwa_confirm_action"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_below="@+id/pwa_confirm_url"
+        android:layout_marginTop="20dp"
+        android:background="@drawable/pwa_gradient"
+        android:gravity="center"
+        android:text="+ Add To Home Screen"
+        android:textAllCaps="true"
+        android:textColor="#fff"
+        android:textStyle="bold" />
+
+</org.mozilla.gecko.toolbar.PwaConfirm>
\ No newline at end of file
--- a/mobile/android/base/java/org/mozilla/gecko/toolbar/PageActionLayout.java
+++ b/mobile/android/base/java/org/mozilla/gecko/toolbar/PageActionLayout.java
@@ -50,16 +50,17 @@ public class PageActionLayout extends Th
     private final Context mContext;
     private final LinearLayout mLayout;
     private final List<PageAction> mPageActionList;
 
     private GeckoPopupMenu mPageActionsMenu;
 
     // By default it's two, can be changed by calling setNumberShown(int)
     private int mMaxVisiblePageActions;
+    private PwaConfirm mPwaConfirm;
 
     public PageActionLayout(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContext = context;
         mLayout = this;
 
         mPageActionList = new ArrayList<PageAction>();
         setNumberShown(DEFAULT_PAGE_ACTIONS_SHOWN);
@@ -107,16 +108,18 @@ public class PageActionLayout extends Th
         }
     }
 
     @Override // BundleEventListener
     public void handleMessage(final String event, final GeckoBundle message,
                               final EventCallback callback) {
         ThreadUtils.assertOnUiThread();
 
+        hidePreviousConfirmPrompt();
+
         if ("PageActions:Add".equals(event)) {
             final String id = message.getString("id");
 
             boolean alreadyAdded = isPwaAdded(id);
             if (alreadyAdded) {
                 return;
             }
 
@@ -126,17 +129,17 @@ public class PageActionLayout extends Th
             final String imageURL = message.getString("icon");
             final boolean important = message.getBoolean("important");
             final boolean useTint = message.getBoolean("useTint");
 
             addPageAction(id, title, imageURL, useTint, new OnPageActionClickListeners() {
                 @Override
                 public void onClick(final String id) {
                     if (id != null && id.equals(PageAction.UUID_PAGE_ACTION_PWA)) {
-                        GeckoApplication.createShortcut();
+                        mPwaConfirm = PwaConfirm.show(getContext());
                         return;
                     }
                     final GeckoBundle data = new GeckoBundle(1);
                     data.putString("id", id);
                     EventDispatcher.getInstance().dispatch("PageActions:Clicked", data);
                 }
 
                 @Override
@@ -359,16 +362,25 @@ public class PageActionLayout extends Th
         mPageActionsMenu.show();
     }
 
     private static interface OnPageActionClickListeners {
         public void onClick(String id);
         public boolean onLongClick(String id);
     }
 
+    private void hidePreviousConfirmPrompt() {
+        if (mPwaConfirm != null) {
+            if (mPwaConfirm.isAttachedToWindow() || mPwaConfirm.getParent() != null) {
+                mPwaConfirm.dismiss();
+            }
+            mPwaConfirm = null;
+        }
+    }
+
     public static class PageAction {
         public static final String UUID_PAGE_ACTION_PWA = "279c269d-6397-4f86-a6d2-452e26456d4a";
 
         private final OnPageActionClickListeners mOnPageActionClickListeners;
         private Drawable mDrawable;
         private final String mTitle;
         private final String mId;
         private final int key;
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/toolbar/PwaConfirm.java
@@ -0,0 +1,102 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; 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.toolbar;
+
+import android.app.Activity;
+import android.content.Context;
+import android.support.annotation.Nullable;
+import android.util.AttributeSet;
+import android.view.KeyEvent;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.RelativeLayout;
+import android.widget.TextView;
+
+import org.mozilla.gecko.GeckoApplication;
+import org.mozilla.gecko.R;
+import org.mozilla.gecko.Tab;
+import org.mozilla.gecko.Tabs;
+
+public class PwaConfirm extends RelativeLayout {
+
+    public static PwaConfirm show(Context context) {
+        if (context instanceof Activity) {
+            final ViewGroup contetView = (ViewGroup) ((Activity) context).findViewById(R.id.main_layout);
+            View parent = LayoutInflater.from(context).inflate(R.layout.pwa_confirm, contetView);
+            return (PwaConfirm) parent.findViewById(R.id.pwa_confirm_root);
+        }
+        return null;
+    }
+
+    public PwaConfirm(Context context) {
+        super(context);
+    }
+
+    public PwaConfirm(Context context, @Nullable AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public PwaConfirm(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+    }
+
+    @Override
+    protected void onAttachedToWindow() {
+        super.onAttachedToWindow();
+        if (isInEditMode()) {
+            return;
+        }
+        init();
+        setupBackpress();
+    }
+
+    private void init() {
+        final OnClickListener dismiss = new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                dismiss();
+            }
+        };
+        final OnClickListener createShortcut = new OnClickListener() {
+
+            @Override
+            public void onClick(View v) {
+                GeckoApplication.createShortcut();
+                dismiss();
+            }
+        };
+        findViewById(R.id.pwa_confirm_root).setOnClickListener(dismiss);
+        findViewById(R.id.pwa_confirm_cancel).setOnClickListener(dismiss);
+        findViewById(R.id.pwa_confirm_action).setOnClickListener(createShortcut);
+
+
+        final Tab selectedTab = Tabs.getInstance().getSelectedTab();
+
+        ((TextView) findViewById(R.id.pwa_confirm_title)).setText(selectedTab.getTitle());
+        ((TextView) findViewById(R.id.pwa_confirm_url)).setText(selectedTab.getURL());
+        ((ImageView) findViewById(R.id.pwa_confirm_icon)).setImageBitmap(selectedTab.getFavicon());
+
+    }
+
+    private void setupBackpress() {
+        setFocusableInTouchMode(true);
+        requestFocus();
+        setOnKeyListener(new OnKeyListener() {
+            @Override
+            public boolean onKey(View v, int keyCode, KeyEvent event) {
+                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
+                    dismiss();
+                }
+                return true;
+            }
+        });
+    }
+
+    public void dismiss() {
+        ((ViewGroup) this.getParent()).removeView(this);
+    }
+}