|
9 | 9 | import com.termux.shared.markdown.MarkdownUtils; |
10 | 10 | import com.termux.shared.models.errors.Error; |
11 | 11 | import com.termux.shared.termux.AndroidUtils; |
12 | | -import com.termux.shared.termux.TermuxConstants; |
13 | | -import com.termux.shared.termux.TermuxUtils; |
14 | 12 |
|
15 | 13 | import java.nio.charset.Charset; |
16 | 14 |
|
|
19 | 17 | */ |
20 | 18 | public class CrashHandler implements Thread.UncaughtExceptionHandler { |
21 | 19 |
|
22 | | - private final Context context; |
| 20 | + private final Context mContext; |
| 21 | + private final CrashHandlerClient mCrashHandlerClient; |
23 | 22 | private final Thread.UncaughtExceptionHandler defaultUEH; |
24 | 23 |
|
25 | 24 | private static final String LOG_TAG = "CrashUtils"; |
26 | 25 |
|
27 | | - private CrashHandler(final Context context) { |
28 | | - this.context = context; |
| 26 | + private CrashHandler(@NonNull final Context context, @NonNull final CrashHandlerClient crashHandlerClient) { |
| 27 | + this.mContext = context; |
| 28 | + this.mCrashHandlerClient = crashHandlerClient; |
29 | 29 | this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); |
30 | 30 | } |
31 | 31 |
|
32 | 32 | public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) { |
33 | | - logCrash(context,thread, throwable); |
| 33 | + logCrash(mContext, mCrashHandlerClient, thread, throwable); |
34 | 34 | defaultUEH.uncaughtException(thread, throwable); |
35 | 35 | } |
36 | 36 |
|
37 | 37 | /** |
38 | 38 | * Set default uncaught crash handler of current thread to {@link CrashHandler}. |
39 | 39 | */ |
40 | | - public static void setCrashHandler(final Context context) { |
| 40 | + public static void setCrashHandler(@NonNull final Context context, @NonNull final CrashHandlerClient crashHandlerClient) { |
41 | 41 | if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof CrashHandler)) { |
42 | | - Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(context)); |
| 42 | + Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(context, crashHandlerClient)); |
43 | 43 | } |
44 | 44 | } |
45 | 45 |
|
46 | 46 | /** |
47 | | - * Log a crash in the crash log file at |
48 | | - * {@link TermuxConstants#TERMUX_CRASH_LOG_FILE_PATH}. |
| 47 | + * Log a crash in the crash log file at {@code crashlogFilePath}. |
49 | 48 | * |
50 | 49 | * @param context The {@link Context} for operations. |
| 50 | + * @param crashHandlerClient The {@link CrashHandlerClient} implementation. |
51 | 51 | * @param thread The {@link Thread} in which the crash happened. |
52 | 52 | * @param throwable The {@link Throwable} thrown for the crash. |
53 | 53 | */ |
54 | | - public static void logCrash(final Context context, final Thread thread, final Throwable throwable) { |
55 | | - |
| 54 | + public static void logCrash(@NonNull final Context context, @NonNull final CrashHandlerClient crashHandlerClient, final Thread thread, final Throwable throwable) { |
56 | 55 | StringBuilder reportString = new StringBuilder(); |
57 | 56 |
|
58 | 57 | reportString.append("## Crash Details\n"); |
59 | 58 | reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Thread", thread.toString(), "-")); |
60 | 59 | reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Timestamp", AndroidUtils.getCurrentTimeStamp(), "-")); |
61 | 60 |
|
62 | 61 | reportString.append("\n\n").append(Logger.getStackTracesMarkdownString("Stacktrace", Logger.getStackTracesStringArray(throwable))); |
63 | | - reportString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(context, true)); |
| 62 | + |
| 63 | + String appInfoMarkdownString = crashHandlerClient.getAppInfoMarkdownString(context); |
| 64 | + if (appInfoMarkdownString != null && !appInfoMarkdownString.isEmpty()) |
| 65 | + reportString.append("\n\n").append(appInfoMarkdownString); |
| 66 | + |
64 | 67 | reportString.append("\n\n").append(AndroidUtils.getDeviceInfoMarkdownString(context)); |
65 | 68 |
|
66 | 69 | // Log report string to logcat |
67 | 70 | Logger.logError(reportString.toString()); |
68 | 71 |
|
69 | 72 | // Write report string to crash log file |
70 | | - Error error = FileUtils.writeStringToFile("crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportString.toString(), false); |
| 73 | + Error error = FileUtils.writeStringToFile("crash log", crashHandlerClient.getCrashLogFilePath(context), |
| 74 | + Charset.defaultCharset(), reportString.toString(), false); |
71 | 75 | if (error != null) { |
72 | 76 | Logger.logErrorExtended(LOG_TAG, error.toString()); |
73 | 77 | } |
74 | 78 | } |
75 | 79 |
|
| 80 | + public interface CrashHandlerClient { |
| 81 | + |
| 82 | + /** |
| 83 | + * Get crash log file path. |
| 84 | + * |
| 85 | + * @param context The {@link Context} passed to {@link CrashHandler#CrashHandler(Context, CrashHandlerClient)}. |
| 86 | + * @return Should return the crash log file path. |
| 87 | + */ |
| 88 | + @NonNull |
| 89 | + String getCrashLogFilePath(Context context); |
| 90 | + |
| 91 | + /** |
| 92 | + * Get app info markdown string to add to crash log. |
| 93 | + * |
| 94 | + * @param context The {@link Context} passed to {@link CrashHandler#CrashHandler(Context, CrashHandlerClient)}. |
| 95 | + * @return Should return app info markdown string. |
| 96 | + */ |
| 97 | + String getAppInfoMarkdownString(Context context); |
| 98 | + |
| 99 | + } |
| 100 | + |
76 | 101 | } |
0 commit comments