Add dynamic App Shortcuts for external automation (Tasker/Samsung Routines) (#676)

Register toggle/start/stop shortcuts via ShortcutManagerCompat on app launch,
enabling Samsung Routines and other launchers to control the Clash service
through long-press app icon shortcuts.

Use launcher icon for shortcut display; suppress activity transitions and
exclude ExternalControlActivity from recents for seamless background control.
This commit is contained in:
sleshep
2026-03-06 09:39:31 +08:00
committed by GitHub
parent 406e4004f2
commit 83261bb3f9
5 changed files with 76 additions and 0 deletions

View File

@@ -65,8 +65,10 @@
<activity
android:name=".ExternalControlActivity"
android:excludeFromRecents="true"
android:exported="true"
android:label="@string/external_control_activity"
android:noHistory="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

View File

@@ -25,6 +25,8 @@ import com.github.kr328.clash.design.R
class ExternalControlActivity : Activity(), CoroutineScope by MainScope() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@Suppress("DEPRECATION")
overridePendingTransition(0, 0)
when(intent.action) {
Intent.ACTION_VIEW -> {
@@ -90,4 +92,10 @@ class ExternalControlActivity : Activity(), CoroutineScope by MainScope() {
stopClashService()
Toast.makeText(this, R.string.external_control_stopped, Toast.LENGTH_LONG).show()
}
override fun finish() {
super.finish()
@Suppress("DEPRECATION")
overridePendingTransition(0, 0)
}
}

View File

@@ -2,14 +2,20 @@ package com.github.kr328.clash
import android.app.Application
import android.content.Context
import android.content.Intent
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import com.github.kr328.clash.common.Global
import com.github.kr328.clash.common.compat.currentProcessName
import com.github.kr328.clash.common.constants.Intents
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.github.kr328.clash.util.clashDir
import java.io.File
import java.io.FileOutputStream
import com.github.kr328.clash.design.R as DesignR
@Suppress("unused")
@@ -30,11 +36,57 @@ class MainApplication : Application() {
if (processName == packageName) {
Remote.launch()
setupShortcuts()
} else {
sendServiceRecreated()
}
}
private fun setupShortcuts() {
val icon = IconCompat.createWithResource(this, R.mipmap.ic_launcher)
val flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS or
Intent.FLAG_ACTIVITY_NO_ANIMATION
val toggle = ShortcutInfoCompat.Builder(this, "toggle_clash")
.setShortLabel(getString(DesignR.string.shortcut_toggle_short))
.setLongLabel(getString(DesignR.string.shortcut_toggle_long))
.setIcon(icon)
.setIntent(
Intent(Intents.ACTION_TOGGLE_CLASH)
.setClassName(this, ExternalControlActivity::class.java.name)
.addFlags(flags)
)
.setRank(0)
.build()
val start = ShortcutInfoCompat.Builder(this, "start_clash")
.setShortLabel(getString(DesignR.string.shortcut_start_short))
.setLongLabel(getString(DesignR.string.shortcut_start_long))
.setIcon(icon)
.setIntent(
Intent(Intents.ACTION_START_CLASH)
.setClassName(this, ExternalControlActivity::class.java.name)
.addFlags(flags)
)
.setRank(1)
.build()
val stop = ShortcutInfoCompat.Builder(this, "stop_clash")
.setShortLabel(getString(DesignR.string.shortcut_stop_short))
.setLongLabel(getString(DesignR.string.shortcut_stop_long))
.setIcon(icon)
.setIntent(
Intent(Intents.ACTION_STOP_CLASH)
.setClassName(this, ExternalControlActivity::class.java.name)
.addFlags(flags)
)
.setRank(2)
.build()
ShortcutManagerCompat.setDynamicShortcuts(this, listOf(toggle, start, stop))
}
private fun extractGeoFiles() {
clashDir.mkdirs()