Changed: Return Error instead of boolean for ActivityUtils.startActivityForResult()

This commit is contained in:
agnostic-apollo
2021-10-29 00:36:25 +05:00
parent 8c43b7f0a1
commit b69d14119e
4 changed files with 48 additions and 36 deletions

View File

@@ -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<Intent> activityResultLauncher) {
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);
@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -18,12 +18,6 @@
<!-- 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>