浏览代码

Added option for enabling crash reporting

Vadik Sirekanyan 5 年之前
父节点
当前提交
c7cb7bb053

+ 3 - 1
app/src/main/java/com/sirekanyan/knigopis/App.kt

@@ -24,7 +24,9 @@ class App : Application() {
 
     override fun attachBaseContext(base: Context?) {
         super.attachBaseContext(base)
-        initCrashReporting()
+        if (config.crashReportEnabled) {
+            initCrashReporting()
+        }
     }
 
     private fun initCrashReporting() {

+ 9 - 0
app/src/main/java/com/sirekanyan/knigopis/feature/MainPresenter.kt

@@ -11,6 +11,7 @@ import com.sirekanyan.knigopis.repository.AuthRepository
 import com.sirekanyan.knigopis.repository.Configuration
 import com.sirekanyan.knigopis.repository.Sorting
 import com.sirekanyan.knigopis.repository.Theme
+import org.acra.ACRA
 
 interface MainPresenter : Presenter {
 
@@ -49,6 +50,7 @@ class MainPresenterImpl(
     override fun init(tab: CurrentTab?) {
         view.setSortOptionChecked(config.sorting)
         view.setThemeOptionChecked(Theme.getCurrent())
+        view.setCrashReportOptionChecked(config.crashReportEnabled)
         val defaultTab = if (auth.isAuthorized()) BOOKS_TAB else NOTES_TAB
         this.currentTab = tab ?: defaultTab
     }
@@ -135,6 +137,13 @@ class MainPresenterImpl(
         view.showAboutDialog()
     }
 
+    override fun onCrashReportOptionClicked(isChecked: Boolean) {
+        config.crashReportEnabled = isChecked
+        if (!isChecked) {
+            ACRA.getErrorReporter().handleException(Exception("Crash reporting was disabled"))
+        }
+    }
+
     override fun onThemeOptionClicked(theme: Theme) {
         config.theme = theme
         theme.setup()

+ 11 - 0
app/src/main/java/com/sirekanyan/knigopis/feature/MainView.kt

@@ -34,6 +34,7 @@ interface MainView {
     fun showProfileOption(isVisible: Boolean)
     fun setSortOptionChecked(sorting: Sorting)
     fun setThemeOptionChecked(theme: Theme)
+    fun setCrashReportOptionChecked(isChecked: Boolean)
 
     interface Callbacks {
         fun onNavigationClicked(itemId: Int)
@@ -42,6 +43,7 @@ interface MainView {
         fun onAboutOptionClicked()
         fun onSortOptionClicked(sorting: Sorting)
         fun onThemeOptionClicked(theme: Theme)
+        fun onCrashReportOptionClicked(isChecked: Boolean)
     }
 
 }
@@ -83,6 +85,11 @@ class MainViewImpl(
                     callbacks.onThemeOptionClicked(Theme.getById(item.itemId))
                     true
                 }
+                R.id.option_crash_report -> {
+                    item.isChecked = !item.isChecked
+                    callbacks.onCrashReportOptionClicked(item.isChecked)
+                    true
+                }
                 R.id.debug_option_clear_cache -> {
                     context.cacheDir.deleteRecursively()
                     context.getSharedPreferences(COMMON_PREFS_NAME, MODE_PRIVATE)
@@ -157,4 +164,8 @@ class MainViewImpl(
         toolbar.menu.findItem(theme.id).isChecked = true
     }
 
+    override fun setCrashReportOptionChecked(isChecked: Boolean) {
+        toolbar.menu.findItem(R.id.option_crash_report).isChecked = isChecked
+    }
+
 }

+ 3 - 0
app/src/main/java/com/sirekanyan/knigopis/repository/Configuration.kt

@@ -2,6 +2,7 @@ package com.sirekanyan.knigopis.repository
 
 import android.app.Application
 import android.content.Context.MODE_PRIVATE
+import com.sirekanyan.knigopis.repository.config.booleanPreference
 import com.sirekanyan.knigopis.repository.config.enumPreference
 
 private const val PREFS_NAME = "config"
@@ -9,10 +10,12 @@ private const val PREFS_NAME = "config"
 interface Configuration {
     var theme: Theme
     var sorting: Sorting
+    var crashReportEnabled: Boolean
 }
 
 class ConfigurationImpl(context: Application) : Configuration {
     internal val prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
     override var theme by enumPreference(Theme.DEFAULT)
     override var sorting by enumPreference(Sorting.DEFAULT)
+    override var crashReportEnabled by booleanPreference()
 }

+ 0 - 1
app/src/main/java/com/sirekanyan/knigopis/repository/config/PropertyDelegate.kt

@@ -23,7 +23,6 @@ class PreferenceDelegate<T>(
 fun intPreference(): PreferenceDelegate<Int> =
     PreferenceDelegate({ key -> getInt(key, 0) }, { key, value -> putInt(key, value) })
 
-@Suppress("Unused")
 fun booleanPreference(): PreferenceDelegate<Boolean> =
     PreferenceDelegate({ key -> getBoolean(key, false) }, { key, value -> putBoolean(key, value) })
 

+ 6 - 0
app/src/main/res/menu/options.xml

@@ -58,6 +58,12 @@
         android:title="@string/main.option.about"
         app:showAsAction="never" />
 
+    <item
+        android:id="@+id/option_crash_report"
+        android:checkable="true"
+        android:title="@string/main.option.crash_report"
+        app:showAsAction="never" />
+
     <!-- for debug only -->
     <item
         android:id="@+id/debug_option_clear_cache"

+ 1 - 0
app/src/main/res/values-ru/strings.xml

@@ -31,6 +31,7 @@
     <string name="main_option_theme_light">Светлая</string>
     <string name="main_option_theme_dark">Тёмная</string>
     <string name="main_option_theme_default">Энергосбережение</string>
+    <string name="main.option.crash_report">Отчеты об ошибках</string>
 
     <!-- login -->
     <string name="login.title">Войти через…</string>

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -30,6 +30,7 @@
     <string name="main_option_theme_light">Light</string>
     <string name="main_option_theme_dark">Dark</string>
     <string name="main_option_theme_default">Set by Battery Saver</string>
+    <string name="main.option.crash_report">Crash reporting</string>
 
     <!-- login -->
     <string name="login.title">Log in with…</string>