mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Added: Add ActivityUtils with functions to start activities for result
This commit is contained in:
@@ -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) {
|
||||
|
@@ -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<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 {@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<Intent> 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;
|
||||
}
|
||||
|
||||
}
|
@@ -18,6 +18,12 @@
|
||||
|
||||
|
||||
|
||||
<!-- ActivityUtils -->
|
||||
<string name="error_failed_to_start_activity">Failed to start %1$s activity</string>
|
||||
<string name="error_failed_to_start_activity_for_result">Failed to start %1$s activity for result</string>
|
||||
|
||||
|
||||
|
||||
<!-- PackageUtils -->
|
||||
<string name="error_app_not_installed_or_disabled_warning">The %1$s (%2$s) app is not installed or is disabled."</string>
|
||||
<string name="error_get_package_context_failed_title">Failed To Get Package Context</string>
|
||||
|
Reference in New Issue
Block a user