From be8351dc0dd58aa3c8163017feff2d2ae8ef205b Mon Sep 17 00:00:00 2001 From: Cristian Luis Duarte Date: Fri, 6 Jul 2018 20:43:52 -0300 Subject: [PATCH] Android - Rollback pause/resume mechanism. Stop server checkIns, reset nodeList before entering background --- android/app/src/main/cpp/native.cpp | 5 +++++ .../hifiinterface/InterfaceActivity.java | 2 ++ interface/src/AndroidHelper.cpp | 4 ++++ interface/src/AndroidHelper.h | 2 ++ interface/src/Application.cpp | 13 ++++++++++--- interface/src/Application.h | 1 + libraries/networking/src/NodeList.cpp | 6 ++++++ libraries/networking/src/NodeList.h | 5 +++++ libraries/networking/src/ResourceCache.cpp | 12 +----------- libraries/networking/src/ResourceCache.h | 9 --------- 10 files changed, 36 insertions(+), 23 deletions(-) diff --git a/android/app/src/main/cpp/native.cpp b/android/app/src/main/cpp/native.cpp index 9b5e5f2543..f3fce2e9e3 100644 --- a/android/app/src/main/cpp/native.cpp +++ b/android/app/src/main/cpp/native.cpp @@ -285,6 +285,11 @@ Java_io_highfidelity_hifiinterface_MainActivity_nativeGetDisplayName(JNIEnv *env return env->NewStringUTF(username.toLatin1().data()); } +JNIEXPORT void JNICALL +Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeBeforeEnterBackground(JNIEnv *env, jobject obj) { + AndroidHelper::instance().notifyBeforeEnterBackground(); +} + JNIEXPORT void JNICALL Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeEnterBackground(JNIEnv *env, jobject obj) { AndroidHelper::instance().notifyEnterBackground(); 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 84fd2678d8..840b2d2b18 100644 --- a/android/app/src/main/java/io/highfidelity/hifiinterface/InterfaceActivity.java +++ b/android/app/src/main/java/io/highfidelity/hifiinterface/InterfaceActivity.java @@ -49,6 +49,7 @@ public class InterfaceActivity extends QtActivity { private native long nativeOnCreate(InterfaceActivity instance, AssetManager assetManager); private native void nativeOnDestroy(); private native void nativeGotoUrl(String url); + private native void nativeBeforeEnterBackground(); private native void nativeEnterBackground(); private native void nativeEnterForeground(); private native long nativeOnExitVr(); @@ -248,6 +249,7 @@ public class InterfaceActivity extends QtActivity { case "Home": case "Privacy Policy": case "Login": { + nativeBeforeEnterBackground(); Intent intent = new Intent(this, MainActivity.class); intent.putExtra(MainActivity.EXTRA_FRAGMENT, activityName); intent.putExtra(MainActivity.EXTRA_BACK_TO_SCENE, backToScene); diff --git a/interface/src/AndroidHelper.cpp b/interface/src/AndroidHelper.cpp index be41d434df..392220f5d3 100644 --- a/interface/src/AndroidHelper.cpp +++ b/interface/src/AndroidHelper.cpp @@ -29,6 +29,10 @@ void AndroidHelper::notifyEnterForeground() { emit enterForeground(); } +void AndroidHelper::notifyBeforeEnterBackground() { + emit beforeEnterBackground(); +} + void AndroidHelper::notifyEnterBackground() { emit enterBackground(); } diff --git a/interface/src/AndroidHelper.h b/interface/src/AndroidHelper.h index cecae4a79e..a6250c1fc7 100644 --- a/interface/src/AndroidHelper.h +++ b/interface/src/AndroidHelper.h @@ -24,6 +24,7 @@ public: void requestActivity(const QString &activityName, const bool backToScene); void notifyLoadComplete(); void notifyEnterForeground(); + void notifyBeforeEnterBackground(); void notifyEnterBackground(); void performHapticFeedback(int duration); @@ -38,6 +39,7 @@ signals: void androidActivityRequested(const QString &activityName, const bool backToScene); void qtAppLoadComplete(); void enterForeground(); + void beforeEnterBackground(); void enterBackground(); void hapticFeedbackRequested(int duration); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 203597c48c..4761055374 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2259,6 +2259,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo qCDebug(interfaceapp) << "Metaverse session ID is" << uuidStringWithoutCurlyBraces(accountManager->getSessionID()); #if defined(Q_OS_ANDROID) + connect(&AndroidHelper::instance(), &AndroidHelper::beforeEnterBackground, this, &Application::beforeEnterBackground); connect(&AndroidHelper::instance(), &AndroidHelper::enterBackground, this, &Application::enterBackground); connect(&AndroidHelper::instance(), &AndroidHelper::enterForeground, this, &Application::enterForeground); AndroidHelper::instance().notifyLoadComplete(); @@ -8299,14 +8300,19 @@ void Application::saveNextPhysicsStats(QString filename) { } #if defined(Q_OS_ANDROID) +void Application::beforeEnterBackground() { + auto nodeList = DependencyManager::get(); + nodeList->setSendDomainServerCheckInEnabled(false); + nodeList->reset(true); + clearDomainOctreeDetails(); +} + void Application::enterBackground() { QMetaObject::invokeMethod(DependencyManager::get().data(), "stop", Qt::BlockingQueuedConnection); if (getActiveDisplayPlugin()->isActive()) { getActiveDisplayPlugin()->deactivate(); } - DependencyManager::get()->pausePendingRequests(); - clearDomainOctreeDetails(); } void Application::enterForeground() { @@ -8315,7 +8321,8 @@ void Application::enterForeground() { if (!getActiveDisplayPlugin() || getActiveDisplayPlugin()->isActive() || !getActiveDisplayPlugin()->activate()) { qWarning() << "Could not re-activate display plugin"; } - DependencyManager::get()->resumePendingRequests(); + auto nodeList = DependencyManager::get(); + nodeList->setSendDomainServerCheckInEnabled(true); } #endif diff --git a/interface/src/Application.h b/interface/src/Application.h index 346ea258da..e74ec59ae2 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -311,6 +311,7 @@ public: void unloadAvatarScripts(); #if defined(Q_OS_ANDROID) + void beforeEnterBackground(); void enterBackground(); void enterForeground(); #endif diff --git a/libraries/networking/src/NodeList.cpp b/libraries/networking/src/NodeList.cpp index c5c49f68fe..c65590bc5e 100644 --- a/libraries/networking/src/NodeList.cpp +++ b/libraries/networking/src/NodeList.cpp @@ -289,6 +289,12 @@ void NodeList::addSetOfNodeTypesToNodeInterestSet(const NodeSet& setOfNodeTypes) } void NodeList::sendDomainServerCheckIn() { + + if (!_sendDomainServerCheckInEnabled) { + qCDebug(networking) << "Refusing to send a domain-server check in while it is disabled."; + return; + } + if (thread() != QThread::currentThread()) { QMetaObject::invokeMethod(this, "sendDomainServerCheckIn", Qt::QueuedConnection); return; diff --git a/libraries/networking/src/NodeList.h b/libraries/networking/src/NodeList.h index c5cf5e9524..78d3fad696 100644 --- a/libraries/networking/src/NodeList.h +++ b/libraries/networking/src/NodeList.h @@ -90,6 +90,9 @@ public: bool getRequestsDomainListData() { return _requestsDomainListData; } void setRequestsDomainListData(bool isRequesting); + bool getSendDomainServerCheckInEnabled() { return _sendDomainServerCheckInEnabled; } + void setSendDomainServerCheckInEnabled(bool enabled) { _sendDomainServerCheckInEnabled = enabled; } + void removeFromIgnoreMuteSets(const QUuid& nodeID); virtual bool isDomainServer() const override { return false; } @@ -169,6 +172,8 @@ private: QTimer _keepAlivePingTimer; bool _requestsDomainListData { false }; + bool _sendDomainServerCheckInEnabled { true }; + mutable QReadWriteLock _ignoredSetLock; tbb::concurrent_unordered_set _ignoredNodeIDs; mutable QReadWriteLock _personalMutedSetLock; diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 464dd76cc2..d07420f87e 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -95,14 +95,6 @@ void ResourceCacheSharedItems::removeRequest(QWeakPointer resource) { } } -void ResourceCacheSharedItems::pausePendingRequests() { - _pendingRequestsPaused = true; -} - -void ResourceCacheSharedItems::resumePendingRequests() { - _pendingRequestsPaused = false; -} - QSharedPointer ResourceCacheSharedItems::getHighestPendingRequest() { // look for the highest priority pending request int highestIndex = -1; @@ -502,9 +494,7 @@ void ResourceCache::requestCompleted(QWeakPointer resource) { sharedItems->removeRequest(resource); --_requestsActive; - if (!sharedItems->pendingRequestsPaused()) { - attemptHighestPriorityRequest(); - } + attemptHighestPriorityRequest(); } bool ResourceCache::attemptHighestPriorityRequest() { diff --git a/libraries/networking/src/ResourceCache.h b/libraries/networking/src/ResourceCache.h index 000e6027d1..a4bd352563 100644 --- a/libraries/networking/src/ResourceCache.h +++ b/libraries/networking/src/ResourceCache.h @@ -71,24 +71,16 @@ public: void removeRequest(QWeakPointer doneRequest); QList> getPendingRequests(); uint32_t getPendingRequestsCount() const; - - void pausePendingRequests(); - void resumePendingRequests(); - QList> getLoadingRequests(); QSharedPointer getHighestPendingRequest(); uint32_t getLoadingRequestsCount() const; - bool pendingRequestsPaused() { return _pendingRequestsPaused; } - private: ResourceCacheSharedItems() = default; mutable Mutex _mutex; QList> _pendingRequests; QList> _loadingRequests; - - bool _pendingRequestsPaused { false }; }; /// Wrapper to expose resources to JS/QML @@ -488,7 +480,6 @@ public slots: private: friend class ResourceCache; friend class ScriptableResource; - friend class ResourceCacheSharedItems; void setLRUKey(int lruKey) { _lruKey = lruKey; }