diff --git a/termux-shared/src/main/java/com/termux/shared/view/ActivityUtils.java b/termux-shared/src/main/java/com/termux/shared/activity/ActivityUtils.java similarity index 65% rename from termux-shared/src/main/java/com/termux/shared/view/ActivityUtils.java rename to termux-shared/src/main/java/com/termux/shared/activity/ActivityUtils.java index 36df09a4..d6b0a8de 100644 --- a/termux-shared/src/main/java/com/termux/shared/view/ActivityUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/activity/ActivityUtils.java @@ -1,4 +1,4 @@ -package com.termux.shared.view; +package com.termux.shared.activity; import android.app.Activity; import android.content.Context; @@ -9,8 +9,6 @@ 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.errors.Error; import com.termux.shared.errors.FunctionErrno; @@ -22,14 +20,15 @@ public class ActivityUtils { /** * Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}. */ - public static boolean startActivityForResult(Context context, int requestCode, @NonNull Intent intent) { + 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 boolean startActivityForResult(Context context, int requestCode, @NonNull Intent intent, boolean logErrorMessage, boolean showErrorMessage) { + public static Error startActivityForResult(Context context, int requestCode, @NonNull Intent intent, + boolean logErrorMessage, boolean showErrorMessage) { return startActivityForResult(context, requestCode, intent, logErrorMessage, showErrorMessage, null); } @@ -50,10 +49,13 @@ public class ActivityUtils { * 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}. + * @return Returns the {@code error} if starting activity was not successful, otherwise {@code null}. + */ - public static boolean startActivityForResult(@NonNull Context context, int requestCode, @NonNull Intent intent, - boolean logErrorMessage, boolean showErrorMessage, @Nullable ActivityResultLauncher activityResultLauncher) { + public static Error startActivityForResult(@NonNull Context context, int requestCode, @NonNull Intent intent, + boolean logErrorMessage, boolean showErrorMessage, + @Nullable ActivityResultLauncher activityResultLauncher) { + Error error; try { if (activityResultLauncher != null) { activityResultLauncher.launch(intent); @@ -63,24 +65,21 @@ public class ActivityUtils { 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, - FunctionErrno.ERRNO_PARAMETER_NOT_INSTANCE_OF.getError("context", "startActivityForResult", "Activity or AppCompatActivity")); - return false; + error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG); + return error; } } } 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; + 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 true; + return null; } } diff --git a/termux-shared/src/main/java/com/termux/shared/activity/ActivityUtilsErrno.java b/termux-shared/src/main/java/com/termux/shared/activity/ActivityUtilsErrno.java new file mode 100644 index 00000000..ef44a314 --- /dev/null +++ b/termux-shared/src/main/java/com/termux/shared/activity/ActivityUtilsErrno.java @@ -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); + } + +} diff --git a/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java b/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java index 355305fc..8e075099 100644 --- a/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java @@ -23,7 +23,7 @@ import com.termux.shared.file.FileUtils; import com.termux.shared.logger.Logger; import com.termux.shared.errors.Error; import com.termux.shared.errors.FunctionErrno; -import com.termux.shared.view.ActivityUtils; +import com.termux.shared.activity.ActivityUtils; import java.util.ArrayList; import java.util.Arrays; @@ -331,24 +331,24 @@ public class PermissionUtils { * {@link AppCompatActivity}. * @param requestCode The request code to use while asking for permission. It must be `>=0` or * will fail silently and will log an exception. - * @return Returns {@code true} if requesting the permission was successful, otherwise {@code false}. + * @return Returns the {@code error} if requesting the permission was not successful, otherwise {@code null}. */ - public static boolean requestManageStorageExternalPermission(@NonNull Context context, int requestCode) { + public static Error requestManageStorageExternalPermission(@NonNull Context context, int requestCode) { Logger.logInfo(LOG_TAG, "Requesting manage external storage permission"); Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); intent.addCategory("android.intent.category.DEFAULT"); intent.setData(Uri.parse("package:" + context.getPackageName())); - boolean result = ActivityUtils.startActivityForResult(context, requestCode, intent); + Error error = ActivityUtils.startActivityForResult(context, requestCode, intent, true, false); // Use fallback if matching Activity did not exist for ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION. - if (!result) { + if (error != null) { intent = new Intent(); intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); return ActivityUtils.startActivityForResult(context, requestCode, intent); } - return true; + return null; } /** @@ -428,9 +428,9 @@ public class PermissionUtils { * {@link AppCompatActivity}. * @param requestCode The request code to use while asking for permission. It must be `>=0` or * will fail silently and will log an exception. - * @return Returns {@code true} if requesting the permission was successful, otherwise {@code false}. + * @return Returns the {@code error} if requesting the permission was not successful, otherwise {@code null}. */ - public static boolean requestDisplayOverOtherAppsPermission(@NonNull Context context, int requestCode) { + public static Error requestDisplayOverOtherAppsPermission(@NonNull Context context, int requestCode) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION); intent.setData(Uri.parse("package:" + context.getPackageName())); return ActivityUtils.startActivityForResult(context, requestCode, intent); @@ -483,10 +483,10 @@ public class PermissionUtils { * {@link AppCompatActivity}. * @param requestCode The request code to use while asking for permission. It must be `>=0` or * will fail silently and will log an exception. - * @return Returns {@code true} if requesting the permission was successful, otherwise {@code false}. + * @return Returns the {@code error} if requesting the permission was not successful, otherwise {@code null}. */ @SuppressLint("BatteryLife") - public static boolean requestDisableBatteryOptimizations(@NonNull Context context, int requestCode) { + public static Error requestDisableBatteryOptimizations(@NonNull Context context, int requestCode) { Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + context.getPackageName())); return ActivityUtils.startActivityForResult(context, requestCode, intent); diff --git a/termux-shared/src/main/res/values/strings.xml b/termux-shared/src/main/res/values/strings.xml index f0bc66d9..219b9177 100644 --- a/termux-shared/src/main/res/values/strings.xml +++ b/termux-shared/src/main/res/values/strings.xml @@ -18,12 +18,6 @@ - - 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