From 02eb14b19000d57b21caa996115861468d22e706 Mon Sep 17 00:00:00 2001 From: Alex Macleod Date: Thu, 20 Feb 2020 18:04:15 +0000 Subject: [PATCH] Fix a crash when multiple browsers share a title The value in the BiMap clashes so replace it with a HashMap from MenuItem to packageName --- .../Fragments/SettingsHandlingFragment.java | 21 +++++++++++++------ .../java/me/ccrama/redditslide/Reddit.java | 4 ++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/me/ccrama/redditslide/Fragments/SettingsHandlingFragment.java b/app/src/main/java/me/ccrama/redditslide/Fragments/SettingsHandlingFragment.java index 9ede4826dc..e6f75d7e06 100644 --- a/app/src/main/java/me/ccrama/redditslide/Fragments/SettingsHandlingFragment.java +++ b/app/src/main/java/me/ccrama/redditslide/Fragments/SettingsHandlingFragment.java @@ -3,10 +3,12 @@ import android.app.Activity; import android.content.Intent; import android.net.Uri; +import android.os.Build; import android.support.annotation.IdRes; import android.support.v7.widget.PopupMenu; import android.support.v7.widget.SwitchCompat; import android.view.KeyEvent; +import android.view.MenuItem; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.CompoundButton; @@ -20,6 +22,7 @@ import java.util.HashMap; import java.util.Locale; +import java.util.Map; import me.ccrama.redditslide.R; import me.ccrama.redditslide.Reddit; @@ -132,7 +135,7 @@ private void setUpBrowserLinkHandling() { .apply(); }); - final BiMap installedBrowsers = Reddit.getInstalledBrowsers(); + final HashMap installedBrowsers = Reddit.getInstalledBrowsers(); if (!installedBrowsers.containsKey(SettingValues.selectedBrowser)) { SettingValues.selectedBrowser = ""; SettingValues.prefs.edit() @@ -147,13 +150,19 @@ private void setUpBrowserLinkHandling() { context.findViewById(R.id.settings_handling_select_browser).setVisibility(View.VISIBLE); context.findViewById(R.id.settings_handling_select_browser).setOnClickListener(v -> { final PopupMenu popupMenu = new PopupMenu(context, v); - for (String name : installedBrowsers.values()) { - popupMenu.getMenu().add(name); + final HashMap packageNames = new HashMap<>(); + + for (Map.Entry entry : installedBrowsers.entrySet()) { + final MenuItem menuItem = popupMenu.getMenu().add(entry.getValue()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + menuItem.setTooltipText(entry.getKey()); + } + + packageNames.put(menuItem, entry.getKey()); } + popupMenu.setOnMenuItemClickListener(item -> { - SettingValues.selectedBrowser = - installedBrowsers.inverse() - .get(item.getTitle()); + SettingValues.selectedBrowser = packageNames.get(item); SettingValues.prefs.edit() .putString(SettingValues.PREF_SELECTED_BROWSER, SettingValues.selectedBrowser) diff --git a/app/src/main/java/me/ccrama/redditslide/Reddit.java b/app/src/main/java/me/ccrama/redditslide/Reddit.java index aef56e04aa..15f261b75f 100644 --- a/app/src/main/java/me/ccrama/redditslide/Reddit.java +++ b/app/src/main/java/me/ccrama/redditslide/Reddit.java @@ -151,11 +151,11 @@ private static boolean isVideoPluginInstalled() { return isPackageInstalled(getAppContext().getString(R.string.youtube_plugin_package)); } - public static BiMap getInstalledBrowsers() { + public static HashMap getInstalledBrowsers() { int packageMatcher = SDK_INT >= M ? PackageManager.MATCH_ALL : PackageManager.GET_DISABLED_COMPONENTS; - final BiMap browserMap = HashBiMap.create(); + HashMap browserMap = new HashMap<>(); final List resolveInfoList = getAppContext().getPackageManager() .queryIntentActivities(