mirror of https://github.com/M66B/FairEmail.git
Improved browser selector
This commit is contained in:
parent
8f1fd6350a
commit
c072793b58
|
@ -31,8 +31,10 @@ import android.content.Intent;
|
|||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
|
@ -49,6 +51,7 @@ import android.util.Pair;
|
|||
import android.view.KeyEvent;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
|
@ -115,6 +118,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||
|
||||
String def = null;
|
||||
List<Package> pkgs = new ArrayList<>();
|
||||
int dp24 = Helper.dp2pixels(context, 24);
|
||||
if (UriHelper.isHyperLink(uri)) {
|
||||
try {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
|
@ -129,10 +133,28 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||
int flags = (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? 0 : PackageManager.MATCH_ALL);
|
||||
List<ResolveInfo> ris = pm.queryIntentActivities(intent, flags);
|
||||
for (ResolveInfo ri : ris) {
|
||||
CharSequence label = ri.activityInfo.applicationInfo.loadLabel(pm);
|
||||
if (label == null)
|
||||
continue;
|
||||
pkgs.add(new Package(label, ri.activityInfo.packageName, false));
|
||||
Resources res = pm.getResourcesForApplication(ri.activityInfo.applicationInfo);
|
||||
Drawable icon;
|
||||
try {
|
||||
icon = res.getDrawable(ri.activityInfo.applicationInfo.icon);
|
||||
// Maximum size = 192x192
|
||||
if (icon != null &&
|
||||
(icon.getIntrinsicWidth() > 256 || icon.getIntrinsicHeight() > 256))
|
||||
icon = null;
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
icon = null;
|
||||
}
|
||||
CharSequence label;
|
||||
try {
|
||||
label = res.getString(ri.activityInfo.applicationInfo.labelRes);
|
||||
} catch (Throwable ex) {
|
||||
Log.w(ex);
|
||||
label = null;
|
||||
}
|
||||
if (icon != null)
|
||||
icon.setBounds(0, 0, dp24, dp24);
|
||||
pkgs.add(new Package(icon, label, ri.activityInfo.packageName, false));
|
||||
|
||||
try {
|
||||
Intent serviceIntent = new Intent();
|
||||
|
@ -140,7 +162,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||
serviceIntent.setPackage(ri.activityInfo.packageName);
|
||||
boolean tabs = (pm.resolveService(serviceIntent, 0) != null);
|
||||
if (tabs)
|
||||
pkgs.add(new Package(label, ri.activityInfo.packageName, true));
|
||||
pkgs.add(new Package(icon, label, ri.activityInfo.packageName, true));
|
||||
} catch (Throwable ex) {
|
||||
Log.e(ex);
|
||||
}
|
||||
|
@ -346,9 +368,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||
}
|
||||
});
|
||||
|
||||
ArrayAdapter<Package> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, android.R.id.text1);
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
||||
adapter.addAll(pkgs);
|
||||
AdapterPackage adapter = new AdapterPackage(getContext(), pkgs);
|
||||
spOpenWith.setAdapter(adapter);
|
||||
|
||||
String open_with_pkg = prefs.getString("open_with_pkg", null);
|
||||
|
@ -657,19 +677,63 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||
}
|
||||
|
||||
private static class Package {
|
||||
CharSequence title;
|
||||
String name;
|
||||
boolean tabs;
|
||||
private Drawable icon;
|
||||
private CharSequence title;
|
||||
private String name;
|
||||
private boolean tabs;
|
||||
|
||||
public Package(CharSequence title, String name, boolean tabs) {
|
||||
public Package(Drawable icon, CharSequence title, String name, boolean tabs) {
|
||||
this.icon = icon;
|
||||
this.title = title;
|
||||
this.name = name;
|
||||
this.tabs = tabs;
|
||||
}
|
||||
}
|
||||
|
||||
public class AdapterPackage extends ArrayAdapter<Package> {
|
||||
private final Context context;
|
||||
private final List<Package> pkgs;
|
||||
private final Drawable external;
|
||||
private final Drawable browser;
|
||||
|
||||
AdapterPackage(@NonNull Context context, List<Package> pkgs) {
|
||||
super(context, 0, pkgs);
|
||||
this.context = context;
|
||||
this.pkgs = pkgs;
|
||||
this.external = context.getDrawable(R.drawable.twotone_open_in_new_24);
|
||||
if (external != null)
|
||||
external.setBounds(0, 0, external.getIntrinsicWidth(), external.getIntrinsicHeight());
|
||||
this.browser = context.getDrawable(R.drawable.twotone_language_24);
|
||||
if (browser != null)
|
||||
browser.setBounds(0, 0, browser.getIntrinsicWidth(), browser.getIntrinsicHeight());
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
|
||||
return getLayout(position, convertView, parent, R.layout.spinner_package);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.title + (tabs ? "" : " \u29c9");
|
||||
public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
|
||||
return getLayout(position, convertView, parent, R.layout.spinner_package);
|
||||
}
|
||||
|
||||
private View getLayout(int position, View convertView, ViewGroup parent, int resid) {
|
||||
View view = LayoutInflater.from(context).inflate(resid, parent, false);
|
||||
TextView text1 = view.findViewById(android.R.id.text1);
|
||||
|
||||
Package pkg = pkgs.get(position);
|
||||
if (pkg != null) {
|
||||
text1.setText(pkg.title == null ? pkg.name : pkg.title.toString());
|
||||
text1.setCompoundDrawablesRelative(
|
||||
pkg.icon == null ? browser : pkg.icon,
|
||||
null,
|
||||
pkg.tabs ? null : external,
|
||||
null);
|
||||
}
|
||||
|
||||
return view;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -210,9 +210,10 @@
|
|||
|
||||
<Spinner
|
||||
android:id="@+id/spOpenWith"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="6dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/tvOpenWith" />
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:padding="12dp">
|
||||
|
||||
<TextView
|
||||
android:id="@android:id/text1"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:drawablePadding="6dp"
|
||||
android:text="Package"
|
||||
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue