Allow browsing links

This commit is contained in:
M66B 2018-12-23 13:34:42 +00:00
parent cdb1695faa
commit 15f89a8dcf
4 changed files with 52 additions and 27 deletions

View File

@ -64,7 +64,6 @@ import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.snackbar.Snackbar;
@ -906,11 +905,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
String url = link[0].getURL();
Uri uri = Uri.parse(url);
if (!"http".equals(uri.getScheme()) && !"https".equals(uri.getScheme())) {
Toast.makeText(context, context.getString(R.string.title_no_viewer, uri.toString()), Toast.LENGTH_LONG).show();
return true;
}
if (BuildConfig.APPLICATION_ID.equals(uri.getHost()) && "/activate/".equals(uri.getPath())) {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
@ -927,13 +921,14 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
@Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse(etLink.getText().toString());
if (!"http".equals(uri.getScheme()) && !"https".equals(uri.getScheme())) {
Toast.makeText(context, context.getString(R.string.title_no_viewer, uri.toString()), Toast.LENGTH_LONG).show();
return;
}
Helper.view(context, owner, uri);
Helper.view(context, owner, uri, false);
}
})
.setNeutralButton(R.string.title_browse, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse(etLink.getText().toString());
Helper.view(context, owner, uri, true);
}
})
.setNegativeButton(R.string.title_no, null)

View File

@ -87,7 +87,7 @@ public class FragmentWebView extends FragmentEx {
setSubtitle(url);
return false;
} else {
Helper.view(getContext(), getViewLifecycleOwner(), Uri.parse(url));
Helper.view(getContext(), getViewLifecycleOwner(), Uri.parse(url), true);
return true;
}
}

View File

@ -28,6 +28,7 @@ import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.TypedArray;
import android.net.ConnectivityManager;
import android.net.Network;
@ -79,6 +80,7 @@ import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;
public class Helper {
static final String TAG = "fairemail";
@ -100,28 +102,55 @@ public class Helper {
static void view(Context context, LifecycleOwner owner, Intent intent) {
Uri uri = intent.getData();
if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme()))
view(context, owner, intent.getData());
view(context, owner, intent.getData(), false);
else
context.startActivity(intent);
}
static void view(Context context, LifecycleOwner owner, Uri uri) {
Log.i(Helper.TAG, "Custom tab=" + uri);
static void view(Context context, LifecycleOwner owner, Uri uri, boolean browse) {
Log.i(Helper.TAG, "View=" + uri);
// https://developer.chrome.com/multidevice/android/customtabs
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(Helper.resolveColor(context, R.attr.colorPrimary));
if (!hasCustomTabs(context, uri))
browse = true;
CustomTabsIntent customTabsIntent = builder.build();
try {
customTabsIntent.launchUrl(context, uri);
} catch (ActivityNotFoundException ex) {
Toast.makeText(context, context.getString(R.string.title_no_viewer, uri.toString()), Toast.LENGTH_LONG).show();
} catch (Throwable ex) {
Helper.unexpectedError(context, owner, ex);
if (browse) {
Intent view = new Intent(Intent.ACTION_VIEW, uri);
PackageManager pm = context.getPackageManager();
if (view.resolveActivity(pm) == null)
Toast.makeText(context, context.getString(R.string.title_no_viewer, uri.toString()), Toast.LENGTH_LONG).show();
else
context.startActivity(view);
} else {
// https://developer.chrome.com/multidevice/android/customtabs
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
builder.setToolbarColor(Helper.resolveColor(context, R.attr.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
try {
customTabsIntent.launchUrl(context, uri);
} catch (ActivityNotFoundException ex) {
Toast.makeText(context, context.getString(R.string.title_no_viewer, uri.toString()), Toast.LENGTH_LONG).show();
} catch (Throwable ex) {
Helper.unexpectedError(context, owner, ex);
}
}
}
static boolean hasCustomTabs(Context context, Uri uri) {
PackageManager pm = context.getPackageManager();
Intent view = new Intent(Intent.ACTION_VIEW, uri);
for (ResolveInfo info : pm.queryIntentActivities(view, 0)) {
Intent intent = new Intent();
intent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
intent.setPackage(info.activityInfo.packageName);
if (pm.resolveService(intent, 0) != null)
return true;
}
return false;
}
static Intent getChooser(Context context, Intent intent) {
PackageManager pm = context.getPackageManager();
if (pm.queryIntentActivities(intent, 0).size() == 1)

View File

@ -390,6 +390,7 @@
<string name="title_no">No</string>
<string name="title_undo">Undo</string>
<string name="title_add">Add</string>
<string name="title_browse">Browse</string>
<string name="title_report">Report</string>
<string name="title_try">Try FairEmail, an open source, privacy friendly email app for Android</string>