From 0b69bf735e7a2bff5734134e62f0c11dc3a5f1cf Mon Sep 17 00:00:00 2001
From: kr328 <kr328app@outlook.com>
Date: Sun, 16 May 2021 18:15:36 +0800
Subject: [PATCH] Improve: remove tracking for flavor 'foss'

---
 app/build.gradle.kts                          | 33 ++++++++-----------
 .../java/com/github/kr328/clash/Tracker.kt    | 14 ++++++++
 .../github/kr328/clash/AppCrashedActivity.kt  | 13 +-------
 .../com/github/kr328/clash/MainApplication.kt | 11 +------
 .../java/com/github/kr328/clash/Tracker.kt    | 31 +++++++++++++++++
 5 files changed, 60 insertions(+), 42 deletions(-)
 create mode 100644 app/src/foss/java/com/github/kr328/clash/Tracker.kt
 create mode 100644 app/src/premium/java/com/github/kr328/clash/Tracker.kt

diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index a470243a..ce41c635 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -45,6 +45,14 @@ android {
         create("premium") {
             dimension = "premium"
             versionNameSuffix = ".premium"
+
+            val appCenterKey = rootProject.file("local.properties").inputStream()
+                .use { Properties().apply { load(it) } }
+                .getProperty("appcenter.key", null)
+
+            Objects.requireNonNull(appCenterKey)
+
+            buildConfigField("String", "APP_CENTER_KEY", "\"$appCenterKey\"")
         }
     }
 
@@ -89,32 +97,19 @@ android {
             isUniversalApk = true
         }
     }
-
-    buildTypes.apply {
-        val properties = Properties().apply {
-            rootProject.file("local.properties").inputStream().use {
-                load(it)
-            }
-        }
-
-        val key = properties.getProperty("appcenter.key", null)
-
-        forEach {
-            if (it.name == "debug" || key == null) {
-                it.buildConfigField("String", "APP_CENTER_KEY", "null")
-            } else {
-                it.buildConfigField("String", "APP_CENTER_KEY", "\"$key\"")
-            }
-        }
-    }
 }
 
 dependencies {
+    val premiumImplementation by configurations
+
     api(project(":core"))
     api(project(":service"))
     api(project(":design"))
     api(project(":common"))
 
+    premiumImplementation("com.microsoft.appcenter:appcenter-analytics:$appcenterVersion")
+    premiumImplementation("com.microsoft.appcenter:appcenter-crashes:$appcenterVersion")
+
     implementation(kotlin("stdlib-jdk7"))
     implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
     implementation("androidx.core:core-ktx:$ktxVersion")
@@ -123,8 +118,6 @@ dependencies {
     implementation("androidx.coordinatorlayout:coordinatorlayout:$coordinatorlayoutVersion")
     implementation("androidx.recyclerview:recyclerview:$recyclerviewVersion")
     implementation("androidx.fragment:fragment:$fragmentVersion")
-    implementation("com.microsoft.appcenter:appcenter-analytics:$appcenterVersion")
-    implementation("com.microsoft.appcenter:appcenter-crashes:$appcenterVersion")
     implementation("com.google.android.material:material:$materialVersion")
 }
 
diff --git a/app/src/foss/java/com/github/kr328/clash/Tracker.kt b/app/src/foss/java/com/github/kr328/clash/Tracker.kt
new file mode 100644
index 00000000..67dc8cfb
--- /dev/null
+++ b/app/src/foss/java/com/github/kr328/clash/Tracker.kt
@@ -0,0 +1,14 @@
+package com.github.kr328.clash
+
+import android.app.Application
+
+@Suppress("UNUSED_PARAMETER")
+object Tracker {
+    fun initialize(application: Application) {
+        // do nothing
+    }
+
+    fun uploadLogcat(logcat: String) {
+        // do nothing
+    }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/github/kr328/clash/AppCrashedActivity.kt b/app/src/main/java/com/github/kr328/clash/AppCrashedActivity.kt
index b94d2858..374c6fa9 100644
--- a/app/src/main/java/com/github/kr328/clash/AppCrashedActivity.kt
+++ b/app/src/main/java/com/github/kr328/clash/AppCrashedActivity.kt
@@ -1,12 +1,9 @@
 package com.github.kr328.clash
 
-import android.os.DeadObjectException
 import com.github.kr328.clash.common.compat.versionCodeCompat
 import com.github.kr328.clash.common.log.Log
 import com.github.kr328.clash.design.AppCrashedDesign
 import com.github.kr328.clash.log.SystemLogcat
-import com.microsoft.appcenter.crashes.Crashes
-import com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.isActive
 import kotlinx.coroutines.withContext
@@ -27,15 +24,7 @@ class AppCrashedActivity : BaseActivity<AppCrashedDesign>() {
             SystemLogcat.dumpCrash()
         }
 
-        if (BuildConfig.APP_CENTER_KEY != null && !BuildConfig.DEBUG) {
-            if (logs.isNotBlank()) {
-                Crashes.trackError(
-                    DeadObjectException(),
-                    mapOf("type" to "app_crashed"),
-                    listOf(ErrorAttachmentLog.attachmentWithText(logs, "logcat.txt"))
-                )
-            }
-        }
+        Tracker.uploadLogcat(logs)
 
         design.setAppLogs(logs)
 
diff --git a/app/src/main/java/com/github/kr328/clash/MainApplication.kt b/app/src/main/java/com/github/kr328/clash/MainApplication.kt
index 63c07893..4393905d 100644
--- a/app/src/main/java/com/github/kr328/clash/MainApplication.kt
+++ b/app/src/main/java/com/github/kr328/clash/MainApplication.kt
@@ -7,9 +7,6 @@ import com.github.kr328.clash.common.compat.currentProcessName
 import com.github.kr328.clash.common.log.Log
 import com.github.kr328.clash.remote.Remote
 import com.github.kr328.clash.service.util.sendServiceRecreated
-import com.microsoft.appcenter.AppCenter
-import com.microsoft.appcenter.analytics.Analytics
-import com.microsoft.appcenter.crashes.Crashes
 
 @Suppress("unused")
 class MainApplication : Application() {
@@ -23,13 +20,7 @@ class MainApplication : Application() {
         super.onCreate()
 
         // Initialize AppCenter
-        if (BuildConfig.APP_CENTER_KEY != null && !BuildConfig.DEBUG) {
-            AppCenter.start(
-                this,
-                BuildConfig.APP_CENTER_KEY,
-                Analytics::class.java, Crashes::class.java
-            )
-        }
+        Tracker.initialize(this)
 
         val processName = currentProcessName
 
diff --git a/app/src/premium/java/com/github/kr328/clash/Tracker.kt b/app/src/premium/java/com/github/kr328/clash/Tracker.kt
new file mode 100644
index 00000000..50428544
--- /dev/null
+++ b/app/src/premium/java/com/github/kr328/clash/Tracker.kt
@@ -0,0 +1,31 @@
+package com.github.kr328.clash
+
+import android.app.Application
+import com.microsoft.appcenter.AppCenter
+import com.microsoft.appcenter.analytics.Analytics
+import com.microsoft.appcenter.crashes.Crashes
+import com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog
+
+object Tracker {
+    fun initialize(application: Application) {
+        if (BuildConfig.APP_CENTER_KEY != null && !BuildConfig.DEBUG) {
+            AppCenter.start(
+                application,
+                BuildConfig.APP_CENTER_KEY,
+                Analytics::class.java, Crashes::class.java
+            )
+        }
+    }
+
+    fun uploadLogcat(logcat: String) {
+        if (BuildConfig.APP_CENTER_KEY != null && !BuildConfig.DEBUG) {
+            if (logcat.isNotBlank()) {
+                Crashes.trackError(
+                    RuntimeException(),
+                    mapOf("type" to "app_crashed"),
+                    listOf(ErrorAttachmentLog.attachmentWithText(logcat, "logcat.txt"))
+                )
+            }
+        }
+    }
+}
\ No newline at end of file
-- 
GitLab