From 021939264fcfd5251bdc9fdb53cc7cfde8fc3f93 Mon Sep 17 00:00:00 2001 From: Steve Johnson Date: Wed, 1 Nov 2023 12:00:37 +0800 Subject: [PATCH] feat: add intent filters for tasker automation (#119) --- README.md | 13 +++ app/src/main/AndroidManifest.xml | 16 +++- .../kr328/clash/ExternalControlActivity.kt | 92 +++++++++++++++++++ .../kr328/clash/ExternalImportActivity.kt | 44 --------- .../kr328/clash/common/constants/Intents.kt | 3 + design/src/main/res/values-ja-rJP/strings.xml | 3 + design/src/main/res/values-ko-rKR/strings.xml | 3 + design/src/main/res/values-ru/strings.xml | 3 + design/src/main/res/values-zh-rHK/strings.xml | 3 + design/src/main/res/values-zh-rTW/strings.xml | 3 + design/src/main/res/values-zh/strings.xml | 3 + design/src/main/res/values/strings.xml | 3 + 12 files changed, 143 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/com/github/kr328/clash/ExternalControlActivity.kt delete mode 100644 app/src/main/java/com/github/kr328/clash/ExternalImportActivity.kt diff --git a/README.md b/README.md index ae76907d..c6d0e669 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,19 @@ Feature of [Clash.Meta](https://github.com/MetaCubeX/Clash.Meta) ./gradlew app:assembleMeta-AlphaRelease ``` +### Automation + +APP package name is `com.github.metacubex.clash.meta` + +- Toggle Clash.Meta service status + - Send intent to `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.TOGGLE_CLASH` +- Start Clash.Meta service + - Send intent to `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.START_CLASH` +- Stop Clash.Meta service + - Send intent to `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.STOP_CLASH` +- Import a profile + - URL Scheme `clash://install-config?url=` or `clashmeta://install-config?url=` + ### Kernel Contribution - CMFA uses the kernel from `android-real` branch under `MetaCubeX/Clash.Meta`, which is a merge of the main `Alpha` branch and `android-open`. diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 31b53255..cbb763a6 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,9 +55,9 @@ @@ -69,6 +69,18 @@ + + + + + + + + + + + + { + val uri = intent.data ?: return finish() + val url = uri.getQueryParameter("url") ?: return finish() + + launch { + val uuid = withProfile { + val type = when (uri.getQueryParameter("type")?.lowercase(Locale.getDefault())) { + "url" -> Profile.Type.Url + "file" -> Profile.Type.File + else -> Profile.Type.Url + } + val name = uri.getQueryParameter("name") ?: getString(R.string.new_profile) + + create(type, name).also { + patch(it, name, url, 0) + } + } + startActivity(PropertiesActivity::class.intent.setUUID(uuid)) + finish() + } + } + + Intents.ACTION_TOGGLE_CLASH -> if(Remote.broadcasts.clashRunning) { + stopClash() + } + else { + startClash() + } + + Intents.ACTION_START_CLASH -> if(!Remote.broadcasts.clashRunning) { + startClash() + } + else { + Toast.makeText(this, R.string.external_control_started, Toast.LENGTH_LONG).show() + } + + Intents.ACTION_STOP_CLASH -> if(Remote.broadcasts.clashRunning) { + stopClash() + } + else { + Toast.makeText(this, R.string.external_control_stopped, Toast.LENGTH_LONG).show() + } + } + return finish() + } + + private fun startClash() { +// if (currentProfile == null) { +// Toast.makeText(this, R.string.no_profile_selected, Toast.LENGTH_LONG).show() +// return +// } + val vpnRequest = startClashService() + if (vpnRequest != null) { + Toast.makeText(this, R.string.unable_to_start_vpn, Toast.LENGTH_LONG).show() + return + } + Toast.makeText(this, R.string.external_control_started, Toast.LENGTH_LONG).show() + } + + private fun stopClash() { + stopClashService() + Toast.makeText(this, R.string.external_control_stopped, Toast.LENGTH_LONG).show() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/github/kr328/clash/ExternalImportActivity.kt b/app/src/main/java/com/github/kr328/clash/ExternalImportActivity.kt deleted file mode 100644 index 619a9131..00000000 --- a/app/src/main/java/com/github/kr328/clash/ExternalImportActivity.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.github.kr328.clash - -import android.app.Activity -import android.content.Intent -import android.os.Bundle -import com.github.kr328.clash.common.util.intent -import com.github.kr328.clash.common.util.setUUID -import com.github.kr328.clash.service.model.Profile -import com.github.kr328.clash.util.withProfile -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.MainScope -import kotlinx.coroutines.launch -import java.util.* - -class ExternalImportActivity : Activity(), CoroutineScope by MainScope() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - if (intent.action != Intent.ACTION_VIEW) - return finish() - - val uri = intent.data ?: return finish() - val url = uri.getQueryParameter("url") ?: return finish() - - launch { - val uuid = withProfile { - val type = when (uri.getQueryParameter("type")?.lowercase(Locale.getDefault())) { - "url" -> Profile.Type.Url - "file" -> Profile.Type.File - else -> Profile.Type.Url - } - val name = uri.getQueryParameter("name") ?: getString(R.string.new_profile) - - create(type, name).also { - patch(it, name, url, 0) - } - } - - startActivity(PropertiesActivity::class.intent.setUUID(uuid)) - - finish() - } - } -} \ No newline at end of file diff --git a/common/src/main/java/com/github/kr328/clash/common/constants/Intents.kt b/common/src/main/java/com/github/kr328/clash/common/constants/Intents.kt index 73236841..b754dc70 100644 --- a/common/src/main/java/com/github/kr328/clash/common/constants/Intents.kt +++ b/common/src/main/java/com/github/kr328/clash/common/constants/Intents.kt @@ -5,6 +5,9 @@ import com.github.kr328.clash.common.util.packageName object Intents { // Public val ACTION_PROVIDE_URL = "$packageName.action.PROVIDE_URL" + val ACTION_START_CLASH = "$packageName.action.START_CLASH" + val ACTION_STOP_CLASH = "$packageName.action.STOP_CLASH" + val ACTION_TOGGLE_CLASH = "$packageName.action.TOGGLE_CLASH" const val EXTRA_NAME = "name" diff --git a/design/src/main/res/values-ja-rJP/strings.xml b/design/src/main/res/values-ja-rJP/strings.xml index 9adaccc6..b3c384a6 100644 --- a/design/src/main/res/values-ja-rJP/strings.xml +++ b/design/src/main/res/values-ja-rJP/strings.xml @@ -245,4 +245,7 @@ %1$s imported Update profile %s completed Update profile %1$s failed: %2$s + External Control + Clash.Meta service started + Clash.Meta service stopped \ No newline at end of file diff --git a/design/src/main/res/values-ko-rKR/strings.xml b/design/src/main/res/values-ko-rKR/strings.xml index 17de7d43..036f01ea 100644 --- a/design/src/main/res/values-ko-rKR/strings.xml +++ b/design/src/main/res/values-ko-rKR/strings.xml @@ -245,4 +245,7 @@ %1$s imported Update profile %s completed Update profile %1$s failed: %2$s + External Control + Clash.Meta service started + Clash.Meta service stopped \ No newline at end of file diff --git a/design/src/main/res/values-ru/strings.xml b/design/src/main/res/values-ru/strings.xml index decc348e..6535a338 100644 --- a/design/src/main/res/values-ru/strings.xml +++ b/design/src/main/res/values-ru/strings.xml @@ -310,4 +310,7 @@ %1$s imported Update profile %s completed Update profile %1$s failed: %2$s + External Control + Clash.Meta service started + Clash.Meta service stopped diff --git a/design/src/main/res/values-zh-rHK/strings.xml b/design/src/main/res/values-zh-rHK/strings.xml index 596a66c9..2940af65 100644 --- a/design/src/main/res/values-zh-rHK/strings.xml +++ b/design/src/main/res/values-zh-rHK/strings.xml @@ -242,4 +242,7 @@ %1$s imported Update profile %s completed Update profile %1$s failed: %2$s + External Control + Clash.Meta service started + Clash.Meta service stopped \ No newline at end of file diff --git a/design/src/main/res/values-zh-rTW/strings.xml b/design/src/main/res/values-zh-rTW/strings.xml index d4cc0879..3980aad0 100644 --- a/design/src/main/res/values-zh-rTW/strings.xml +++ b/design/src/main/res/values-zh-rTW/strings.xml @@ -242,4 +242,7 @@ %1$s imported Update profile %s completed Update profile %1$s failed: %2$s + External Control + Clash.Meta service started + Clash.Meta service stopped diff --git a/design/src/main/res/values-zh/strings.xml b/design/src/main/res/values-zh/strings.xml index 994a489a..9d17afcf 100644 --- a/design/src/main/res/values-zh/strings.xml +++ b/design/src/main/res/values-zh/strings.xml @@ -245,4 +245,7 @@ %1$s 已导入 更新配置 %s 成功 更新配置 %1$s 失败: %2$s + External Control + Clash.Meta 服务已启动 + Clash.Meta 服务已停止 \ No newline at end of file diff --git a/design/src/main/res/values/strings.xml b/design/src/main/res/values/strings.xml index 91ad0907..ec0c9819 100644 --- a/design/src/main/res/values/strings.xml +++ b/design/src/main/res/values/strings.xml @@ -310,4 +310,7 @@ %1$s imported Update profile %s completed Update profile %1$s failed: %2$s + External Control + Clash.Meta service started + Clash.Meta service stopped