Skip to content

Commit

Permalink
feature: replace ACRA crash report with own implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kaajjo committed Jul 21, 2024
1 parent f4dc3d9 commit 670da01
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 92 deletions.
2 changes: 0 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ dependencies {
implementation(libs.datastore.preferences)

implementation(libs.appcompat)
implementation(libs.acra.dialog)
implementation(libs.acra.mail)

implementation(libs.aboutLibraries)

Expand Down
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@
android:supportsRtl="true"
android:theme="@style/Theme.LibreSudoku"
tools:targetApi="tiramisu">
<activity
android:name=".ui.app_crash.CrashActivity"
android:exported="false"
android:theme="@style/Theme.LibreSudoku" />

<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/Theme.LibreSudoku">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter android:label="Import sudoku">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down
30 changes: 0 additions & 30 deletions app/src/main/java/com/kaajjo/libresudoku/LibreSudokuApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,13 @@ import android.app.Application
import android.content.Context
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import com.kaajjo.libresudoku.di.ACRA_SHARED_PREFS_NAME
import dagger.hilt.android.HiltAndroidApp
import org.acra.config.dialog
import org.acra.config.mailSender
import org.acra.data.StringFormat
import org.acra.ktx.initAcra
import java.time.LocalDate
import javax.inject.Inject

@HiltAndroidApp
class LibreSudokuApp : Application(), Configuration.Provider {
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)

if (!BuildConfig.DEBUG) {
// Only user can send a crash report
initAcra {
buildConfigClass = BuildConfig::class.java
reportFormat = StringFormat.KEY_VALUE_LIST

dialog {
title = getString(R.string.app_name)
text = getString(R.string.dialog_crash_report_text)
negativeButtonText = getString(R.string.dialog_no)
positiveButtonText = getString(R.string.dialog_yes)
resTheme = android.R.style.Theme_DeviceDefault_Dialog
}

mailSender {
mailTo = "[email protected]"
reportFileName = "Report_${LocalDate.now()}_v${BuildConfig.VERSION_NAME}.txt"
subject = "LibreSudoku crash report"
reportAsFile = true
}
sharedPreferencesName = ACRA_SHARED_PREFS_NAME
}
}
}

@Inject
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/kaajjo/libresudoku/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navOptions
import com.kaajjo.libresudoku.core.PreferencesConstants
import com.kaajjo.libresudoku.core.utils.GlobalExceptionHandler
import com.kaajjo.libresudoku.data.datastore.AppSettingsManager
import com.kaajjo.libresudoku.data.datastore.ThemeSettingsManager
import com.kaajjo.libresudoku.destinations.HomeScreenDestination
import com.kaajjo.libresudoku.destinations.ImportFromFileScreenDestination
import com.kaajjo.libresudoku.destinations.MoreScreenDestination
import com.kaajjo.libresudoku.destinations.StatisticsScreenDestination
import com.kaajjo.libresudoku.destinations.WelcomeScreenDestination
import com.kaajjo.libresudoku.ui.app_crash.CrashActivity
import com.kaajjo.libresudoku.ui.components.navigation_bar.NavigationBarComponent
import com.kaajjo.libresudoku.ui.theme.BoardColors
import com.kaajjo.libresudoku.ui.theme.LibreSudokuTheme
Expand All @@ -60,6 +62,9 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WindowCompat.setDecorFitsSystemWindows(window, false)
if (!BuildConfig.DEBUG) {
GlobalExceptionHandler.initialize(applicationContext, CrashActivity::class.java)
}

setContent {
val mainViewModel: MainActivityViewModel = hiltViewModel()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/kaajjo/libresudoku/core/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kaajjo.libresudoku.core

const val GITHUB_REPOSITORY = "https://github.com/kaajjo/Libre-Sudoku"
const val GITHUB_NEW_ISSUE = "https://github.com/kaajjo/Libre-Sudoku/issues/new"
const val WEBLATE_ENGAGE = "https://hosted.weblate.org/engage/libresudoku/"
const val TELEGRAM_CHANNEL = "https://t.me/libresudoku"
const val CRYPTO_BTC = "bc1qhpx222kmk5v6tqrra524fezwjqr0lhjq77ee8q"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.kaajjo.libresudoku.core.utils

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.util.Log
import kotlin.system.exitProcess

class GlobalExceptionHandler<T : Activity> private constructor(
private val applicationContext: Context,
private val defaultHandler: Thread.UncaughtExceptionHandler,
private val activityToBeLaunched: Class<T>
) : Thread.UncaughtExceptionHandler {

override fun uncaughtException(p0: Thread, p1: Throwable) {
kotlin.runCatching {
Log.e(this.toString(), p1.stackTraceToString())
applicationContext.launchActivity(activityToBeLaunched, p1)
exitProcess(0)
}.getOrElse {
defaultHandler.uncaughtException(p0, p1)
}
}

private fun <T : Activity> Context.launchActivity(
activity: Class<T>,
exception: Throwable
) {
val crashedIntent = Intent(applicationContext, activity).apply {
putExtra(INTENT_DATA_NAME, "$exception\n${Log.getStackTraceString(exception)}")
addFlags(defFlags)
}
applicationContext.startActivity(crashedIntent)
}

companion object {
private const val INTENT_DATA_NAME = "GlobalExceptionHandler"
private const val defFlags = Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK

fun <T : Activity> initialize(
applicationContext: Context,
activityToBeLaunched: Class<T>
) = Thread.setDefaultUncaughtExceptionHandler(
GlobalExceptionHandler(
applicationContext,
Thread.getDefaultUncaughtExceptionHandler() as Thread.UncaughtExceptionHandler,
activityToBeLaunched
)
)

fun Activity.getExceptionString(): String = intent.getStringExtra(INTENT_DATA_NAME) ?: ""
}
}

This file was deleted.

6 changes: 0 additions & 6 deletions app/src/main/java/com/kaajjo/libresudoku/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.kaajjo.libresudoku.di

import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import com.kaajjo.libresudoku.data.database.AppDatabase
import com.kaajjo.libresudoku.data.database.dao.BoardDao
import com.kaajjo.libresudoku.data.database.dao.FolderDao
Expand Down Expand Up @@ -37,11 +36,6 @@ class AppModule {
fun provideDatabaseRepository(appDatabase: AppDatabase): DatabaseRepository
= DatabaseRepositoryImpl(appDatabase)

@Provides
@Singleton
fun provideAcraSharedPrefs(@ApplicationContext context: Context): SharedPreferences
= context.getSharedPreferences(ACRA_SHARED_PREFS_NAME, Context.MODE_PRIVATE)

@Provides
@Singleton
fun provideFolderRepository(folderDao: FolderDao): FolderRepository
Expand Down
Loading

0 comments on commit 670da01

Please sign in to comment.