HapeLee 1 год назад
Родитель
Сommit
8159187dc8

+ 6 - 0
app/src/main/AndroidManifest.xml

@@ -550,6 +550,12 @@
                 tools:node="remove" />
         </provider>
 
+        <provider
+            android:name="com.google.firebase.provider.FirebaseInitProvider"
+            android:authorities="${applicationId}.firebaseinitprovider"
+            android:exported="false"
+            tools:node="remove" />
+
         <meta-data
             android:name="channel"
             android:value="${APP_CHANNEL_VALUE}" />

+ 2 - 4
app/src/main/java/io/legato/kazusa/App.kt

@@ -5,8 +5,6 @@ import android.app.Notification
 import android.app.NotificationChannel
 import android.app.NotificationManager
 import android.content.Context
-import android.content.SharedPreferences
-import android.content.pm.ActivityInfo
 import android.content.pm.ApplicationInfo
 import android.content.res.Configuration
 import android.os.Build
@@ -17,7 +15,6 @@ import com.jeremyliao.liveeventbus.logger.DefaultLogger
 import com.script.rhino.ReadOnlyJavaObject
 import com.script.rhino.RhinoScriptEngine
 import com.script.rhino.RhinoWrapFactory
-import io.legato.kazusa.base.AppContextWrapper
 import io.legato.kazusa.constant.AppConst.channelIdDownload
 import io.legato.kazusa.constant.AppConst.channelIdReadAloud
 import io.legato.kazusa.constant.AppConst.channelIdWeb
@@ -40,7 +37,6 @@ import io.legato.kazusa.help.LifecycleHelp
 import io.legato.kazusa.help.RuleBigDataHelp
 import io.legato.kazusa.help.book.BookHelp
 import io.legato.kazusa.help.config.AppConfig
-import io.legato.kazusa.help.config.ThemeConfig.applyDayNight
 import io.legato.kazusa.help.config.ThemeConfig.applyDayNightInit
 import io.legato.kazusa.help.coroutine.Coroutine
 import io.legato.kazusa.help.http.Cronet
@@ -51,6 +47,7 @@ import io.legato.kazusa.help.source.SourceHelp
 import io.legato.kazusa.help.storage.Backup
 import io.legato.kazusa.model.BookCover
 import io.legato.kazusa.utils.ChineseUtils
+import io.legato.kazusa.utils.FirebaseManager
 import io.legato.kazusa.utils.LogUtils
 import io.legato.kazusa.utils.defaultSharedPreferences
 import io.legato.kazusa.utils.getPrefBoolean
@@ -70,6 +67,7 @@ class App : Application() {
     override fun onCreate() {
         super.onCreate()
         DynamicColors.applyToActivitiesIfAvailable(this)
+        FirebaseManager.initFromPreferences(this)
         CrashHandler(this)
         if (isDebuggable) {
             ThreadUtils.setThreadAssertsDisabledForTesting(true)

+ 1 - 0
app/src/main/java/io/legato/kazusa/constant/PreferKey.kt

@@ -169,6 +169,7 @@ object PreferKey {
     const val readAloudByMediaButton = "readAloudByMediaButton"
     const val showMangaUi = "showMangaUi"
     const val disableMangaScale = "disableMangaScale"
+    const val firebaseEnabled = "firebaseEnabled"
 
     const val cPrimary = "colorPrimary"
     const val cAccent = "colorAccent"

+ 5 - 0
app/src/main/java/io/legato/kazusa/help/config/AppConfig.kt

@@ -668,6 +668,11 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener {
         }
     }
 
+    var firebaseEnabled: Boolean
+        get() = appCtx.getPrefBoolean(PreferKey.firebaseEnabled, false)
+        set(value) {
+            appCtx.putPrefBoolean(PreferKey.firebaseEnabled, value)
+        }
 
     //跳转到漫画界面不使用富文本模式
     val showMangaUi: Boolean

+ 8 - 0
app/src/main/java/io/legato/kazusa/ui/config/OtherConfigFragment.kt

@@ -27,6 +27,7 @@ import io.legato.kazusa.receiver.SharedReceiverActivity
 import io.legato.kazusa.service.WebService
 import io.legato.kazusa.ui.file.HandleFileContract
 import io.legato.kazusa.ui.widget.number.NumberPickerDialog
+import io.legato.kazusa.utils.FirebaseManager
 import io.legato.kazusa.utils.LogUtils
 import io.legato.kazusa.utils.getPrefBoolean
 import io.legato.kazusa.utils.postEvent
@@ -218,6 +219,13 @@ class OtherConfigFragment : PreferenceFragment(),
             PreferKey.sourceEditMaxLine -> {
                 upPreferenceSummary(key, AppConfig.sourceEditMaxLine.toString())
             }
+
+            PreferKey.firebaseEnabled -> {
+                sharedPreferences?.let {
+                    FirebaseManager.setEnabled(requireContext(), it.getBoolean(key, true))
+                }
+            }
+
         }
     }
 

+ 45 - 0
app/src/main/java/io/legato/kazusa/utils/FirebaseManager.kt

@@ -0,0 +1,45 @@
+package io.legato.kazusa.utils
+
+import android.content.Context
+import androidx.core.content.edit
+import androidx.preference.PreferenceManager
+import com.google.firebase.FirebaseApp
+import com.google.firebase.analytics.FirebaseAnalytics
+
+object FirebaseManager {
+
+    private const val PREF_KEY = "firebaseEnabled"
+    private var isEnabled = false
+
+    fun initFromPreferences(context: Context) {
+        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
+        val enabled = prefs.getBoolean(PREF_KEY, true)
+        applyState(context, enabled)
+    }
+
+    fun setEnabled(context: Context, enabled: Boolean) {
+        val prefs = PreferenceManager.getDefaultSharedPreferences(context)
+        prefs.edit { putBoolean(PREF_KEY, enabled) }
+        applyState(context, enabled)
+    }
+
+    private fun applyState(context: Context, enabled: Boolean) {
+        if (enabled) {
+            if (FirebaseApp.getApps(context).isEmpty()) {
+                FirebaseApp.initializeApp(context)
+            }
+            FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(true)
+        } else {
+            try {
+                if (FirebaseApp.getApps(context).isNotEmpty()) {
+                    FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(false)
+                    FirebaseApp.getInstance().delete()
+                }
+            } catch (_: Exception) {
+            }
+        }
+        isEnabled = enabled
+    }
+
+    fun isFirebaseEnabled(): Boolean = isEnabled
+}

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

@@ -1236,5 +1236,6 @@
     <string name="descending_order">降序</string>
     <string name="checking_update">检查更新中...</string>
     <string name="search_empty">没有内容哦</string>
+    <string name="firebase_enable_title">匿名统计</string>
 
 </resources>

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

@@ -1239,5 +1239,7 @@
     <string name="descending_order">降序</string>
     <string name="checking_update">检查更新中...</string>
     <string name="search_empty">没有内容哦</string>
+    <string name="firebase_enable_title">匿名统计</string>
+    <string name="firebase_enable_summary">允许应用使用Google Firebase进行崩溃和性能统计</string>
 
 </resources>

+ 6 - 0
app/src/main/res/xml/pref_config_other.xml

@@ -66,6 +66,12 @@
         app:iconSpaceReserved="false"
         app:layout="@layout/view_preference_category">
 
+        <io.legato.kazusa.lib.prefs.SwitchPreference
+            android:defaultValue="true"
+            android:key="firebaseEnable"
+            android:summary="@string/firebase_enable_summary"
+            android:title="@string/firebase_enable_title" />
+
         <io.legato.kazusa.lib.prefs.Preference
             android:key="localPassword"
             android:summary="@string/set_local_password_summary"