From 307e7642b93b77fa3e191949fcd85452e5b59bac Mon Sep 17 00:00:00 2001 From: William Brawner Date: Thu, 22 Aug 2024 16:59:04 -0600 Subject: [PATCH] Fix StrictMode disk access violations --- .../wbrawner/simplemarkdown/MarkdownApplication.kt | 4 +++- .../wbrawner/simplemarkdown/utility/FileHelper.kt | 7 ++++--- .../simplemarkdown/utility/PreferenceHelper.kt | 13 ++++++++++--- core/build.gradle.kts | 1 + .../simplemarkdown/core/ErrorReporterTree.kt | 9 +++++---- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownApplication.kt b/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownApplication.kt index b4f3b9b..2a550e6 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownApplication.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/MarkdownApplication.kt @@ -38,7 +38,9 @@ class MarkdownApplication : Application() { } } } - Timber.plant(ErrorReporterTree.create(this)) + coroutineScope.launch { + Timber.plant(ErrorReporterTree.create(this@MarkdownApplication)) + } super.onCreate() ReviewHelper.init(this) fileHelper = AndroidFileHelper(this) diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/utility/FileHelper.kt b/app/src/main/java/com/wbrawner/simplemarkdown/utility/FileHelper.kt index e74879e..b51fbd6 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/utility/FileHelper.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/utility/FileHelper.kt @@ -3,7 +3,6 @@ package com.wbrawner.simplemarkdown.utility import android.content.Context import android.content.Intent import android.os.Environment -import android.provider.MediaStore import androidx.core.net.toUri import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext @@ -34,8 +33,10 @@ interface FileHelper { } class AndroidFileHelper(private val context: Context) : FileHelper { - override val defaultDirectory: File = context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) - ?: context.filesDir + override val defaultDirectory: File by lazy { + context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) + ?: context.filesDir + } override suspend fun open(source: URI): Pair? = withContext(Dispatchers.IO) { val uri = source.toString().toUri() diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/utility/PreferenceHelper.kt b/app/src/main/java/com/wbrawner/simplemarkdown/utility/PreferenceHelper.kt index d26a3a5..633d59f 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/utility/PreferenceHelper.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/utility/PreferenceHelper.kt @@ -19,10 +19,17 @@ interface PreferenceHelper { } class AndroidPreferenceHelper(context: Context, private val coroutineScope: CoroutineScope = CoroutineScope(Dispatchers.IO)): PreferenceHelper { - private val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context) - private val states = Preference.entries.associateWith { MutableStateFlow(get(it)) } + private val sharedPreferences by lazy { + PreferenceManager.getDefaultSharedPreferences(context) + } + private val states by lazy { + val allPrefs: Map = sharedPreferences.all + Preference.entries.associateWith { preference -> + MutableStateFlow(allPrefs[preference.key] ?: preference.default) + } + } - override fun get(preference: Preference): Any? = sharedPreferences.all[preference.key]?: preference.default + override fun get(preference: Preference): Any? = states[preference]?.value override fun set(preference: Preference, value: Any?) { sharedPreferences.edit { diff --git a/core/build.gradle.kts b/core/build.gradle.kts index fbcd679..a50b5a2 100644 --- a/core/build.gradle.kts +++ b/core/build.gradle.kts @@ -54,6 +54,7 @@ android { } dependencies { + implementation(libs.kotlinx.coroutines.core) implementation(libs.acra.core) implementation(libs.acra.http) runtimeOnly(libs.acra.limiter) diff --git a/core/src/main/java/com/wbrawner/simplemarkdown/core/ErrorReporterTree.kt b/core/src/main/java/com/wbrawner/simplemarkdown/core/ErrorReporterTree.kt index 10a7c7f..7a12067 100644 --- a/core/src/main/java/com/wbrawner/simplemarkdown/core/ErrorReporterTree.kt +++ b/core/src/main/java/com/wbrawner/simplemarkdown/core/ErrorReporterTree.kt @@ -2,7 +2,8 @@ package com.wbrawner.simplemarkdown.core import android.app.Application import android.util.Log -import org.acra.ACRA +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import org.acra.config.httpSender import org.acra.data.StringFormat import org.acra.ktx.initAcra @@ -17,18 +18,18 @@ class ErrorReporterTree private constructor(): Timber.Tree() { } companion object { - fun create(application: Application): ErrorReporterTree { + suspend fun create(application: Application): ErrorReporterTree { application.createErrorReporterTree() return ErrorReporterTree() } } } -private fun Application.createErrorReporterTree() { +private suspend fun Application.createErrorReporterTree() = withContext(Dispatchers.IO) { initAcra { reportFormat = StringFormat.JSON httpSender { - uri = "${BuildConfig.ACRA_URL}/report" /*best guess, you may need to adjust this*/ + uri = "${BuildConfig.ACRA_URL}/report" basicAuthLogin = BuildConfig.ACRA_USER basicAuthPassword = BuildConfig.ACRA_PASS httpMethod = HttpSender.Method.POST