Files
pay/modules/backup-sync/BackupNFCHandler.cpp
T

117 lines
3.4 KiB
C++
Raw Permalink Normal View History

2025-11-12 13:54:22 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2025 Tom Zander <tom@flowee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BackupNFCHandler.h"
#include <SeedsBackup.h>
#include <FloweePay.h>
#include <AccountInfo.h>
#include <utils/Logger.h>
#include <utils/streaming/BufferPools.h>
#include <utils/streaming/StreamingUtils.h>
#ifdef TARGET_OS_Android
#include <QJniObject>
#include <QGuiApplication>
#endif
BackupNFCHandler::BackupNFCHandler(QObject *parent)
: QObject(parent)
{
connect(FloweePay::instance(), &FloweePay::nfcStatusChanged, this, [=]() {
auto newStatus = FloweePay::instance()->nfcStatus();
if (newStatus == WalletEnums::NFC_WriteSuccess)
m_writing = false;
if (m_writing && newStatus != WalletEnums::NFC_WriteWait)
QTimer::singleShot(200, this, &BackupNFCHandler::start);
emit nfcStatusChanged();
});
#ifdef TARGET_OS_Android
QJniObject(QNativeInterface::QAndroidApplication::context())
.callObjectMethod("checkNfcStatus", "()V");
#endif
}
BackupNFCHandler::~BackupNFCHandler()
{
#ifdef TARGET_OS_Android
QJniObject(QNativeInterface::QAndroidApplication::context())
.callObjectMethod("stopNfcWriteMode", "()V");
#endif
}
void BackupNFCHandler::start()
{
if (!m_account)
return;
if (!m_account->isHDWallet())
return;
if (nfcStatus() == WalletEnums::NFC_Disabled)
return;
m_writing = true;
SeedsBackup data(m_account->wallet());
auto backupData = data.toBuffer();
#ifdef TARGET_OS_Android
QJniEnvironment env;
// Convert QByteArray to jbyteArray
jsize length = backupData.size();
if (length == 0)
return;
jbyteArray javaArray = env->NewByteArray(length);
if (!javaArray)
return;
assert(javaArray);
env->SetByteArrayRegion(javaArray, 0, length, reinterpret_cast<const jbyte*>(backupData.begin()));
QJniObject(QNativeInterface::QAndroidApplication::context())
.callObjectMethod("startNfcWriteMode", "([B)V", javaArray);
env->DeleteLocalRef(javaArray); // optional, but useful.
#endif
}
WalletEnums::NfcStatus BackupNFCHandler::nfcStatus() const
{
// this handler only has a lifetime limited to our modules'
// gui being shown, as such the actual status has to be stored
// in the App singleton which has a lifetime that matches better.
return FloweePay::instance()->nfcStatus();
}
QObject *BackupNFCHandler::account() const
{
return m_account;
}
void BackupNFCHandler::setAccount(QObject *account)
{
if (m_account == account)
return;
m_account = qobject_cast<AccountInfo*>(account);
emit accountChanged();
}
void BackupNFCHandler::openSettings()
{
#ifdef TARGET_OS_Android
QJniObject(QNativeInterface::QAndroidApplication::context())
.callObjectMethod("openNFCSettings", "()V");
#endif
}