Skip to content

Commit

Permalink
#128 setup custom activity on crash
Browse files Browse the repository at this point in the history
  • Loading branch information
javadjafari1 committed Jan 11, 2024
1 parent 5ea2174 commit ec28c32
Show file tree
Hide file tree
Showing 9 changed files with 823 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ dependencies {
debugImplementation(libs.composeUiManifest)

implementation(libs.appMetrica)
implementation(libs.caoc)
}

kover {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
android:theme="@style/Theme.App.Starting"
android:usesCleartextTraffic="true"
tools:targetApi="tiramisu">
<activity
android:name=".main.CrashActivity"
android:exported="false"
android:process=":error_activity"
android:theme="@style/Theme.Backgroundable" />
<activity
android:name=".main.MainActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ir.thatsmejavad.backgroundable

import android.app.Application
import cat.ereza.customactivityoncrash.config.CaocConfig
import coil.Coil
import coil.ImageLoader
import com.yandex.metrica.YandexMetrica
Expand All @@ -9,6 +10,7 @@ import ir.thatsmejavad.backgroundable.core.Constants.REQUEST_TIMEOUT_IN_SECONDS
import ir.thatsmejavad.backgroundable.di.components.AppComponent
import ir.thatsmejavad.backgroundable.di.components.DaggerAppComponent
import ir.thatsmejavad.backgroundable.di.modules.ApplicationModule
import ir.thatsmejavad.backgroundable.main.CrashActivity
import okhttp3.Dispatcher
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit
Expand All @@ -20,6 +22,7 @@ class BackgroundableApplication : Application() {

override fun onCreate() {
super.onCreate()
setupCAOC()
setupCoil()
setupAppMetrica()
appComponent = DaggerAppComponent.builder()
Expand Down Expand Up @@ -52,4 +55,13 @@ class BackgroundableApplication : Application() {
YandexMetrica.activate(applicationContext, config)
YandexMetrica.enableActivityAutoTracking(this)
}

private fun setupCAOC() {
CaocConfig.Builder.create()
.backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT)
.enabled(!BuildConfig.DEBUG)
.logErrorOnRestart(false)
.errorActivity(CrashActivity::class.java)
.apply()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ir.thatsmejavad.backgroundable.main

import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import ir.thatsmejavad.backgroundable.R
import ir.thatsmejavad.backgroundable.core.sealeds.ThemeColor
import ir.thatsmejavad.backgroundable.ui.BackgroundableTheme

class CrashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val systemInDarkTheme = isSystemInDarkTheme()
BackgroundableTheme(
themeColor = ThemeColor.Skobeloff,
darkTheme = systemInDarkTheme
) {
LaunchedEffect(systemInDarkTheme) {
setSystemBarsColor(systemInDarkTheme)
}
Box(
Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
.navigationBarsPadding()
.statusBarsPadding()
) {
Column(
modifier = Modifier
.align(Alignment.Center)
.padding(24.dp),
verticalArrangement = Arrangement.spacedBy(24.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(R.drawable.ic_crash_image),
contentDescription = "crash image"
)

Text(
text = stringResource(R.string.label_crash_message),
style = MaterialTheme.typography.headlineSmall,
color = MaterialTheme.colorScheme.onBackground,
)

Button(
modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = {
finish()
Intent(this@CrashActivity, MainActivity::class.java)
.apply {
startActivity(this)
}
},
) {
Text(text = stringResource(R.string.label_ok))
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import androidx.compose.material3.NavigationBarItemDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -107,26 +107,8 @@ class MainActivity : AppCompatActivity() {
Theme.FollowSystem -> isSystemInDarkTheme()
Theme.LightTheme -> false
}
DisposableEffect(isDark) {
enableEdgeToEdge(
statusBarStyle = if (isDark) {
SystemBarStyle.dark(Color.Transparent.toArgb())
} else {
SystemBarStyle.light(
scrim = Color.Transparent.toArgb(),
darkScrim = Color.Black.copy(alpha = 0.3f).toArgb()
)
},
navigationBarStyle = if (isDark) {
SystemBarStyle.dark(Color.Transparent.toArgb())
} else {
SystemBarStyle.light(
Color.Transparent.toArgb(),
Color.Black.copy(alpha = 0.3f).toArgb()
)
}
)
onDispose {}
LaunchedEffect(isDark) {
setSystemBarsColor(isDark)
}
BackgroundableApp()
}
Expand Down Expand Up @@ -242,3 +224,24 @@ private fun BackgroundableNavigationBar(
}
}
}

internal fun AppCompatActivity.setSystemBarsColor(isDark: Boolean) {
enableEdgeToEdge(
statusBarStyle = if (isDark) {
SystemBarStyle.dark(Color.Transparent.toArgb())
} else {
SystemBarStyle.light(
scrim = Color.Transparent.toArgb(),
darkScrim = Color.Black.copy(alpha = 0.3f).toArgb()
)
},
navigationBarStyle = if (isDark) {
SystemBarStyle.dark(Color.Transparent.toArgb())
} else {
SystemBarStyle.light(
Color.Transparent.toArgb(),
Color.Black.copy(alpha = 0.3f).toArgb()
)
}
)
}
Loading

0 comments on commit ec28c32

Please sign in to comment.