|
1 | 1 | package com.wstxda.switchai.logic |
2 | 2 |
|
3 | | -import android.content.ActivityNotFoundException |
4 | 3 | import android.content.Context |
5 | 4 | import android.content.Intent |
6 | 5 | import android.widget.Toast |
@@ -30,16 +29,20 @@ fun Context.openAssistant( |
30 | 29 | } |
31 | 30 | } |
32 | 31 |
|
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() |
34 | 37 |
|
35 | | - val handled = pkg?.takeIf { it.isNotEmpty() }?.let { openOnStore(it) } ?: false |
| 38 | + val handled = pkg?.let { openOnStore(it) } ?: false |
36 | 39 |
|
37 | 40 | showToast(errorMessage) |
38 | 41 | return handled |
39 | 42 | } |
40 | 43 |
|
41 | 44 | fun Context.openAssistant(intent: Intent): Boolean = runCatching { |
42 | | - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK |
| 45 | + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) |
43 | 46 | startActivity(intent) |
44 | 47 | true |
45 | 48 | }.getOrElse { false } |
@@ -91,27 +94,19 @@ fun Context.openAssistantShizuku( |
91 | 94 | } |
92 | 95 |
|
93 | 96 | fun Context.openOnStore(packageName: String): Boolean { |
| 97 | + if (packageName.isBlank()) return false |
| 98 | + |
94 | 99 | val marketIntent = Intent( |
95 | 100 | Intent.ACTION_VIEW, "market://details?id=$packageName".toUri() |
96 | | - ).apply { |
97 | | - flags = Intent.FLAG_ACTIVITY_NEW_TASK |
98 | | - } |
| 101 | + ) |
99 | 102 |
|
100 | 103 | 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 | + ) |
105 | 108 |
|
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) |
115 | 110 | } |
116 | 111 |
|
117 | 112 | fun Context.showToast(message: Int) { |
|
0 commit comments