Przeglądaj źródła

check clash installed

x1ongzhu 1 rok temu
rodzic
commit
ed860da47f

+ 12 - 0
app/src/main/java/com/example/modifier/Global.kt

@@ -777,4 +777,16 @@ object Global {
         syncTime()
         optimize()
     }
+
+    @JvmStatic
+    fun isClashInstalled(): Boolean {
+        val context = Utils.getContext()
+        val packageManager = context.packageManager
+        return try {
+            packageManager.getPackageInfo("com.github.kr328.clash", 0)
+            true
+        } catch (e: Exception) {
+            false
+        }
+    }
 }

+ 25 - 23
app/src/main/java/com/example/modifier/service/ModifierService.kt

@@ -1089,35 +1089,37 @@ class ModifierService : AccessibilityService(), Emitter.Listener {
                 try {
                     val device = KtorClient.get(DeviceApi.Id(id = Utils.getUniqueID()))
                         .body<DeviceResponse>()
+                    if (Global.isClashInstalled()) {
 
-                    val prefs = getSharedPreferences("settings", Context.MODE_PRIVATE)
-                    if (TextUtils.isEmpty(device.clashProfile)) {
-                        prefs.edit()
-                            .remove("clash_profile")
-                            .apply()
-                        val intent = Intent()
-                        intent.action = "com.github.metacubex.clash.meta.action.STOP_CLASH"
-                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
-                        startActivity(intent)
-                    } else {
-                        val oldProfile = prefs.getString("clash_profile", "")
-                        if (oldProfile != device.clashProfile) {
+                        val prefs = getSharedPreferences("settings", Context.MODE_PRIVATE)
+                        if (TextUtils.isEmpty(device.clashProfile)) {
                             prefs.edit()
-                                .putString("clash_profile", device.clashProfile)
+                                .remove("clash_profile")
                                 .apply()
                             val intent = Intent()
-                            intent.action = "com.github.metacubex.clash.meta.action.USE_PROFILE"
-                            intent.putExtra(
-                                "base64",
-                                Base64.encodeToString(
-                                    device.clashProfile!!.toByteArray(),
-                                    Base64.DEFAULT
-                                )
-                            )
-                            intent.putExtra("name", device.pinCountry)
+                            intent.action = "com.github.metacubex.clash.meta.action.STOP_CLASH"
                             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                             startActivity(intent)
-                            delay(5000)
+                        } else {
+                            val oldProfile = prefs.getString("clash_profile", "")
+                            if (oldProfile != device.clashProfile) {
+                                prefs.edit()
+                                    .putString("clash_profile", device.clashProfile)
+                                    .apply()
+                                val intent = Intent()
+                                intent.action = "com.github.metacubex.clash.meta.action.USE_PROFILE"
+                                intent.putExtra(
+                                    "base64",
+                                    Base64.encodeToString(
+                                        device.clashProfile!!.toByteArray(),
+                                        Base64.DEFAULT
+                                    )
+                                )
+                                intent.putExtra("name", device.pinCountry)
+                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+                                startActivity(intent)
+                                delay(5000)
+                            }
                         }
                     }