Inject the context to simplify the API

And more cleanup
This commit is contained in:
Benoit Marty 2022-01-25 16:59:21 +01:00 committed by Benoit Marty
parent 4893429d73
commit 1a8c31f926
6 changed files with 41 additions and 55 deletions

View file

@ -16,7 +16,6 @@
package im.vector.app
import android.content.Context
import android.content.SharedPreferences
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.features.rageshake.BugReporter
@ -46,7 +45,6 @@ class AutoRageShaker @Inject constructor(
private val sessionDataSource: ActiveSessionDataSource,
private val activeSessionHolder: ActiveSessionHolder,
private val bugReporter: BugReporter,
private val context: Context,
private val vectorPreferences: VectorPreferences
) : Session.Listener, SharedPreferences.OnSharedPreferenceChangeListener {
@ -136,7 +134,6 @@ class AutoRageShaker @Inject constructor(
private fun sendRageShake(target: E2EMessageDetected) {
bugReporter.sendBugReport(
context = context,
reportType = ReportType.AUTO_UISI,
withDevicesLogs = true,
withCrashLogs = true,
@ -218,7 +215,6 @@ class AutoRageShaker @Inject constructor(
val matchingIssue = event.content?.get("recipient_rageshake")?.toString() ?: ""
bugReporter.sendBugReport(
context = context,
reportType = ReportType.AUTO_UISI_SENDER,
withDevicesLogs = true,
withCrashLogs = true,

View file

@ -120,7 +120,7 @@ class VectorApplication :
vectorAnalytics.init()
invitesAcceptor.initialize()
autoRageShaker.initialize()
vectorUncaughtExceptionHandler.activate(this)
vectorUncaughtExceptionHandler.activate()
// Remove Log handler statically added by Jitsi
Timber.forest()

View file

@ -473,14 +473,14 @@ class HomeActivity :
override fun onResume() {
super.onResume()
if (vectorUncaughtExceptionHandler.didAppCrash(this)) {
vectorUncaughtExceptionHandler.clearAppCrashStatus(this)
if (vectorUncaughtExceptionHandler.didAppCrash()) {
vectorUncaughtExceptionHandler.clearAppCrashStatus()
MaterialAlertDialogBuilder(this)
.setMessage(R.string.send_bug_report_app_crashed)
.setCancelable(false)
.setPositiveButton(R.string.yes) { _, _ -> bugReporter.openBugReportScreen(this) }
.setNegativeButton(R.string.no) { _, _ -> bugReporter.deleteCrashFile(this) }
.setNegativeButton(R.string.no) { _, _ -> bugReporter.deleteCrashFile() }
.show()
} else {
showDisclaimerDialog(this)

View file

@ -151,7 +151,7 @@ class BugReportActivity : VectorBaseActivity<ActivityBugReportBinding>() {
views.bugReportProgressView.isVisible = true
views.bugReportProgressView.progress = 0
bugReporter.sendBugReport(this,
bugReporter.sendBugReport(
reportType,
views.bugReportButtonIncludeLogs.isChecked,
views.bugReportButtonIncludeCrashLogs.isChecked,
@ -249,7 +249,7 @@ class BugReportActivity : VectorBaseActivity<ActivityBugReportBinding>() {
override fun onBackPressed() {
// Ensure there is no crash status remaining, which will be sent later on by mistake
bugReporter.deleteCrashFile(this)
bugReporter.deleteCrashFile()
super.onBackPressed()
}

View file

@ -68,6 +68,7 @@ import javax.inject.Singleton
*/
@Singleton
class BugReporter @Inject constructor(
private val context: Context,
private val activeSessionHolder: ActiveSessionHolder,
private val versionProvider: VersionProvider,
private val vectorPreferences: VectorPreferences,
@ -153,7 +154,6 @@ class BugReporter @Inject constructor(
/**
* Send a bug report.
*
* @param context the application context
* @param reportType The report type (bug, suggestion, feedback)
* @param withDevicesLogs true to include the device log
* @param withCrashLogs true to include the crash logs
@ -163,8 +163,7 @@ class BugReporter @Inject constructor(
* @param listener the listener
*/
@SuppressLint("StaticFieldLeak")
fun sendBugReport(context: Context,
reportType: ReportType,
fun sendBugReport(reportType: ReportType,
withDevicesLogs: Boolean,
withCrashLogs: Boolean,
withKeyRequestHistory: Boolean,
@ -182,7 +181,7 @@ class BugReporter @Inject constructor(
var reportURL: String? = null
withContext(Dispatchers.IO) {
var bugDescription = theBugDescription
val crashCallStack = getCrashDescription(context)
val crashCallStack = getCrashDescription()
if (null != crashCallStack) {
bugDescription += "\n\n\n\n--------------------------------- crash call stack ---------------------------------\n"
@ -203,7 +202,7 @@ class BugReporter @Inject constructor(
}
if (!mIsCancelled && (withCrashLogs || withDevicesLogs)) {
val gzippedLogcat = saveLogCat(context, false)
val gzippedLogcat = saveLogCat(false)
if (null != gzippedLogcat) {
if (gzippedFiles.size == 0) {
@ -213,7 +212,7 @@ class BugReporter @Inject constructor(
}
}
val crashDescription = getCrashFile(context)
val crashDescription = getCrashFile()
if (crashDescription.exists()) {
val compressedCrashDescription = compressFile(crashDescription)
@ -265,7 +264,7 @@ class BugReporter @Inject constructor(
// build the multi part request
val builder = BugReporterMultipartBody.Builder()
.addFormDataPart("text", text)
.addFormDataPart("app", rageShakeAppNameForReport(context, reportType))
.addFormDataPart("app", rageShakeAppNameForReport(reportType))
.addFormDataPart("user_agent", Matrix.getInstance(context).getUserAgent())
.addFormDataPart("user_id", userId)
.addFormDataPart("can_contact", canContact.toString())
@ -352,9 +351,9 @@ class BugReporter @Inject constructor(
}
}
if (getCrashFile(context).exists()) {
if (getCrashFile().exists()) {
builder.addFormDataPart("label", "crash")
deleteCrashFile(context)
deleteCrashFile()
}
val requestBody = builder.build()
@ -487,20 +486,16 @@ class BugReporter @Inject constructor(
activity.startActivity(BugReportActivity.intent(activity, reportType))
}
private fun rageShakeAppNameForReport(context: Context, reportType: ReportType): String {
private fun rageShakeAppNameForReport(reportType: ReportType): String {
// As per https://github.com/matrix-org/rageshake
// app: Identifier for the application (eg 'riot-web').
// Should correspond to a mapping configured in the configuration file for github issue reporting to work.
// (see R.string.bug_report_url for configured RS server)
return when (reportType) {
return context.getString(when (reportType) {
ReportType.AUTO_UISI_SENDER,
ReportType.AUTO_UISI -> {
context.getString(R.string.bug_report_auto_uisi_app_name)
}
else -> {
context.getString(R.string.bug_report_app_name)
}
}
ReportType.AUTO_UISI -> R.string.bug_report_auto_uisi_app_name
else -> R.string.bug_report_app_name
})
}
// ==============================================================================================================
// crash report management
@ -509,20 +504,17 @@ class BugReporter @Inject constructor(
/**
* Provides the crash file
*
* @param context the context
* @return the crash file
*/
private fun getCrashFile(context: Context): File {
private fun getCrashFile(): File {
return File(context.cacheDir.absolutePath, CRASH_FILENAME)
}
/**
* Remove the crash file
*
* @param context
*/
fun deleteCrashFile(context: Context) {
val crashFile = getCrashFile(context)
fun deleteCrashFile() {
val crashFile = getCrashFile()
if (crashFile.exists()) {
crashFile.delete()
@ -535,11 +527,10 @@ class BugReporter @Inject constructor(
/**
* Save the crash report
*
* @param context the context
* @param crashDescription teh crash description
*/
fun saveCrashReport(context: Context, crashDescription: String) {
val crashFile = getCrashFile(context)
fun saveCrashReport(crashDescription: String) {
val crashFile = getCrashFile()
if (crashFile.exists()) {
crashFile.delete()
@ -557,11 +548,10 @@ class BugReporter @Inject constructor(
/**
* Read the crash description file and return its content.
*
* @param context teh context
* @return the crash description
*/
private fun getCrashDescription(context: Context): String? {
val crashFile = getCrashFile(context)
private fun getCrashDescription(): String? {
val crashFile = getCrashFile()
if (crashFile.exists()) {
try {
@ -650,11 +640,10 @@ class BugReporter @Inject constructor(
/**
* Save the logcat
*
* @param context the context
* @param isErrorLogcat true to save the error logcat
* @return the file if the operation succeeds
*/
private fun saveLogCat(context: Context, isErrorLogcat: Boolean): File? {
private fun saveLogCat(isErrorLogcat: Boolean): File? {
val logCatErrFile = File(context.cacheDir.absolutePath, if (isErrorLogcat) LOG_CAT_ERROR_FILENAME else LOG_CAT_FILENAME)
if (logCatErrFile.exists()) {

View file

@ -30,9 +30,12 @@ import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter: BugReporter,
private val versionProvider: VersionProvider,
private val versionCodeProvider: VersionCodeProvider) : Thread.UncaughtExceptionHandler {
class VectorUncaughtExceptionHandler @Inject constructor(
context: Context,
private val bugReporter: BugReporter,
private val versionProvider: VersionProvider,
private val versionCodeProvider: VersionCodeProvider
) : Thread.UncaughtExceptionHandler {
// key to save the crash status
companion object {
@ -41,13 +44,12 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter
private var previousHandler: Thread.UncaughtExceptionHandler? = null
private lateinit var context: Context
private val preferences = DefaultSharedPreferences.getInstance(context)
/**
* Activate this handler
*/
fun activate(context: Context) {
this.context = context
fun activate() {
previousHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler(this)
}
@ -61,7 +63,7 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter
*/
override fun uncaughtException(thread: Thread, throwable: Throwable) {
Timber.v("Uncaught exception: $throwable")
DefaultSharedPreferences.getInstance(context).edit {
preferences.edit {
putBoolean(PREFS_CRASH_KEY, true)
}
val b = StringBuilder()
@ -103,7 +105,7 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter
val bugDescription = b.toString()
Timber.e("FATAL EXCEPTION $bugDescription")
bugReporter.saveCrashReport(context, bugDescription)
bugReporter.saveCrashReport(bugDescription)
// Show the classical system popup
previousHandler?.uncaughtException(thread, throwable)
@ -114,16 +116,15 @@ class VectorUncaughtExceptionHandler @Inject constructor(private val bugReporter
*
* @return true if the application crashed
*/
fun didAppCrash(context: Context): Boolean {
return DefaultSharedPreferences.getInstance(context)
.getBoolean(PREFS_CRASH_KEY, false)
fun didAppCrash(): Boolean {
return preferences.getBoolean(PREFS_CRASH_KEY, false)
}
/**
* Clear the crash status
*/
fun clearAppCrashStatus(context: Context) {
DefaultSharedPreferences.getInstance(context).edit {
fun clearAppCrashStatus() {
preferences.edit {
remove(PREFS_CRASH_KEY)
}
}