Add network state listening and reconnection

Vpn reconnects when the default network is changed
This commit is contained in:
albexk 2023-12-11 15:16:50 +03:00
parent 8cc5846808
commit 1576aed1ea
15 changed files with 240 additions and 223 deletions

View file

@ -67,6 +67,14 @@ AndroidController::AndroidController() : QObject()
},
Qt::QueuedConnection);
connect(
this, &AndroidController::vpnReconnecting, this,
[this]() {
qDebug() << "Android event: VPN reconnecting";
emit connectionStateChanged(Vpn::ConnectionState::Reconnecting);
},
Qt::QueuedConnection);
connect(
this, &AndroidController::configImported, this,
[]() {
@ -101,6 +109,7 @@ bool AndroidController::initialize()
{"onVpnPermissionRejected", "()V", reinterpret_cast<void *>(onVpnPermissionRejected)},
{"onVpnConnected", "()V", reinterpret_cast<void *>(onVpnConnected)},
{"onVpnDisconnected", "()V", reinterpret_cast<void *>(onVpnDisconnected)},
{"onVpnReconnecting", "()V", reinterpret_cast<void *>(onVpnReconnecting)},
{"onStatisticsUpdate", "(JJ)V", reinterpret_cast<void *>(onStatisticsUpdate)},
{"onConfigImported", "()V", reinterpret_cast<void *>(onConfigImported)},
{"decodeQrCode", "(Ljava/lang/String;)Z", reinterpret_cast<bool *>(decodeQrCode)}
@ -187,6 +196,7 @@ Vpn::ConnectionState AndroidController::convertState(AndroidController::Connecti
case AndroidController::ConnectionState::CONNECTING: return Vpn::ConnectionState::Connecting;
case AndroidController::ConnectionState::DISCONNECTED: return Vpn::ConnectionState::Disconnected;
case AndroidController::ConnectionState::DISCONNECTING: return Vpn::ConnectionState::Disconnecting;
case AndroidController::ConnectionState::RECONNECTING: return Vpn::ConnectionState::Reconnecting;
case AndroidController::ConnectionState::UNKNOWN: return Vpn::ConnectionState::Unknown;
}
}
@ -199,6 +209,7 @@ QString AndroidController::textConnectionState(AndroidController::ConnectionStat
case AndroidController::ConnectionState::CONNECTING: return "CONNECTING";
case AndroidController::ConnectionState::DISCONNECTED: return "DISCONNECTED";
case AndroidController::ConnectionState::DISCONNECTING: return "DISCONNECTING";
case AndroidController::ConnectionState::RECONNECTING: return "RECONNECTING";
case AndroidController::ConnectionState::UNKNOWN: return "UNKNOWN";
}
}
@ -260,6 +271,15 @@ void AndroidController::onVpnDisconnected(JNIEnv *env, jobject thiz)
emit AndroidController::instance()->vpnDisconnected();
}
// static
void AndroidController::onVpnReconnecting(JNIEnv *env, jobject thiz)
{
Q_UNUSED(env);
Q_UNUSED(thiz);
emit AndroidController::instance()->vpnReconnecting();
}
// static
void AndroidController::onStatisticsUpdate(JNIEnv *env, jobject thiz, jlong rxBytes, jlong txBytes)
{

View file

@ -23,6 +23,7 @@ public:
CONNECTING,
DISCONNECTED,
DISCONNECTING,
RECONNECTING,
UNKNOWN
};
@ -40,6 +41,7 @@ signals:
void vpnPermissionRejected();
void vpnConnected();
void vpnDisconnected();
void vpnReconnecting();
void statisticsUpdated(quint64 rxBytes, quint64 txBytes);
void configImported();
void importConfigFromOutside(QString &data);
@ -60,6 +62,7 @@ private:
static void onVpnPermissionRejected(JNIEnv *env, jobject thiz);
static void onVpnConnected(JNIEnv *env, jobject thiz);
static void onVpnDisconnected(JNIEnv *env, jobject thiz);
static void onVpnReconnecting(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);