mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-09 20:14:04 +08:00
Changed: Return Error instead of boolean for ActivityUtils.startActivityForResult()
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package com.termux.shared.activity;
|
||||
|
||||
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.errors.Error;
|
||||
import com.termux.shared.errors.FunctionErrno;
|
||||
|
||||
|
||||
public class ActivityUtils {
|
||||
|
||||
private static final String LOG_TAG = "ActivityUtils";
|
||||
|
||||
/**
|
||||
* Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}.
|
||||
*/
|
||||
public static Error 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 Error 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<Intent>} 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 the {@code error} if starting activity was not successful, otherwise {@code null}.
|
||||
|
||||
*/
|
||||
public static Error startActivityForResult(@NonNull Context context, int requestCode, @NonNull Intent intent,
|
||||
boolean logErrorMessage, boolean showErrorMessage,
|
||||
@Nullable ActivityResultLauncher<Intent> activityResultLauncher) {
|
||||
Error error;
|
||||
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 {
|
||||
error = FunctionErrno.ERRNO_PARAMETER_NOT_INSTANCE_OF.getError("context", "startActivityForResult", "Activity or AppCompatActivity");
|
||||
if (logErrorMessage)
|
||||
error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
return error;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,19 @@
|
||||
package com.termux.shared.activity;
|
||||
|
||||
import com.termux.shared.errors.Errno;
|
||||
|
||||
public class ActivityUtilsErrno extends Errno {
|
||||
|
||||
public static final String TYPE = "ActivityUtils Error";
|
||||
|
||||
|
||||
/* 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");
|
||||
|
||||
|
||||
ActivityUtilsErrno(final String type, final int code, final String message) {
|
||||
super(type, code, message);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user