Added: Add ActivityUtils.startActivity() and catch uncaught exceptions in TermuxActivity

This commit is contained in:
agnostic-apollo
2021-10-29 00:58:48 +05:00
parent b69d14119e
commit f857bf2968
3 changed files with 69 additions and 11 deletions

View File

@@ -33,6 +33,7 @@ import android.widget.Toast;
import com.termux.R; import com.termux.R;
import com.termux.app.terminal.TermuxActivityRootView; import com.termux.app.terminal.TermuxActivityRootView;
import com.termux.shared.activities.ReportActivity; import com.termux.shared.activities.ReportActivity;
import com.termux.shared.activity.ActivityUtils;
import com.termux.shared.data.IntentUtils; import com.termux.shared.data.IntentUtils;
import com.termux.shared.android.PermissionUtils; import com.termux.shared.android.PermissionUtils;
import com.termux.shared.termux.TermuxConstants; import com.termux.shared.termux.TermuxConstants;
@@ -524,7 +525,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
private void setSettingsButtonView() { private void setSettingsButtonView() {
ImageButton settingsButton = findViewById(R.id.settings_button); ImageButton settingsButton = findViewById(R.id.settings_button);
settingsButton.setOnClickListener(v -> { settingsButton.setOnClickListener(v -> {
startActivity(new Intent(this, SettingsActivity.class)); ActivityUtils.startActivity(this, new Intent(this, SettingsActivity.class));
}); });
} }
@@ -643,10 +644,10 @@ public final class TermuxActivity extends Activity implements ServiceConnection
toggleKeepScreenOn(); toggleKeepScreenOn();
return true; return true;
case CONTEXT_MENU_HELP_ID: case CONTEXT_MENU_HELP_ID:
startActivity(new Intent(this, HelpActivity.class)); ActivityUtils.startActivity(this, new Intent(this, HelpActivity.class));
return true; return true;
case CONTEXT_MENU_SETTINGS_ID: case CONTEXT_MENU_SETTINGS_ID:
startActivity(new Intent(this, SettingsActivity.class)); ActivityUtils.startActivity(this, new Intent(this, SettingsActivity.class));
return true; return true;
case CONTEXT_MENU_REPORT_ID: case CONTEXT_MENU_REPORT_ID:
mTermuxTerminalViewClient.reportIssueFromTranscript(); mTermuxTerminalViewClient.reportIssueFromTranscript();
@@ -689,7 +690,9 @@ public final class TermuxActivity extends Activity implements ServiceConnection
// The startActivity() call is not documented to throw IllegalArgumentException. // The startActivity() call is not documented to throw IllegalArgumentException.
// However, crash reporting shows that it sometimes does, so catch it here. // However, crash reporting shows that it sometimes does, so catch it here.
new AlertDialog.Builder(this).setMessage(getString(R.string.error_styling_not_installed)) new AlertDialog.Builder(this).setMessage(getString(R.string.error_styling_not_installed))
.setPositiveButton(R.string.action_styling_install, (dialog, which) -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(TermuxConstants.TERMUX_STYLING_FDROID_PACKAGE_URL)))).setNegativeButton(android.R.string.cancel, null).show(); .setPositiveButton(R.string.action_styling_install,
(dialog, which) -> ActivityUtils.startActivity(this, new Intent(Intent.ACTION_VIEW, Uri.parse(TermuxConstants.TERMUX_STYLING_FDROID_PACKAGE_URL))))
.setNegativeButton(android.R.string.cancel, null).show();
} }
} }
private void toggleKeepScreenOn() { private void toggleKeepScreenOn() {
@@ -719,7 +722,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
* if targeting targetSdkVersion 30 (android 11) and running on sdk 30 (android 11) and higher. * if targeting targetSdkVersion 30 (android 11) and running on sdk 30 (android 11) and higher.
*/ */
public void requestStoragePermission(boolean isPermissionCallback) { public void requestStoragePermission(boolean isPermissionCallback) {
new Thread() { new Thread() {
@Override @Override
public void run() { public void run() {
// Do not ask for permission again // Do not ask for permission again
@@ -934,7 +937,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
public static void startTermuxActivity(@NonNull final Context context) { public static void startTermuxActivity(@NonNull final Context context) {
context.startActivity(newInstance(context)); ActivityUtils.startActivity(context, newInstance(context));
} }
public static Intent newInstance(@NonNull final Context context) { public static Intent newInstance(@NonNull final Context context) {

View File

@@ -17,6 +17,51 @@ public class ActivityUtils {
private static final String LOG_TAG = "ActivityUtils"; private static final String LOG_TAG = "ActivityUtils";
/**
* Wrapper for {@link #startActivity(Context, Intent, boolean, boolean)}.
*/
public static Error startActivity(@NonNull Context context, @NonNull Intent intent) {
return startActivity(context, intent, true, true);
}
/**
* Start an {@link Activity}.
*
* @param context The context for operations.
* @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. The {@code context} must not be
* {@code null}.
* @return Returns the {@code error} if starting activity was not successful, otherwise {@code null}.
*/
public static Error startActivity(Context context, @NonNull Intent intent,
boolean logErrorMessage, boolean showErrorMessage) {
Error error;
String activityName = intent.getComponent() != null ? intent.getComponent().getClassName() : "Unknown";
if (context == null) {
error = ActivityUtilsErrno.ERRNO_STARTING_ACTIVITY_WITH_NULL_CONTEXT.getError(activityName);
if (logErrorMessage)
error.logErrorAndShowToast(null, LOG_TAG);
return error;
}
try {
context.startActivity(intent);
} catch (Exception e) {
error = ActivityUtilsErrno.ERRNO_START_ACTIVITY_FAILED_WITH_EXCEPTION.getError(e, activityName, e.getMessage());
if (logErrorMessage)
error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
return error;
}
return null;
}
/** /**
* Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}. * Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}.
*/ */
@@ -44,22 +89,32 @@ public class ActivityUtils {
* @param intent The {@link Intent} to send to start the activity. * @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 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 * @param showErrorMessage If an error message toast should be shown if failed to start activity
* in addition to logging a message. * in addition to logging a message. The {@code context} must not be
* {@code null}.
* @param activityResultLauncher The {@link ActivityResultLauncher<Intent>} to use for start the * @param activityResultLauncher The {@link ActivityResultLauncher<Intent>} to use for start the
* activity. If this is {@code null}, then * activity. If this is {@code null}, then
* {@link Activity#startActivity(Intent)} will be used instead. * {@link Activity#startActivityForResult(Intent, int)} will be
* used instead.
* Note that later is deprecated. * Note that later is deprecated.
* @return Returns the {@code error} if starting activity was not successful, otherwise {@code null}. * @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, public static Error startActivityForResult(Context context, int requestCode, @NonNull Intent intent,
boolean logErrorMessage, boolean showErrorMessage, boolean logErrorMessage, boolean showErrorMessage,
@Nullable ActivityResultLauncher<Intent> activityResultLauncher) { @Nullable ActivityResultLauncher<Intent> activityResultLauncher) {
Error error; Error error;
String activityName = intent.getComponent() != null ? intent.getComponent().getClassName() : "Unknown";
try { try {
if (activityResultLauncher != null) { if (activityResultLauncher != null) {
activityResultLauncher.launch(intent); activityResultLauncher.launch(intent);
} else { } else {
if (context == null) {
error = ActivityUtilsErrno.ERRNO_STARTING_ACTIVITY_WITH_NULL_CONTEXT.getError(activityName);
if (logErrorMessage)
error.logErrorAndShowToast(null, LOG_TAG);
return error;
}
if (context instanceof AppCompatActivity) if (context instanceof AppCompatActivity)
((AppCompatActivity) context).startActivityForResult(intent, requestCode); ((AppCompatActivity) context).startActivityForResult(intent, requestCode);
else if (context instanceof Activity) else if (context instanceof Activity)
@@ -72,7 +127,6 @@ public class ActivityUtils {
} }
} }
} catch (Exception e) { } 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()); error = ActivityUtilsErrno.ERRNO_START_ACTIVITY_FOR_RESULT_FAILED_WITH_EXCEPTION.getError(e, activityName, e.getMessage());
if (logErrorMessage) if (logErrorMessage)
error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG); error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);

View File

@@ -9,7 +9,8 @@ public class ActivityUtilsErrno extends Errno {
/* Errors for starting activities (100-150) */ /* 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_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"); public static final Errno ERRNO_START_ACTIVITY_FOR_RESULT_FAILED_WITH_EXCEPTION = new Errno(TYPE, 101, "Failed to start \"%1$s\" activity for result.\nException: %2$s");
public static final Errno ERRNO_STARTING_ACTIVITY_WITH_NULL_CONTEXT = new Errno(TYPE, 102, "Cannot start \"%1$s\" activity with null Context");
ActivityUtilsErrno(final String type, final int code, final String message) { ActivityUtilsErrno(final String type, final int code, final String message) {