mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 10:45:23 +08:00
Changed: Return Error instead of boolean for ActivityUtils.startActivityForResult()
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package com.termux.shared.view;
|
package com.termux.shared.activity;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -9,8 +9,6 @@ import androidx.annotation.NonNull;
|
|||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
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.Error;
|
||||||
import com.termux.shared.errors.FunctionErrno;
|
import com.termux.shared.errors.FunctionErrno;
|
||||||
|
|
||||||
@@ -22,14 +20,15 @@ public class ActivityUtils {
|
|||||||
/**
|
/**
|
||||||
* Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}.
|
* 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);
|
return startActivityForResult(context, requestCode, intent, true, true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}.
|
* 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);
|
return startActivityForResult(context, requestCode, intent, logErrorMessage, showErrorMessage, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,10 +49,13 @@ public class ActivityUtils {
|
|||||||
* activity. If this is {@code null}, then
|
* activity. If this is {@code null}, then
|
||||||
* {@link Activity#startActivity(Intent)} will be used instead.
|
* {@link Activity#startActivity(Intent)} will be used instead.
|
||||||
* Note that later is deprecated.
|
* 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,
|
public static Error startActivityForResult(@NonNull Context context, int requestCode, @NonNull Intent intent,
|
||||||
boolean logErrorMessage, boolean showErrorMessage, @Nullable ActivityResultLauncher<Intent> activityResultLauncher) {
|
boolean logErrorMessage, boolean showErrorMessage,
|
||||||
|
@Nullable ActivityResultLauncher<Intent> activityResultLauncher) {
|
||||||
|
Error error;
|
||||||
try {
|
try {
|
||||||
if (activityResultLauncher != null) {
|
if (activityResultLauncher != null) {
|
||||||
activityResultLauncher.launch(intent);
|
activityResultLauncher.launch(intent);
|
||||||
@@ -63,24 +65,21 @@ public class ActivityUtils {
|
|||||||
else if (context instanceof Activity)
|
else if (context instanceof Activity)
|
||||||
((Activity) context).startActivityForResult(intent, requestCode);
|
((Activity) context).startActivityForResult(intent, requestCode);
|
||||||
else {
|
else {
|
||||||
|
error = FunctionErrno.ERRNO_PARAMETER_NOT_INSTANCE_OF.getError("context", "startActivityForResult", "Activity or AppCompatActivity");
|
||||||
if (logErrorMessage)
|
if (logErrorMessage)
|
||||||
Error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG,
|
error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
|
||||||
FunctionErrno.ERRNO_PARAMETER_NOT_INSTANCE_OF.getError("context", "startActivityForResult", "Activity or AppCompatActivity"));
|
return error;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
if (logErrorMessage) {
|
String activityName = intent.getComponent() != null ? intent.getComponent().getClassName() : "Unknown";
|
||||||
String activityName = intent.getComponent() != null ? intent.getComponent().getShortClassName() : "Unknown";
|
error = ActivityUtilsErrno.ERRNO_START_ACTIVITY_FOR_RESULT_FAILED_WITH_EXCEPTION.getError(e, activityName, e.getMessage());
|
||||||
String errmsg = context.getString(R.string.error_failed_to_start_activity_for_result, activityName);
|
if (logErrorMessage)
|
||||||
Logger.logStackTraceWithMessage(LOG_TAG, errmsg, e);
|
error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
|
||||||
if (showErrorMessage)
|
return error;
|
||||||
Logger.showToast(context, errmsg + ": " + e.getMessage(), true);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -23,7 +23,7 @@ import com.termux.shared.file.FileUtils;
|
|||||||
import com.termux.shared.logger.Logger;
|
import com.termux.shared.logger.Logger;
|
||||||
import com.termux.shared.errors.Error;
|
import com.termux.shared.errors.Error;
|
||||||
import com.termux.shared.errors.FunctionErrno;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -331,24 +331,24 @@ public class PermissionUtils {
|
|||||||
* {@link AppCompatActivity}.
|
* {@link AppCompatActivity}.
|
||||||
* @param requestCode The request code to use while asking for permission. It must be `>=0` or
|
* @param requestCode The request code to use while asking for permission. It must be `>=0` or
|
||||||
* will fail silently and will log an exception.
|
* 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");
|
Logger.logInfo(LOG_TAG, "Requesting manage external storage permission");
|
||||||
|
|
||||||
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
|
Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
|
||||||
intent.addCategory("android.intent.category.DEFAULT");
|
intent.addCategory("android.intent.category.DEFAULT");
|
||||||
intent.setData(Uri.parse("package:" + context.getPackageName()));
|
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.
|
// 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 = new Intent();
|
||||||
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
||||||
return ActivityUtils.startActivityForResult(context, requestCode, intent);
|
return ActivityUtils.startActivityForResult(context, requestCode, intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -428,9 +428,9 @@ public class PermissionUtils {
|
|||||||
* {@link AppCompatActivity}.
|
* {@link AppCompatActivity}.
|
||||||
* @param requestCode The request code to use while asking for permission. It must be `>=0` or
|
* @param requestCode The request code to use while asking for permission. It must be `>=0` or
|
||||||
* will fail silently and will log an exception.
|
* 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 intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
|
||||||
intent.setData(Uri.parse("package:" + context.getPackageName()));
|
intent.setData(Uri.parse("package:" + context.getPackageName()));
|
||||||
return ActivityUtils.startActivityForResult(context, requestCode, intent);
|
return ActivityUtils.startActivityForResult(context, requestCode, intent);
|
||||||
@@ -483,10 +483,10 @@ public class PermissionUtils {
|
|||||||
* {@link AppCompatActivity}.
|
* {@link AppCompatActivity}.
|
||||||
* @param requestCode The request code to use while asking for permission. It must be `>=0` or
|
* @param requestCode The request code to use while asking for permission. It must be `>=0` or
|
||||||
* will fail silently and will log an exception.
|
* 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")
|
@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 intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
|
||||||
intent.setData(Uri.parse("package:" + context.getPackageName()));
|
intent.setData(Uri.parse("package:" + context.getPackageName()));
|
||||||
return ActivityUtils.startActivityForResult(context, requestCode, intent);
|
return ActivityUtils.startActivityForResult(context, requestCode, intent);
|
||||||
|
@@ -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 -->
|
<!-- 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_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>
|
<string name="error_get_package_context_failed_title">Failed To Get Package Context</string>
|
||||||
|
Reference in New Issue
Block a user