|
| 1 | +package com.termux.app.crash; |
| 2 | + |
| 3 | +import android.app.Notification; |
| 4 | +import android.app.NotificationManager; |
| 5 | +import android.app.PendingIntent; |
| 6 | +import android.content.Context; |
| 7 | +import android.content.Intent; |
| 8 | + |
| 9 | +import androidx.annotation.Nullable; |
| 10 | + |
| 11 | +import com.termux.R; |
| 12 | +import com.termux.app.activities.ReportActivity; |
| 13 | +import com.termux.app.file.FileUtils; |
| 14 | +import com.termux.app.models.ReportInfo; |
| 15 | +import com.termux.app.models.UserAction; |
| 16 | +import com.termux.app.settings.preferences.TermuxAppSharedPreferences; |
| 17 | +import com.termux.app.settings.preferences.TermuxPreferenceConstants; |
| 18 | +import com.termux.app.utils.DataUtils; |
| 19 | +import com.termux.app.utils.Logger; |
| 20 | +import com.termux.app.utils.MarkdownUtils; |
| 21 | +import com.termux.app.utils.NotificationUtils; |
| 22 | +import com.termux.app.utils.TermuxUtils; |
| 23 | + |
| 24 | +import com.termux.app.TermuxConstants; |
| 25 | + |
| 26 | +import java.nio.charset.Charset; |
| 27 | + |
| 28 | +public class CrashUtils { |
| 29 | + |
| 30 | + private static final String NOTIFICATION_CHANNEL_ID_CRASH_REPORT_ERRORS = "termux_crash_reports_notification_channel"; |
| 31 | + private static final String NOTIFICATION_CHANNEL_NAME_CRASH_REPORT_ERRORS = TermuxConstants.TERMUX_APP_NAME + " Crash Reports"; |
| 32 | + |
| 33 | + private static final String LOG_TAG = "CrashUtils"; |
| 34 | + |
| 35 | + /** |
| 36 | + * Log a crash in the crash log file at |
| 37 | + * {@link TermuxConstants#TERMUX_CRASH_LOG_FILE_PATH}. |
| 38 | + * |
| 39 | + * @param context The {@link Context} for operations. |
| 40 | + * @param thread The {@link Thread} in which the crash happened. |
| 41 | + * @param thread The {@link Throwable} thrown for the crash. |
| 42 | + */ |
| 43 | + public static void logCrash(final Context context, final Thread thread, final Throwable throwable) { |
| 44 | + |
| 45 | + StringBuilder reportString = new StringBuilder(); |
| 46 | + |
| 47 | + reportString.append("## Crash Details\n"); |
| 48 | + reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Thread", thread.toString(), "-")); |
| 49 | + reportString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Crash Timestamp", TermuxUtils.getCurrentTimeStamp(), "-")); |
| 50 | + |
| 51 | + reportString.append("\n\n").append(Logger.getStackTracesMarkdownString("Stacktrace", Logger.getStackTraceStringArray(throwable))); |
| 52 | + reportString.append("\n\n").append(TermuxUtils.getAppInfoMarkdownString(context, true)); |
| 53 | + reportString.append("\n\n").append(TermuxUtils.getDeviceInfoMarkdownString(context)); |
| 54 | + |
| 55 | + // Log report string to logcat |
| 56 | + Logger.logError(reportString.toString()); |
| 57 | + |
| 58 | + // Write report string to crash log file |
| 59 | + String errmsg = FileUtils.writeStringToFile(context, "crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportString.toString(), false); |
| 60 | + if(errmsg != null) { |
| 61 | + Logger.logError(LOG_TAG, errmsg); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Notify the user of a previous app crash by reading the crash info from the crash log file at |
| 67 | + * {@link TermuxConstants#TERMUX_CRASH_LOG_FILE_PATH}. |
| 68 | + * |
| 69 | + * If the crash log file exists and is not empty and |
| 70 | + * {@link TermuxPreferenceConstants.TERMUX_APP#KEY_CRASH_REPORT_NOTIFICATIONS_ENABLED} is |
| 71 | + * enabled, then a notification will be shown for the crash on the |
| 72 | + * {@link #NOTIFICATION_CHANNEL_NAME_CRASH_REPORT_ERRORS} channel, otherwise nothing will be done. |
| 73 | + * |
| 74 | + * After reading from the crash log file, it will be moved to {@link TermuxConstants#TERMUX_CRASH_LOG_BACKUP_FILE_PATH}. |
| 75 | + * |
| 76 | + * @param context The {@link Context} for operations. |
| 77 | + * @param logTagParam The log tag to use for logging. |
| 78 | + */ |
| 79 | + public static void notifyCrash(final Context context, final String logTagParam) { |
| 80 | + if(context == null) return; |
| 81 | + |
| 82 | + |
| 83 | + TermuxAppSharedPreferences preferences = new TermuxAppSharedPreferences(context); |
| 84 | + // If user has disabled notifications for crashes |
| 85 | + if (!preferences.getCrashReportNotificationsEnabled()) |
| 86 | + return; |
| 87 | + |
| 88 | + new Thread() { |
| 89 | + @Override |
| 90 | + public void run() { |
| 91 | + String logTag = DataUtils.getDefaultIfNull(logTagParam, LOG_TAG); |
| 92 | + |
| 93 | + if(!FileUtils.regularFileExists(TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, false)) |
| 94 | + return; |
| 95 | + |
| 96 | + String errmsg; |
| 97 | + StringBuilder reportStringBuilder = new StringBuilder(); |
| 98 | + |
| 99 | + // Read report string from crash log file |
| 100 | + errmsg = FileUtils.readStringFromFile(context, "crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, Charset.defaultCharset(), reportStringBuilder, false); |
| 101 | + if(errmsg != null) { |
| 102 | + Logger.logError(logTag, errmsg); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + // Move crash log file to backup location if it exists |
| 107 | + FileUtils.moveRegularFile(context, "crash log", TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH, TermuxConstants.TERMUX_CRASH_LOG_BACKUP_FILE_PATH, true); |
| 108 | + if(errmsg != null) { |
| 109 | + Logger.logError(logTag, errmsg); |
| 110 | + } |
| 111 | + |
| 112 | + String reportString = reportStringBuilder.toString(); |
| 113 | + |
| 114 | + if(reportString == null || reportString.isEmpty()) |
| 115 | + return; |
| 116 | + |
| 117 | + // Send a notification to show the crash log which when clicked will open the {@link ReportActivity} |
| 118 | + // to show the details of the crash |
| 119 | + String title = TermuxConstants.TERMUX_APP_NAME + " Crash Report"; |
| 120 | + |
| 121 | + Logger.logDebug(logTag, "The crash log file at \"" + TermuxConstants.TERMUX_CRASH_LOG_FILE_PATH + "\" found. Sending \"" + title + "\" notification."); |
| 122 | + |
| 123 | + Intent notificationIntent = ReportActivity.newInstance(context, new ReportInfo(UserAction.CRASH_REPORT, logTag, title, null, reportString, "\n\n" + TermuxUtils.getReportIssueMarkdownString(context), true)); |
| 124 | + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); |
| 125 | + |
| 126 | + // Setup the notification channel if not already set up |
| 127 | + setupCrashReportsNotificationChannel(context); |
| 128 | + |
| 129 | + // Build the notification |
| 130 | + Notification.Builder builder = getCrashReportsNotificationBuilder(context, title, null, null, pendingIntent, NotificationUtils.NOTIFICATION_MODE_VIBRATE); |
| 131 | + if(builder == null) return; |
| 132 | + |
| 133 | + // Send the notification |
| 134 | + int nextNotificationId = NotificationUtils.getNextNotificationId(context); |
| 135 | + NotificationManager notificationManager = NotificationUtils.getNotificationManager(context); |
| 136 | + if(notificationManager != null) |
| 137 | + notificationManager.notify(nextNotificationId, builder.build()); |
| 138 | + } |
| 139 | + }.start(); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Get {@link Notification.Builder} for {@link #NOTIFICATION_CHANNEL_ID_CRASH_REPORT_ERRORS} |
| 144 | + * and {@link #NOTIFICATION_CHANNEL_NAME_CRASH_REPORT_ERRORS}. |
| 145 | + * |
| 146 | + * @param context The {@link Context} for operations. |
| 147 | + * @param title The title for the notification. |
| 148 | + * @param notifiationText The second line text of the notification. |
| 149 | + * @param notificationBigText The full text of the notification that may optionally be styled. |
| 150 | + * @param pendingIntent The {@link PendingIntent} which should be sent when notification is clicked. |
| 151 | + * @param notificationMode The notification mode. It must be one of {@code NotificationUtils.NOTIFICATION_MODE_*}. |
| 152 | + * @return Returns the {@link Notification.Builder}. |
| 153 | + */ |
| 154 | + @Nullable |
| 155 | + public static Notification.Builder getCrashReportsNotificationBuilder(final Context context, final CharSequence title, final CharSequence notifiationText, final CharSequence notificationBigText, final PendingIntent pendingIntent, final int notificationMode) { |
| 156 | + |
| 157 | + Notification.Builder builder = NotificationUtils.geNotificationBuilder(context, |
| 158 | + NOTIFICATION_CHANNEL_ID_CRASH_REPORT_ERRORS, Notification.PRIORITY_HIGH, |
| 159 | + title, notifiationText, notificationBigText, pendingIntent, notificationMode); |
| 160 | + |
| 161 | + if(builder == null) return null; |
| 162 | + |
| 163 | + // Enable timestamp |
| 164 | + builder.setShowWhen(true); |
| 165 | + |
| 166 | + // Set notification icon |
| 167 | + builder.setSmallIcon(R.drawable.ic_error_notification); |
| 168 | + |
| 169 | + // Set background color for small notification icon |
| 170 | + builder.setColor(0xFF607D8B); |
| 171 | + |
| 172 | + // Dismiss on click |
| 173 | + builder.setAutoCancel(true); |
| 174 | + |
| 175 | + return builder; |
| 176 | + } |
| 177 | + |
| 178 | + /** |
| 179 | + * Setup the notification channel for {@link #NOTIFICATION_CHANNEL_ID_CRASH_REPORT_ERRORS} and |
| 180 | + * {@link #NOTIFICATION_CHANNEL_NAME_CRASH_REPORT_ERRORS}. |
| 181 | + * |
| 182 | + * @param context The {@link Context} for operations. |
| 183 | + */ |
| 184 | + public static void setupCrashReportsNotificationChannel(final Context context) { |
| 185 | + NotificationUtils.setupNotificationChannel(context, NOTIFICATION_CHANNEL_ID_CRASH_REPORT_ERRORS, |
| 186 | + NOTIFICATION_CHANNEL_NAME_CRASH_REPORT_ERRORS, NotificationManager.IMPORTANCE_HIGH); |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments