added qr code scanner for ios

This commit is contained in:
vladimir.kuznetsov 2023-08-13 11:28:32 +05:00
parent c1c68cf72d
commit 14fa0b4fd3
19 changed files with 128 additions and 69 deletions

View file

@ -42,8 +42,11 @@ namespace
return ConfigTypes::Amnezia;
}
#ifdef Q_OS_ANDROID
#if defined Q_OS_ANDROID
ImportController *mInstance = nullptr;
#endif
#ifdef Q_OS_ANDROID
constexpr auto AndroidCameraActivity = "org.amnezia.vpn.qt.CameraActivity";
#endif
} // namespace
@ -264,17 +267,6 @@ QJsonObject ImportController::extractWireGuardConfig(const QString &data)
}
#ifdef Q_OS_ANDROID
void ImportController::startDecodingQr()
{
AndroidController::instance()->startQrReaderActivity();
}
void ImportController::stopDecodingQr()
{
QJniObject::callStaticMethod<void>(AndroidCameraActivity, "stopQrCodeReader", "()V");
emit qrDecodingFinished();
}
void ImportController::onNewQrCodeDataChunk(JNIEnv *env, jobject thiz, jstring data)
{
Q_UNUSED(thiz);
@ -296,6 +288,30 @@ void ImportController::onNewQrCodeDataChunk(JNIEnv *env, jobject thiz, jstring d
mInstance->parseQrCodeChunk(parcelBody);
}
}
#endif
#if defined Q_OS_ANDROID || defined Q_OS_IOS
void ImportController::startDecodingQr()
{
m_qrCodeChunks.clear();
m_totalQrCodeChunksCount = 0;
m_receivedQrCodeChunksCount = 0;
#if defined Q_OS_IOS
m_isQrCodeProcessed = true;
#endif
#if defined Q_OS_ANDROID
AndroidController::instance()->startQrReaderActivity();
#endif
}
void ImportController::stopDecodingQr()
{
#if defined Q_OS_ANDROID
QJniObject::callStaticMethod<void>(AndroidCameraActivity, "stopQrCodeReader", "()V");
#endif
emit qrDecodingFinished();
}
void ImportController::parseQrCodeChunk(const QString &code)
{
@ -333,8 +349,10 @@ void ImportController::parseQrCodeChunk(const QString &code)
bool ok = extractConfigFromQr(data);
if (ok) {
m_isQrCodeProcessed = false;
qDebug() << "stopDecodingQr";
stopDecodingQr();
} else {
qDebug() << "error while extracting data from qr";
m_qrCodeChunks.clear();
m_totalQrCodeChunksCount = 0;
m_receivedQrCodeChunksCount = 0;
@ -344,8 +362,19 @@ void ImportController::parseQrCodeChunk(const QString &code)
bool ok = extractConfigFromQr(ba);
if (ok) {
m_isQrCodeProcessed = false;
qDebug() << "stopDecodingQr";
stopDecodingQr();
}
}
}
double ImportController::getQrCodeScanProgressBarValue()
{
return (1.0 / m_totalQrCodeChunksCount) * m_receivedQrCodeChunksCount;
}
QString ImportController::getQrCodeScanProgressString()
{
return tr("Scanned %1 of %2.").arg(m_receivedQrCodeChunksCount).arg(m_totalQrCodeChunksCount);
}
#endif

View file

@ -28,8 +28,12 @@ public slots:
QString getConfig();
QString getConfigFileName();
#if defined Q_OS_ANDROID
#if defined Q_OS_ANDROID || defined Q_OS_IOS
void startDecodingQr();
void parseQrCodeChunk(const QString &code);
double getQrCodeScanProgressBarValue();
QString getQrCodeScanProgressString();
#endif
signals:
@ -43,10 +47,11 @@ private:
QJsonObject extractOpenVpnConfig(const QString &data);
QJsonObject extractWireGuardConfig(const QString &data);
#if defined Q_OS_ANDROID
#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);
void parseQrCodeChunk(const QString &code);
#endif
QSharedPointer<ServersModel> m_serversModel;
@ -56,7 +61,7 @@ private:
QJsonObject m_config;
QString m_configFileName;
#if defined Q_OS_ANDROID
#if defined Q_OS_ANDROID || defined Q_OS_IOS
QMap<int, QByteArray> m_qrCodeChunks;
bool m_isQrCodeProcessed;
int m_totalQrCodeChunksCount;