mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-19 13:03:36 +00:00
[android] openUri, failMessage may be null.
pastk: includes changes from OM 33c4e22246 and 10be769f62 Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
committed by
Konstantin Pastbin
parent
c6040d8ce6
commit
c6cd23fb24
@@ -202,17 +202,33 @@ public class Utils
|
||||
if (TextUtils.isEmpty(url))
|
||||
return;
|
||||
|
||||
final Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
Uri uri =
|
||||
isHttpOrHttpsScheme(url) ? Uri.parse(url) : new Uri.Builder().scheme("http").appendEncodedPath(url).build();
|
||||
|
||||
Utils.openUri(context, uri, R.string.browser_not_available);
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to open a URI in another app via the system app chooser.
|
||||
* @param context the app context
|
||||
* @param uri the URI to open.
|
||||
* @param failMessage string id: message to show in a toast when the system can't find an app to open with.
|
||||
*/
|
||||
public static void openUri(@NonNull Context context, @NonNull Uri uri, @Nullable Integer failMessage)
|
||||
{
|
||||
final Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setData(uri);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.addCategory(Intent.CATEGORY_BROWSABLE);
|
||||
|
||||
try
|
||||
{
|
||||
context.startActivity(intent);
|
||||
}
|
||||
catch (ActivityNotFoundException e)
|
||||
{
|
||||
Toast.makeText(context, context.getString(R.string.browser_not_available), Toast.LENGTH_LONG).show();
|
||||
if (failMessage != null)
|
||||
Toast.makeText(context, context.getString(failMessage), Toast.LENGTH_LONG).show();
|
||||
Logger.e(TAG, "ActivityNotFoundException", e);
|
||||
}
|
||||
catch (AndroidRuntimeException e)
|
||||
@@ -223,28 +239,6 @@ public class Utils
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to open a URI in another app via the system app chooser.
|
||||
* @param context the app context
|
||||
* @param uri the URI to open.
|
||||
* @param failMessage string id: message to show in a toast when the system can't find an app to open with.
|
||||
* @param action (optional) the Intent action to use. If none is provided, defaults to Intent.ACTION_VIEW.
|
||||
*/
|
||||
public static void openUri(@NonNull Context context, @NonNull Uri uri, Integer failMessage, @NonNull String... action)
|
||||
{
|
||||
final String act = (action != null && action.length > 0 && action[0] != null) ? action[0] : Intent.ACTION_VIEW;
|
||||
final Intent intent = new Intent(act);
|
||||
intent.setData(uri);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
// https://developer.android.com/guide/components/intents-common
|
||||
// check that an app exists to open with, otherwise it'll crash
|
||||
if (intent.resolveActivity(context.getPackageManager()) != null)
|
||||
context.startActivity(intent);
|
||||
else
|
||||
Toast.makeText(context, failMessage, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private static boolean isHttpOrHttpsScheme(@NonNull String url)
|
||||
{
|
||||
return url.startsWith("http://") || url.startsWith("https://");
|
||||
|
||||
Reference in New Issue
Block a user