From 5a73da6fdfa35efcecd86679b0d1e960aa8ceee5 Mon Sep 17 00:00:00 2001 From: Gabriel Calero Date: Thu, 17 May 2018 14:05:49 -0300 Subject: [PATCH] Bugfix: stop audio when app starts --- android/app/src/main/cpp/native.cpp | 10 ++++++++ .../hifiinterface/InterfaceActivity.java | 8 ++++++- interface/src/AndroidHelper.cpp | 8 +++++++ interface/src/AndroidHelper.h | 4 ++++ interface/src/Application.cpp | 3 ++- interface/src/Application_jni.cpp | 24 ------------------- 6 files changed, 31 insertions(+), 26 deletions(-) delete mode 100644 interface/src/Application_jni.cpp diff --git a/android/app/src/main/cpp/native.cpp b/android/app/src/main/cpp/native.cpp index 5822eb77a4..3179534b34 100644 --- a/android/app/src/main/cpp/native.cpp +++ b/android/app/src/main/cpp/native.cpp @@ -278,4 +278,14 @@ Java_io_highfidelity_hifiinterface_MainActivity_nativeGetDisplayName(JNIEnv *env return env->NewStringUTF(username.toLatin1().data()); } +JNIEXPORT void JNICALL +Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeEnterBackground(JNIEnv *env, jobject obj) { + AndroidHelper::instance().notifyEnterBackground(); +} + +JNIEXPORT void JNICALL +Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeEnterForeground(JNIEnv *env, jobject obj) { + AndroidHelper::instance().notifyEnterForeground(); +} + } diff --git a/android/app/src/main/java/io/highfidelity/hifiinterface/InterfaceActivity.java b/android/app/src/main/java/io/highfidelity/hifiinterface/InterfaceActivity.java index acd6a60eb7..00b50ccee0 100644 --- a/android/app/src/main/java/io/highfidelity/hifiinterface/InterfaceActivity.java +++ b/android/app/src/main/java/io/highfidelity/hifiinterface/InterfaceActivity.java @@ -15,6 +15,7 @@ import android.content.Intent; import android.content.res.AssetManager; import android.net.Uri; import android.os.Bundle; +import android.os.Handler; import android.os.Vibrator; import android.view.HapticFeedbackConstants; import android.view.WindowManager; @@ -113,7 +114,9 @@ public class InterfaceActivity extends QtActivity { @Override protected void onPause() { super.onPause(); - nativeEnterBackground(); + if (!isLoading) { + nativeEnterBackground(); + } //gvrApi.pauseTracking(); } @@ -212,6 +215,9 @@ public class InterfaceActivity extends QtActivity { public void onAppLoadedComplete() { super.isLoading = false; + new Handler(getMainLooper()).postDelayed(() -> { + nativeEnterBackground(); + }, 2000); } public void performHapticFeedback(int duration) { diff --git a/interface/src/AndroidHelper.cpp b/interface/src/AndroidHelper.cpp index 246fb06941..ef7319e63f 100644 --- a/interface/src/AndroidHelper.cpp +++ b/interface/src/AndroidHelper.cpp @@ -49,6 +49,14 @@ void AndroidHelper::notifyEnterForeground() { emit enterForeground(); } +void AndroidHelper::notifyEnterBackground() { + emit enterBackground(); +} + +void AndroidHelper::performHapticFeedback(int duration) { + emit hapticFeedbackRequested(duration); +} + void AndroidHelper::showLoginDialog() { emit androidActivityRequested("Login", true); } diff --git a/interface/src/AndroidHelper.h b/interface/src/AndroidHelper.h index 13ce981fd8..007c0db4a5 100644 --- a/interface/src/AndroidHelper.h +++ b/interface/src/AndroidHelper.h @@ -26,6 +26,8 @@ public: void init(); void requestActivity(const QString &activityName, const bool backToScene); void notifyLoadComplete(); + void notifyEnterForeground(); + void notifyEnterBackground(); void performHapticFeedback(int duration); @@ -39,6 +41,8 @@ public slots: signals: void androidActivityRequested(const QString &activityName, const bool backToScene); void qtAppLoadComplete(); + void enterForeground(); + void enterBackground(); void hapticFeedbackRequested(int duration); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 6086b9bfd8..9679fc1e8b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2265,6 +2265,8 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo #if defined(Q_OS_ANDROID) AndroidHelper::instance().init(); + connect(&AndroidHelper::instance(), &AndroidHelper::enterBackground, this, &Application::enterBackground); + connect(&AndroidHelper::instance(), &AndroidHelper::enterForeground, this, &Application::enterForeground); AndroidHelper::instance().notifyLoadComplete(); #endif } @@ -8290,5 +8292,4 @@ void Application::enterForeground() { } #endif -#include "Application_jni.cpp" #include "Application.moc" diff --git a/interface/src/Application_jni.cpp b/interface/src/Application_jni.cpp deleted file mode 100644 index 5e9f1ac29e..0000000000 --- a/interface/src/Application_jni.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#if defined(Q_OS_ANDROID) - -#include -#include "AndroidHelper.h" - -extern "C" { - -JNIEXPORT void -Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeEnterBackground(JNIEnv *env, jobject obj) { - if (qApp) { - qApp->enterBackground(); - } -} - -JNIEXPORT void -Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeEnterForeground(JNIEnv *env, jobject obj) { - if (qApp) { - qApp->enterForeground(); - } -} - - -} -#endif \ No newline at end of file