Bug 1365868 - Add basic context menu: Open in Firefox. r=maliu
authorNevin Chen <cnevinchen@gmail.com>
Thu, 07 Sep 2017 15:52:16 +0800
changeset 429231 4c9a3b09cfc154970eaa62a826d1e92122c7dd7f
parent 429230 5e511ebfe50519f4f6dcc31c5b3f493b61eb9a74
child 429232 dd3736e98e4e7558af2f202803bce36278a26c66
child 429280 20f6207757f3f493b5613ac3e862850cff4fcb97
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmaliu
bugs1365868
milestone57.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 1365868 - Add basic context menu: Open in Firefox. r=maliu MozReview-Commit-ID: BxjFKKNcCiQ
mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
--- a/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
+++ b/mobile/android/base/java/org/mozilla/gecko/customtabs/CustomTabsActivity.java
@@ -1,59 +1,67 @@
 /* -*- 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.customtabs;
 
 import android.app.PendingIntent;
+import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.Browser;
 import android.support.annotation.ColorInt;
 import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
 import android.support.design.widget.Snackbar;
 import android.support.v4.util.SparseArrayCompat;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.text.TextUtils;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
+import android.widget.ListView;
 import android.widget.ProgressBar;
 
 import org.mozilla.gecko.ActivityHandlerHelper;
+import org.mozilla.gecko.BrowserApp;
 import org.mozilla.gecko.DoorHangerPopup;
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoView;
 import org.mozilla.gecko.GeckoViewSettings;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.SnackbarBuilder;
 import org.mozilla.gecko.Telemetry;
 import org.mozilla.gecko.TelemetryContract;
 import org.mozilla.gecko.menu.GeckoMenu;
 import org.mozilla.gecko.menu.GeckoMenuInflater;
 import org.mozilla.gecko.mozglue.SafeIntent;
 import org.mozilla.gecko.permissions.Permissions;
+import org.mozilla.gecko.prompts.Prompt;
+import org.mozilla.gecko.prompts.PromptListItem;
 import org.mozilla.gecko.prompts.PromptService;
 import org.mozilla.gecko.util.ActivityUtils;
 import org.mozilla.gecko.util.Clipboard;
 import org.mozilla.gecko.util.ColorUtil;
 import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.IntentUtils;
+import org.mozilla.gecko.util.ThreadUtils;
+import org.mozilla.gecko.util.URIUtils;
 import org.mozilla.gecko.widget.GeckoPopupMenu;
 
 import java.util.List;
 
 public class CustomTabsActivity extends AppCompatActivity
                                 implements GeckoMenu.Callback,
                                            GeckoView.ContentListener,
                                            GeckoView.NavigationListener,
@@ -592,10 +600,68 @@ public class CustomTabsActivity extends 
             getSupportActionBar().hide();
         } else {
             getSupportActionBar().show();
         }
     }
 
     @Override
     public void onContextMenu(GeckoView view, int screenX, int screenY,
-                              String uri, String elementSrc) {}
+                              final String uri, final String elementSrc) {
+
+        final String content = uri != null ? uri : elementSrc != null ? elementSrc : "";
+        final Uri validUri = getValidURL(content);
+        if (validUri == null) {
+            return;
+        }
+
+        runOnUiThread(new Runnable() {
+            @Override
+            public void run() {
+                openInFennec(validUri, CustomTabsActivity.this);
+            }
+        });
+    }
+
+    void openInFennec(final Uri uri, final Context context) {
+        ThreadUtils.assertOnUiThread();
+
+        final Prompt prompt = new Prompt(context, new Prompt.PromptCallback() {
+            @Override
+            public void onPromptFinished(final GeckoBundle result) {
+
+                final int itemId = result.getInt("button", -1);
+
+                if (itemId == -1) {
+                    // this is the error case, we shouldn't have this situation.
+                    return;
+                }
+                Intent intent = new Intent(context, BrowserApp.class);
+                // BrowserApp's onNewIntent will check action so below is required
+                intent.setAction(Intent.ACTION_VIEW);
+                intent.setData(uri);
+                intent.setPackage(context.getPackageName());
+                context.startActivity(intent);
+
+            }
+        });
+
+        final PromptListItem[] items = new PromptListItem[1];
+        items[0] = new PromptListItem(context.getResources().getString(R.string.overlay_share_open_browser_btn_label));
+        prompt.show("", "", items, ListView.CHOICE_MODE_NONE);
+
+    }
+
+    @Nullable
+    Uri getValidURL(@NonNull String urlString) {
+        final Uri uri = Uri.parse(urlString);
+        if (uri == null) {
+            return null;
+        }
+        final String scheme = uri.getScheme();
+        // currently we only support http and https to open in Firefox
+        if (scheme.equals("http") || scheme.equals("https")) {
+            return uri;
+        } else {
+            return null;
+        }
+    }
 }