diff --git a/termux-shared/src/main/java/com/termux/shared/models/errors/FunctionErrno.java b/termux-shared/src/main/java/com/termux/shared/models/errors/FunctionErrno.java index bade0e1d..ff3759cc 100644 --- a/termux-shared/src/main/java/com/termux/shared/models/errors/FunctionErrno.java +++ b/termux-shared/src/main/java/com/termux/shared/models/errors/FunctionErrno.java @@ -12,6 +12,7 @@ public class FunctionErrno extends Errno { public static final Errno ERRNO_UNSET_PARAMETER = new Errno(TYPE, 102, "The %1$s parameter passed to \"%2$s\" must be set."); public static final Errno ERRNO_UNSET_PARAMETERS = new Errno(TYPE, 103, "The %1$s parameters passed to \"%2$s\" must be set."); public static final Errno ERRNO_INVALID_PARAMETER = new Errno(TYPE, 104, "The %1$s parameter passed to \"%2$s\" is invalid.\"%3$s\""); + public static final Errno ERRNO_PARAMETER_NOT_INSTANCE_OF = new Errno(TYPE, 104, "The %1$s parameter passed to \"%2$s\" is not an instance of %3$s."); FunctionErrno(final String type, final int code, final String message) { diff --git a/termux-shared/src/main/java/com/termux/shared/view/ActivityUtils.java b/termux-shared/src/main/java/com/termux/shared/view/ActivityUtils.java new file mode 100644 index 00000000..963d5970 --- /dev/null +++ b/termux-shared/src/main/java/com/termux/shared/view/ActivityUtils.java @@ -0,0 +1,86 @@ +package com.termux.shared.view; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; + +import androidx.activity.result.ActivityResultLauncher; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + +import com.termux.shared.R; +import com.termux.shared.logger.Logger; +import com.termux.shared.models.errors.Error; +import com.termux.shared.models.errors.FunctionErrno; + + +public class ActivityUtils { + + private static final String LOG_TAG = "ActivityUtils"; + + /** + * Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}. + */ + public static boolean startActivityForResult(Context context, int requestCode, @NonNull Intent intent) { + return startActivityForResult(context, requestCode, intent, true, true, null); + } + + /** + * Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}. + */ + public static boolean startActivityForResult(Context context, int requestCode, @NonNull Intent intent, boolean logErrorMessage, boolean showErrorMessage) { + return startActivityForResult(context, requestCode, intent, logErrorMessage, showErrorMessage, null); + } + + /** + * Start an {@link Activity} for result. + * + * @param context The context for operations. It must be an instance of {@link Activity} or + * {@link AppCompatActivity}. It is ignored if {@code activityResultLauncher} + * is not {@code null}. + * @param requestCode The request code to use while sending intent. This must be >= 0, otherwise + * exception will be raised. This is ignored if {@code activityResultLauncher} + * is {@code null}. + * @param intent The {@link Intent} to send to start the activity. + * @param logErrorMessage If an error message should be logged if failed to start activity. + * @param showErrorMessage If an error message toast should be shown if failed to start activity + * in addition to logging a message. + * @param activityResultLauncher The {@link ActivityResultLauncher} to use for start the + * activity. If this is {@code null}, then + * {@link Activity#startActivity(Intent)} will be used instead. + * Note that later is deprecated. + * @return Returns {@code true} if starting activity was successful, otherwise {@code false}. + */ + public static boolean startActivityForResult(@NonNull Context context, int requestCode, @NonNull Intent intent, + boolean logErrorMessage, boolean showErrorMessage, @Nullable ActivityResultLauncher activityResultLauncher) { + try { + if (activityResultLauncher != null) { + activityResultLauncher.launch(intent); + } else { + if (context instanceof AppCompatActivity) + ((AppCompatActivity) context).startActivityForResult(intent, requestCode); + else if (context instanceof Activity) + ((Activity) context).startActivityForResult(intent, requestCode); + else { + if (logErrorMessage) + Error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG, + FunctionErrno.ERRNO_PARAMETER_NOT_INSTANCE_OF.getError("context", "startActivityForResult", "Activity or AppCompatActivity")); + return false; + } + } + } catch (Exception e) { + if (logErrorMessage) { + String activityName = intent.getComponent() != null ? intent.getComponent().getShortClassName() : "Unknown"; + String errmsg = context.getString(R.string.error_failed_to_start_activity_for_result, activityName); + Logger.logStackTraceWithMessage(LOG_TAG, errmsg, e); + if (showErrorMessage) + Logger.showToast(context, errmsg + ": " + e.getMessage(), true); + } + return false; + } + + return true; + } + +} diff --git a/termux-shared/src/main/res/values/strings.xml b/termux-shared/src/main/res/values/strings.xml index 06433ecb..074ce959 100644 --- a/termux-shared/src/main/res/values/strings.xml +++ b/termux-shared/src/main/res/values/strings.xml @@ -18,6 +18,12 @@ + + Failed to start %1$s activity + Failed to start %1$s activity for result + + + The %1$s (%2$s) app is not installed or is disabled." Failed To Get Package Context