Skip to content

Commit

Permalink
[AOSP] Migrate to GameActivity
Browse files Browse the repository at this point in the history
NativeActivity was the original choice for the Android activity for
the AOSP port because it's a natural choice for an activity with
native code. That was causing issues in MagicLeap2 because key
strokes in external bluetooth keyboards were filtered out.

By switching to GameActivity we can get rid of that limitation.
The migration only required an small adjustment in the back button
handling code because the onKeyUp() event for the back button was
not triggering the onBackPressed() as it does in NativeActivity.
  • Loading branch information
svillar committed Oct 3, 2024
1 parent 805521f commit 6d8007f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
10 changes: 10 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ elseif(HVR)
${CMAKE_SOURCE_DIR}/src/hvr/cpp/native-lib.cpp
${CMAKE_SOURCE_DIR}/src/main/cpp/BrowserEGLContext.cpp
)
elseif(AOSP)
target_sources(
native-lib
PUBLIC
${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.cpp
${CMAKE_SOURCE_DIR}/src/main/cpp/BrowserEGLContext.cpp
)
find_package(game-activity REQUIRED CONFIG)
target_link_libraries(native-lib PRIVATE game-activity::game-activity_static)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -u Java_com_google_androidgamesdk_GameActivity_initializeNativeCode")
else()
target_sources(
native-lib
Expand Down
5 changes: 4 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,9 @@ dependencies {
implementation 'net.lingala.zip4j:zip4j:2.11.5'
implementation 'org.apache.commons:commons-math3:3.6.1'

// AOSP
aospImplementation 'androidx.games:games-activity:3.0.5'

// HVR
hvrImplementation fileTree(dir: "${project.rootDir}/third_party/hvr", include: ['*.jar'])
hvrImplementation 'com.huawei.agconnect:agconnect-core-harmony:1.1.0.300'
Expand All @@ -728,7 +731,7 @@ dependencies {
// Snapdragon Spaces
spacesImplementation fileTree(dir: "${project.rootDir}/third_party/spaces", include: ['*.aar'])

// Vission Glass
// Vision Glass
visionglassImplementation fileTree(dir: "${project.rootDir}/third_party/aliceimu/", include: ['*.aar'])
visionglassImplementation fileTree(dir: "${project.rootDir}/third_party/hvr", include: ['*.jar'])
visionglassImplementation 'com.huawei.agconnect:agconnect-core-harmony:1.1.0.300'
Expand Down
18 changes: 9 additions & 9 deletions app/src/aosp/java/com/igalia/wolvic/PlatformActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

package com.igalia.wolvic;

import android.app.NativeActivity;
import android.content.Intent;
import android.view.KeyEvent;

import com.google.androidgamesdk.GameActivity;
import com.igalia.wolvic.ui.widgets.WidgetManagerDelegate;

public class PlatformActivity extends NativeActivity {
public class PlatformActivity extends GameActivity {

public static boolean filterPermission(final String aPermission) {
// Dummy implementation.
Expand Down Expand Up @@ -41,14 +41,14 @@ protected String getEyeTrackingPermissionString() {
}

@Override
public void onBackPressed() {
queueRunnable(new Runnable() {
@Override
public void run() {
platformExit();
}
});
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// GameActivity does not invoke onBackPressed() on KEYCODE_BACK
onBackPressed();
}
return super.onKeyUp(keyCode, event);
}

protected native void queueRunnable(Runnable aRunnable);
protected native boolean platformExit();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ void handleBack() {
return;
}
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
});
}

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/cpp/BrowserEGLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
#include <EGL/eglext.h>

#ifndef HVR
#if (AOSP)
#include <game-activity/native_app_glue/android_native_app_glue.h>
#else
#include <android_native_app_glue.h>
#endif
#endif

namespace crow {

Expand Down
22 changes: 18 additions & 4 deletions app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
#include "vrb/Logger.h"
#include "vrb/GLError.h"
#include "BrowserEGLContext.h"
#if (AOSP)
#include <game-activity/native_app_glue/android_native_app_glue.h>
#else
#include <android_native_app_glue.h>
#endif
#include <cstdlib>
#include <vrb/RunnableQueue.h>
#if defined(OPENXR)
Expand Down Expand Up @@ -142,13 +146,19 @@ android_main(android_app *aAppState) {

sAppContext->mQueue->AttachToThread();

#if (AOSP)
jobject activity = aAppState->activity->javaGameActivity;
#else
jobject activity = aAppState->activity->clazz;
#endif

// Create Browser context
crow::VRBrowser::InitializeJava(jniEnv, aAppState->activity->clazz);
crow::VRBrowser::InitializeJava(jniEnv, activity);

// Create device delegate
sAppContext->mJavaContext.env = jniEnv;
sAppContext->mJavaContext.vm = aAppState->activity->vm;
sAppContext->mJavaContext.activity = aAppState->activity->clazz;
sAppContext->mJavaContext.activity = activity;

#if defined(OCULUSVR) && defined(STORE_BUILD)
if (!ovr_IsPlatformInitialized()) {
Expand All @@ -171,8 +181,8 @@ android_main(android_app *aAppState) {
BrowserWorld::Instance().RegisterDeviceDelegate(sAppContext->mDevice);

// Initialize java
auto assetManager = GetAssetManager(jniEnv, aAppState->activity->clazz);
BrowserWorld::Instance().InitializeJava(jniEnv, aAppState->activity->clazz, assetManager);
auto assetManager = GetAssetManager(jniEnv, activity);
BrowserWorld::Instance().InitializeJava(jniEnv, activity, assetManager);
jniEnv->DeleteLocalRef(assetManager);

auto MaybeInitGLAndEnterVR = [aAppState]() {
Expand Down Expand Up @@ -208,7 +218,11 @@ android_main(android_app *aAppState) {
// We need to call ProcessEvents to make sure we receive the event.
sAppContext->mDevice->ProcessEvents();
if (sAppContext->mDevice->ShouldExitRenderLoop()) {
#if (AOSP)
GameActivity_finish(aAppState->activity);
#else
ANativeActivity_finish(aAppState->activity);
#endif
continue;
}
#endif
Expand Down

0 comments on commit 6d8007f

Please sign in to comment.