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

Commit

Permalink
Merge pull request #3125 from Alexendoo/duplicate-app-name-crash
Browse files Browse the repository at this point in the history
Fix a crash when multiple browsers share the same title
  • Loading branch information
ccrama authored May 24, 2020
2 parents 87669e1 + 02eb14b commit c364426
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@
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.*;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import me.ccrama.redditslide.R;
import me.ccrama.redditslide.Reddit;
import me.ccrama.redditslide.SettingValues;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import me.ccrama.redditslide.R;
import me.ccrama.redditslide.Reddit;
import me.ccrama.redditslide.SettingValues;


public class SettingsHandlingFragment implements CompoundButton.OnCheckedChangeListener {
Expand Down Expand Up @@ -119,19 +127,15 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
private void setUpBrowserLinkHandling() {
RadioGroup radioGroup = context.findViewById(R.id.settings_handling_select_browser_type);
radioGroup.check(LinkHandlingMode.idResFromValue(SettingValues.linkHandlingMode));
radioGroup.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
SettingValues.linkHandlingMode = LinkHandlingMode.valueFromIdRes(checkedId);
SettingValues.prefs.edit()
.putInt(SettingValues.PREF_LINK_HANDLING_MODE,
SettingValues.linkHandlingMode)
.apply();
}
});
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
SettingValues.linkHandlingMode = LinkHandlingMode.valueFromIdRes(checkedId);
SettingValues.prefs.edit()
.putInt(SettingValues.PREF_LINK_HANDLING_MODE,
SettingValues.linkHandlingMode)
.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 @@ -144,34 +148,32 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
context.findViewById(R.id.settings_handling_select_browser).setVisibility(View.GONE);
} else {
context.findViewById(R.id.settings_handling_select_browser).setVisibility(View.VISIBLE);
context.findViewById(R.id.settings_handling_select_browser)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final PopupMenu popupMenu = new PopupMenu(context, v);
for (String name : installedBrowsers.values()) {
popupMenu.getMenu().add(name);
}
popupMenu.setOnMenuItemClickListener(
new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
SettingValues.selectedBrowser =
installedBrowsers.inverse()
.get(item.getTitle());
SettingValues.prefs.edit()
.putString(SettingValues.PREF_SELECTED_BROWSER,
SettingValues.selectedBrowser)
.apply();
((TextView) context.findViewById(
R.id.settings_handling_browser)).setText(
item.getTitle());
return true;
}
});
popupMenu.show();
}
});
context.findViewById(R.id.settings_handling_select_browser).setOnClickListener(v -> {
final PopupMenu popupMenu = new PopupMenu(context, v);
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 = packageNames.get(item);
SettingValues.prefs.edit()
.putString(SettingValues.PREF_SELECTED_BROWSER,
SettingValues.selectedBrowser)
.apply();
((TextView) context.findViewById(
R.id.settings_handling_browser)).setText(
item.getTitle());
return true;
});
popupMenu.show();
});
}
}

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 c364426

Please sign in to comment.