CameraActivity refactoring
This commit is contained in:
parent
679bd4e4c9
commit
e625543b94
13 changed files with 193 additions and 224 deletions
|
@ -63,6 +63,9 @@
|
|||
|
||||
<activity
|
||||
android:name=".CameraActivity"
|
||||
android:excludeFromRecents="true"
|
||||
android:launchMode="singleTask"
|
||||
android:taskAffinity=""
|
||||
android:exported="false" />
|
||||
|
||||
<activity
|
||||
|
|
|
@ -75,6 +75,10 @@ android {
|
|||
isUniversalApk = false
|
||||
}
|
||||
}
|
||||
|
||||
lint {
|
||||
disable += "InvalidFragmentVersionForActivityResult"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -82,11 +86,9 @@ dependencies {
|
|||
implementation(project(":qt"))
|
||||
implementation(project(":utils"))
|
||||
implementation(libs.androidx.core)
|
||||
implementation(libs.androidx.appcompat)
|
||||
implementation(libs.androidx.activity)
|
||||
implementation(libs.androidx.security.crypto)
|
||||
implementation(libs.kotlinx.coroutines)
|
||||
implementation(libs.bundles.androidx.camera)
|
||||
implementation(libs.google.mlkit)
|
||||
// todo: remove after finish refactoring
|
||||
implementation(libs.androidx.constraintlayout)
|
||||
}
|
||||
|
|
|
@ -2,15 +2,15 @@
|
|||
agp = "8.1.3"
|
||||
kotlin = "1.9.20"
|
||||
androidx-core = "1.12.0"
|
||||
androidx-appcompat = "1.6.1"
|
||||
androidx-camera = "1.2.3"
|
||||
androidx-activity = "1.8.1"
|
||||
androidx-camera = "1.3.0"
|
||||
androidx-security-crypto = "1.1.0-alpha06"
|
||||
kotlinx-coroutines = "1.7.3"
|
||||
google-mlkit = "17.2.0"
|
||||
|
||||
[libraries]
|
||||
androidx-core = { module = "androidx.core:core-ktx", version.ref = "androidx-core" }
|
||||
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" }
|
||||
androidx-activity = { module = "androidx.activity:activity-ktx", version.ref = "androidx-activity" }
|
||||
androidx-camera-core = { module = "androidx.camera:camera-core", version.ref = "androidx-camera" }
|
||||
androidx-camera-camera2 = { module = "androidx.camera:camera-camera2", version.ref = "androidx-camera" }
|
||||
androidx-camera-lifecycle = { module = "androidx.camera:camera-lifecycle", version.ref = "androidx-camera" }
|
||||
|
@ -18,8 +18,6 @@ androidx-camera-view = { module = "androidx.camera:camera-view", version.ref = "
|
|||
androidx-security-crypto = { module = "androidx.security:security-crypto-ktx", version.ref = "androidx-security-crypto" }
|
||||
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
|
||||
google-mlkit = { module = "com.google.mlkit:barcode-scanning", version.ref = "google-mlkit" }
|
||||
# todo: remove after finish refactoring
|
||||
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version = "2.1.4" }
|
||||
|
||||
[bundles]
|
||||
androidx-camera = [
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".CameraActivity">
|
||||
|
||||
<androidx.camera.view.PreviewView
|
||||
android:id="@+id/viewFinder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
11
client/android/res/layout/camera_preview.xml
Normal file
11
client/android/res/layout/camera_preview.xml
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.camera.view.PreviewView
|
||||
android:id="@+id/viewFinder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" />
|
||||
|
||||
</FrameLayout>
|
|
@ -9,18 +9,12 @@ import org.qtproject.qt.android.bindings.QtActivity
|
|||
|
||||
private const val TAG = "AmneziaActivity"
|
||||
|
||||
private const val CAMERA_ACTION_CODE = 101
|
||||
private const val CREATE_FILE_ACTION_CODE = 102
|
||||
|
||||
class AmneziaActivity : QtActivity() {
|
||||
|
||||
private var tmpFileContentToSave: String = ""
|
||||
|
||||
private fun startQrCodeActivity() {
|
||||
val intent = Intent(this, CameraActivity::class.java)
|
||||
startActivityForResult(intent, CAMERA_ACTION_CODE)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
if (requestCode == CREATE_FILE_ACTION_CODE && resultCode == RESULT_OK) {
|
||||
data?.data?.also { uri ->
|
||||
|
@ -97,6 +91,8 @@ class AmneziaActivity : QtActivity() {
|
|||
@Suppress("unused")
|
||||
fun startQrCodeReader() {
|
||||
Log.v(TAG, "Start camera")
|
||||
startQrCodeActivity()
|
||||
Intent(this, CameraActivity::class.java).also {
|
||||
startActivity(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,16 @@
|
|||
package org.amnezia.vpn
|
||||
|
||||
import android.content.res.Configuration
|
||||
// import org.amnezia.vpn.shadowsocks.core.Core
|
||||
// import org.amnezia.vpn.shadowsocks.core.VpnManager
|
||||
import org.qtproject.qt.android.bindings.QtActivity
|
||||
import androidx.camera.camera2.Camera2Config
|
||||
import androidx.camera.core.CameraSelector
|
||||
import androidx.camera.core.CameraXConfig
|
||||
import org.qtproject.qt.android.bindings.QtApplication
|
||||
import android.app.Application
|
||||
|
||||
class AmneziaApplication: org.qtproject.qt.android.bindings.QtApplication() {
|
||||
class AmneziaApplication : QtApplication(), CameraXConfig.Provider {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
/* Core.init(this, QtActivity::class)
|
||||
VpnManager.getInstance().init(this) */
|
||||
}
|
||||
override fun getCameraXConfig(): CameraXConfig = CameraXConfig.Builder
|
||||
.fromConfig(Camera2Config.defaultConfig())
|
||||
.setMinimumLoggingLevel(android.util.Log.ERROR)
|
||||
.setAvailableCamerasLimiter(CameraSelector.DEFAULT_BACK_CAMERA)
|
||||
.build()
|
||||
|
||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||
super.onConfigurationChanged(newConfig)
|
||||
// Core.updateNotificationChannels()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,167 +4,151 @@ import android.Manifest
|
|||
import android.annotation.SuppressLint
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.MotionEvent.ACTION_DOWN
|
||||
import android.view.MotionEvent.ACTION_UP
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.camera.core.*
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
|
||||
import androidx.camera.core.CameraSelector
|
||||
import androidx.camera.core.ExperimentalGetImage
|
||||
import androidx.camera.core.FocusMeteringAction
|
||||
import androidx.camera.core.FocusMeteringAction.FLAG_AE
|
||||
import androidx.camera.core.FocusMeteringAction.FLAG_AF
|
||||
import androidx.camera.core.ImageAnalysis
|
||||
import androidx.camera.core.Preview
|
||||
import androidx.camera.lifecycle.ProcessCameraProvider
|
||||
import androidx.camera.view.PreviewView
|
||||
import androidx.core.content.ContextCompat
|
||||
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
|
||||
import com.google.mlkit.vision.barcode.BarcodeScannerOptions.Builder
|
||||
import com.google.mlkit.vision.barcode.BarcodeScanning
|
||||
import com.google.mlkit.vision.barcode.ZoomSuggestionOptions
|
||||
import com.google.mlkit.vision.barcode.common.Barcode
|
||||
import com.google.mlkit.vision.common.InputImage
|
||||
import org.amnezia.vpn.R
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
import org.amnezia.vpn.databinding.CameraPreviewBinding
|
||||
import org.amnezia.vpn.qt.QtAndroidController
|
||||
|
||||
private const val TAG = "CameraActivity"
|
||||
|
||||
class CameraActivity : AppCompatActivity() {
|
||||
class CameraActivity : ComponentActivity() {
|
||||
|
||||
private val CAMERA_REQUEST = 100
|
||||
|
||||
private lateinit var cameraExecutor: ExecutorService
|
||||
private lateinit var analyzerExecutor: ExecutorService
|
||||
|
||||
private lateinit var viewFinder: PreviewView
|
||||
|
||||
companion object {
|
||||
private lateinit var instance: CameraActivity
|
||||
|
||||
@JvmStatic fun getInstance(): CameraActivity {
|
||||
return instance
|
||||
}
|
||||
|
||||
@JvmStatic fun stopQrCodeReader() {
|
||||
CameraActivity.getInstance().finish()
|
||||
}
|
||||
}
|
||||
|
||||
external fun passDataToDecoder(data: String)
|
||||
private lateinit var viewBinding: CameraPreviewBinding
|
||||
private lateinit var cameraProvider: ProcessCameraProvider
|
||||
|
||||
@ExperimentalGetImage
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_camera)
|
||||
viewBinding = CameraPreviewBinding.inflate(layoutInflater)
|
||||
setContentView(viewBinding.root)
|
||||
|
||||
viewFinder = findViewById(R.id.viewFinder)
|
||||
|
||||
cameraExecutor = Executors.newSingleThreadExecutor()
|
||||
analyzerExecutor = Executors.newSingleThreadExecutor()
|
||||
|
||||
instance = this
|
||||
|
||||
checkPermissions()
|
||||
|
||||
configureVideoPreview()
|
||||
checkPermissions(onSuccess = ::startCamera, onFail = ::finish)
|
||||
}
|
||||
|
||||
private fun checkPermissions() {
|
||||
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
requestPermissions(arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
if (requestCode == CAMERA_REQUEST) {
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
Toast.makeText(this, "CameraX permission granted", Toast.LENGTH_SHORT).show();
|
||||
private fun checkPermissions(onSuccess: () -> Unit, onFail: () -> Unit) {
|
||||
if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
||||
onSuccess()
|
||||
} else {
|
||||
Toast.makeText(this, "CameraX permission denied", Toast.LENGTH_SHORT).show();
|
||||
val requestPermissionLauncher =
|
||||
registerForActivityResult(RequestPermission()) { isGranted ->
|
||||
if (isGranted) {
|
||||
Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show()
|
||||
onSuccess()
|
||||
} else {
|
||||
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show()
|
||||
onFail()
|
||||
}
|
||||
}
|
||||
requestPermissionLauncher.launch(Manifest.permission.CAMERA)
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("UnsafeOptInUsageError", "ClickableViewAccessibility")
|
||||
private fun configureVideoPreview() {
|
||||
@ExperimentalGetImage
|
||||
private fun startCamera() {
|
||||
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
|
||||
val imageCapture = ImageCapture.Builder().build()
|
||||
|
||||
cameraProviderFuture.addListener({
|
||||
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
|
||||
|
||||
val preview = Preview.Builder().build()
|
||||
|
||||
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
|
||||
|
||||
val imageAnalyzer = BarCodeAnalyzer()
|
||||
|
||||
val analysisUseCase = ImageAnalysis.Builder()
|
||||
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
|
||||
.build()
|
||||
|
||||
analysisUseCase.setAnalyzer(analyzerExecutor, imageAnalyzer)
|
||||
|
||||
try {
|
||||
preview.setSurfaceProvider(viewFinder.surfaceProvider)
|
||||
cameraProvider.unbindAll()
|
||||
val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture, analysisUseCase)
|
||||
viewFinder.setOnTouchListener(View.OnTouchListener { view: View, motionEvent: MotionEvent ->
|
||||
when (motionEvent.action) {
|
||||
MotionEvent.ACTION_DOWN -> return@OnTouchListener true
|
||||
MotionEvent.ACTION_UP -> {
|
||||
val factory = viewFinder.meteringPointFactory
|
||||
val point = factory.createPoint(motionEvent.x, motionEvent.y)
|
||||
val action = FocusMeteringAction.Builder(point).build()
|
||||
camera.cameraControl.startFocusAndMetering(action)
|
||||
return@OnTouchListener true
|
||||
}
|
||||
else -> return@OnTouchListener false
|
||||
}
|
||||
})
|
||||
} catch(exc: Exception) {
|
||||
Log.e("WUTT", "Use case binding failed", exc)
|
||||
}
|
||||
cameraProvider = cameraProviderFuture.get()
|
||||
bindPreview()
|
||||
bindImageAnalysis()
|
||||
}, ContextCompat.getMainExecutor(this))
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
cameraExecutor.shutdown()
|
||||
analyzerExecutor.shutdown()
|
||||
|
||||
super.onDestroy()
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private fun bindPreview() {
|
||||
val viewFinder = viewBinding.viewFinder
|
||||
val preview = Preview.Builder().build().also {
|
||||
it.setSurfaceProvider(viewFinder.surfaceProvider)
|
||||
}
|
||||
|
||||
val barcodesSet = mutableSetOf<String>()
|
||||
val camera = cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, preview)
|
||||
|
||||
private inner class BarCodeAnalyzer(): ImageAnalysis.Analyzer {
|
||||
viewFinder.setOnTouchListener { _, motionEvent ->
|
||||
when (motionEvent.action) {
|
||||
ACTION_DOWN -> true
|
||||
ACTION_UP -> {
|
||||
val point = viewFinder
|
||||
.meteringPointFactory.createPoint(motionEvent.x, motionEvent.x)
|
||||
|
||||
private val options = BarcodeScannerOptions.Builder()
|
||||
val action = FocusMeteringAction
|
||||
.Builder(point, FLAG_AF or FLAG_AE).build()
|
||||
|
||||
camera.cameraControl.startFocusAndMetering(action)
|
||||
true
|
||||
}
|
||||
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ExperimentalGetImage
|
||||
private fun bindImageAnalysis() {
|
||||
val imageAnalysis = ImageAnalysis.Builder().build()
|
||||
|
||||
val camera = cameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, imageAnalysis)
|
||||
|
||||
val barcodeScanner = BarcodeScanning.getClient(
|
||||
Builder()
|
||||
.setBarcodeFormats(Barcode.FORMAT_QR_CODE)
|
||||
.build()
|
||||
.setZoomSuggestionOptions(
|
||||
ZoomSuggestionOptions.Builder { zoomLevel ->
|
||||
camera.cameraControl.setZoomRatio(zoomLevel)
|
||||
true
|
||||
}.apply {
|
||||
camera.cameraInfo.zoomState.value?.maxZoomRatio?.let { maxZoomRation ->
|
||||
setMaxSupportedZoomRatio(maxZoomRation)
|
||||
}
|
||||
}.build()
|
||||
).build()
|
||||
)
|
||||
|
||||
private val scanner = BarcodeScanning.getClient(options)
|
||||
// optimization
|
||||
val checkedBarcodes = hashSetOf<String>()
|
||||
|
||||
@SuppressLint("UnsafeOptInUsageError")
|
||||
override fun analyze(imageProxy: ImageProxy) {
|
||||
val mediaImage = imageProxy.image
|
||||
|
||||
if (mediaImage != null) {
|
||||
val image = InputImage.fromMediaImage(mediaImage, imageProxy.imageInfo.rotationDegrees)
|
||||
|
||||
scanner.process(image)
|
||||
.addOnSuccessListener { barcodes ->
|
||||
if (barcodes.isNotEmpty()) {
|
||||
val barcode = barcodes[0]
|
||||
if (barcode != null) {
|
||||
val str = barcode?.displayValue ?: ""
|
||||
if (str.isNotEmpty()) {
|
||||
val isAdded = barcodesSet.add(str)
|
||||
if (isAdded) {
|
||||
passDataToDecoder(str)
|
||||
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(this)) { imageProxy ->
|
||||
imageProxy.image?.let { InputImage.fromMediaImage(it, imageProxy.imageInfo.rotationDegrees) }
|
||||
?.let { image ->
|
||||
barcodeScanner.process(image).addOnSuccessListener { barcodes ->
|
||||
barcodes.firstOrNull()?.let { barcode ->
|
||||
barcode.displayValue?.let { code ->
|
||||
if (code.isNotEmpty() && code !in checkedBarcodes) {
|
||||
if (QtAndroidController.decodeQrCode(code)) {
|
||||
barcodeScanner.close()
|
||||
stopCamera()
|
||||
}
|
||||
checkedBarcodes.add(code)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
imageProxy.close()
|
||||
}
|
||||
.addOnFailureListener {
|
||||
}.addOnFailureListener {
|
||||
Log.e(TAG, "Processing QR-code image failed: ${it.message}")
|
||||
}.addOnCompleteListener {
|
||||
imageProxy.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopCamera() {
|
||||
cameraProvider.unbindAll()
|
||||
finish()
|
||||
}
|
||||
}
|
|
@ -15,4 +15,6 @@ object QtAndroidController {
|
|||
external fun onStatisticsUpdate(rxBytes: Long, txBytes: Long)
|
||||
|
||||
external fun onConfigImported()
|
||||
|
||||
external fun decodeQrCode(data: String): Boolean
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
#include <QJsonDocument>
|
||||
|
||||
#include "android_controller.h"
|
||||
#include "ui/controllers/importController.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
|
@ -101,6 +102,7 @@ bool AndroidController::initialize()
|
|||
{"onVpnDisconnected", "()V", reinterpret_cast<void *>(onVpnDisconnected)},
|
||||
{"onStatisticsUpdate", "(JJ)V", reinterpret_cast<void *>(onStatisticsUpdate)},
|
||||
{"onConfigImported", "()V", reinterpret_cast<void *>(onConfigImported)},
|
||||
{"decodeQrCode", "(Ljava/lang/String;)Z", reinterpret_cast<bool *>(decodeQrCode)}
|
||||
};
|
||||
|
||||
QJniEnvironment env;
|
||||
|
@ -240,6 +242,7 @@ void AndroidController::onStatisticsUpdate(JNIEnv *env, jobject thiz, jlong rxBy
|
|||
emit AndroidController::instance()->statisticsUpdated((quint64) rxBytes, (quint64) txBytes);
|
||||
}
|
||||
|
||||
// static
|
||||
void AndroidController::onConfigImported(JNIEnv *env, jobject thiz)
|
||||
{
|
||||
Q_UNUSED(env);
|
||||
|
@ -247,3 +250,18 @@ void AndroidController::onConfigImported(JNIEnv *env, jobject thiz)
|
|||
|
||||
emit AndroidController::instance()->configImported();
|
||||
}
|
||||
|
||||
// static
|
||||
bool AndroidController::decodeQrCode(JNIEnv *env, jobject thiz, jstring data)
|
||||
{
|
||||
Q_UNUSED(thiz);
|
||||
|
||||
const char *buffer = env->GetStringUTFChars(data, nullptr);
|
||||
if (!buffer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QString code(buffer);
|
||||
env->ReleaseStringUTFChars(data, buffer);
|
||||
return ImportController::decodeQrCode(code);
|
||||
}
|
||||
|
|
|
@ -50,6 +50,7 @@ private:
|
|||
static void onVpnDisconnected(JNIEnv *env, jobject thiz);
|
||||
static void onStatisticsUpdate(JNIEnv *env, jobject thiz, jlong rxBytes, jlong txBytes);
|
||||
static void onConfigImported(JNIEnv *env, jobject thiz);
|
||||
static bool decodeQrCode(JNIEnv *env, jobject thiz, jstring data);
|
||||
|
||||
template <typename Ret, typename ...Args>
|
||||
static auto callActivityMethod(const char *methodName, const char *signature,
|
||||
|
|
|
@ -7,9 +7,7 @@
|
|||
|
||||
#include "core/errorstrings.h"
|
||||
#ifdef Q_OS_ANDROID
|
||||
#include "../../platforms/android/android_controller.h"
|
||||
#include "../../platforms/android/androidutils.h"
|
||||
#include <QJniObject>
|
||||
#include "platforms/android/android_controller.h"
|
||||
#endif
|
||||
#ifdef Q_OS_IOS
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
|
@ -48,10 +46,6 @@ namespace
|
|||
#if defined Q_OS_ANDROID
|
||||
ImportController *mInstance = nullptr;
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
constexpr auto AndroidCameraActivity = "org.amnezia.vpn.CameraActivity";
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
ImportController::ImportController(const QSharedPointer<ServersModel> &serversModel,
|
||||
|
@ -61,18 +55,6 @@ ImportController::ImportController(const QSharedPointer<ServersModel> &serversMo
|
|||
{
|
||||
#ifdef Q_OS_ANDROID
|
||||
mInstance = this;
|
||||
|
||||
AndroidUtils::runOnAndroidThreadAsync([]() {
|
||||
JNINativeMethod methods[] {
|
||||
{ "passDataToDecoder", "(Ljava/lang/String;)V", reinterpret_cast<void *>(onNewQrCodeDataChunk) },
|
||||
};
|
||||
|
||||
QJniObject javaClass(AndroidCameraActivity);
|
||||
QJniEnvironment env;
|
||||
jclass objectClass = env->GetObjectClass(javaClass.object<jobject>());
|
||||
env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0]));
|
||||
env->DeleteLocalRef(objectClass);
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -320,26 +302,20 @@ QJsonObject ImportController::extractWireGuardConfig(const QString &data)
|
|||
}
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
void ImportController::onNewQrCodeDataChunk(JNIEnv *env, jobject thiz, jstring data)
|
||||
static QMutex qrDecodeMutex;
|
||||
|
||||
// static
|
||||
bool ImportController::decodeQrCode(const QString &code)
|
||||
{
|
||||
Q_UNUSED(thiz);
|
||||
const char *buffer = env->GetStringUTFChars(data, nullptr);
|
||||
if (!buffer) {
|
||||
return;
|
||||
}
|
||||
QMutexLocker lock(&qrDecodeMutex);
|
||||
|
||||
QString parcelBody(buffer);
|
||||
env->ReleaseStringUTFChars(data, buffer);
|
||||
|
||||
if (mInstance != nullptr) {
|
||||
if (!mInstance->m_isQrCodeProcessed) {
|
||||
mInstance->m_qrCodeChunks.clear();
|
||||
mInstance->m_isQrCodeProcessed = true;
|
||||
mInstance->m_totalQrCodeChunksCount = 0;
|
||||
mInstance->m_receivedQrCodeChunksCount = 0;
|
||||
}
|
||||
mInstance->parseQrCodeChunk(parcelBody);
|
||||
}
|
||||
return mInstance->parseQrCodeChunk(code);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -360,17 +336,14 @@ void ImportController::startDecodingQr()
|
|||
|
||||
void ImportController::stopDecodingQr()
|
||||
{
|
||||
#if defined Q_OS_ANDROID
|
||||
QJniObject::callStaticMethod<void>(AndroidCameraActivity, "stopQrCodeReader", "()V");
|
||||
#endif
|
||||
emit qrDecodingFinished();
|
||||
}
|
||||
|
||||
void ImportController::parseQrCodeChunk(const QString &code)
|
||||
bool ImportController::parseQrCodeChunk(const QString &code)
|
||||
{
|
||||
// qDebug() << code;
|
||||
if (!m_isQrCodeProcessed)
|
||||
return;
|
||||
return false;
|
||||
|
||||
// check if chunk received
|
||||
QByteArray ba = QByteArray::fromBase64(code.toUtf8(), QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals);
|
||||
|
@ -404,6 +377,7 @@ void ImportController::parseQrCodeChunk(const QString &code)
|
|||
m_isQrCodeProcessed = false;
|
||||
qDebug() << "stopDecodingQr";
|
||||
stopDecodingQr();
|
||||
return true;
|
||||
} else {
|
||||
qDebug() << "error while extracting data from qr";
|
||||
m_qrCodeChunks.clear();
|
||||
|
@ -417,8 +391,10 @@ void ImportController::parseQrCodeChunk(const QString &code)
|
|||
m_isQrCodeProcessed = false;
|
||||
qDebug() << "stopDecodingQr";
|
||||
stopDecodingQr();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
double ImportController::getQrCodeScanProgressBarValue()
|
||||
|
|
|
@ -7,9 +7,6 @@
|
|||
#include "core/defs.h"
|
||||
#include "ui/models/containers_model.h"
|
||||
#include "ui/models/servers_model.h"
|
||||
#ifdef Q_OS_ANDROID
|
||||
#include "jni.h"
|
||||
#endif
|
||||
|
||||
class ImportController : public QObject
|
||||
{
|
||||
|
@ -30,12 +27,16 @@ public slots:
|
|||
|
||||
#if defined Q_OS_ANDROID || defined Q_OS_IOS
|
||||
void startDecodingQr();
|
||||
void parseQrCodeChunk(const QString &code);
|
||||
bool parseQrCodeChunk(const QString &code);
|
||||
|
||||
double getQrCodeScanProgressBarValue();
|
||||
QString getQrCodeScanProgressString();
|
||||
#endif
|
||||
|
||||
#if defined Q_OS_ANDROID
|
||||
static bool decodeQrCode(const QString &code);
|
||||
#endif
|
||||
|
||||
signals:
|
||||
void importFinished();
|
||||
void importErrorOccurred(const QString &errorMessage);
|
||||
|
@ -50,9 +51,6 @@ private:
|
|||
#if defined Q_OS_ANDROID || defined Q_OS_IOS
|
||||
void stopDecodingQr();
|
||||
#endif
|
||||
#if defined Q_OS_ANDROID
|
||||
static void onNewQrCodeDataChunk(JNIEnv *env, jobject thiz, jstring data);
|
||||
#endif
|
||||
|
||||
QSharedPointer<ServersModel> m_serversModel;
|
||||
QSharedPointer<ContainersModel> m_containersModel;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue