From 2ff13270ed00a26e355830ad2ee39c3abc79e01f Mon Sep 17 00:00:00 2001 From: Gabriel Calero Date: Mon, 2 Apr 2018 12:22:21 -0300 Subject: [PATCH] Go back to GoTo screen --- android/app/src/main/cpp/native.cpp | 27 +++++++++++++++ .../hifiinterface/GotoActivity.java | 18 ++++++++-- .../hifiinterface/InterfaceActivity.java | 22 ++++++++++++ .../qml/+android/AddressBarDialog.qml | 4 +-- .../qml/hifi/+android/bottomHudOptions.qml | 9 ++--- interface/src/AndroidHelper.cpp | 16 +++++++++ interface/src/AndroidHelper.h | 34 +++++++++++++++++++ interface/src/Application.cpp | 6 ++++ interface/src/Application.h | 6 ++++ scripts/system/+android/goto.js | 16 +++++++-- 10 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 interface/src/AndroidHelper.cpp create mode 100644 interface/src/AndroidHelper.h diff --git a/android/app/src/main/cpp/native.cpp b/android/app/src/main/cpp/native.cpp index 13daf4c471..ca46cd8c4b 100644 --- a/android/app/src/main/cpp/native.cpp +++ b/android/app/src/main/cpp/native.cpp @@ -17,6 +17,13 @@ #include #include +#include +#include + +#include +#include "AndroidHelper.h" + +QAndroidJniObject __activity; void tempMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { if (!message.isEmpty()) { @@ -133,14 +140,34 @@ void unpackAndroidAssets() { } } +void openGotoActivity(const QString& a) { + __activity.callMethod("openGotoActivity", "()V"); +} + extern "C" { JNIEXPORT void Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeOnCreate(JNIEnv* env, jobject obj, jobject instance, jobject asset_mgr) { qDebug() << "nativeOnCreate On thread " << QThread::currentThreadId(); g_assetManager = AAssetManager_fromJava(env, asset_mgr); + __activity = QAndroidJniObject(instance); auto oldMessageHandler = qInstallMessageHandler(tempMessageHandler); unpackAndroidAssets(); qInstallMessageHandler(oldMessageHandler); + + QObject::connect(&AndroidHelper::instance(), &AndroidHelper::androidActivityRequested, [](const QString& a) { + __activity.callMethod("openGotoActivity", "()V"); + }); +} + +JNIEXPORT void Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeOnDestroy(JNIEnv* env, jobject obj) { + QObject::disconnect(&AndroidHelper::instance(), &AndroidHelper::androidActivityRequested, + nullptr, nullptr); + +} + +JNIEXPORT void Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeGotoUrl(JNIEnv* env, jobject obj, jstring url) { + QAndroidJniObject jniUrl("java/lang/String", "(Ljava/lang/String;)V", url); + DependencyManager::get()->handleLookupString(jniUrl.toString()); } JNIEXPORT void Java_io_highfidelity_hifiinterface_InterfaceActivity_nativeOnPause(JNIEnv* env, jobject obj) { diff --git a/android/app/src/main/java/io/highfidelity/hifiinterface/GotoActivity.java b/android/app/src/main/java/io/highfidelity/hifiinterface/GotoActivity.java index 004dafe23c..5782837634 100644 --- a/android/app/src/main/java/io/highfidelity/hifiinterface/GotoActivity.java +++ b/android/app/src/main/java/io/highfidelity/hifiinterface/GotoActivity.java @@ -25,6 +25,10 @@ import io.highfidelity.hifiinterface.view.DomainAdapter; public class GotoActivity extends AppCompatActivity { + /** + * Set this intent extra param to NOT start a new InterfaceActivity after a domain is selected" + */ + public static final String PARAM_NOT_START_INTERFACE_ACTIVITY = "not_start_interface_activity"; private DomainAdapter domainAdapter; private DrawerLayout mDrawerLayout; private ProgressDialog mDialog; @@ -82,6 +86,11 @@ public class GotoActivity extends AppCompatActivity { Intent intent = new Intent(GotoActivity.this, InterfaceActivity.class); intent.putExtra(InterfaceActivity.DOMAIN_URL, domain.url); GotoActivity.this.finish(); + if (getIntent() != null && + getIntent().hasExtra(PARAM_NOT_START_INTERFACE_ACTIVITY) && + getIntent().getBooleanExtra(PARAM_NOT_START_INTERFACE_ACTIVITY, false)) { + intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + } startActivity(intent); } }); @@ -99,7 +108,13 @@ public class GotoActivity extends AppCompatActivity { preloadQt(); - showActivityIndicator(); + if (getIntent() == null || + !getIntent().hasExtra(PARAM_NOT_START_INTERFACE_ACTIVITY) || + !getIntent().getBooleanExtra(PARAM_NOT_START_INTERFACE_ACTIVITY, false)) { + preloadQt(); + showActivityIndicator(); + } + } private void showActivityIndicator() { @@ -109,7 +124,6 @@ public class GotoActivity extends AppCompatActivity { mDialog.setMessage("Please wait..."); mDialog.setCancelable(false); mDialog.show(); - preloadQt(); } private void cancelActivityIndicator() { 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 68d545d483..0f059a3ff5 100644 --- a/android/app/src/main/java/io/highfidelity/hifiinterface/InterfaceActivity.java +++ b/android/app/src/main/java/io/highfidelity/hifiinterface/InterfaceActivity.java @@ -37,6 +37,8 @@ public class InterfaceActivity extends QtActivity { private native long nativeOnCreate(InterfaceActivity instance, AssetManager assetManager); //private native void nativeOnPause(); //private native void nativeOnResume(); + private native void nativeOnDestroy(); + private native void nativeGotoUrl(String url); //private native void nativeOnStop(); //private native void nativeOnStart(); //private native void saveRealScreenSize(int width, int height); @@ -137,6 +139,12 @@ public class InterfaceActivity extends QtActivity { //gvrApi.resumeTracking(); } + @Override + protected void onDestroy() { + super.onDestroy(); + nativeOnDestroy(); + } + @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); @@ -179,4 +187,18 @@ public class InterfaceActivity extends QtActivity { } } + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + if (intent.hasExtra(DOMAIN_URL)) { + nativeGotoUrl(intent.getStringExtra(DOMAIN_URL)); + } + } + + public void openGotoActivity() { + Intent intent = new Intent(this, GotoActivity.class); + intent.putExtra(GotoActivity.PARAM_NOT_START_INTERFACE_ACTIVITY, true); + startActivity(intent); + } + } \ No newline at end of file diff --git a/interface/resources/qml/+android/AddressBarDialog.qml b/interface/resources/qml/+android/AddressBarDialog.qml index 4477d512fc..e3fcad2b5e 100644 --- a/interface/resources/qml/+android/AddressBarDialog.qml +++ b/interface/resources/qml/+android/AddressBarDialog.qml @@ -99,8 +99,8 @@ Item { y: android.dimen.atLeast1440p ? 280 : 210 imageURL: "../../icons/home.svg" onClicked: { - addressBarDialog.loadHome(); - bar.shown = false; + sendToScript({method: 'openAndroidActivity', params: {}}); + hide(); } anchors { leftMargin: android.dimen.atLeast1440p ? 75 : 56 diff --git a/interface/resources/qml/hifi/+android/bottomHudOptions.qml b/interface/resources/qml/hifi/+android/bottomHudOptions.qml index 860298149f..22beccf531 100644 --- a/interface/resources/qml/hifi/+android/bottomHudOptions.qml +++ b/interface/resources/qml/hifi/+android/bottomHudOptions.qml @@ -47,17 +47,14 @@ Item { spacing: 0 flow: Flow.LeftToRight layoutDirection: Flow.LeftToRight - anchors.fill: parent + anchors.horizontalCenter: parent.horizontalCenter anchors.margins: 12 Rectangle { id: hideButton - height: android.dimen.headerHideWidth - width: android.dimen.headerHideHeight + height: android.dimen.headerHideHeight + width: android.dimen.headerHideWidth color: "#00000000" - anchors { - horizontalCenter: parent.horizontalCenter - } Image { id: hideIcon source: "../../../icons/show-up.svg" diff --git a/interface/src/AndroidHelper.cpp b/interface/src/AndroidHelper.cpp new file mode 100644 index 0000000000..9ae08314ae --- /dev/null +++ b/interface/src/AndroidHelper.cpp @@ -0,0 +1,16 @@ +// +// AndroidHelper.cpp +// interface/src +// +// Created by Gabriel Calero & Cristian Duarte on 3/30/18. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#include "AndroidHelper.h" +#include + +void AndroidHelper::requestActivity(const QString &activityName) { + emit androidActivityRequested(activityName); +} \ No newline at end of file diff --git a/interface/src/AndroidHelper.h b/interface/src/AndroidHelper.h new file mode 100644 index 0000000000..7e5e5a0a39 --- /dev/null +++ b/interface/src/AndroidHelper.h @@ -0,0 +1,34 @@ +// +// AndroidHelper.h +// interface/src +// +// Created by Gabriel Calero & Cristian Duarte on 3/30/18. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_Android_Helper_h +#define hifi_Android_Helper_h + +#include + +class AndroidHelper : public QObject { + Q_OBJECT +public: + static AndroidHelper& instance() { + static AndroidHelper instance; + return instance; + } + void requestActivity(const QString &activityName); + AndroidHelper(AndroidHelper const&) = delete; + void operator=(AndroidHelper const&) = delete; +signals: + void androidActivityRequested(const QString &activityName); + +private: + AndroidHelper() {} +}; + +#endif \ No newline at end of file diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 1324ff9aa3..4684aa126b 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -7865,4 +7865,10 @@ void Application::saveNextPhysicsStats(QString filename) { _physicsEngine->saveNextPhysicsStats(filename); } +void Application::openAndroidActivity(const QString& activityName) { +#if defined(Q_OS_ANDROID) + AndroidHelper::instance().requestActivity(activityName); +#endif +} + #include "Application.moc" diff --git a/interface/src/Application.h b/interface/src/Application.h index d7fbb48a58..ddace2a205 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -78,6 +78,10 @@ #include "Sound.h" +#if defined(Q_OS_ANDROID) +#include "AndroidHelper.h" +#endif + class OffscreenGLCanvas; class GLCanvas; class FaceTracker; @@ -397,6 +401,8 @@ public slots: Q_INVOKABLE bool askBeforeSetAvatarUrl(const QString& avatarUrl) { return askToSetAvatarUrl(avatarUrl); } + Q_INVOKABLE void openAndroidActivity(const QString& activityName); + private slots: void onDesktopRootItemCreated(QQuickItem* qmlContext); void onDesktopRootContextCreated(QQmlContext* qmlContext); diff --git a/scripts/system/+android/goto.js b/scripts/system/+android/goto.js index 2019af9077..39519d8c23 100644 --- a/scripts/system/+android/goto.js +++ b/scripts/system/+android/goto.js @@ -33,6 +33,9 @@ function fromQml(message) { // messages are {method, params}, like json-rpc. See module.exports.hide(); module.exports.onHidden(); break; + case 'openAndroidActivity': + App.openAndroidActivity("Hello"); + break; default: print('[goto-android.js] Unrecognized message from AddressBarDialog.qml:', JSON.stringify(message)); } @@ -43,6 +46,7 @@ function sendToQml(message) { } var isVisible = false; +var qmlConnected = false; var notifyShownChange; module.exports = { init: function() { @@ -52,17 +56,25 @@ module.exports = { }); }, show: function() { + if (isVisible) return; Controller.setVPadHidden(true); if (window) { - window.fromQml.connect(fromQml); + if (!qmlConnected) { + window.fromQml.connect(fromQml); + qmlConnected = true; + } window.setVisible(true); isVisible = true; } }, hide: function() { + if (!isVisible) return; Controller.setVPadHidden(false); if (window) { - window.fromQml.disconnect(fromQml); + if (qmlConnected) { + window.fromQml.disconnect(fromQml); + qmlConnected = false; + } window.setVisible(false); } isVisible = false;