forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcraCrashReporter.kt
More file actions
415 lines (385 loc) · 17.3 KB
/
AcraCrashReporter.kt
File metadata and controls
415 lines (385 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Copyright (c) 2022 lukstbit <lukstbit@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
import androidx.core.content.edit
import androidx.core.content.pm.PackageInfoCompat
import androidx.webkit.WebViewCompat
import com.ichi2.anki.analytics.AnkiDroidCrashReportDialog
import com.ichi2.anki.analytics.UsageAnalytics
import com.ichi2.anki.analytics.UsageAnalytics.sendAnalyticsException
import com.ichi2.anki.common.crashreporting.CrashReportService
import com.ichi2.anki.common.crashreporting.CrashReporter
import com.ichi2.anki.common.crashreporting.CrashReporter.Companion.FEEDBACK_REPORT_ALWAYS
import com.ichi2.anki.common.crashreporting.CrashReporter.Companion.FEEDBACK_REPORT_ASK
import com.ichi2.anki.common.crashreporting.CrashReporter.Companion.FEEDBACK_REPORT_KEY
import com.ichi2.anki.common.crashreporting.CrashReporter.Companion.FEEDBACK_REPORT_NEVER
import com.ichi2.anki.common.exception.ManuallyReportedException
import com.ichi2.anki.common.time.TimeManager
import com.ichi2.anki.exception.UserSubmittedException
import com.ichi2.anki.preferences.sharedPrefs
import com.ichi2.anki.servicelayer.ThrowableFilterService
import com.ichi2.utils.WebViewDebugging.setDataDirectorySuffix
import org.acra.ACRA
import org.acra.ReportField
import org.acra.config.CoreConfigurationBuilder
import org.acra.config.DialogConfigurationBuilder
import org.acra.config.HttpSenderConfigurationBuilder
import org.acra.config.LimiterConfigurationBuilder
import org.acra.config.LimiterData
import org.acra.config.ToastConfigurationBuilder
import org.acra.sender.HttpSender
import timber.log.Timber
private object AcraCrashReporter : CrashReporter {
/** Our ACRA configurations, initialized during Application.onCreate() */
@JvmStatic
private var logcatArgs =
arrayOf(
"-t",
"500",
"-v",
"time",
"ActivityManager:I",
"SQLiteLog:W",
AnkiDroidApp.TAG + ":D",
"rsdroid:E",
"*:S",
)
@JvmStatic
private var dialogEnabled = true
@JvmStatic
private lateinit var toastText: String
@JvmStatic
lateinit var acraCoreConfigBuilder: CoreConfigurationBuilder
private set
private lateinit var application: Application
private const val WEBVIEW_VER_NAME = "WEBVIEW_VER_NAME"
private const val MIN_INTERVAL_MS = 60000
private const val EXCEPTION_MESSAGE = "Exception report sent by user manually. See: 'Comment/USER_COMMENT'"
private enum class ToastType(
@StringRes private val toastMessageRes: Int,
) {
AUTO_TOAST(R.string.feedback_auto_toast_text),
MANUAL_TOAST(R.string.feedback_for_manual_toast_text),
;
fun getToastMessage(context: Context) = context.getString(toastMessageRes)
}
private fun createAcraCoreConfigBuilder(): CoreConfigurationBuilder {
val builder =
CoreConfigurationBuilder()
.withBuildConfigClass(com.ichi2.anki.BuildConfig::class.java) // AnkiDroid BuildConfig - Acrarium#319
.withExcludeMatchingSharedPreferencesKeys("username", "hkey", "currentSyncUri", "browser_search_history")
.withSharedPreferencesName("acra")
.withReportContent(
ReportField.REPORT_ID,
ReportField.APP_VERSION_CODE,
ReportField.APP_VERSION_NAME,
ReportField.PACKAGE_NAME,
ReportField.FILE_PATH,
ReportField.PHONE_MODEL,
ReportField.ANDROID_VERSION,
ReportField.BUILD,
ReportField.BRAND,
ReportField.PRODUCT,
ReportField.TOTAL_MEM_SIZE,
ReportField.AVAILABLE_MEM_SIZE,
ReportField.BUILD_CONFIG,
ReportField.CUSTOM_DATA,
ReportField.STACK_TRACE,
ReportField.STACK_TRACE_HASH,
ReportField.CRASH_CONFIGURATION,
ReportField.USER_COMMENT,
ReportField.USER_APP_START_DATE,
ReportField.USER_CRASH_DATE,
ReportField.LOGCAT,
ReportField.INSTALLATION_ID,
ReportField.ENVIRONMENT,
ReportField.SHARED_PREFERENCES,
// ReportField.MEDIA_CODEC_LIST,
ReportField.THREAD_DETAILS,
).withLogcatArguments(*logcatArgs)
.withPluginConfigurations(
DialogConfigurationBuilder()
.withReportDialogClass(AnkiDroidCrashReportDialog::class.java)
.withCommentPrompt(application.getString(R.string.empty_string))
.withTitle(application.getString(R.string.feedback_title))
.withText(application.getString(R.string.feedback_default_text))
.withPositiveButtonText(application.getString(R.string.feedback_report))
.withResIcon(R.drawable.logo_star_144dp)
.withEnabled(dialogEnabled)
.build(),
HttpSenderConfigurationBuilder()
.withHttpMethod(HttpSender.Method.PUT)
.withUri(BuildConfig.ACRA_URL)
.withEnabled(true)
.build(),
ToastConfigurationBuilder()
.withText(toastText)
.withEnabled(true)
.build(),
LimiterConfigurationBuilder()
.withExceptionClassLimit(1000)
.withStacktraceLimit(1)
.withDeleteReportsOnAppUpdate(true)
.withResetLimitsOnAppUpdate(true)
.withEnabled(true)
.build(),
)
ACRA.init(application, builder)
acraCoreConfigBuilder = builder
fetchWebViewInformation().let {
ACRA.errorReporter.putCustomData(WEBVIEW_VER_NAME, it[WEBVIEW_VER_NAME] ?: "")
ACRA.errorReporter.putCustomData("WEBVIEW_VER_CODE", it["WEBVIEW_VER_CODE"] ?: "")
}
return builder
}
/**
* Use this method to initialize the ACRA CoreConfigurationBuilder in Application.onCreate().
* The ACRA process needs a WebView for optimal UsageAnalytics values but it can't have the same
* data directory. Analytics falls back to a sensible default if this is not set.
*/
@JvmStatic
fun initialize(application: Application) {
this.application = application
// FIXME ACRA needs to reinitialize after language is changed, but with the new language
// this is difficult because the Application (AnkiDroidApp) does not change it's baseContext
// perhaps a solution could be to change AnkiDroidApp to have a context wrapper that it sets
// as baseContext, and that wrapper allows a resources/configuration update, then
// in GeneralSettingsFragment for the language dialog change listener, the context wrapper
// could be updated directly with the new locale code so that calling getString on would fetch
// the new language string ?
toastText = ToastType.AUTO_TOAST.getToastMessage(application)
// Setup logging and crash reporting
if (BuildConfig.DEBUG) {
setDebugACRAConfig(application.sharedPrefs())
} else {
setProductionACRAConfig(application.sharedPrefs())
}
if (ACRA.isACRASenderServiceProcess() && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
try {
setDataDirectorySuffix("acra")
} catch (e: java.lang.Exception) {
Timber.w(e, "Failed to set WebView data directory")
}
}
}
/**
* Set the reporting mode for ACRA based on the value of the FEEDBACK_REPORT_KEY preference
* @param value value of FEEDBACK_REPORT_KEY preference
*/
override fun setReportingMode(value: String) {
application.sharedPrefs().edit {
// Set the ACRA disable value
if (value == FEEDBACK_REPORT_NEVER) {
putBoolean(ACRA.PREF_DISABLE_ACRA, true)
} else {
putBoolean(ACRA.PREF_DISABLE_ACRA, false)
// Switch between auto-report via toast and manual report via dialog
if (value == FEEDBACK_REPORT_ALWAYS) {
dialogEnabled = false
toastText = ToastType.AUTO_TOAST.getToastMessage(application)
} else if (value == FEEDBACK_REPORT_ASK) {
createAcraCoreConfigBuilder()
dialogEnabled = true
toastText = ToastType.MANUAL_TOAST.getToastMessage(application)
}
createAcraCoreConfigBuilder()
}
}
}
/**
* Turns ACRA reporting off completely and persists it to shared prefs
* But expands logcat search in case developer manually re-enables it
*
* @param prefs SharedPreferences object the reporting state is persisted in
*/
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun setDebugACRAConfig(prefs: SharedPreferences) {
// Disable crash reporting
setReportingMode(FEEDBACK_REPORT_NEVER)
prefs.edit { putString(FEEDBACK_REPORT_KEY, FEEDBACK_REPORT_NEVER) }
// Use a wider logcat filter in case crash reporting manually re-enabled
logcatArgs = arrayOf("-t", "1500", "-v", "long", "ACRA:S")
createAcraCoreConfigBuilder()
}
/**
* Puts ACRA Reporting mode into user-specified mode, with default of "ask first"
*
* @param prefs SharedPreferences object the reporting state is persisted in
*/
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun setProductionACRAConfig(prefs: SharedPreferences) {
// Enable or disable crash reporting based on user setting
setReportingMode(prefs.getString(FEEDBACK_REPORT_KEY, FEEDBACK_REPORT_ASK)!!)
}
private fun fetchWebViewInformation(): HashMap<String, String> {
val webViewInfo = hashMapOf<String, String>()
webViewInfo[WEBVIEW_VER_NAME] = ""
webViewInfo["WEBVIEW_VER_CODE"] = ""
try {
val pi = WebViewCompat.getCurrentWebViewPackage(application)
if (pi == null) {
Timber.w("Could not get WebView package information")
return webViewInfo
}
pi.versionName?.let { webViewInfo[WEBVIEW_VER_NAME] = it }
webViewInfo["WEBVIEW_VER_CODE"] = PackageInfoCompat.getLongVersionCode(pi).toString()
} catch (e: Throwable) {
Timber.w(e)
}
return webViewInfo
}
/** Used when we don't have an exception to throw, but we know something is wrong and want to diagnose it */
override fun sendExceptionReport(
message: String?,
origin: String?,
) = sendExceptionReport(ManuallyReportedException(message), origin)
override fun sendExceptionReport(
e: Throwable,
origin: String?,
additionalInfo: String?,
onlyIfSilent: Boolean,
) = sendExceptionReport(e, origin, additionalInfo, onlyIfSilent, application.applicationContext)
override fun sendExceptionReport(
e: Throwable,
origin: String?,
additionalInfo: String?,
onlyIfSilent: Boolean,
context: Context,
) {
sendAnalyticsException(e, false)
AnkiDroidApp.sentExceptionReportHack = true
val reportMode =
context
.sharedPrefs()
.getString(FEEDBACK_REPORT_KEY, FEEDBACK_REPORT_ASK)
if (onlyIfSilent) {
if (FEEDBACK_REPORT_ALWAYS != reportMode) {
Timber.i("sendExceptionReport - onlyIfSilent true, but ACRA is not 'always accept'. Skipping report send.")
return
}
}
if (FEEDBACK_REPORT_NEVER != reportMode) {
if (ThrowableFilterService.shouldDiscardThrowable(e)) return
ACRA.errorReporter.putCustomData("origin", origin ?: "")
ACRA.errorReporter.putCustomData("additionalInfo", additionalInfo ?: "")
ACRA.errorReporter.handleException(e)
}
}
fun isProperServiceProcess(): Boolean = ACRA.isACRASenderServiceProcess()
override fun isEnabled(
context: Context,
defaultValue: Boolean,
): Boolean {
if (!context.sharedPrefs().contains(ACRA.PREF_DISABLE_ACRA)) {
// we shouldn't use defaultValue below, as it would be inverted which complicated understanding.
Timber.w("No default value for '%s'", ACRA.PREF_DISABLE_ACRA)
return defaultValue
}
return !context.sharedPrefs().getBoolean(ACRA.PREF_DISABLE_ACRA, true)
}
/**
* If you want to make sure that the next exception of any time is posted, you need to clear limiter data
*
* @param context the context leading to the directory with ACRA limiter data
*/
override fun deleteLimiterData(context: Context) {
try {
LimiterData().store(context)
} catch (e: Exception) {
Timber.w(e, "Unable to clear ACRA limiter data")
}
}
override fun onPreferenceChanged(
ctx: Context,
newValue: String,
) {
setReportingMode(newValue)
// If the user changed error reporting, make sure future reports have a chance to post
deleteLimiterData(ctx)
// We also need to re-chain our UncaughtExceptionHandlers
UsageAnalytics.reInitialize()
ThrowableFilterService.reInitialize()
}
/**
* @return the status of the report, true if the report was sent, false if the report is already
* submitted
*/
override fun sendReport(activity: android.app.Activity): Boolean {
val ankiActivity = activity as AnkiActivity
val preferences = ankiActivity.sharedPrefs()
val reportMode = preferences.getString(FEEDBACK_REPORT_KEY, "")
return if (FEEDBACK_REPORT_NEVER == reportMode) {
preferences.edit { putBoolean(ACRA.PREF_DISABLE_ACRA, false) }
toastText = ToastType.MANUAL_TOAST.getToastMessage(application)
createAcraCoreConfigBuilder()
val sendStatus = sendReportFor(ankiActivity)
dialogEnabled = false
createAcraCoreConfigBuilder()
preferences.edit { putBoolean(ACRA.PREF_DISABLE_ACRA, true) }
sendStatus
} else {
sendReportFor(ankiActivity)
}
}
private fun sendReportFor(activity: AnkiActivity): Boolean {
val currentTimestamp = TimeManager.time.intTimeMS()
val lastReportTimestamp = getTimestampOfLastReport(activity)
return if (currentTimestamp - lastReportTimestamp > MIN_INTERVAL_MS) {
deleteLimiterData(activity)
sendExceptionReport(
UserSubmittedException(EXCEPTION_MESSAGE),
"AnkiDroidApp.HelpDialog",
)
true
} else {
false
}
}
/**
* Check the ACRA report store and return the timestamp of the last report.
*
* @param activity the Activity used for Context access when interrogating ACRA reports
* @return the timestamp of the most recent report, or -1 if no reports at all
*/
private fun getTimestampOfLastReport(activity: AnkiActivity): Long =
LimiterData
.load(activity)
.reportMetadata
.filter { it.exceptionClass == UserSubmittedException::class.java.name }
.maxOfOrNull { it.timestamp?.timeInMillis ?: -1L } ?: -1L
}
/**
* Initializes ACRA crash reporting and wires it up as the
* global [CrashReportService] reporter.
*/
context(application: Application)
fun initializeAcraCrashReporter() {
AcraCrashReporter.initialize(application)
CrashReportService.setReporter(AcraCrashReporter)
}
fun isAcraSenderProcess(): Boolean = AcraCrashReporter.isProperServiceProcess()
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
val CrashReportService.acraCoreConfigBuilder: CoreConfigurationBuilder
get() = AcraCrashReporter.acraCoreConfigBuilder
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun setDebugACRAConfig(sharedPrefs: SharedPreferences) = AcraCrashReporter.setDebugACRAConfig(sharedPrefs)
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun setProductionACRAConfig(sharedPrefs: SharedPreferences) = AcraCrashReporter.setProductionACRAConfig(sharedPrefs)