mirror of
https://github.com/M66B/FairEmail.git
synced 2026-01-03 19:34:15 +01:00
Improved browser selector
This commit is contained in:
@@ -31,8 +31,10 @@ import android.content.Intent;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.ResolveInfo;
|
import android.content.pm.ResolveInfo;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.graphics.Paint;
|
import android.graphics.Paint;
|
||||||
import android.graphics.Typeface;
|
import android.graphics.Typeface;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@@ -49,6 +51,7 @@ import android.util.Pair;
|
|||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
import android.view.inputmethod.EditorInfo;
|
import android.view.inputmethod.EditorInfo;
|
||||||
import android.widget.AdapterView;
|
import android.widget.AdapterView;
|
||||||
import android.widget.ArrayAdapter;
|
import android.widget.ArrayAdapter;
|
||||||
@@ -115,6 +118,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||||||
|
|
||||||
String def = null;
|
String def = null;
|
||||||
List<Package> pkgs = new ArrayList<>();
|
List<Package> pkgs = new ArrayList<>();
|
||||||
|
int dp24 = Helper.dp2pixels(context, 24);
|
||||||
if (UriHelper.isHyperLink(uri)) {
|
if (UriHelper.isHyperLink(uri)) {
|
||||||
try {
|
try {
|
||||||
PackageManager pm = context.getPackageManager();
|
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);
|
int flags = (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? 0 : PackageManager.MATCH_ALL);
|
||||||
List<ResolveInfo> ris = pm.queryIntentActivities(intent, flags);
|
List<ResolveInfo> ris = pm.queryIntentActivities(intent, flags);
|
||||||
for (ResolveInfo ri : ris) {
|
for (ResolveInfo ri : ris) {
|
||||||
CharSequence label = ri.activityInfo.applicationInfo.loadLabel(pm);
|
Resources res = pm.getResourcesForApplication(ri.activityInfo.applicationInfo);
|
||||||
if (label == null)
|
Drawable icon;
|
||||||
continue;
|
try {
|
||||||
pkgs.add(new Package(label, ri.activityInfo.packageName, false));
|
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 {
|
try {
|
||||||
Intent serviceIntent = new Intent();
|
Intent serviceIntent = new Intent();
|
||||||
@@ -140,7 +162,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||||||
serviceIntent.setPackage(ri.activityInfo.packageName);
|
serviceIntent.setPackage(ri.activityInfo.packageName);
|
||||||
boolean tabs = (pm.resolveService(serviceIntent, 0) != null);
|
boolean tabs = (pm.resolveService(serviceIntent, 0) != null);
|
||||||
if (tabs)
|
if (tabs)
|
||||||
pkgs.add(new Package(label, ri.activityInfo.packageName, true));
|
pkgs.add(new Package(icon, label, ri.activityInfo.packageName, true));
|
||||||
} catch (Throwable ex) {
|
} catch (Throwable ex) {
|
||||||
Log.e(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);
|
AdapterPackage adapter = new AdapterPackage(getContext(), pkgs);
|
||||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
||||||
adapter.addAll(pkgs);
|
|
||||||
spOpenWith.setAdapter(adapter);
|
spOpenWith.setAdapter(adapter);
|
||||||
|
|
||||||
String open_with_pkg = prefs.getString("open_with_pkg", null);
|
String open_with_pkg = prefs.getString("open_with_pkg", null);
|
||||||
@@ -657,19 +677,63 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static class Package {
|
private static class Package {
|
||||||
CharSequence title;
|
private Drawable icon;
|
||||||
String name;
|
private CharSequence title;
|
||||||
boolean tabs;
|
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.title = title;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.tabs = tabs;
|
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
|
@Override
|
||||||
public String toString() {
|
public View getDropDownView(int position, View convertView, @NonNull ViewGroup parent) {
|
||||||
return this.title + (tabs ? "" : " \u29c9");
|
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
|
<Spinner
|
||||||
android:id="@+id/spOpenWith"
|
android:id="@+id/spOpenWith"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="0dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginTop="6dp"
|
android:layout_marginTop="6dp"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toBottomOf="@id/tvOpenWith" />
|
app:layout_constraintTop_toBottomOf="@id/tvOpenWith" />
|
||||||
|
|
||||||
|
|||||||
19
app/src/main/res/layout/spinner_package.xml
Normal file
19
app/src/main/res/layout/spinner_package.xml
Normal file
@@ -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>
|
||||||
Reference in New Issue
Block a user