forked from LawnchairLauncher/lawnchair
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'LawnchairLauncher:15-dev' into trunk
- Loading branch information
Showing
10 changed files
with
108 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,41 @@ | ||
{ | ||
"labels": [ | ||
"dependencies", | ||
labels: [ | ||
'dependencies', | ||
], | ||
"extends": [ | ||
"config:base", | ||
extends: [ | ||
'config:recommended', | ||
], | ||
"packageRules": [ | ||
packageRules: [ | ||
{ | ||
"groupName": "Kotlin and KSP", | ||
"matchPackagePrefixes": [ | ||
"com.google.devtools.ksp", | ||
groupName: 'Kotlin and KSP', | ||
matchPackageNames: [ | ||
'com.google.devtools.ksp{/,}**', | ||
'/org.jetbrains.kotlin.*/', | ||
], | ||
"matchPackagePatterns": [ | ||
"org.jetbrains.kotlin.*", | ||
] | ||
}, | ||
{ | ||
"groupName": "AGP", | ||
"matchPackagePatterns": [ | ||
"com.android.*", | ||
] | ||
groupName: 'AGP', | ||
matchPackageNames: [ | ||
'/com.android.*/', | ||
], | ||
}, | ||
{ | ||
"groupName": "dev.rikka.tools.refine", | ||
"matchPackagePrefixes": [ | ||
"dev.rikka.tools.refine", | ||
] | ||
groupName: 'dev.rikka.tools.refine', | ||
matchPackageNames: [ | ||
'dev.rikka.tools.refine{/,}**', | ||
], | ||
}, | ||
{ | ||
"groupName": "androidx.lifecycle", | ||
"matchPackagePatterns": [ | ||
"androidx.lifecycle:*", | ||
] | ||
groupName: 'androidx.lifecycle', | ||
matchPackageNames: [ | ||
'/androidx.lifecycle:*/', | ||
], | ||
}, | ||
{ | ||
"groupName": "Dagger", | ||
"matchPackagePrefixes": [ | ||
"com.google.dagger", | ||
] | ||
} | ||
] | ||
groupName: 'Dagger', | ||
matchPackageNames: [ | ||
'com.google.dagger{/,}**', | ||
], | ||
}, | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
lawnchair/src/app/lawnchair/gestures/handlers/OpenAssistantHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package app.lawnchair.gestures.handlers | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.provider.Settings | ||
import android.util.Log | ||
import app.lawnchair.LawnchairLauncher | ||
|
||
class OpenAssistantHandler(context: Context) : GestureHandler(context) { | ||
|
||
override suspend fun onTrigger(launcher: LawnchairLauncher) { | ||
try { | ||
val component = getCurrentlySelectedDefaultAssistant(context) | ||
Log.d(TAG, "Detected assistant: $component") | ||
|
||
when { | ||
component == null -> noValidAssistantFound() | ||
isActivity(component) -> launchIntent(Intent(Intent.ACTION_ASSIST).setComponent(component)) | ||
isService(component) -> launchIntent(Intent(Intent.ACTION_VOICE_COMMAND)) | ||
isInstalled(component.packageName) -> launchIntent(context.packageManager.getLaunchIntentForPackage(component.packageName)) | ||
else -> noValidAssistantFound() | ||
} | ||
} catch (e: Exception) { | ||
Log.e(TAG, "Error launching assistant", e) | ||
noValidAssistantFound() | ||
} | ||
} | ||
|
||
private fun launchIntent(intent: Intent?) { | ||
intent?.apply { | ||
flags = Intent.FLAG_ACTIVITY_NEW_TASK | ||
runCatching { context.startActivity(this) } | ||
.onFailure { Log.e(TAG, "Failed to launch intent: $intent", it) } | ||
} ?: Log.e(TAG, "Intent is null, cannot launch.") | ||
} | ||
|
||
private fun noValidAssistantFound() { | ||
Log.e(TAG, "No valid assistant found, opening voice input settings.") | ||
launchIntent(Intent(Settings.ACTION_VOICE_INPUT_SETTINGS)) | ||
} | ||
|
||
private fun getCurrentlySelectedDefaultAssistant(context: Context): ComponentName? = Settings.Secure.getString(context.contentResolver, "assistant") | ||
?.takeIf { it.isNotEmpty() } | ||
?.let(ComponentName::unflattenFromString) | ||
?: context.packageManager.resolveActivity(Intent(Intent.ACTION_ASSIST), PackageManager.MATCH_DEFAULT_ONLY) | ||
?.activityInfo?.let { ComponentName(it.packageName, it.name) } | ||
|
||
private fun isActivity(component: ComponentName) = context.packageManager.queryIntentActivities(Intent().setComponent(component), PackageManager.MATCH_DEFAULT_ONLY).isNotEmpty() | ||
|
||
private fun isService(component: ComponentName) = context.packageManager.getInstalledPackages(PackageManager.GET_SERVICES) | ||
.any { it.packageName == component.packageName && it.services?.any { service -> service.name == component.className } == true } | ||
|
||
private fun isInstalled(packageName: String) = runCatching { | ||
context.packageManager.getPackageInfo(packageName, 0) | ||
true | ||
}.getOrDefault(false) | ||
|
||
companion object { | ||
private const val TAG = "OpenAssistantHandler" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters