termux-app
137 строк · 6.3 Кб
1package com.termux.shared.activity;
2
3import android.app.Activity;
4import android.content.Context;
5import android.content.Intent;
6
7import androidx.activity.result.ActivityResultLauncher;
8import androidx.annotation.NonNull;
9import androidx.annotation.Nullable;
10import androidx.appcompat.app.AppCompatActivity;
11
12import com.termux.shared.errors.Error;
13import com.termux.shared.errors.FunctionErrno;
14
15
16public class ActivityUtils {
17
18private static final String LOG_TAG = "ActivityUtils";
19
20/**
21* Wrapper for {@link #startActivity(Context, Intent, boolean, boolean)}.
22*/
23public static Error startActivity(@NonNull Context context, @NonNull Intent intent) {
24return startActivity(context, intent, true, true);
25}
26
27/**
28* Start an {@link Activity}.
29*
30* @param context The context for operations.
31* @param intent The {@link Intent} to send to start the activity.
32* @param logErrorMessage If an error message should be logged if failed to start activity.
33* @param showErrorMessage If an error message toast should be shown if failed to start activity
34* in addition to logging a message. The {@code context} must not be
35* {@code null}.
36* @return Returns the {@code error} if starting activity was not successful, otherwise {@code null}.
37*/
38public static Error startActivity(Context context, @NonNull Intent intent,
39boolean logErrorMessage, boolean showErrorMessage) {
40Error error;
41String activityName = intent.getComponent() != null ? intent.getComponent().getClassName() : "Unknown";
42
43if (context == null) {
44error = ActivityErrno.ERRNO_STARTING_ACTIVITY_WITH_NULL_CONTEXT.getError(activityName);
45if (logErrorMessage)
46error.logErrorAndShowToast(null, LOG_TAG);
47return error;
48}
49
50try {
51context.startActivity(intent);
52} catch (Exception e) {
53error = ActivityErrno.ERRNO_START_ACTIVITY_FAILED_WITH_EXCEPTION.getError(e, activityName, e.getMessage());
54if (logErrorMessage)
55error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
56return error;
57}
58
59return null;
60}
61
62
63
64/**
65* Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}.
66*/
67public static Error startActivityForResult(Context context, int requestCode, @NonNull Intent intent) {
68return startActivityForResult(context, requestCode, intent, true, true, null);
69}
70
71/**
72* Wrapper for {@link #startActivityForResult(Context, int, Intent, boolean, boolean, ActivityResultLauncher)}.
73*/
74public static Error startActivityForResult(Context context, int requestCode, @NonNull Intent intent,
75boolean logErrorMessage, boolean showErrorMessage) {
76return startActivityForResult(context, requestCode, intent, logErrorMessage, showErrorMessage, null);
77}
78
79/**
80* Start an {@link Activity} for result.
81*
82* @param context The context for operations. It must be an instance of {@link Activity} or
83* {@link AppCompatActivity}. It is ignored if {@code activityResultLauncher}
84* is not {@code null}.
85* @param requestCode The request code to use while sending intent. This must be >= 0, otherwise
86* exception will be raised. This is ignored if {@code activityResultLauncher}
87* is {@code null}.
88* @param intent The {@link Intent} to send to start the activity.
89* @param logErrorMessage If an error message should be logged if failed to start activity.
90* @param showErrorMessage If an error message toast should be shown if failed to start activity
91* in addition to logging a message. The {@code context} must not be
92* {@code null}.
93* @param activityResultLauncher The {@link ActivityResultLauncher<Intent>} to use for start the
94* activity. If this is {@code null}, then
95* {@link Activity#startActivityForResult(Intent, int)} will be
96* used instead.
97* Note that later is deprecated.
98* @return Returns the {@code error} if starting activity was not successful, otherwise {@code null}.
99*/
100public static Error startActivityForResult(Context context, int requestCode, @NonNull Intent intent,
101boolean logErrorMessage, boolean showErrorMessage,
102@Nullable ActivityResultLauncher<Intent> activityResultLauncher) {
103Error error;
104String activityName = intent.getComponent() != null ? intent.getComponent().getClassName() : "Unknown";
105try {
106if (activityResultLauncher != null) {
107activityResultLauncher.launch(intent);
108} else {
109if (context == null) {
110error = ActivityErrno.ERRNO_STARTING_ACTIVITY_WITH_NULL_CONTEXT.getError(activityName);
111if (logErrorMessage)
112error.logErrorAndShowToast(null, LOG_TAG);
113return error;
114}
115
116if (context instanceof AppCompatActivity)
117((AppCompatActivity) context).startActivityForResult(intent, requestCode);
118else if (context instanceof Activity)
119((Activity) context).startActivityForResult(intent, requestCode);
120else {
121error = FunctionErrno.ERRNO_PARAMETER_NOT_INSTANCE_OF.getError("context", "startActivityForResult", "Activity or AppCompatActivity");
122if (logErrorMessage)
123error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
124return error;
125}
126}
127} catch (Exception e) {
128error = ActivityErrno.ERRNO_START_ACTIVITY_FOR_RESULT_FAILED_WITH_EXCEPTION.getError(e, activityName, e.getMessage());
129if (logErrorMessage)
130error.logErrorAndShowToast(showErrorMessage ? context : null, LOG_TAG);
131return error;
132}
133
134return null;
135}
136
137}
138