mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 10:45:23 +08:00
Added: Add ActivityUtils.startActivity() and catch uncaught exceptions in TermuxActivity
This commit is contained in:
@@ -17,6 +17,51 @@ public class ActivityUtils {
|
||||
|
||||
private static final String LOG_TAG = "ActivityUtils";
|
||||
|
||||
/**
|
||||
* Wrapper for {@link #startActivity(Context, Intent, boolean, boolean)}.
|
||||
*/
|
||||
public static Error startActivity(@NonNull Context context, @NonNull Intent intent) {
|
||||
return startActivity(context, intent, true, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an {@link Activity}.
|
||||
*
|
||||
* @param context The context for operations.
|
||||
* @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. The {@code context} must not be
|
||||
* {@code null}.
|
||||
* @return Returns the {@code error} if starting activity was not successful, otherwise {@code null}.
|
||||
|
||||
*/
|
||||
public static Error startActivity(Context context, @NonNull Intent intent,
|
||||
boolean logErrorMessage, boolean showErrorMessage) {
|
||||
Error error;
|
||||
String activityName = intent.getComponent() != null ? intent.getComponent().getClassName() : "Unknown";
|
||||
|
||||
if (context == null) {
|
||||
error = ActivityUtilsErrno.ERRNO_STARTING_ACTIVITY_WITH_NULL_CONTEXT.getError(activityName);
|
||||
if (logErrorMessage)
|
||||
error.logErrorAndShowToast(null, LOG_TAG);
|
||||
return error;
|
||||
}
|
||||
|
||||
try {
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
error = ActivityUtilsErrno.ERRNO_START_ACTIVITY_FAILED_WITH_EXCEPTION.getError(e, activityName, e.getMessage());
|
||||
if (logErrorMessage)
|
||||
error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
|
||||
return error;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}.
|
||||
*/
|
||||
@@ -44,22 +89,32 @@ public class ActivityUtils {
|
||||
* @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.
|
||||
* in addition to logging a message. The {@code context} must not be
|
||||
* {@code null}.
|
||||
* @param activityResultLauncher The {@link ActivityResultLauncher<Intent>} to use for start the
|
||||
* activity. If this is {@code null}, then
|
||||
* {@link Activity#startActivity(Intent)} will be used instead.
|
||||
* {@link Activity#startActivityForResult(Intent, int)} will be
|
||||
* used instead.
|
||||
* Note that later is deprecated.
|
||||
* @return Returns the {@code error} if starting activity was not successful, otherwise {@code null}.
|
||||
|
||||
*/
|
||||
public static Error startActivityForResult(@NonNull Context context, int requestCode, @NonNull Intent intent,
|
||||
public static Error startActivityForResult(Context context, int requestCode, @NonNull Intent intent,
|
||||
boolean logErrorMessage, boolean showErrorMessage,
|
||||
@Nullable ActivityResultLauncher<Intent> activityResultLauncher) {
|
||||
Error error;
|
||||
String activityName = intent.getComponent() != null ? intent.getComponent().getClassName() : "Unknown";
|
||||
try {
|
||||
if (activityResultLauncher != null) {
|
||||
activityResultLauncher.launch(intent);
|
||||
} else {
|
||||
if (context == null) {
|
||||
error = ActivityUtilsErrno.ERRNO_STARTING_ACTIVITY_WITH_NULL_CONTEXT.getError(activityName);
|
||||
if (logErrorMessage)
|
||||
error.logErrorAndShowToast(null, LOG_TAG);
|
||||
return error;
|
||||
}
|
||||
|
||||
if (context instanceof AppCompatActivity)
|
||||
((AppCompatActivity) context).startActivityForResult(intent, requestCode);
|
||||
else if (context instanceof Activity)
|
||||
@@ -72,7 +127,6 @@ public class ActivityUtils {
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String activityName = intent.getComponent() != null ? intent.getComponent().getClassName() : "Unknown";
|
||||
error = ActivityUtilsErrno.ERRNO_START_ACTIVITY_FOR_RESULT_FAILED_WITH_EXCEPTION.getError(e, activityName, e.getMessage());
|
||||
if (logErrorMessage)
|
||||
error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
|
||||
|
@@ -9,7 +9,8 @@ public class ActivityUtilsErrno extends Errno {
|
||||
|
||||
/* Errors for starting activities (100-150) */
|
||||
public static final Errno ERRNO_START_ACTIVITY_FAILED_WITH_EXCEPTION = new Errno(TYPE, 100, "Failed to start \"%1$s\" activity.\nException: %2$s");
|
||||
public static final Errno ERRNO_START_ACTIVITY_FOR_RESULT_FAILED_WITH_EXCEPTION = new Errno(TYPE, 100, "Failed to start \"%1$s\" activity for result.\nException: %2$s");
|
||||
public static final Errno ERRNO_START_ACTIVITY_FOR_RESULT_FAILED_WITH_EXCEPTION = new Errno(TYPE, 101, "Failed to start \"%1$s\" activity for result.\nException: %2$s");
|
||||
public static final Errno ERRNO_STARTING_ACTIVITY_WITH_NULL_CONTEXT = new Errno(TYPE, 102, "Cannot start \"%1$s\" activity with null Context");
|
||||
|
||||
|
||||
ActivityUtilsErrno(final String type, final int code, final String message) {
|
||||
|
Reference in New Issue
Block a user