Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qr scan #307

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ dependencies {
implementation(libs.androidx.coordinator)
implementation(libs.androidx.recyclerview)
implementation(libs.google.material)


}

tasks.getByName("clean", type = Delete::class) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,9 @@
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>

<meta-data
android:name="com.google.mlkit.vision.DEPENDENCIES"
android:value="barcode_ui"/>
</application>
</manifest>
10 changes: 9 additions & 1 deletion app/src/main/java/com/github/kr328/clash/NewProfileActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ class NewProfileActivity : BaseActivity<NewProfileDesign>() {
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()

Expand Down Expand Up @@ -137,7 +145,7 @@ class NewProfileActivity : BaseActivity<NewProfileDesign>() {
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
}
}
}
1 change: 1 addition & 0 deletions design/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link

@wwqgtxx wwqgtxx Aug 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<NewProfileDesign.Request>(context) {
sealed class Request {
Expand Down Expand Up @@ -38,7 +41,31 @@ class NewProfileDesign(context: Context) : Design<NewProfileDesign.Request>(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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions design/src/main/res/drawable/baseline_qr_code_scanner.xml
Original file line number Diff line number Diff line change
@@ -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>
1 change: 1 addition & 0 deletions design/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<string name="import_from_file">ファイルからインポート</string>
<string name="url">URL</string>
<string name="import_from_url">URLからインポート</string>
<string name="import_from_qr">QRコードからインポート</string>
<string name="external">外部入力</string>
<string name="format_type_unsaved">%s (未保存)</string>
<string name="application_broken">アプリが破損しています</string>
Expand Down
1 change: 1 addition & 0 deletions design/src/main/res/values-ko-rKR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="url">URL</string>
<string name="import_from_url">URL에서 가져오기</string>
<string name="external">외부</string>
<string name="import_from_qr">QR코드에서 가져오기</string>
<string name="format_type_unsaved">%s (저장되지 않음)</string>
<string name="application_broken">앱 오류</string>
<string name="application_crashed">앱 중지</string>
Expand Down
1 change: 1 addition & 0 deletions design/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="url">URL</string>
<string name="import_from_url">Импорт из URL</string>
<string name="external">Внешний</string>
<string name="import_from_qr">Импорт из QR-кода</string>
<string name="format_type_unsaved">%s (не сохранён)</string>

<string name="application_broken">Приложение сломано</string>
Expand Down
1 change: 1 addition & 0 deletions design/src/main/res/values-zh-rHK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<string name="import_from_file">從文件導入</string>
<string name="import_from_url">從 URL 導入</string>
<string name="interface_">界面</string>
<string name="import_from_qr">從二維碼導入</string>
<string name="invalid_url">無效的 URL</string>
<string name="launch_name">Clash Meta</string>
<string name="logcat">Logcat</string>
Expand Down
1 change: 1 addition & 0 deletions design/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="history">歷史</string>
<string name="import_from_file">從檔案匯入</string>
<string name="import_from_url">從 URL 匯入</string>
<string name="import_from_qr">從二維碼導入</string>
<string name="interface_">介面</string>
<string name="invalid_url">無效 URL</string>
<string name="launch_name">Clash Meta</string>
Expand Down
1 change: 1 addition & 0 deletions design/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="history">历史</string>
<string name="import_from_file">从文件导入</string>
<string name="import_from_url">从 URL 导入</string>
<string name="import_from_qr">从二维码导入</string>
<string name="interface_">界面</string>
<string name="invalid_url">无效的 URL</string>
<string name="launch_name">Clash Meta</string>
Expand Down
2 changes: 2 additions & 0 deletions design/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
<string name="file">File</string>
<string name="import_from_file">Import from File</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_qr">Import from QR</string>
<string name="external">External</string>
<string name="format_type_unsaved">%s (Unsaved)</string>

Expand Down
Loading