From 63751957632d59c835345418d542c22c3a59b03c Mon Sep 17 00:00:00 2001 From: beasonxu Date: Fri, 30 Aug 2024 09:11:53 +0800 Subject: [PATCH] add qr scanner function --- app/build.gradle.kts | 2 ++ app/src/main/AndroidManifest.xml | 4 +++ .../github/kr328/clash/NewProfileActivity.kt | 10 ++++++- design/build.gradle.kts | 1 + .../kr328/clash/design/NewProfileDesign.kt | 29 ++++++++++++++++++- .../clash/design/model/ProfileProvider.kt | 11 +++++++ .../res/drawable/baseline_qr_code_scanner.xml | 9 ++++++ design/src/main/res/values-ja-rJP/strings.xml | 1 + design/src/main/res/values-ko-rKR/strings.xml | 1 + design/src/main/res/values-ru/strings.xml | 1 + design/src/main/res/values-zh-rHK/strings.xml | 1 + design/src/main/res/values-zh-rTW/strings.xml | 1 + design/src/main/res/values-zh/strings.xml | 1 + design/src/main/res/values/strings.xml | 2 ++ 14 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 design/src/main/res/drawable/baseline_qr_code_scanner.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 20f61121..c38b51e5 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -24,6 +24,8 @@ dependencies { implementation(libs.androidx.coordinator) implementation(libs.androidx.recyclerview) implementation(libs.google.material) + + } tasks.getByName("clean", type = Delete::class) { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cbb763a6..2291a8cc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -193,5 +193,9 @@ + + diff --git a/app/src/main/java/com/github/kr328/clash/NewProfileActivity.kt b/app/src/main/java/com/github/kr328/clash/NewProfileActivity.kt index 8df167d1..4ab79cb3 100644 --- a/app/src/main/java/com/github/kr328/clash/NewProfileActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/NewProfileActivity.kt @@ -46,6 +46,14 @@ class NewProfileActivity : BaseActivity() { create(Profile.Type.File, name) is ProfileProvider.Url -> create(Profile.Type.Url, name) + is ProfileProvider.QR -> { + val provider = it.provider as ProfileProvider.QR + if (provider.url!=null){ + create(Profile.Type.Url, name, provider.url!!) + }else{ + null + } + } is ProfileProvider.External -> { val data = p.get() @@ -137,7 +145,7 @@ class NewProfileActivity : BaseActivity() { ProfileProvider.External(name.toString(), summary.toString(), icon, intent) } - listOf(ProfileProvider.File(self), ProfileProvider.Url(self)) + providers + listOf(ProfileProvider.File(self), ProfileProvider.Url(self),ProfileProvider.QR(self)) + providers } } } diff --git a/design/build.gradle.kts b/design/build.gradle.kts index 6e4421e6..74fd10e3 100644 --- a/design/build.gradle.kts +++ b/design/build.gradle.kts @@ -18,4 +18,5 @@ dependencies { implementation(libs.androidx.fragment) implementation(libs.androidx.viewpager) implementation(libs.google.material) + implementation("com.google.android.gms:play-services-code-scanner:16.1.0") } diff --git a/design/src/main/java/com/github/kr328/clash/design/NewProfileDesign.kt b/design/src/main/java/com/github/kr328/clash/design/NewProfileDesign.kt index e38d01e7..ee59f874 100644 --- a/design/src/main/java/com/github/kr328/clash/design/NewProfileDesign.kt +++ b/design/src/main/java/com/github/kr328/clash/design/NewProfileDesign.kt @@ -6,6 +6,9 @@ import com.github.kr328.clash.design.adapter.ProfileProviderAdapter import com.github.kr328.clash.design.databinding.DesignNewProfileBinding import com.github.kr328.clash.design.model.ProfileProvider import com.github.kr328.clash.design.util.* +import com.google.mlkit.vision.barcode.common.Barcode +import com.google.mlkit.vision.codescanner.GmsBarcodeScannerOptions +import com.google.mlkit.vision.codescanner.GmsBarcodeScanning class NewProfileDesign(context: Context) : Design(context) { sealed class Request { @@ -38,7 +41,31 @@ class NewProfileDesign(context: Context) : Design(cont } private fun requestCreate(provider: ProfileProvider) { - requests.trySend(Request.Create(provider)) + if (provider is ProfileProvider.QR){ + val options = GmsBarcodeScannerOptions.Builder() + .setBarcodeFormats( + Barcode.FORMAT_QR_CODE, + Barcode.FORMAT_AZTEC) + .enableAutoZoom() // available on 16.1.0 and higher + .build() + val scanner = GmsBarcodeScanning.getClient(context,options) + scanner.startScan() + .addOnSuccessListener { barcode -> + // Task completed successfully + provider.url = barcode.rawValue + requests.trySend(Request.Create(provider)) + } + .addOnCanceledListener { + // Task canceled + } + .addOnFailureListener { e -> + // Task failed with an exception + } + + }else{ + requests.trySend(Request.Create(provider)) + } + } private fun requestDetail(provider: ProfileProvider): Boolean { diff --git a/design/src/main/java/com/github/kr328/clash/design/model/ProfileProvider.kt b/design/src/main/java/com/github/kr328/clash/design/model/ProfileProvider.kt index 7b09c22b..7df22ebb 100644 --- a/design/src/main/java/com/github/kr328/clash/design/model/ProfileProvider.kt +++ b/design/src/main/java/com/github/kr328/clash/design/model/ProfileProvider.kt @@ -14,6 +14,8 @@ sealed class ProfileProvider { get() = context.getString(R.string.import_from_file) override val icon: Drawable? get() = context.getDrawableCompat(R.drawable.ic_baseline_attach_file) + + } class Url(private val context: Context) : ProfileProvider() { @@ -25,6 +27,15 @@ sealed class ProfileProvider { get() = context.getDrawableCompat(R.drawable.ic_baseline_cloud_download) } + class QR(private val context: Context) : ProfileProvider() { + override val name: String + get() = context.getString(R.string.qr) + override val summary: String + get() = context.getString(R.string.import_from_qr) + override val icon: Drawable? + get() = context.getDrawableCompat(R.drawable.baseline_qr_code_scanner) + var url:String? =null + } class External( override val name: String, override val summary: String, diff --git a/design/src/main/res/drawable/baseline_qr_code_scanner.xml b/design/src/main/res/drawable/baseline_qr_code_scanner.xml new file mode 100644 index 00000000..0c334193 --- /dev/null +++ b/design/src/main/res/drawable/baseline_qr_code_scanner.xml @@ -0,0 +1,9 @@ + + + + diff --git a/design/src/main/res/values-ja-rJP/strings.xml b/design/src/main/res/values-ja-rJP/strings.xml index 65f2ed9f..fc9d7b5c 100644 --- a/design/src/main/res/values-ja-rJP/strings.xml +++ b/design/src/main/res/values-ja-rJP/strings.xml @@ -32,6 +32,7 @@ ファイルからインポート URL URLからインポート + QRコードからインポート 外部入力 %s (未保存) アプリが破損しています diff --git a/design/src/main/res/values-ko-rKR/strings.xml b/design/src/main/res/values-ko-rKR/strings.xml index f09bd825..42bc18b8 100644 --- a/design/src/main/res/values-ko-rKR/strings.xml +++ b/design/src/main/res/values-ko-rKR/strings.xml @@ -33,6 +33,7 @@ URL URL에서 가져오기 외부 + QR코드에서 가져오기 %s (저장되지 않음) 앱 오류 앱 중지 diff --git a/design/src/main/res/values-ru/strings.xml b/design/src/main/res/values-ru/strings.xml index a1e42712..a8652d33 100644 --- a/design/src/main/res/values-ru/strings.xml +++ b/design/src/main/res/values-ru/strings.xml @@ -41,6 +41,7 @@ URL Импорт из URL Внешний + Импорт из QR-кода %s (не сохранён) Приложение сломано diff --git a/design/src/main/res/values-zh-rHK/strings.xml b/design/src/main/res/values-zh-rHK/strings.xml index 28fe0152..ea71598a 100644 --- a/design/src/main/res/values-zh-rHK/strings.xml +++ b/design/src/main/res/values-zh-rHK/strings.xml @@ -42,6 +42,7 @@ 從文件導入 從 URL 導入 界面 + 從二維碼導入 無效的 URL Clash Meta Logcat diff --git a/design/src/main/res/values-zh-rTW/strings.xml b/design/src/main/res/values-zh-rTW/strings.xml index 42bf15cc..6ad965df 100644 --- a/design/src/main/res/values-zh-rTW/strings.xml +++ b/design/src/main/res/values-zh-rTW/strings.xml @@ -41,6 +41,7 @@ 歷史 從檔案匯入 從 URL 匯入 + 從二維碼導入 介面 無效 URL Clash Meta diff --git a/design/src/main/res/values-zh/strings.xml b/design/src/main/res/values-zh/strings.xml index dc92cdac..8d402fa2 100644 --- a/design/src/main/res/values-zh/strings.xml +++ b/design/src/main/res/values-zh/strings.xml @@ -41,6 +41,7 @@ 历史 从文件导入 从 URL 导入 + 从二维码导入 界面 无效的 URL Clash Meta diff --git a/design/src/main/res/values/strings.xml b/design/src/main/res/values/strings.xml index a6c87acd..4f3f45aa 100644 --- a/design/src/main/res/values/strings.xml +++ b/design/src/main/res/values/strings.xml @@ -39,7 +39,9 @@ File Import from File URL + QR Import from URL + Import from QR External %s (Unsaved)