Skip to content

Commit 78dd252

Browse files
committed
Improve qr scanner
1 parent 29e02d2 commit 78dd252

File tree

6 files changed

+78
-31
lines changed

6 files changed

+78
-31
lines changed

app/src/main/java/io/nekohasekai/sfa/ui/profile/QRScanActivity.kt

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.view.Menu
1010
import android.view.MenuItem
1111
import androidx.activity.result.contract.ActivityResultContract
1212
import androidx.activity.result.contract.ActivityResultContracts
13+
import androidx.camera.core.Camera
1314
import androidx.camera.core.CameraSelector
1415
import androidx.camera.core.ImageAnalysis
1516
import androidx.camera.core.Preview
@@ -69,6 +70,9 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
6970
}
7071
}
7172
private val vendorAnalyzer = Vendor.createQRCodeAnalyzer(onSuccess, onFailure)
73+
private lateinit var cameraProvider: ProcessCameraProvider
74+
private lateinit var cameraPreview: Preview
75+
private lateinit var camera: Camera
7276

7377
private fun startCamera() {
7478
val cameraProviderFuture = try {
@@ -78,23 +82,23 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
7882
return
7983
}
8084
cameraProviderFuture.addListener({
81-
val cameraProvider = try {
85+
cameraProvider = try {
8286
cameraProviderFuture.get()
8387
} catch (e: Exception) {
8488
fatalError(e)
8589
return@addListener
8690
}
8791

88-
val preview = Preview.Builder().build()
92+
cameraPreview = Preview.Builder().build()
8993
.also { it.setSurfaceProvider(binding.previewView.surfaceProvider) }
9094
imageAnalysis = ImageAnalysis.Builder().build()
9195
imageAnalyzer = vendorAnalyzer ?: ZxingQRCodeAnalyzer(onSuccess, onFailure)
9296
imageAnalysis.setAnalyzer(analysisExecutor, imageAnalyzer)
9397
cameraProvider.unbindAll()
9498

9599
try {
96-
cameraProvider.bindToLifecycle(
97-
this, CameraSelector.DEFAULT_BACK_CAMERA, preview, imageAnalysis
100+
camera = cameraProvider.bindToLifecycle(
101+
this, CameraSelector.DEFAULT_BACK_CAMERA, cameraPreview, imageAnalysis
98102
)
99103
} catch (e: Exception) {
100104
fatalError(e)
@@ -134,20 +138,46 @@ class QRScanActivity : AbstractActivity<ActivityQrScanBinding>() {
134138
}
135139

136140
override fun onCreateOptionsMenu(menu: Menu): Boolean {
141+
menuInflater.inflate(R.menu.qr_scan_menu, menu)
137142
if (vendorAnalyzer == null) {
138-
return false
143+
menu.findItem(R.id.action_use_vendor_analyzer).isEnabled = false
144+
} else {
145+
menu.findItem(R.id.action_use_vendor_analyzer).isChecked = true
139146
}
140-
menuInflater.inflate(R.menu.qr_scan_menu, menu)
141147
return true
142148
}
143149

144150
override fun onOptionsItemSelected(item: MenuItem): Boolean {
145151
when (item.itemId) {
146-
R.id.action_disable_vendor_analyzer -> {
152+
R.id.action_use_front_camera -> {
153+
item.isChecked = !item.isChecked
154+
cameraProvider.unbindAll()
155+
try {
156+
camera = cameraProvider.bindToLifecycle(
157+
this,
158+
if (!item.isChecked) CameraSelector.DEFAULT_BACK_CAMERA else CameraSelector.DEFAULT_FRONT_CAMERA,
159+
cameraPreview,
160+
imageAnalysis
161+
)
162+
} catch (e: Exception) {
163+
fatalError(e)
164+
}
165+
}
166+
167+
R.id.action_enable_torch -> {
168+
item.isChecked = !item.isChecked
169+
camera.cameraControl.enableTorch(item.isChecked)
170+
}
171+
172+
R.id.action_use_vendor_analyzer -> {
173+
item.isChecked = !item.isChecked
147174
imageAnalysis.clearAnalyzer()
148-
imageAnalyzer = ZxingQRCodeAnalyzer(onSuccess, onFailure)
175+
imageAnalyzer = if (item.isChecked) {
176+
vendorAnalyzer!!
177+
} else {
178+
ZxingQRCodeAnalyzer(onSuccess, onFailure)
179+
}
149180
imageAnalysis.setAnalyzer(analysisExecutor, imageAnalyzer)
150-
item.isVisible = false
151181
}
152182

153183
else -> return super.onOptionsItemSelected(item)

app/src/main/java/io/nekohasekai/sfa/ui/profileoverride/PerAppProxyActivity.kt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,27 @@ class PerAppProxyActivity : AbstractActivity<ActivityPerAppProxyBinding>() {
8383
setTitle(R.string.title_per_app_proxy)
8484

8585
lifecycleScope.launch {
86-
proxyMode = if (Settings.perAppProxyMode == Settings.PER_APP_PROXY_INCLUDE) {
87-
Settings.PER_APP_PROXY_INCLUDE
88-
} else {
89-
Settings.PER_APP_PROXY_EXCLUDE
90-
}
91-
withContext(Dispatchers.Main) {
92-
if (proxyMode == Settings.PER_APP_PROXY_INCLUDE) {
93-
binding.perAppProxyMode.setText(R.string.per_app_proxy_mode_include_description)
86+
withContext(Dispatchers.IO) {
87+
proxyMode = if (Settings.perAppProxyMode == Settings.PER_APP_PROXY_INCLUDE) {
88+
Settings.PER_APP_PROXY_INCLUDE
9489
} else {
95-
binding.perAppProxyMode.setText(R.string.per_app_proxy_mode_exclude_description)
90+
Settings.PER_APP_PROXY_EXCLUDE
91+
}
92+
withContext(Dispatchers.Main) {
93+
if (proxyMode == Settings.PER_APP_PROXY_INCLUDE) {
94+
binding.perAppProxyMode.setText(R.string.per_app_proxy_mode_include_description)
95+
} else {
96+
binding.perAppProxyMode.setText(R.string.per_app_proxy_mode_exclude_description)
97+
}
98+
}
99+
reloadApplicationList()
100+
filterApplicationList()
101+
withContext(Dispatchers.Main) {
102+
adapter = ApplicationAdapter(displayPackages)
103+
binding.appList.adapter = adapter
104+
delay(500L)
105+
binding.progress.isVisible = false
96106
}
97-
}
98-
reloadApplicationList()
99-
filterApplicationList()
100-
withContext(Dispatchers.Main) {
101-
adapter = ApplicationAdapter(displayPackages)
102-
binding.appList.adapter = adapter
103-
delay(500L)
104-
binding.progress.isVisible = false
105107
}
106108
}
107109
}

app/src/main/res/layout/sheet_add_profile.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<TextView
3535
android:layout_width="0dp"
3636
android:layout_weight="1"
37+
android:textSize="16sp"
3738
android:text="@string/profile_add_scan_qr_code"
3839
android:gravity="center_vertical"
3940
android:layout_height="match_parent">
@@ -58,6 +59,7 @@
5859
<TextView
5960
android:layout_width="0dp"
6061
android:layout_weight="1"
62+
android:textSize="16sp"
6163
android:text="@string/profile_add_create_manually"
6264
android:gravity="center_vertical"
6365
android:layout_height="match_parent">

app/src/main/res/menu/qr_scan_menu.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@
22
<menu xmlns:android="http://schemas.android.com/apk/res/android">
33

44
<item
5-
android:id="@+id/action_disable_vendor_analyzer"
6-
android:title="@string/disable_vendor_analyzer" />
5+
android:checkable="true"
6+
android:id="@+id/action_use_front_camera"
7+
android:title="@string/profile_add_scan_use_front_camera" />
8+
9+
<item
10+
android:checkable="true"
11+
android:id="@+id/action_enable_torch"
12+
android:title="@string/profile_add_scan_enable_torch" />
13+
14+
<item
15+
android:checkable="true"
16+
android:id="@+id/action_use_vendor_analyzer"
17+
android:title="@string/profile_add_scan_use_vendor_analyzer" />
18+
719

820
</menu>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
<string name="profile_source_import">Import</string>
4343

4444
<string name="profile_add_scan_qr_code">Scan QR code</string>
45-
<string name="profile_add_import_from_clipboard">Import remote profile from clipboard</string>
45+
<string name="profile_add_scan_use_front_camera">Front camera</string>
46+
<string name="profile_add_scan_use_vendor_analyzer">MLKit analyzer</string>
47+
<string name="profile_add_scan_enable_torch">Torch</string>
48+
4649
<string name="profile_add_create_manually">Create Manually</string>
4750

4851
<string name="menu_undo">Undo</string>
@@ -189,6 +192,5 @@
189192
<string name="open_settings">Open Settings</string>
190193

191194
<string name="settings_title_core">Core</string>
192-
<string name="disable_vendor_analyzer">Disable Google MLKit</string>
193195

194196
</resources>

app/src/other/java/io/nekohasekai/sfa/vendor/Vendor.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package io.nekohasekai.sfa.vendor
22

33
import android.app.Activity
44
import androidx.camera.core.ImageAnalysis
5-
import java.lang.Exception
65

76
object Vendor : VendorInterface {
87

0 commit comments

Comments
 (0)