Skip to content

Commit 6e8e80e

Browse files
committed
Fix assistant store fallback in R8 optimization
1 parent e1e649d commit 6e8e80e

2 files changed

Lines changed: 27 additions & 39 deletions

File tree

‎app/proguard-rules.pro‎

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
# Add project specific ProGuard rules here.
2-
# You can control the set of applied configuration files using the
3-
# proguardFiles setting in build.gradle.
4-
#
5-
# For more details, see
6-
# http://developer.android.com/guide/developing/tools/proguard.html
1+
# Preserve the assistant maps during this diagnostic test.
2+
-keep class com.wstxda.switchai.utils.AssistantsMap {
3+
*;
4+
}
75

8-
# If your project uses WebView with JS, uncomment the following
9-
# and specify the fully qualified class name to the JavaScript interface
10-
# class:
11-
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12-
# public *;
13-
#}
6+
# Preserve assistant companions used by the reflection fallback.
7+
-keep class com.wstxda.switchai.assistant.**$Companion {
8+
*;
9+
}
1410

15-
# Uncomment this to preserve the line number information for
16-
# debugging stack traces.
17-
#-keepattributes SourceFile,LineNumberTable
18-
19-
# If you keep the line number information, uncomment this to
20-
# hide the original source file name.
21-
#-renamesourcefileattribute SourceFile
11+
# Preserve the fields exposing companion instances.
12+
-keepclassmembers class com.wstxda.switchai.assistant.** {
13+
public static final ** Companion;
14+
}

‎app/src/main/java/com/wstxda/switchai/logic/AssistantLauncher.kt‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.wstxda.switchai.logic
22

3-
import android.content.ActivityNotFoundException
43
import android.content.Context
54
import android.content.Intent
65
import android.widget.Toast
@@ -30,16 +29,20 @@ fun Context.openAssistant(
3029
}
3130
}
3231

33-
val pkg = this::class.companionObjectInstance.let { it as? AssistantProperties }?.packageName
32+
val pkg = intents.firstNotNullOfOrNull { intent ->
33+
(intent.component?.packageName ?: intent.`package`)?.takeIf { it.isNotBlank() }
34+
} ?: runCatching {
35+
(this::class.companionObjectInstance as? AssistantProperties)?.packageName?.takeIf { it.isNotBlank() }
36+
}.getOrNull()
3437

35-
val handled = pkg?.takeIf { it.isNotEmpty() }?.let { openOnStore(it) } ?: false
38+
val handled = pkg?.let { openOnStore(it) } ?: false
3639

3740
showToast(errorMessage)
3841
return handled
3942
}
4043

4144
fun Context.openAssistant(intent: Intent): Boolean = runCatching {
42-
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
45+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
4346
startActivity(intent)
4447
true
4548
}.getOrElse { false }
@@ -91,27 +94,19 @@ fun Context.openAssistantShizuku(
9194
}
9295

9396
fun Context.openOnStore(packageName: String): Boolean {
97+
if (packageName.isBlank()) return false
98+
9499
val marketIntent = Intent(
95100
Intent.ACTION_VIEW, "market://details?id=$packageName".toUri()
96-
).apply {
97-
flags = Intent.FLAG_ACTIVITY_NEW_TASK
98-
}
101+
)
99102

100103
val webSearchIntent = Intent(
101-
Intent.ACTION_VIEW, "https://www.google.com/search?q=$packageName+android+app".toUri()
102-
).apply {
103-
flags = Intent.FLAG_ACTIVITY_NEW_TASK
104-
}
104+
Intent.ACTION_VIEW,
105+
"https://www.google.com/search".toUri().buildUpon()
106+
.appendQueryParameter("q", "$packageName android app").build()
107+
)
105108

106-
return try {
107-
startActivity(marketIntent)
108-
true
109-
} catch (_: ActivityNotFoundException) {
110-
runCatching {
111-
startActivity(webSearchIntent)
112-
true
113-
}.getOrElse { false }
114-
}
109+
return openAssistant(marketIntent) || openAssistant(webSearchIntent)
115110
}
116111

117112
fun Context.showToast(message: Int) {

0 commit comments

Comments
 (0)