diff --git a/client/3rd/qtkeychain b/client/3rd/qtkeychain index f197cdb9..c6f0b663 160000 --- a/client/3rd/qtkeychain +++ b/client/3rd/qtkeychain @@ -1 +1 @@ -Subproject commit f197cdb935b0cfd9881fdc6860874cb8379d1238 +Subproject commit c6f0b66318f8da6917fb4681103f7303b1836194 diff --git a/client/android/AndroidManifest.xml b/client/android/AndroidManifest.xml index 55cffe20..18ffbd9b 100644 --- a/client/android/AndroidManifest.xml +++ b/client/android/AndroidManifest.xml @@ -1,10 +1,5 @@ - + @@ -23,34 +18,19 @@ - - - - + + - - - - + + + - + @@ -58,14 +38,13 @@ - - - - + + + - + @@ -73,14 +52,13 @@ - - - - + + + - + @@ -88,101 +66,22 @@ - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - + - - - - - - - - - - - - - - - - - + + - - - + - - - + + diff --git a/client/android/build.gradle b/client/android/build.gradle index 12129720..69eda3e5 100644 --- a/client/android/build.gradle +++ b/client/android/build.gradle @@ -20,7 +20,7 @@ buildscript { } dependencies { - classpath 'com.android.tools.build:gradle:4.0.0' + classpath 'com.android.tools.build:gradle:7.2.1' classpath 'com.github.ben-manes:gradle-versions-plugin:0.21.0' classpath 'com.vanniktech:gradle-maven-publish-plugin:0.8.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" @@ -72,7 +72,11 @@ android { compileSdkVersion androidCompileSdkVersion.toInteger() - //buildToolsVersion '28.0.3' + buildToolsVersion androidBuildToolsVersion + ndkVersion androidNdkVersion + + // Extract native libraries from the APK + packagingOptions.jniLibs.useLegacyPackaging true dexOptions { javaMaxHeapSize "3g" @@ -81,9 +85,9 @@ android { sourceSets { main { manifest.srcFile 'AndroidManifest.xml' - java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java'] - aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl'] - res.srcDirs = [qt5AndroidDir + '/res', 'res'] + java.srcDirs = [qtAndroidDir + '/src', 'src', 'java'] + aidl.srcDirs = [qtAndroidDir + '/src', 'src', 'aidl'] + res.srcDirs = [qtAndroidDir + '/res', 'res'] resources.srcDirs = ['resources'] renderscript.srcDirs = ['src'] assets.srcDirs = ['assets'] @@ -139,6 +143,7 @@ android { debug { //applicationIdSuffix ".debug" //versionNameSuffix "-debug" + minifyEnabled false externalNativeBuild { cmake { arguments "-DANDROID_PACKAGE_NAME=${groupName}", "-DGRADLE_USER_HOME=${project.gradle.gradleUserHomeDir}" diff --git a/client/android/gradle/wrapper/gradle-wrapper.properties b/client/android/gradle/wrapper/gradle-wrapper.properties index 4e1cc9db..669386b8 100644 --- a/client/android/gradle/wrapper/gradle-wrapper.properties +++ b/client/android/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/client/android/res/values/libs.xml b/client/android/res/values/libs.xml index 6b1a4a2a..fe63866f 100644 --- a/client/android/res/values/libs.xml +++ b/client/android/res/values/libs.xml @@ -1,11 +1,6 @@ - - https://download.qt.io/ministro/android/qt5/qt-5.14 - - - + @@ -19,4 +14,8 @@ + + + + diff --git a/client/android/src/org/amnezia/vpn/AuthHelper.java b/client/android/src/org/amnezia/vpn/AuthHelper.java index b30aa8f5..940d03c2 100644 --- a/client/android/src/org/amnezia/vpn/AuthHelper.java +++ b/client/android/src/org/amnezia/vpn/AuthHelper.java @@ -3,7 +3,7 @@ package org.amnezia.vpn; import android.content.Context; import android.app.KeyguardManager; import android.content.Intent; -import org.qtproject.qt5.android.bindings.QtActivity; +import org.qtproject.qt.android.bindings.QtActivity; import static android.content.Context.KEYGUARD_SERVICE; diff --git a/client/android/src/org/amnezia/vpn/VPNService.kt b/client/android/src/org/amnezia/vpn/VPNService.kt index 23e9ff0c..a01e3852 100644 --- a/client/android/src/org/amnezia/vpn/VPNService.kt +++ b/client/android/src/org/amnezia/vpn/VPNService.kt @@ -153,31 +153,6 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface { private var flags = 0 private var startId = 0 - private lateinit var mMessenger: Messenger - - internal class ExternalConfigImportHandler( - context: Context, - private val serviceBinder: VPNServiceBinder, - private val applicationContext: Context = context.applicationContext - ) : Handler() { - - override fun handleMessage(msg: Message) { - when (msg.what) { - IMPORT_COMMAND_CODE -> { - val data = msg.data.getString(IMPORT_CONFIG_KEY) - - if (data != null) { - serviceBinder.importConfig(data) - } - } - - else -> { - super.handleMessage(msg) - } - } - } - } - fun init() { if (mAlreadyInitialised) { return @@ -216,13 +191,6 @@ class VPNService : BaseVpnService(), LocalDnsService.Interface { override fun onBind(intent: Intent): IBinder { Log.v(tag, "Aman: onBind....................") - if (intent.action != null && intent.action == IMPORT_ACTION_CODE) { - Log.v(tag, "Service bind for import of config") - mMessenger = Messenger(ExternalConfigImportHandler(this, mBinder)) - return mMessenger.binder - } - - Log.v(tag, "Regular service bind") when (mProtocol) { "shadowsocks" -> { when (intent.action) { diff --git a/client/android/src/org/amnezia/vpn/VPNServiceBinder.kt b/client/android/src/org/amnezia/vpn/VPNServiceBinder.kt index 640a0657..239269a5 100644 --- a/client/android/src/org/amnezia/vpn/VPNServiceBinder.kt +++ b/client/android/src/org/amnezia/vpn/VPNServiceBinder.kt @@ -33,6 +33,7 @@ class VPNServiceBinder(service: VPNService) : Binder() { const val setNotificationText = 8 const val setFallBackNotification = 9 const val shareConfig = 10 + const val importConfig = 11 } /** @@ -75,13 +76,14 @@ class VPNServiceBinder(service: VPNService) : Binder() { ACTIONS.resumeActivate -> { // [data] is empty // Activate the current tunnel - try { - mResumeConfig?.let { this.mService.turnOn(it) } + Log.i(tag, "resume activate") + try { + mResumeConfig?.let { this.mService.turnOn(it) } } catch (e: Exception) { Log.e(tag, "An Error occurred while enabling the VPN: ${e.localizedMessage}") } return true - } + } ACTIONS.deactivate -> { // [data] here is empty @@ -90,6 +92,7 @@ class VPNServiceBinder(service: VPNService) : Binder() { } ACTIONS.registerEventListener -> { + Log.i(tag, "register: start") // [data] contains the Binder that we need to dispatch the Events val binder = data.readStrongBinder() mListener = binder @@ -150,6 +153,23 @@ class VPNServiceBinder(service: VPNService) : Binder() { return true } + ACTIONS.importConfig -> { + val buffer = data.readString() + + val obj = JSONObject() + obj.put("config", buffer) + + val resultString = obj.toString() + + Log.i(tag, "Transact import config request") + + if (mListener != null) { + dispatchEvent(EVENTS.configImport, resultString) + } else { + mImportedConfig = resultString + } + } + IBinder.LAST_CALL_TRANSACTION -> { Log.e(tag, "The OS Requested to shut down the VPN") this.mService.turnOff() @@ -176,9 +196,12 @@ class VPNServiceBinder(service: VPNService) : Binder() { try { mListener?.let { if (it.isBinderAlive) { + Log.i(tag, "Dispatching event: binder alive") val data = Parcel.obtain() data.writeByteArray(payload?.toByteArray(charset("UTF-8"))) it.transact(code, data, Parcel.obtain(), 0) + } else { + Log.i(tag, "Dispatching event: binder NOT alive") } } } catch (e: DeadObjectException) { @@ -197,23 +220,7 @@ class VPNServiceBinder(service: VPNService) : Binder() { const val statisticUpdate = 3 const val backendLogs = 4 const val activationError = 5 - const val configImport = 6 - } - - fun importConfig(config: String) { - val obj = JSONObject() - obj.put("config", config) - - val resultString = obj.toString() - - Log.i(tag, "Transact import config request") - - if (mListener != null) { - Log.i(tag, "binder alive") - dispatchEvent(EVENTS.configImport, resultString) - } else { - Log.i(tag, "binder NOT alive") - mImportedConfig = resultString - } + const val permissionRequired = 6 + const val configImport = 7 } } diff --git a/client/android/src/org/amnezia/vpn/qt/AmneziaApp.kt b/client/android/src/org/amnezia/vpn/qt/AmneziaApp.kt index 7888f6d6..24c33ffc 100644 --- a/client/android/src/org/amnezia/vpn/qt/AmneziaApp.kt +++ b/client/android/src/org/amnezia/vpn/qt/AmneziaApp.kt @@ -3,8 +3,8 @@ package org.amnezia.vpn.qt import android.content.res.Configuration import org.amnezia.vpn.shadowsocks.core.Core import org.amnezia.vpn.shadowsocks.core.VpnManager -import org.qtproject.qt5.android.bindings.QtActivity -import org.qtproject.qt5.android.bindings.QtApplication +import org.qtproject.qt.android.bindings.QtActivity +import org.qtproject.qt.android.bindings.QtApplication import android.app.Application class AmneziaApp: Application() { diff --git a/client/android/src/org/amnezia/vpn/qt/VPNActivity.kt b/client/android/src/org/amnezia/vpn/qt/VPNActivity.kt index c5b5107e..673c4b59 100644 --- a/client/android/src/org/amnezia/vpn/qt/VPNActivity.kt +++ b/client/android/src/org/amnezia/vpn/qt/VPNActivity.kt @@ -1,3 +1,7 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + package org.amnezia.vpn.qt; import android.Manifest @@ -20,18 +24,33 @@ import org.amnezia.vpn.VPNServiceBinder import org.amnezia.vpn.IMPORT_COMMAND_CODE import org.amnezia.vpn.IMPORT_ACTION_CODE import org.amnezia.vpn.IMPORT_CONFIG_KEY -import org.qtproject.qt5.android.bindings.QtActivity +import org.qtproject.qt.android.bindings.QtActivity import java.io.* -class VPNActivity : org.qtproject.qt5.android.bindings.QtActivity() { +class VPNActivity : org.qtproject.qt.android.bindings.QtActivity() { private var configString: String? = null - private var vpnServiceBinder: Messenger? = null + private var vpnServiceBinder: IBinder? = null private var isBound = false private val TAG = "VPNActivity" private val STORAGE_PERMISSION_CODE = 42 + companion object { + private lateinit var instance: VPNActivity + + @JvmStatic fun getInstance(): VPNActivity { + return instance + } + + @JvmStatic fun connectService() { + VPNActivity.getInstance().initServiceConnection() + } + + @JvmStatic fun sendToService(actionCode: Int, body: String) { + VPNActivity.getInstance().dispatchParcel(actionCode, body) + } + } override fun onCreate(savedInstanceState: Bundle?) { val newIntent = intent @@ -42,6 +61,48 @@ class VPNActivity : org.qtproject.qt5.android.bindings.QtActivity() { } super.onCreate(savedInstanceState) + + instance = this; + } + + override fun getSystemService(name: String): Any? { + return if (Build.VERSION.SDK_INT >= 29 && name == "clipboard") { + // QT will always attempt to read the clipboard if content is there. + // since we have no use of the clipboard in android 10+ + // we _can_ return null + // And we defnitly should since android 12 displays clipboard access. + null + } else { + super.getSystemService(name) + } + } + + external fun handleBackButton(): Boolean + + external fun onServiceMessage(actionCode: Int, body: String?) + external fun qtOnServiceConnected() + external fun qtOnServiceDisconnected() + + private fun dispatchParcel(actionCode: Int, body: String) { + if (!isBound) { + Log.d(TAG, "dispatchParcel: not bound") + return + } else { + Log.d(TAG, "dispatchParcel: bound") + } + + val out: Parcel = Parcel.obtain() + out.writeByteArray(body.toByteArray()) + + try { + vpnServiceBinder?.transact(actionCode, out, Parcel.obtain(), 0) + } catch (e: DeadObjectException) { + isBound = false + vpnServiceBinder = null + qtOnServiceDisconnected() + } catch (e: RemoteException) { + e.printStackTrace() + } } override fun onNewIntent(newIntent: Intent) { @@ -63,19 +124,11 @@ class VPNActivity : org.qtproject.qt5.android.bindings.QtActivity() { override fun onResume() { super.onResume() - if (configString != null && !isBound) { - bindVpnService() + if (configString != null && isBound) { + sendImportConfigCommand() } } - override fun onPause() { - if (vpnServiceBinder != null && isBound) { - unbindService(connection) - isBound = false - } - super.onPause() - } - private fun isReadStorageAllowed(): Boolean { val permissionStatus = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) return permissionStatus == PackageManager.PERMISSION_GRANTED @@ -90,8 +143,15 @@ class VPNActivity : org.qtproject.qt5.android.bindings.QtActivity() { if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.d(TAG, "Storage read permission granted") + if (configString == null) { + configString = processIntent(intent, intent.action!!) + } + if (configString != null) { - bindVpnService() + Log.d(TAG, "not empty") + sendImportConfigCommand() + } else { + Log.d(TAG, "empty") } } else { Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show() @@ -99,17 +159,6 @@ class VPNActivity : org.qtproject.qt5.android.bindings.QtActivity() { } } - private fun bindVpnService() { - try { - val intent = Intent(this, VPNService::class.java) - intent.action = IMPORT_ACTION_CODE - - bindService(intent, connection, Context.BIND_AUTO_CREATE) - } catch (e: Exception) { - e.printStackTrace() - } - } - private fun processIntent(intent: Intent, action: String): String? { val scheme = intent.scheme @@ -158,23 +207,35 @@ class VPNActivity : org.qtproject.qt5.android.bindings.QtActivity() { } } + private fun sendImportConfigCommand() { + if (configString != null) { + val msg: Parcel = Parcel.obtain() + msg.writeString(configString!!) + + try { + vpnServiceBinder?.transact(ACTION_IMPORT_CONFIG, msg, Parcel.obtain(), 0) + } catch (e: RemoteException) { + e.printStackTrace() + } + + configString = null + } + } + private var connection: ServiceConnection = object : ServiceConnection { override fun onServiceConnected(className: ComponentName, binder: IBinder) { - vpnServiceBinder = Messenger(binder) + vpnServiceBinder = binder - if (configString != null) { - val msg: Message = Message.obtain(null, IMPORT_COMMAND_CODE, 0, 0) - val bundle = Bundle() - bundle.putString(IMPORT_CONFIG_KEY, configString!!) - msg.data = bundle - - try { - vpnServiceBinder?.send(msg) - } catch (e: RemoteException) { - e.printStackTrace() - } - - configString = null + // This is called when the connection with the service has been + // established, giving us the object we can use to + // interact with the service. We are communicating with the + // service using a Messenger, so here we get a client-side + // representation of that from the raw IBinder object. + if (registerBinder()){ + qtOnServiceConnected(); + } else { + qtOnServiceDisconnected(); + return } isBound = true @@ -183,9 +244,75 @@ class VPNActivity : org.qtproject.qt5.android.bindings.QtActivity() { override fun onServiceDisconnected(className: ComponentName) { vpnServiceBinder = null isBound = false + qtOnServiceDisconnected(); } } + private fun registerBinder(): Boolean { + val binder = VPNClientBinder() + val out: Parcel = Parcel.obtain() + out.writeStrongBinder(binder) + + try { + // Register our IBinder Listener + vpnServiceBinder?.transact(ACTION_REGISTER_LISTENER, out, Parcel.obtain(), 0) + return true + } catch (e: DeadObjectException) { + isBound = false + vpnServiceBinder = null + } catch (e: RemoteException) { + e.printStackTrace() + } + return false + } + + private fun initServiceConnection() { + // We already have a connection to the service, + // just need to re-register the binder + if (isBound && vpnServiceBinder!!.isBinderAlive() && registerBinder()) { + qtOnServiceConnected() + return + } + + bindService(Intent(this, VPNService::class.java), connection, Context.BIND_AUTO_CREATE) + } + + // TODO: Move all ipc codes into a shared lib. + // this is getting out of hand. + private val PERMISSION_TRANSACTION = 1337 + private val ACTION_REGISTER_LISTENER = 3 + private val ACTION_RESUME_ACTIVATE = 7 + private val ACTION_IMPORT_CONFIG = 11 + private val EVENT_PERMISSION_REQURED = 6 + private val EVENT_DISCONNECTED = 2 + + + fun onPermissionRequest(code: Int, data: Parcel?) { + if (code != EVENT_PERMISSION_REQURED) { + return + } + + val x = Intent() + x.readFromParcel(data) + + startActivityForResult(x, PERMISSION_TRANSACTION) + } + + override protected fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == PERMISSION_TRANSACTION) { + // THATS US! + if (resultCode == RESULT_OK) { + // Prompt accepted, tell service to retry. + dispatchParcel(ACTION_RESUME_ACTIVATE, "") + } else { + // Tell the Client we've disconnected + onServiceMessage(EVENT_DISCONNECTED, "") + } + return + } + super.onActivityResult(requestCode, resultCode, data) + } + override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { if (keyCode == KeyEvent.KEYCODE_BACK && event.repeatCount == 0) { onBackPressed() diff --git a/client/android/src/org/amnezia/vpn/qt/VPNApplication.java b/client/android/src/org/amnezia/vpn/qt/VPNApplication.java index 29514633..639b5a1e 100644 --- a/client/android/src/org/amnezia/vpn/qt/VPNApplication.java +++ b/client/android/src/org/amnezia/vpn/qt/VPNApplication.java @@ -5,7 +5,7 @@ import androidx.annotation.NonNull; import org.amnezia.vpn.shadowsocks.core.Core; import org.amnezia.vpn.shadowsocks.core.VpnManager; -public class VPNApplication extends org.qtproject.qt5.android.bindings.QtApplication { +public class VPNApplication extends org.qtproject.qt.android.bindings.QtApplication { private static VPNApplication instance; @Override diff --git a/client/android/src/org/amnezia/vpn/qt/VPNClientBinder.kt b/client/android/src/org/amnezia/vpn/qt/VPNClientBinder.kt new file mode 100644 index 00000000..6a8fa086 --- /dev/null +++ b/client/android/src/org/amnezia/vpn/qt/VPNClientBinder.kt @@ -0,0 +1,27 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.amnezia.vpn.qt + +import android.os.Binder +import android.os.Parcel +import android.util.Log + +const val permissionRequired = 6 + +class VPNClientBinder() : Binder() { + + override fun onTransact(code: Int, data: Parcel, reply: Parcel?, flags: Int): Boolean { + if (code == permissionRequired) { + VPNActivity.getInstance().onPermissionRequest(code, data) + return true + } + + val buffer = data.createByteArray() + val stringData = buffer?.let { String(it) } + VPNActivity.getInstance().onServiceMessage(code, stringData) + + return true + } +} diff --git a/client/android/src/org/ftylitak/qzxing/QZXingLiveActivity.java b/client/android/src/org/ftylitak/qzxing/QZXingLiveActivity.java index 37ccb6ce..b6a4649d 100644 --- a/client/android/src/org/ftylitak/qzxing/QZXingLiveActivity.java +++ b/client/android/src/org/ftylitak/qzxing/QZXingLiveActivity.java @@ -2,7 +2,7 @@ package org.ftylitak.qzxing; import android.Manifest; import android.content.pm.PackageManager; -import org.qtproject.qt5.android.bindings.QtActivity; +import org.qtproject.qt.android.bindings.QtActivity; import static org.ftylitak.qzxing.Utilities.REQUEST_CAMERA; public class QZXingLiveActivity extends QtActivity { diff --git a/client/client.pro b/client/client.pro index f365cbec..a5d1f4f5 100644 --- a/client/client.pro +++ b/client/client.pro @@ -1,4 +1,4 @@ -QT += widgets core gui network xml remoteobjects quick svg quickcontrols2 core5compat +QT += widgets core gui network xml remoteobjects quick svg quickcontrols2 equals(QT_MAJOR_VERSION, 6): QT += core5compat TARGET = AmneziaVPN @@ -46,6 +46,8 @@ HEADERS += \ debug.h \ defines.h \ managementserver.h \ + platforms/android/androidutils.h \ + platforms/android/androidvpnactivity.h \ platforms/ios/MobileUtils.h \ platforms/linux/leakdetector.h \ protocols/protocols_defs.h \ @@ -108,6 +110,8 @@ SOURCES += \ debug.cpp \ main.cpp \ managementserver.cpp \ + platforms/android/androidutils.cpp \ + platforms/android/androidvpnactivity.cpp \ platforms/ios/MobileUtils.cpp \ platforms/linux/leakdetector.cpp \ protocols/protocols_defs.cpp \ @@ -246,15 +250,12 @@ android { versionAtLeast(QT_VERSION, 6.0.0) { # We need to include qtprivate api's # As QAndroidBinder is not yet implemented with a public api - QT+=core-private - ANDROID_ABIS=ANDROID_TARGET_ARCH - - # for not changing qtkeychain sources for qt6 - QT -= androidextras - } - else { - QT += androidextras + QT += core-private + ANDROID_ABIS = $$ANDROID_TARGET_ARCH } +# else { +# QT += androidextras +# } DEFINES += MVPN_ANDROID @@ -298,6 +299,7 @@ android { ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android for (abi, ANDROID_ABIS): { + equals(ANDROID_TARGET_ARCH,$$abi) { LIBS += $$PWD/3rd/OpenSSL/lib/android/$${abi}/libcrypto.a LIBS += $$PWD/3rd/OpenSSL/lib/android/$${abi}/libssl.a diff --git a/client/platforms/android/android_controller.cpp b/client/platforms/android/android_controller.cpp index 36ed9397..543f2814 100644 --- a/client/platforms/android/android_controller.cpp +++ b/client/platforms/android/android_controller.cpp @@ -1,9 +1,7 @@ -//#include -//#include -//#include -//#include -//#include -//#include +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #include #include #include @@ -12,54 +10,119 @@ #include #include #include -//#include - -#include - #include "android_controller.h" -#include "core/errorstrings.h" +#include "private/qandroidextras_p.h" #include "ui/pages_logic/StartPageLogic.h" -// Binder Codes for VPNServiceBinder -// See also - VPNServiceBinder.kt -// Actions that are Requestable -const int ACTION_ACTIVATE = 1; -const int ACTION_DEACTIVATE = 2; -const int ACTION_REGISTER_LISTENER = 3; -const int ACTION_REQUEST_STATISTIC = 4; -const int ACTION_REQUEST_GET_LOG = 5; -const int ACTION_REQUEST_CLEANUP_LOG = 6; -const int ACTION_RESUME_ACTIVATE = 7; -const int ACTION_SET_NOTIFICATION_TEXT = 8; -const int ACTION_SET_NOTIFICATION_FALLBACK = 9; -const int ACTION_SHARE_CONFIG = 10; - -// Event Types that will be Dispatched after registration -const int EVENT_INIT = 0; -const int EVENT_CONNECTED = 1; -const int EVENT_DISCONNECTED = 2; -const int EVENT_STATISTIC_UPDATE = 3; -const int EVENT_BACKEND_LOGS = 4; -const int EVENT_ACTIVATION_ERROR = 5; -const int EVENT_CONFIG_IMPORT = 6; +#include "androidvpnactivity.h" +#include "androidutils.h" namespace { AndroidController* s_instance = nullptr; constexpr auto PERMISSIONHELPER_CLASS = "org/amnezia/vpn/qt/VPNPermissionHelper"; - } // namespace -AndroidController::AndroidController(): - m_binder(this) +AndroidController::AndroidController() { + s_instance = this; + auto activity = AndroidVPNActivity::instance(); + + connect(activity, &AndroidVPNActivity::serviceConnected, this, []() { + qDebug() << "Transact: service connected"; + AndroidVPNActivity::sendToService(ServiceAction::ACTION_REQUEST_STATISTIC, ""); + }, Qt::QueuedConnection); + + connect(activity, &AndroidVPNActivity::eventInitialized, this, + [this](const QString& parcelBody) { + // We might get multiple Init events as widgets, or fragments + // might query this. + if (m_init) { + return; + } + + qDebug() << "Transact: init"; + + m_init = true; + + auto doc = QJsonDocument::fromJson(parcelBody.toUtf8()); + qlonglong time = doc.object()["time"].toVariant().toLongLong(); + emit initialized( + true, doc.object()["connected"].toBool(), + time > 0 ? QDateTime::fromMSecsSinceEpoch(time) : QDateTime()); + + setFallbackConnectedNotification(); + }, Qt::QueuedConnection); + + connect(activity, &AndroidVPNActivity::eventConnected, this, + [this](const QString& parcelBody) { + Q_UNUSED(parcelBody); + qDebug() << "Transact: connected"; + emit connectionStateChanged(VpnProtocol::Connected); + }, Qt::QueuedConnection); + + connect(activity, &AndroidVPNActivity::eventDisconnected, this, + [this]() { + qDebug() << "Transact: disconnected"; + emit connectionStateChanged(VpnProtocol::Disconnected); + }, Qt::QueuedConnection); + + connect(activity, &AndroidVPNActivity::eventStatisticUpdate, this, + [](const QString& parcelBody) { + qDebug() << "Transact:: update"; + + auto doc = QJsonDocument::fromJson(parcelBody.toUtf8()); + + // TODO: merge with "Android bandwidth" branch + + // emit statusUpdated(doc.object()["endpoint"].toString(), + // doc.object()["deviceIpv4"].toString(), + // doc.object()["tx_bytes"].toInt(), + // doc.object()["rx_bytes"].toInt()); + }, Qt::QueuedConnection); + + connect(activity, &AndroidVPNActivity::eventBackendLogs, this, + [this](const QString& parcelBody) { + qDebug() << "Transact: backend logs"; + + QString buffer = parcelBody.toUtf8(); + if (m_logCallback) { + m_logCallback(buffer); + } + }, Qt::QueuedConnection); + + connect(activity, &AndroidVPNActivity::eventActivationError, this, + [this](const QString& parcelBody) { + Q_UNUSED(parcelBody) + qDebug() << "Transact: error"; + emit connectionStateChanged(VpnProtocol::Error); + }, Qt::QueuedConnection); + + connect(activity, &AndroidVPNActivity::eventConfigImport, this, + [this](const QString& parcelBody) { + qDebug() << "Transact: config import"; + auto doc = QJsonDocument::fromJson(parcelBody.toUtf8()); + + QString buffer = doc.object()["config"].toString(); + qDebug() << "Transact: config string" << buffer; + importConfig(buffer); + }, Qt::QueuedConnection); + + connect(activity, &AndroidVPNActivity::serviceDisconnected, this, + [this]() { + qDebug() << "Transact: service disconnected"; + m_serviceConnected = false; + }, Qt::QueuedConnection); } AndroidController* AndroidController::instance() { - if (!s_instance) s_instance = new AndroidController(); + if (!s_instance) { + s_instance = new AndroidController(); + } + return s_instance; } @@ -73,71 +136,43 @@ bool AndroidController::initialize(StartPageLogic *startPageLogic) JNINativeMethod methods[]{{"startActivityForResult", "(Landroid/content/Intent;)V", reinterpret_cast(startActivityForResult)}}; - QAndroidJniObject javaClass(PERMISSIONHELPER_CLASS); - QAndroidJniEnvironment env; + QJniObject javaClass(PERMISSIONHELPER_CLASS); + QJniEnvironment env; jclass objectClass = env->GetObjectClass(javaClass.object()); - env->RegisterNatives(objectClass, methods, - sizeof(methods) / sizeof(methods[0])); + env->RegisterNatives(objectClass, methods, sizeof(methods) / sizeof(methods[0])); env->DeleteLocalRef(objectClass); - auto appContext = QtAndroid::androidActivity().callObjectMethod( - "getApplicationContext", "()Landroid/content/Context;"); + AndroidVPNActivity::connectService(); - QAndroidJniObject::callStaticMethod( - "org/amnezia/vpn/VPNService", "startService", - "(Landroid/content/Context;)V", appContext.object()); - - // Start the VPN Service (if not yet) and Bind to it - const bool bindResult = QtAndroid::bindService( - QAndroidIntent(appContext.object(), "org.amnezia.vpn.VPNService"), - *this, QtAndroid::BindFlag::AutoCreate); - qDebug() << "Binding to the service..." << bindResult; - - return bindResult; + return true; } ErrorCode AndroidController::start() { - - //qDebug().noquote() << "AndroidController::start" << QJsonDocument(m_rawConfig).toJson(); qDebug() << "Prompting for VPN permission"; - auto appContext = QtAndroid::androidActivity().callObjectMethod( + QJniObject activity = AndroidUtils::getActivity(); + auto appContext = activity.callObjectMethod( "getApplicationContext", "()Landroid/content/Context;"); - QAndroidJniObject::callStaticMethod( + QJniObject::callStaticMethod( PERMISSIONHELPER_CLASS, "startService", "(Landroid/content/Context;)V", appContext.object()); - QAndroidParcel sendData; - sendData.writeData(QJsonDocument(m_vpnConfig).toJson()); - bool activateResult = false; - while (!activateResult){ - activateResult = m_serviceBinder.transact(ACTION_ACTIVATE, sendData, nullptr); - } + QJsonDocument doc(m_vpnConfig); + AndroidVPNActivity::sendToService(ServiceAction::ACTION_ACTIVATE, doc.toJson()); - return activateResult ? NoError : UnknownError; + return NoError; } void AndroidController::stop() { qDebug() << "AndroidController::stop"; -// if (reason != ReasonNone) { -// // Just show that we're disconnected -// // we're doing the actual disconnect once -// // the vpn-service has the new server ready in Action->Activate -// emit disconnected(); -// qCritical() << "deactivation skipped for Switching"; -// return; -// } - - QAndroidParcel nullData; - m_serviceBinder.transact(ACTION_DEACTIVATE, nullData, nullptr); + AndroidVPNActivity::sendToService(ServiceAction::ACTION_DEACTIVATE, QString()); } // Activates the tunnel that is currently set // in the VPN Service void AndroidController::resumeStart() { - QAndroidParcel nullData; - m_serviceBinder.transact(ACTION_RESUME_ACTIVATE, nullData, nullptr); + AndroidVPNActivity::sendToService(ServiceAction::ACTION_RESUME_ACTIVATE, QString()); } /* @@ -146,14 +181,13 @@ void AndroidController::resumeStart() { void AndroidController::setNotificationText(const QString& title, const QString& message, int timerSec) { - QJsonObject args; - args["title"] = title; - args["message"] = message; - args["sec"] = timerSec; - QJsonDocument doc(args); - QAndroidParcel data; - data.writeData(doc.toJson()); - m_serviceBinder.transact(ACTION_SET_NOTIFICATION_TEXT, data, nullptr); + QJsonObject args; + args["title"] = title; + args["message"] = message; + args["sec"] = timerSec; + QJsonDocument doc(args); + + AndroidVPNActivity::sendToService(ServiceAction::ACTION_SET_NOTIFICATION_TEXT, doc.toJson()); } void AndroidController::shareConfig(const QString& configContent, const QString& suggestedName) { @@ -161,9 +195,8 @@ void AndroidController::shareConfig(const QString& configContent, const QString& rootObject["data"] = configContent; rootObject["suggestedName"] = suggestedName; QJsonDocument doc(rootObject); - QAndroidParcel parcel; - parcel.writeData(doc.toJson()); - m_serviceBinder.transact(ACTION_SHARE_CONFIG, parcel, nullptr); + + AndroidVPNActivity::sendToService(ServiceAction::ACTION_SHARE_CONFIG, doc.toJson()); } /* @@ -172,64 +205,40 @@ void AndroidController::shareConfig(const QString& configContent, const QString& * e.g via always-on vpn */ void AndroidController::setFallbackConnectedNotification() { - QJsonObject args; - args["title"] = tr("AmneziaVPN"); - //% "Ready for you to connect" - //: Refers to the app - which is currently running the background and waiting - args["message"] = tr("VPN Connected"); - QJsonDocument doc(args); - QAndroidParcel data; - data.writeData(doc.toJson()); - m_serviceBinder.transact(ACTION_SET_NOTIFICATION_FALLBACK, data, nullptr); + QJsonObject args; + args["title"] = tr("AmneziaVPN"); + //% "Ready for you to connect" + //: Refers to the app - which is currently running the background and waiting + args["message"] = tr("VPN Connected"); + QJsonDocument doc(args); + + AndroidVPNActivity::sendToService(ServiceAction::ACTION_SET_NOTIFICATION_FALLBACK, doc.toJson()); } void AndroidController::checkStatus() { - qDebug() << "check status"; + qDebug() << "check status"; - QAndroidParcel nullParcel; - m_serviceBinder.transact(ACTION_REQUEST_STATISTIC, nullParcel, nullptr); + AndroidVPNActivity::sendToService(ServiceAction::ACTION_REQUEST_STATISTIC, QString()); } void AndroidController::getBackendLogs(std::function&& a_callback) { - qDebug() << "get logs"; + qDebug() << "get logs"; - m_logCallback = std::move(a_callback); - QAndroidParcel nullData, replyData; - m_serviceBinder.transact(ACTION_REQUEST_GET_LOG, nullData, &replyData); + m_logCallback = std::move(a_callback); + + AndroidVPNActivity::sendToService(ServiceAction::ACTION_REQUEST_GET_LOG, QString()); } void AndroidController::cleanupBackendLogs() { - qDebug() << "cleanup logs"; + qDebug() << "cleanup logs"; - QAndroidParcel nullParcel; - m_serviceBinder.transact(ACTION_REQUEST_CLEANUP_LOG, nullParcel, nullptr); + AndroidVPNActivity::sendToService(ServiceAction::ACTION_REQUEST_CLEANUP_LOG, QString()); } void AndroidController::importConfig(const QString& data){ m_startPageLogic->importConnectionFromCode(data); } -void AndroidController::onServiceConnected( - const QString& name, const QAndroidBinder& serviceBinder) { - qDebug() << "Server " + name + " connected"; - - Q_UNUSED(name); - - m_serviceBinder = serviceBinder; - - // Send the Service our Binder to recive incoming Events - QAndroidParcel binderParcel; - binderParcel.writeBinder(m_binder); - m_serviceBinder.transact(ACTION_REGISTER_LISTENER, binderParcel, nullptr); -} - -void AndroidController::onServiceDisconnected(const QString& name) { - qDebug() << "Server disconnected"; - m_serviceConnected = false; - Q_UNUSED(name); - // TODO: Maybe restart? Or crash? -} - const QJsonObject &AndroidController::vpnConfig() const { return m_vpnConfig; @@ -240,88 +249,6 @@ void AndroidController::setVpnConfig(const QJsonObject &newVpnConfig) m_vpnConfig = newVpnConfig; } - -/** - * @brief AndroidController::VPNBinder::onTransact - * @param code the Event-Type we get From the VPNService See - * @param data - Might contain UTF-8 JSON in case the Event has a payload - * @param reply - always null - * @param flags - unused - * @return Returns true is the code was a valid Event Code - */ -bool AndroidController::VPNBinder::onTransact(int code, - const QAndroidParcel& data, - const QAndroidParcel& reply, - QAndroidBinder::CallType flags) { - Q_UNUSED(data); - Q_UNUSED(reply); - Q_UNUSED(flags); - - QJsonDocument doc; - QString buffer; - switch (code) { - case EVENT_INIT: - qDebug() << "Transact: init"; - doc = QJsonDocument::fromJson(data.readData()); - emit m_controller->initialized( - true, doc.object()["connected"].toBool(), - QDateTime::fromMSecsSinceEpoch( - doc.object()["time"].toVariant().toLongLong())); - // Pass a localised version of the Fallback string for the Notification - m_controller->setFallbackConnectedNotification(); - - break; - case EVENT_CONNECTED: - qDebug() << "Transact: connected"; - emit m_controller->connectionStateChanged(VpnProtocol::Connected); - break; - case EVENT_DISCONNECTED: - qDebug() << "Transact: disconnected"; - emit m_controller->connectionStateChanged(VpnProtocol::Disconnected); - break; - case EVENT_STATISTIC_UPDATE: - qDebug() << "Transact:: update"; - - // Data is here a JSON String - doc = QJsonDocument::fromJson(data.readData()); - // TODO update counters -// emit m_controller->statusUpdated(doc.object()["endpoint"].toString(), -// doc.object()["deviceIpv4"].toString(), -// doc.object()["totalTX"].toInt(), -// doc.object()["totalRX"].toInt()); - break; - case EVENT_BACKEND_LOGS: - qDebug() << "Transact: backend logs"; - - buffer = readUTF8Parcel(data); - if (m_controller->m_logCallback) { - m_controller->m_logCallback(buffer); - } - break; - case EVENT_ACTIVATION_ERROR: - qDebug() << "Transact: error"; - emit m_controller->connectionStateChanged(VpnProtocol::Error); - break; - case EVENT_CONFIG_IMPORT: - qDebug() << "Transact: config import"; - doc = QJsonDocument::fromJson(data.readData()); - buffer = doc.object()["config"].toString(); - qDebug() << "Transact: config string" << buffer; - m_controller->importConfig(buffer); - break; - default: - qWarning() << "Transact: Invalid!"; - break; - } - - return true; -} - -QString AndroidController::VPNBinder::readUTF8Parcel(QAndroidParcel data) { - // 106 is the Code for UTF-8 - return QTextCodec::codecForMib(106)->toUnicode(data.readData()); -} - const int ACTIVITY_RESULT_OK = 0xffffffff; /** * @brief Starts the Given intent in Context of the QTActivity @@ -330,34 +257,34 @@ const int ACTIVITY_RESULT_OK = 0xffffffff; */ void AndroidController::startActivityForResult(JNIEnv *env, jobject, jobject intent) { - qDebug() << "start activity"; + qDebug() << "start vpnPermissionHelper"; Q_UNUSED(env); - QtAndroid::startActivity(intent, 1337, - [](int receiverRequestCode, int resultCode, - const QAndroidJniObject& data) { - // Currently this function just used in - // VPNService.kt::checkPersmissions. So the result - // we're getting is if the User gave us the - // Vpn.bind permission. In case of NO we should - // abort. - Q_UNUSED(receiverRequestCode); - Q_UNUSED(data); - AndroidController* controller = - AndroidController::instance(); - if (!controller) { - return; - } + QtAndroidPrivate::startActivity(intent, 1337, + [](int receiverRequestCode, int resultCode, + const QJniObject& data) { + // Currently this function just used in + // VPNService.kt::checkPersmissions. So the result + // we're getting is if the User gave us the + // Vpn.bind permission. In case of NO we should + // abort. + Q_UNUSED(receiverRequestCode); + Q_UNUSED(data); - if (resultCode == ACTIVITY_RESULT_OK) { - qDebug() << "VPN PROMPT RESULT - Accepted"; - controller->resumeStart(); - return; - } - // If the request got rejected abort the current - // connection. - qWarning() << "VPN PROMPT RESULT - Rejected"; - emit controller->connectionStateChanged(VpnProtocol::Disconnected); - }); + AndroidController* controller = AndroidController::instance(); + if (!controller) { + return; + } + + if (resultCode == ACTIVITY_RESULT_OK) { + qDebug() << "VPN PROMPT RESULT - Accepted"; + controller->resumeStart(); + return; + } + // If the request got rejected abort the current + // connection. + qWarning() << "VPN PROMPT RESULT - Rejected"; + emit controller->connectionStateChanged(VpnProtocol::Disconnected); + }); return; } diff --git a/client/platforms/android/android_controller.h b/client/platforms/android/android_controller.h index cbe36a31..bd2c2f01 100644 --- a/client/platforms/android/android_controller.h +++ b/client/platforms/android/android_controller.h @@ -1,18 +1,20 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + #ifndef ANDROID_CONTROLLER_H #define ANDROID_CONTROLLER_H -//#include -//#include -#include +#include +#include -#include "ui/uilogic.h" #include "ui/pages_logic/StartPageLogic.h" #include "protocols/vpnprotocol.h" using namespace amnezia; -class AndroidController : public QObject, public QAndroidServiceConnection +class AndroidController : public QObject { Q_OBJECT @@ -37,8 +39,8 @@ public: void importConfig(const QString& data); // from QAndroidServiceConnection - void onServiceConnected(const QString& name, const QAndroidBinder& serviceBinder) override; - void onServiceDisconnected(const QString& name) override; +// void onServiceConnected(const QString& name, const QAndroidBinder& serviceBinder) override; +// void onServiceDisconnected(const QString& name) override; const QJsonObject &vpnConfig() const; void setVpnConfig(const QJsonObject &newVpnConfig); @@ -60,6 +62,7 @@ protected: private: + bool m_init = false; //Protocol m_protocol; QJsonObject m_vpnConfig; @@ -68,22 +71,22 @@ private: bool m_serviceConnected = false; std::function m_logCallback; - QAndroidBinder m_serviceBinder; - class VPNBinder : public QAndroidBinder { - public: - VPNBinder(AndroidController* controller) : m_controller(controller) {} +// QAndroidBinder m_serviceBinder; +// class VPNBinder : public QAndroidBinder { +// public: +// VPNBinder(AndroidController* controller) : m_controller(controller) {} - bool onTransact(int code, const QAndroidParcel& data, - const QAndroidParcel& reply, - QAndroidBinder::CallType flags) override; +// bool onTransact(int code, const QAndroidParcel& data, +// const QAndroidParcel& reply, +// QAndroidBinder::CallType flags) override; - QString readUTF8Parcel(QAndroidParcel data); +// QString readUTF8Parcel(QAndroidParcel data); - private: - AndroidController* m_controller = nullptr; - }; +// private: +// AndroidController* m_controller = nullptr; +// }; - VPNBinder m_binder; +// VPNBinder m_binder; static void startActivityForResult(JNIEnv* env, jobject /*thiz*/, jobject intent); }; diff --git a/client/protocols/android_vpnprotocol.cpp b/client/protocols/android_vpnprotocol.cpp index cf9e443a..067296f0 100644 --- a/client/protocols/android_vpnprotocol.cpp +++ b/client/protocols/android_vpnprotocol.cpp @@ -1,9 +1,3 @@ -//#include -//#include -//#include -//#include -//#include -//#include #include #include #include @@ -12,12 +6,8 @@ #include #include #include -//#include -#include - #include "android_vpnprotocol.h" -#include "core/errorstrings.h" #include "platforms/android/android_controller.h" diff --git a/client/protocols/android_vpnprotocol.h b/client/protocols/android_vpnprotocol.h index cdcff2c6..6dbe2fdd 100644 --- a/client/protocols/android_vpnprotocol.h +++ b/client/protocols/android_vpnprotocol.h @@ -1,16 +1,11 @@ #ifndef ANDROID_VPNPROTOCOL_H #define ANDROID_VPNPROTOCOL_H -#include -#include - #include "vpnprotocol.h" #include "protocols/protocols_defs.h" using namespace amnezia; - - class AndroidVpnProtocol : public VpnProtocol { Q_OBJECT @@ -32,7 +27,6 @@ protected: private: Proto m_protocol; - }; #endif // ANDROID_VPNPROTOCOL_H diff --git a/client/ui/pages_logic/ServerSettingsLogic.cpp b/client/ui/pages_logic/ServerSettingsLogic.cpp index 3b123174..14927f9f 100644 --- a/client/ui/pages_logic/ServerSettingsLogic.cpp +++ b/client/ui/pages_logic/ServerSettingsLogic.cpp @@ -11,9 +11,7 @@ #include #if defined(Q_OS_ANDROID) -#include -#include -#include +#include "androidutils.h" #endif ServerSettingsLogic::ServerSettingsLogic(UiLogic *logic, QObject *parent): @@ -24,9 +22,7 @@ ServerSettingsLogic::ServerSettingsLogic(UiLogic *logic, QObject *parent): m_pushButtonShareFullVisible{true}, m_pushButtonClearText{tr("Clear server from Amnezia software")}, m_pushButtonClearClientCacheText{tr("Clear client cached profile")} -{ - -} +{ } void ServerSettingsLogic::onUpdatePage() { @@ -134,7 +130,7 @@ void ServerSettingsLogic::onLineEditDescriptionEditingFinished() #if defined(Q_OS_ANDROID) /* Auth result handler for Android */ -void authResultReceiver::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data) +void authResultReceiver::handleActivityResult(int receiverRequestCode, int resultCode, const QJniObject &data) { qDebug() << "receiverRequestCode" << receiverRequestCode << "resultCode" << resultCode; @@ -149,16 +145,17 @@ void ServerSettingsLogic::onPushButtonShareFullClicked() { #if defined(Q_OS_ANDROID) /* We use builtin keyguard for ssh key export protection on Android */ - auto appContext = QtAndroid::androidActivity().callObjectMethod( + QJniObject activity = AndroidUtils::getActivity(); + auto appContext = activity.callObjectMethod( "getApplicationContext", "()Landroid/content/Context;"); if (appContext.isValid()) { QAndroidActivityResultReceiver *receiver = new authResultReceiver(uiLogic(), uiLogic()->selectedServerIndex); - auto intent = QAndroidJniObject::callStaticObjectMethod( + auto intent = QJniObject::callStaticObjectMethod( "org/amnezia/vpn/AuthHelper", "getAuthIntent", "(Landroid/content/Context;)Landroid/content/Intent;", appContext.object()); if (intent.isValid()) { if (intent.object() != nullptr) { - QtAndroid::startActivity(intent.object(), 1, receiver); + QtAndroidPrivate::startActivity(intent.object(), 1, receiver); } } else { uiLogic()->pageLogic()->updateSharingPage(uiLogic()->selectedServerIndex, DockerContainer::None); diff --git a/client/ui/pages_logic/ServerSettingsLogic.h b/client/ui/pages_logic/ServerSettingsLogic.h index e2d57422..d561ec48 100644 --- a/client/ui/pages_logic/ServerSettingsLogic.h +++ b/client/ui/pages_logic/ServerSettingsLogic.h @@ -4,7 +4,8 @@ #include "PageLogicBase.h" #if defined(Q_OS_ANDROID) -#include +#include +#include #endif class UiLogic; @@ -52,7 +53,7 @@ public: ~authResultReceiver() {} public: - void handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data) override; + void handleActivityResult(int receiverRequestCode, int resultCode, const QJniObject &data) override; private: int m_serverIndex; diff --git a/client/ui/pages_logic/StartPageLogic.cpp b/client/ui/pages_logic/StartPageLogic.cpp index 8c24b0d8..03ace70a 100644 --- a/client/ui/pages_logic/StartPageLogic.cpp +++ b/client/ui/pages_logic/StartPageLogic.cpp @@ -11,8 +11,8 @@ #include #ifdef Q_OS_ANDROID -#include -#include "platforms/android/android_controller.h" +#include +#include "androidutils.h" #endif StartPageLogic::StartPageLogic(UiLogic *logic, QObject *parent): @@ -26,8 +26,9 @@ StartPageLogic::StartPageLogic(UiLogic *logic, QObject *parent): { #ifdef Q_OS_ANDROID // Set security screen for Android app - QtAndroid::runOnAndroidThread([]() { - QAndroidJniObject window = QtAndroid::androidActivity().callObjectMethod("getWindow", "()Landroid/view/Window;"); + AndroidUtils::runOnAndroidThreadSync([]() { + QJniObject activity = AndroidUtils::getActivity(); + QJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;"); if (window.isValid()){ const int FLAG_SECURE = 8192; window.callMethod("addFlags", "(I)V", FLAG_SECURE);