Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Fix a crash when multiple browsers share a title
Browse files Browse the repository at this point in the history
The value in the BiMap clashes so replace it with a HashMap from
MenuItem to packageName
  • Loading branch information
Alexendoo committed Feb 20, 2020
1 parent d6c6c00 commit 02eb14b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -132,7 +135,7 @@ private void setUpBrowserLinkHandling() {
.apply();
});

final BiMap<String, String> installedBrowsers = Reddit.getInstalledBrowsers();
final HashMap<String, String> installedBrowsers = Reddit.getInstalledBrowsers();
if (!installedBrowsers.containsKey(SettingValues.selectedBrowser)) {
SettingValues.selectedBrowser = "";
SettingValues.prefs.edit()
Expand All @@ -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<MenuItem, String> packageNames = new HashMap<>();

for (Map.Entry<String, String> 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)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/me/ccrama/redditslide/Reddit.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ private static boolean isVideoPluginInstalled() {
return isPackageInstalled(getAppContext().getString(R.string.youtube_plugin_package));
}

public static BiMap<String, String> getInstalledBrowsers() {
public static HashMap<String, String> getInstalledBrowsers() {
int packageMatcher =
SDK_INT >= M ? PackageManager.MATCH_ALL : PackageManager.GET_DISABLED_COMPONENTS;

final BiMap<String, String> browserMap = HashBiMap.create();
HashMap<String, String> browserMap = new HashMap<>();

final List<ResolveInfo> resolveInfoList = getAppContext().getPackageManager()
.queryIntentActivities(
Expand Down

0 comments on commit 02eb14b

Please sign in to comment.