add qr scanner function

This commit is contained in:
beasonxu 2024-08-30 09:11:53 +08:00
parent a9b10acac2
commit 6375195763
14 changed files with 72 additions and 2 deletions

View File

@ -24,6 +24,8 @@ dependencies {
implementation(libs.androidx.coordinator) implementation(libs.androidx.coordinator)
implementation(libs.androidx.recyclerview) implementation(libs.androidx.recyclerview)
implementation(libs.google.material) implementation(libs.google.material)
} }
tasks.getByName("clean", type = Delete::class) { tasks.getByName("clean", type = Delete::class) {

View File

@ -193,5 +193,9 @@
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" /> <action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter> </intent-filter>
</receiver> </receiver>
<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="barcode_ui"/>
</application> </application>
</manifest> </manifest>

View File

@ -46,6 +46,14 @@ class NewProfileActivity : BaseActivity<NewProfileDesign>() {
create(Profile.Type.File, name) create(Profile.Type.File, name)
is ProfileProvider.Url -> is ProfileProvider.Url ->
create(Profile.Type.Url, name) 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 -> { is ProfileProvider.External -> {
val data = p.get() val data = p.get()
@ -137,7 +145,7 @@ class NewProfileActivity : BaseActivity<NewProfileDesign>() {
ProfileProvider.External(name.toString(), summary.toString(), icon, intent) 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
} }
} }
} }

View File

@ -18,4 +18,5 @@ dependencies {
implementation(libs.androidx.fragment) implementation(libs.androidx.fragment)
implementation(libs.androidx.viewpager) implementation(libs.androidx.viewpager)
implementation(libs.google.material) implementation(libs.google.material)
implementation("com.google.android.gms:play-services-code-scanner:16.1.0")
} }

View File

@ -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.databinding.DesignNewProfileBinding
import com.github.kr328.clash.design.model.ProfileProvider import com.github.kr328.clash.design.model.ProfileProvider
import com.github.kr328.clash.design.util.* 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<NewProfileDesign.Request>(context) { class NewProfileDesign(context: Context) : Design<NewProfileDesign.Request>(context) {
sealed class Request { sealed class Request {
@ -38,7 +41,31 @@ class NewProfileDesign(context: Context) : Design<NewProfileDesign.Request>(cont
} }
private fun requestCreate(provider: ProfileProvider) { 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 { private fun requestDetail(provider: ProfileProvider): Boolean {

View File

@ -14,6 +14,8 @@ sealed class ProfileProvider {
get() = context.getString(R.string.import_from_file) get() = context.getString(R.string.import_from_file)
override val icon: Drawable? override val icon: Drawable?
get() = context.getDrawableCompat(R.drawable.ic_baseline_attach_file) get() = context.getDrawableCompat(R.drawable.ic_baseline_attach_file)
} }
class Url(private val context: Context) : ProfileProvider() { class Url(private val context: Context) : ProfileProvider() {
@ -25,6 +27,15 @@ sealed class ProfileProvider {
get() = context.getDrawableCompat(R.drawable.ic_baseline_cloud_download) 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( class External(
override val name: String, override val name: String,
override val summary: String, override val summary: String,

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="@android:color/white" android:pathData="M9.5,6.5v3h-3v-3H9.5M11,5H5v6h6V5L11,5zM9.5,14.5v3h-3v-3H9.5M11,13H5v6h6V13L11,13zM17.5,6.5v3h-3v-3H17.5M19,5h-6v6h6V5L19,5zM13,13h1.5v1.5H13V13zM14.5,14.5H16V16h-1.5V14.5zM16,13h1.5v1.5H16V13zM13,16h1.5v1.5H13V16zM14.5,17.5H16V19h-1.5V17.5zM16,16h1.5v1.5H16V16zM17.5,14.5H19V16h-1.5V14.5zM17.5,17.5H19V19h-1.5V17.5zM22,7h-2V4h-3V2h5V7zM22,22v-5h-2v3h-3v2H22zM2,22h5v-2H4v-3H2V22zM2,2v5h2V4h3V2H2z"/>
</vector>

View File

@ -32,6 +32,7 @@
<string name="import_from_file">ファイルからインポート</string> <string name="import_from_file">ファイルからインポート</string>
<string name="url">URL</string> <string name="url">URL</string>
<string name="import_from_url">URLからインポート</string> <string name="import_from_url">URLからインポート</string>
<string name="import_from_qr">QRコードからインポート</string>
<string name="external">外部入力</string> <string name="external">外部入力</string>
<string name="format_type_unsaved">%s (未保存)</string> <string name="format_type_unsaved">%s (未保存)</string>
<string name="application_broken">アプリが破損しています</string> <string name="application_broken">アプリが破損しています</string>

View File

@ -33,6 +33,7 @@
<string name="url">URL</string> <string name="url">URL</string>
<string name="import_from_url">URL에서 가져오기</string> <string name="import_from_url">URL에서 가져오기</string>
<string name="external">외부</string> <string name="external">외부</string>
<string name="import_from_qr">QR코드에서 가져오기</string>
<string name="format_type_unsaved">%s (저장되지 않음)</string> <string name="format_type_unsaved">%s (저장되지 않음)</string>
<string name="application_broken">앱 오류</string> <string name="application_broken">앱 오류</string>
<string name="application_crashed">앱 중지</string> <string name="application_crashed">앱 중지</string>

View File

@ -41,6 +41,7 @@
<string name="url">URL</string> <string name="url">URL</string>
<string name="import_from_url">Импорт из URL</string> <string name="import_from_url">Импорт из URL</string>
<string name="external">Внешний</string> <string name="external">Внешний</string>
<string name="import_from_qr">Импорт из QR-кода</string>
<string name="format_type_unsaved">%s (не сохранён)</string> <string name="format_type_unsaved">%s (не сохранён)</string>
<string name="application_broken">Приложение сломано</string> <string name="application_broken">Приложение сломано</string>

View File

@ -42,6 +42,7 @@
<string name="import_from_file">從文件導入</string> <string name="import_from_file">從文件導入</string>
<string name="import_from_url">從 URL 導入</string> <string name="import_from_url">從 URL 導入</string>
<string name="interface_">界面</string> <string name="interface_">界面</string>
<string name="import_from_qr">從二維碼導入</string>
<string name="invalid_url">無效的 URL</string> <string name="invalid_url">無效的 URL</string>
<string name="launch_name">Clash Meta</string> <string name="launch_name">Clash Meta</string>
<string name="logcat">Logcat</string> <string name="logcat">Logcat</string>

View File

@ -41,6 +41,7 @@
<string name="history">歷史</string> <string name="history">歷史</string>
<string name="import_from_file">從檔案匯入</string> <string name="import_from_file">從檔案匯入</string>
<string name="import_from_url">從 URL 匯入</string> <string name="import_from_url">從 URL 匯入</string>
<string name="import_from_qr">從二維碼導入</string>
<string name="interface_">介面</string> <string name="interface_">介面</string>
<string name="invalid_url">無效 URL</string> <string name="invalid_url">無效 URL</string>
<string name="launch_name">Clash Meta</string> <string name="launch_name">Clash Meta</string>

View File

@ -41,6 +41,7 @@
<string name="history">历史</string> <string name="history">历史</string>
<string name="import_from_file">从文件导入</string> <string name="import_from_file">从文件导入</string>
<string name="import_from_url">从 URL 导入</string> <string name="import_from_url">从 URL 导入</string>
<string name="import_from_qr">从二维码导入</string>
<string name="interface_">界面</string> <string name="interface_">界面</string>
<string name="invalid_url">无效的 URL</string> <string name="invalid_url">无效的 URL</string>
<string name="launch_name">Clash Meta</string> <string name="launch_name">Clash Meta</string>

View File

@ -39,7 +39,9 @@
<string name="file">File</string> <string name="file">File</string>
<string name="import_from_file">Import from File</string> <string name="import_from_file">Import from File</string>
<string name="url">URL</string> <string name="url">URL</string>
<string name="qr" translatable="false">QR</string>
<string name="import_from_url">Import from URL</string> <string name="import_from_url">Import from URL</string>
<string name="import_from_qr">Import from QR</string>
<string name="external">External</string> <string name="external">External</string>
<string name="format_type_unsaved">%s (Unsaved)</string> <string name="format_type_unsaved">%s (Unsaved)</string>