From eac47010d3b628c9ba7b6299388667290fb485eb Mon Sep 17 00:00:00 2001
From: SeeLook <seelook@gmail.com>
Date: Sat, 15 Feb 2025 09:39:34 +0100
Subject: [PATCH] Remove AndroidExtras references, use new ones

---
 src/core/Android/tandroid.cpp    | 141 +++++++++++++++----------------
 src/dialogs/ttunerdialogitem.cpp |   5 --
 src/sound/tsound.cpp             |  12 ++-
 3 files changed, 74 insertions(+), 84 deletions(-)

diff --git a/src/core/Android/tandroid.cpp b/src/core/Android/tandroid.cpp
index 3c369f84..78626aec 100644
--- a/src/core/Android/tandroid.cpp
+++ b/src/core/Android/tandroid.cpp
@@ -17,8 +17,6 @@
  ***************************************************************************/
 
 #include "tandroid.h"
-#include <QtAndroidExtras/qandroidfunctions.h>
-#include <QtAndroidExtras/qandroidjnienvironment.h>
 #include <QtCore/qdatetime.h>
 #include <QtCore/qfileinfo.h>
 #include <QtCore/qstandardpaths.h>
@@ -27,12 +25,14 @@
 
 #include <QtCore/qdebug.h>
 
+using namespace QNativeInterface;
+
 void Tandroid::keepScreenOn(bool on)
 {
-    QtAndroid::runOnAndroidThread([on] {
-        QAndroidJniObject activity = QtAndroid::androidActivity();
+    QAndroidApplication::runOnAndroidMainThread([on] {
+        auto activity = QJniObject(QAndroidApplication::context());
         if (activity.isValid()) {
-            QAndroidJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
+            auto window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
 
             if (window.isValid()) {
                 const int FLAG_KEEP_SCREEN_ON = 128;
@@ -42,47 +42,45 @@ void Tandroid::keepScreenOn(bool on)
                     window.callMethod<void>("clearFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
             }
         }
-        QAndroidJniEnvironment env;
-        if (env->ExceptionCheck())
-            env->ExceptionClear();
+        QJniEnvironment env;
+        env.checkAndClearExceptions();
     });
 }
 
 void Tandroid::disableRotation(bool disRot)
 {
     int orientation = disRot ? 0 : 10; // SCREEN_ORIENTATION_LANDSCAPE or SCREEN_ORIENTATION_FULL_SENSOR
-    QtAndroid::runOnAndroidThread([orientation] {
-        QAndroidJniObject activity = QtAndroid::androidActivity();
+    QAndroidApplication::runOnAndroidMainThread([orientation] {
+        auto activity = QJniObject(QAndroidApplication::context());
         if (activity.isValid())
             activity.callMethod<void>("setRequestedOrientation", "(I)V", orientation);
-        QAndroidJniEnvironment env;
-        if (env->ExceptionCheck())
-            env->ExceptionClear();
+        QJniEnvironment env;
+        env.checkAndClearExceptions();
     });
 }
 
 int Tandroid::getAPIlevelNr()
 {
-    return QtAndroid::androidSdkVersion();
+    return QAndroidApplication::sdkVersion();
 }
 
 bool Tandroid::hasWriteAccess()
 {
-    if (QtAndroid::androidSdkVersion() < 23)
-        return true;
-    else
-        return QtAndroid::checkPermission(QStringLiteral("android.permission.WRITE_EXTERNAL_STORAGE")) == QtAndroid::PermissionResult::Granted;
+    // if (QAndroidApplication::sdkVersion() < 23) // TODO
+    return false; // true;
+    // else
+    // return QtAndroid::checkPermission(QStringLiteral("android.permission.WRITE_EXTERNAL_STORAGE")) == QtAndroid::PermissionResult::Granted;
 }
 
 void Tandroid::askForWriteAcces()
 {
-    if (QtAndroid::androidSdkVersion() >= 23) {
-        const QString writeID("android.permission.WRITE_EXTERNAL_STORAGE");
-        if (QtAndroid::checkPermission(writeID) != QtAndroid::PermissionResult::Granted) {
-            auto perms = QtAndroid::requestPermissionsSync(QStringList() << writeID);
-            qDebug() << writeID << (perms[writeID] == QtAndroid::PermissionResult::Granted);
-        }
-    }
+    // if (QtAndroid::androidSdkVersion() >= 23) {
+    //     const QString writeID("android.permission.WRITE_EXTERNAL_STORAGE");
+    //     if (QtAndroid::checkPermission(writeID) != QtAndroid::PermissionResult::Granted) {
+    //         auto perms = QtAndroid::requestPermissionsSync(QStringList() << writeID);
+    //         qDebug() << writeID << (perms[writeID] == QtAndroid::PermissionResult::Granted);
+    //     }
+    // }
 }
 
 QString Tandroid::getExternalPath()
@@ -113,67 +111,66 @@ QString Tandroid::getExternalPath()
 QString Tandroid::getRunArgument()
 {
     QString argument;
-    QAndroidJniObject activity = QtAndroid::androidActivity();
+    auto activity = QJniObject(QAndroidApplication::context());
     if (activity.isValid()) {
-        QAndroidJniObject intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
+        QJniObject intent = activity.callObjectMethod("getIntent", "()Landroid/content/Intent;");
         if (intent.isValid()) {
-            QAndroidJniObject data = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
+            QJniObject data = intent.callObjectMethod("getData", "()Landroid/net/Uri;");
             if (data.isValid()) {
-                QAndroidJniObject arg = data.callObjectMethod("getPath", "()Ljava/lang/String;");
+                QJniObject arg = data.callObjectMethod("getPath", "()Ljava/lang/String;");
                 if (arg.isValid())
                     argument = arg.toString();
             }
         }
     }
-    QAndroidJniEnvironment env;
-    if (env->ExceptionCheck())
-        env->ExceptionClear();
+    QJniEnvironment env;
+    env.checkAndClearExceptions();
     return argument;
 }
 
 void Tandroid::restartNootka()
 {
-    auto activity = QtAndroid::androidActivity();
-    auto packageManager = activity.callObjectMethod("getPackageManager", "()Landroid/content/pm/PackageManager;");
-    auto activityIntent = packageManager.callObjectMethod("getLaunchIntentForPackage",
-                                                          "(Ljava/lang/String;)Landroid/content/Intent;",
-                                                          activity.callObjectMethod("getPackageName", "()Ljava/lang/String;").object());
-    auto pendingIntent =
-        QAndroidJniObject::callStaticObjectMethod("android/app/PendingIntent",
-                                                  "getActivity",
-                                                  "(Landroid/content/Context;ILandroid/content/Intent;I)Landroid/app/PendingIntent;",
-                                                  activity.object(),
-                                                  jint(0),
-                                                  activityIntent.object(),
-                                                  QAndroidJniObject::getStaticField<jint>("android/content/Intent", "FLAG_ACTIVITY_CLEAR_TOP"));
-    auto alarmManager =
-        activity.callObjectMethod("getSystemService",
-                                  "(Ljava/lang/String;)Ljava/lang/Object;",
-                                  QAndroidJniObject::getStaticObjectField("android/content/Context", "ALARM_SERVICE", "Ljava/lang/String;").object());
-    alarmManager.callMethod<void>("set",
-                                  "(IJLandroid/app/PendingIntent;)V",
-                                  QAndroidJniObject::getStaticField<jint>("android/app/AlarmManager", "RTC"),
-                                  jlong(QDateTime::currentMSecsSinceEpoch() + 750),
-                                  pendingIntent.object());
-    QAndroidJniEnvironment env;
-    if (env->ExceptionCheck())
-        env->ExceptionClear();
-}
+    //     auto activity = QtAndroid::androidActivity();
+    //     auto packageManager = activity.callObjectMethod("getPackageManager", "()Landroid/content/pm/PackageManager;");
+    //     auto activityIntent = packageManager.callObjectMethod("getLaunchIntentForPackage",
+    //                                                           "(Ljava/lang/String;)Landroid/content/Intent;",
+    //                                                           activity.callObjectMethod("getPackageName", "()Ljava/lang/String;").object());
+    //     auto pendingIntent =
+    //         QAndroidJniObject::callStaticObjectMethod("android/app/PendingIntent",
+    //                                                   "getActivity",
+    //                                                   "(Landroid/content/Context;ILandroid/content/Intent;I)Landroid/app/PendingIntent;",
+    //                                                   activity.object(),
+    //                                                   jint(0),
+    //                                                   activityIntent.object(),
+    //                                                   QAndroidJniObject::getStaticField<jint>("android/content/Intent", "FLAG_ACTIVITY_CLEAR_TOP"));
+    //     auto alarmManager =
+    //         activity.callObjectMethod("getSystemService",
+    //                                   "(Ljava/lang/String;)Ljava/lang/Object;",
+    //                                   QAndroidJniObject::getStaticObjectField("android/content/Context", "ALARM_SERVICE", "Ljava/lang/String;").object());
+    //     alarmManager.callMethod<void>("set",
+    //                                   "(IJLandroid/app/PendingIntent;)V",
+    //                                   QAndroidJniObject::getStaticField<jint>("android/app/AlarmManager", "RTC"),
+    //                                   jlong(QDateTime::currentMSecsSinceEpoch() + 750),
+    //                                   pendingIntent.object());
+    //     QAndroidJniEnvironment env;
+    //     if (env->ExceptionCheck())
+    //         env->ExceptionClear();
+    // }
 
-void Tandroid::sendExam(const QString &title, const QString &message, const QString &filePath)
-{
-    QAndroidJniObject jTitle = QAndroidJniObject::fromString(title);
-    QAndroidJniObject jMessage = QAndroidJniObject::fromString(message);
-    QAndroidJniObject jFile = QAndroidJniObject::fromString(filePath);
-    QAndroidJniObject::callStaticMethod<void>("net/sf/nootka/TshareExam",
-                                              "send",
-                                              "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
-                                              jTitle.object<jstring>(),
-                                              jMessage.object<jstring>(),
-                                              jFile.object<jstring>());
-    QAndroidJniEnvironment env;
-    if (env->ExceptionCheck())
-        env->ExceptionClear();
+    // void Tandroid::sendExam(const QString &title, const QString &message, const QString &filePath)
+    // {
+    //     QAndroidJniObject jTitle = QAndroidJniObject::fromString(title);
+    //     QAndroidJniObject jMessage = QAndroidJniObject::fromString(message);
+    //     QAndroidJniObject jFile = QAndroidJniObject::fromString(filePath);
+    //     QAndroidJniObject::callStaticMethod<void>("net/sf/nootka/TshareExam",
+    //                                               "send",
+    //                                               "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
+    //                                               jTitle.object<jstring>(),
+    //                                               jMessage.object<jstring>(),
+    //                                               jFile.object<jstring>());
+    //     QAndroidJniEnvironment env;
+    //     if (env->ExceptionCheck())
+    //         env->ExceptionClear();
 }
 
 QString Tandroid::accountName()
diff --git a/src/dialogs/ttunerdialogitem.cpp b/src/dialogs/ttunerdialogitem.cpp
index 7849ea02..ebc635e6 100644
--- a/src/dialogs/ttunerdialogitem.cpp
+++ b/src/dialogs/ttunerdialogitem.cpp
@@ -26,11 +26,6 @@
 #include <QtCore/qdebug.h>
 #include <QtCore/qtimer.h>
 
-#if defined(Q_OS_ANDROID)
-#include <QtAndroidExtras/qandroidfunctions.h>
-#include <QtAndroidExtras/qandroidjnienvironment.h>
-#endif
-
 TtunerDialogItem::TtunerDialogItem(QQuickItem *parent)
     : QQuickItem(parent)
 {
diff --git a/src/sound/tsound.cpp b/src/sound/tsound.cpp
index ca11486e..7017a598 100644
--- a/src/sound/tsound.cpp
+++ b/src/sound/tsound.cpp
@@ -20,8 +20,6 @@
 #if defined(Q_OS_ANDROID)
 #include "tqtaudioin.h"
 #include "tqtaudioout.h"
-#include <QtAndroidExtras/qandroidfunctions.h>
-#include <QtAndroidExtras/qandroidjnienvironment.h>
 #else
 //   #include "tmidiout.h"
 #include "tnotesbaritem.h"
@@ -536,17 +534,17 @@ void Tsound::setTunerMode(bool isTuner)
 #if defined(Q_OS_ANDROID)
 int Tsound::maxVolRange() const
 {
-    return QAndroidJniObject::callStaticMethod<jint>("net/sf/nootka/ToutVolume", "maxStreamVolume");
+    return QJniObject::callStaticMethod<jint>("net/sf/nootka/ToutVolume", "maxStreamVolume");
 }
 
 int Tsound::currentVol() const
 {
-    return QAndroidJniObject::callStaticMethod<jint>("net/sf/nootka/ToutVolume", "streamVolume");
+    return QJniObject::callStaticMethod<jint>("net/sf/nootka/ToutVolume", "streamVolume");
 }
 
 void Tsound::setVol(int v)
 {
-    QAndroidJniObject::callStaticMethod<void>("net/sf/nootka/ToutVolume", "setStreamVolume", "(I)V", v);
+    QJniObject::callStaticMethod<void>("net/sf/nootka/ToutVolume", "setStreamVolume", "(I)V", v);
 }
 
 void Tsound::setTouchHandling(bool th)
@@ -644,14 +642,14 @@ bool Tsound::eventFilter(QObject *watched, QEvent *event)
             if (m_volKeyTimer.elapsed() > 100) {
                 if (!m_tunerMode) {
                     if (playing()) {
-                        QAndroidJniObject::callStaticMethod<void>("net/sf/nootka/ToutVolume", "show");
+                        QJniObject::callStaticMethod<void>("net/sf/nootka/ToutVolume", "show");
                         if (ke->key() == Qt::Key_VolumeDown)
                             m_currVol--;
                         else
                             m_currVol++;
                         m_currVol = qBound(0, m_currVol, m_maxVol);
                         setVol(m_currVol);
-                        m_currVol = QAndroidJniObject::callStaticMethod<jint>("net/sf/nootka/ToutVolume", "streamVolume");
+                        m_currVol = QJniObject::callStaticMethod<jint>("net/sf/nootka/ToutVolume", "streamVolume");
                     } else if (!GLOB->isExam())
                         QTimer::singleShot(10, this, &Tsound::volumeKeyPressed);
                 } else {
-- 
GitLab