@@ -2,14 +2,20 @@ package com.github.kr328.clash
22
33import android.app.Application
44import android.content.Context
5+ import android.content.Intent
6+ import androidx.core.content.pm.ShortcutInfoCompat
7+ import androidx.core.content.pm.ShortcutManagerCompat
8+ import androidx.core.graphics.drawable.IconCompat
59import com.github.kr328.clash.common.Global
610import com.github.kr328.clash.common.compat.currentProcessName
11+ import com.github.kr328.clash.common.constants.Intents
712import com.github.kr328.clash.common.log.Log
813import com.github.kr328.clash.remote.Remote
914import com.github.kr328.clash.service.util.sendServiceRecreated
1015import com.github.kr328.clash.util.clashDir
1116import java.io.File
1217import java.io.FileOutputStream
18+ import com.github.kr328.clash.design.R as DesignR
1319
1420
1521@Suppress(" unused" )
@@ -32,13 +38,59 @@ class MainApplication : Application() {
3238
3339 if (processName == packageName) {
3440 Remote .launch()
41+ setupShortcuts()
3542 wifiAutomator = WifiAutomator (this )
3643 wifiAutomator?.start()
3744 } else {
3845 sendServiceRecreated()
3946 }
4047 }
4148
49+ private fun setupShortcuts () {
50+ val icon = IconCompat .createWithResource(this , R .mipmap.ic_launcher)
51+ val flags = Intent .FLAG_ACTIVITY_NEW_TASK or
52+ Intent .FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS or
53+ Intent .FLAG_ACTIVITY_NO_ANIMATION
54+
55+ val toggle = ShortcutInfoCompat .Builder (this , " toggle_clash" )
56+ .setShortLabel(getString(DesignR .string.shortcut_toggle_short))
57+ .setLongLabel(getString(DesignR .string.shortcut_toggle_long))
58+ .setIcon(icon)
59+ .setIntent(
60+ Intent (Intents .ACTION_TOGGLE_CLASH )
61+ .setClassName(this , ExternalControlActivity ::class .java.name)
62+ .addFlags(flags)
63+ )
64+ .setRank(0 )
65+ .build()
66+
67+ val start = ShortcutInfoCompat .Builder (this , " start_clash" )
68+ .setShortLabel(getString(DesignR .string.shortcut_start_short))
69+ .setLongLabel(getString(DesignR .string.shortcut_start_long))
70+ .setIcon(icon)
71+ .setIntent(
72+ Intent (Intents .ACTION_START_CLASH )
73+ .setClassName(this , ExternalControlActivity ::class .java.name)
74+ .addFlags(flags)
75+ )
76+ .setRank(1 )
77+ .build()
78+
79+ val stop = ShortcutInfoCompat .Builder (this , " stop_clash" )
80+ .setShortLabel(getString(DesignR .string.shortcut_stop_short))
81+ .setLongLabel(getString(DesignR .string.shortcut_stop_long))
82+ .setIcon(icon)
83+ .setIntent(
84+ Intent (Intents .ACTION_STOP_CLASH )
85+ .setClassName(this , ExternalControlActivity ::class .java.name)
86+ .addFlags(flags)
87+ )
88+ .setRank(2 )
89+ .build()
90+
91+ ShortcutManagerCompat .setDynamicShortcuts(this , listOf (toggle, start, stop))
92+ }
93+
4294 override fun onTerminate () {
4395 super .onTerminate()
4496 wifiAutomator?.stop()
0 commit comments