From ee078c1495320b425b79dee075b7844007eea731 Mon Sep 17 00:00:00 2001 From: anthony restaino Date: Tue, 2 May 2017 22:04:31 -0400 Subject: [PATCH] Fix bug with background tabs opening urls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a background tab opened a URL that triggered the intent chooser, and the user chose to open the URL in the browser, the current tab would open the URL instead of the originating URL. Using the tab’s hashcode, the original tab now will open the URL. --- .../lightning/activity/TabsManager.java | 20 +++ .../lightning/browser/BrowserPresenter.java | 31 ++--- .../browser/lightning/utils/IntentUtils.java | 6 +- .../browser/lightning/view/LightningView.java | 118 +++++++++--------- 4 files changed, 100 insertions(+), 75 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java index 98098d6..ee189a3 100644 --- a/app/src/main/java/acr/browser/lightning/activity/TabsManager.java +++ b/app/src/main/java/acr/browser/lightning/activity/TabsManager.java @@ -556,6 +556,26 @@ public class TabsManager { return mCurrentTab; } + /** + * Returns the {@link LightningView} with + * the provided hash, or null if there is + * no tab with the hash. + * + * @param hashCode the hashcode. + * @return the tab with an identical hash, or null. + */ + @Nullable + public synchronized LightningView getTabForHashCode(int hashCode) { + for (LightningView tab : mTabList) { + if (tab.getWebView() != null) { + if (tab.getWebView().hashCode() == hashCode) { + return tab; + } + } + } + return null; + } + /** * Switch the current tab to the one at the given position. * It returns the selected tab that has been switced to. diff --git a/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java b/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java index 0163d61..3d61693 100644 --- a/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java +++ b/app/src/main/java/acr/browser/lightning/browser/BrowserPresenter.java @@ -61,16 +61,16 @@ public class BrowserPresenter { */ public void setupTabs(@Nullable Intent intent) { mTabsModel.initializeTabs((Activity) mView, intent, mIsIncognito) - .subscribeOn(Schedulers.main()) - .subscribe(new CompletableOnSubscribe() { - @Override - public void onComplete() { - // At this point we always have at least a tab in the tab manager - mView.notifyTabViewInitialized(); - mView.updateTabNumber(mTabsModel.size()); - tabChanged(mTabsModel.last()); - } - }); + .subscribeOn(Schedulers.main()) + .subscribe(new CompletableOnSubscribe() { + @Override + public void onComplete() { + // At this point we always have at least a tab in the tab manager + mView.notifyTabViewInitialized(); + mView.updateTabNumber(mTabsModel.size()); + tabChanged(mTabsModel.last()); + } + }); } /** @@ -220,13 +220,16 @@ public class BrowserPresenter { } else { url = null; } - int num = 0; + int tabHashCode = 0; if (intent != null && intent.getExtras() != null) { - num = intent.getExtras().getInt(Constants.INTENT_ORIGIN); + tabHashCode = intent.getExtras().getInt(Constants.INTENT_ORIGIN); } - if (num == 1) { - loadUrlInCurrentView(url); + if (tabHashCode != 0) { + LightningView tab = mTabsModel.getTabForHashCode(tabHashCode); + if (tab != null) { + tab.loadUrl(url); + } } else if (url != null) { if (url.startsWith(Constants.FILE)) { mView.showBlockedLocalFileDialog(new DialogInterface.OnClickListener() { diff --git a/app/src/main/java/acr/browser/lightning/utils/IntentUtils.java b/app/src/main/java/acr/browser/lightning/utils/IntentUtils.java index 11cb21a..d7727a8 100644 --- a/app/src/main/java/acr/browser/lightning/utils/IntentUtils.java +++ b/app/src/main/java/acr/browser/lightning/utils/IntentUtils.java @@ -31,7 +31,7 @@ public class IntentUtils { "(?:http|https|file)://" + "|(?:inline|data|about|javascript):" + "|(?:.*:.*@)" + ')' + "(.*)"); - public IntentUtils(Activity activity) { + public IntentUtils(@NonNull Activity activity) { mActivity = activity; } @@ -63,7 +63,7 @@ public class IntentUtils { } } if (tab != null) { - intent.putExtra(Constants.INTENT_ORIGIN, 1); + intent.putExtra(Constants.INTENT_ORIGIN, tab.hashCode()); } Matcher m = ACCEPTED_URI_SCHEMA.matcher(url); @@ -84,7 +84,7 @@ public class IntentUtils { * Search for intent handlers that are specific to this URL aka, specialized * apps like google maps or youtube */ - private boolean isSpecializedHandlerAvailable(Intent intent) { + private boolean isSpecializedHandlerAvailable(@NonNull Intent intent) { PackageManager pm = mActivity.getPackageManager(); List handlers = pm.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER); diff --git a/app/src/main/java/acr/browser/lightning/view/LightningView.java b/app/src/main/java/acr/browser/lightning/view/LightningView.java index d1205cb..8798e17 100644 --- a/app/src/main/java/acr/browser/lightning/view/LightningView.java +++ b/app/src/main/java/acr/browser/lightning/view/LightningView.java @@ -77,20 +77,20 @@ public class LightningView { private static final int API = android.os.Build.VERSION.SDK_INT; private static final int SCROLL_UP_THRESHOLD = Utils.dpToPx(10); - private static String sHomepage; - private static String sDefaultUserAgent; + @Nullable private static String sHomepage; + @Nullable private static String sDefaultUserAgent; private static float sMaxFling; private static final float[] sNegativeColorArray = { - -1.0f, 0, 0, 0, 255, // red - 0, -1.0f, 0, 0, 255, // green - 0, 0, -1.0f, 0, 255, // blue - 0, 0, 0, 1.0f, 0 // alpha + -1.0f, 0, 0, 0, 255, // red + 0, -1.0f, 0, 0, 255, // green + 0, 0, -1.0f, 0, 255, // blue + 0, 0, 0, 1.0f, 0 // alpha }; private static final float[] sIncreaseContrastColorArray = { - 2.0f, 0, 0, 0, -160.f, // red - 0, 2.0f, 0, 0, -160.f, // green - 0, 0, 2.0f, 0, -160.f, // blue - 0, 0, 0, 1.0f, 0 // alpha + 2.0f, 0, 0, 0, -160.f, // red + 0, 2.0f, 0, 0, -160.f, // green + 0, 0, 2.0f, 0, -160.f, // blue + 0, 0, 0, 1.0f, 0 // alpha }; @NonNull private final LightningViewTitle mTitle; @@ -193,6 +193,8 @@ public class LightningView { if (mWebView == null) { return; } + + Preconditions.checkNonNull(sHomepage); switch (sHomepage) { case Constants.SCHEME_HOMEPAGE: loadStartpage(); @@ -213,15 +215,15 @@ public class LightningView { */ private void loadStartpage() { new StartPage().getHomepage() - .subscribeOn(Schedulers.io()) - .observeOn(Schedulers.main()) - .subscribe(new SingleOnSubscribe() { - @Override - public void onItem(@Nullable String item) { - Preconditions.checkNonNull(item); - loadUrl(item); - } - }); + .subscribeOn(Schedulers.io()) + .observeOn(Schedulers.main()) + .subscribe(new SingleOnSubscribe() { + @Override + public void onItem(@Nullable String item) { + Preconditions.checkNonNull(item); + loadUrl(item); + } + }); } /** @@ -231,15 +233,15 @@ public class LightningView { */ public void loadBookmarkpage() { new BookmarkPage(mActivity).getBookmarkPage() - .subscribeOn(Schedulers.io()) - .observeOn(Schedulers.main()) - .subscribe(new SingleOnSubscribe() { - @Override - public void onItem(@Nullable String item) { - Preconditions.checkNonNull(item); - loadUrl(item); - } - }); + .subscribeOn(Schedulers.io()) + .observeOn(Schedulers.main()) + .subscribe(new SingleOnSubscribe() { + @Override + public void onItem(@Nullable String item) { + Preconditions.checkNonNull(item); + loadUrl(item); + } + }); } /** @@ -368,7 +370,7 @@ public class LightningView { } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, - !mPreferences.getBlockThirdPartyCookiesEnabled()); + !mPreferences.getBlockThirdPartyCookiesEnabled()); } } @@ -421,44 +423,44 @@ public class LightningView { } getPathObservable("appcache").subscribeOn(Schedulers.io()) + .observeOn(Schedulers.main()) + .subscribe(new SingleOnSubscribe() { + @Override + public void onItem(@Nullable File item) { + Preconditions.checkNonNull(item); + settings.setAppCachePath(item.getPath()); + } + }); + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { + getPathObservable("geolocation").subscribeOn(Schedulers.io()) .observeOn(Schedulers.main()) .subscribe(new SingleOnSubscribe() { @Override public void onItem(@Nullable File item) { Preconditions.checkNonNull(item); - settings.setAppCachePath(item.getPath()); + //noinspection deprecation + settings.setGeolocationDatabasePath(item.getPath()); } }); - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { - getPathObservable("geolocation").subscribeOn(Schedulers.io()) - .observeOn(Schedulers.main()) - .subscribe(new SingleOnSubscribe() { - @Override - public void onItem(@Nullable File item) { - Preconditions.checkNonNull(item); - //noinspection deprecation - settings.setGeolocationDatabasePath(item.getPath()); - } - }); } getPathObservable("databases").subscribeOn(Schedulers.io()) - .observeOn(Schedulers.main()) - .subscribe(new SingleOnSubscribe() { - @Override - public void onItem(@Nullable File item) { - if (API < Build.VERSION_CODES.KITKAT) { - Preconditions.checkNonNull(item); - //noinspection deprecation - settings.setDatabasePath(item.getPath()); - } + .observeOn(Schedulers.main()) + .subscribe(new SingleOnSubscribe() { + @Override + public void onItem(@Nullable File item) { + if (API < Build.VERSION_CODES.KITKAT) { + Preconditions.checkNonNull(item); + //noinspection deprecation + settings.setDatabasePath(item.getPath()); } + } - @Override - public void onComplete() { - } - }); + @Override + public void onComplete() { + } + }); } @@ -714,7 +716,7 @@ public class LightningView { break; case 1: ColorMatrixColorFilter filterInvert = new ColorMatrixColorFilter( - sNegativeColorArray); + sNegativeColorArray); mPaint.setColorFilter(filterInvert); setHardwareRendering(); @@ -743,7 +745,7 @@ public class LightningView { case 4: ColorMatrixColorFilter IncreaseHighContrast = new ColorMatrixColorFilter( - sIncreaseContrastColorArray); + sIncreaseContrastColorArray); mPaint.setColorFilter(IncreaseHighContrast); setHardwareRendering(); break; @@ -972,7 +974,7 @@ public class LightningView { if (currentUrl != null && UrlUtils.isSpecialUrl(currentUrl)) { if (currentUrl.endsWith(HistoryPage.FILENAME)) { if (url != null) { - mBookmarksDialogBuilder.showLongPressedHistoryLinkDialog(mActivity,mUIController, url); + mBookmarksDialogBuilder.showLongPressedHistoryLinkDialog(mActivity, mUIController, url); } else if (result != null && result.getExtra() != null) { final String newUrl = result.getExtra(); mBookmarksDialogBuilder.showLongPressedHistoryLinkDialog(mActivity, mUIController, newUrl);