diff --git a/src/xrt/auxiliary/android/android_custom_surface.cpp b/src/xrt/auxiliary/android/android_custom_surface.cpp index 4c3367050..dc03ef642 100644 --- a/src/xrt/auxiliary/android/android_custom_surface.cpp +++ b/src/xrt/auxiliary/android/android_custom_surface.cpp @@ -15,6 +15,7 @@ #include "util/u_logging.h" #include "wrap/android.app.h" +#include "wrap/android.content.h" #include "wrap/android.view.h" #include "org.freedesktop.monado.auxiliary.hpp" @@ -22,6 +23,7 @@ using wrap::android::app::Activity; +using wrap::android::content::Context; using wrap::android::view::SurfaceHolder; using wrap::org::freedesktop::monado::auxiliary::MonadoView; using xrt::auxiliary::android::loadClassFromRuntimeApk; @@ -140,12 +142,13 @@ android_custom_surface_wait_get_surface(struct android_custom_surface *custom_su bool android_custom_surface_get_display_metrics(struct _JavaVM *vm, - void *activity, + void *context, struct xrt_android_display_metrics *out_metrics) { jni::init(vm); + try { - auto clazz = loadClassFromRuntimeApk((jobject)activity, MonadoView::getFullyQualifiedTypeName()); + auto clazz = loadClassFromRuntimeApk((jobject)context, MonadoView::getFullyQualifiedTypeName()); if (clazz.isNull()) { U_LOG_E("Could not load class '%s' from package '%s'", MonadoView::getFullyQualifiedTypeName(), XRT_ANDROID_PACKAGE); @@ -155,9 +158,9 @@ android_custom_surface_get_display_metrics(struct _JavaVM *vm, // Teach the wrapper our class before we start to use it. MonadoView::staticInitClass((jclass)clazz.object().getHandle()); - jni::Object displayMetrics = MonadoView::getDisplayMetrics(Activity((jobject)activity)); + jni::Object displayMetrics = MonadoView::getDisplayMetrics(Context((jobject)context)); //! @todo implement non-deprecated codepath for api 30+ - float displayRefreshRate = MonadoView::getDisplayRefreshRate(Activity((jobject)activity)); + float displayRefreshRate = MonadoView::getDisplayRefreshRate(Context((jobject)context)); if (displayRefreshRate == 0.0) { displayRefreshRate = 60.0f; } @@ -171,7 +174,6 @@ android_custom_surface_get_display_metrics(struct _JavaVM *vm, .ydpi = displayMetrics.get("scaledDensity"), .refresh_rate = displayRefreshRate}; return true; - } catch (std::exception const &e) { U_LOG_E("Could not get display metrics: %s", e.what()); return false; diff --git a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.cpp b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.cpp index 2625df3e6..bef0d651e 100644 --- a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.cpp +++ b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.cpp @@ -19,8 +19,9 @@ namespace org::freedesktop::monado::auxiliary { attachToActivity1(classRef().getStaticMethod( "attachToActivity", "(Landroid/app/Activity;)Lorg/freedesktop/monado/auxiliary/MonadoView;")), getDisplayMetrics(classRef().getStaticMethod("getDisplayMetrics", - "(Landroid/app/Activity;)Landroid/util/DisplayMetrics;")), - getDisplayRefreshRate(classRef().getStaticMethod("getDisplayRefreshRate", "(Landroid/app/Activity;)F")), + "(Landroid/content/Context;)Landroid/util/DisplayMetrics;")), + getDisplayRefreshRate( + classRef().getStaticMethod("getDisplayRefreshRate", "(Landroid/content/Context;)F")), getNativePointer(classRef().getMethod("getNativePointer", "()J")), markAsDiscardedByNative(classRef().getMethod("markAsDiscardedByNative", "()V")), waitGetSurfaceHolder(classRef().getMethod("waitGetSurfaceHolder", "(I)Landroid/view/SurfaceHolder;")) diff --git a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.hpp b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.hpp index 57209bd51..001a592cf 100644 --- a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.hpp +++ b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.hpp @@ -17,6 +17,10 @@ namespace android::app { class Activity; } // namespace android::app +namespace android::content { + class Context; +} // namespace android::content + namespace android::view { class SurfaceHolder; } // namespace android::view @@ -78,25 +82,25 @@ namespace org::freedesktop::monado::auxiliary { * Wrapper for the getDisplayMetrics static method * * Java prototype: - * `public static android.util.DisplayMetrics getDisplayMetrics(android.app.Activity);` + * `public static android.util.DisplayMetrics getDisplayMetrics(android.content.Context);` * - * JNI signature: (Landroid/app/Activity;)Landroid/util/DisplayMetrics; + * JNI signature: (Landroid/content/Context;)Landroid/util/DisplayMetrics; * */ static jni::Object - getDisplayMetrics(android::app::Activity const &activity); + getDisplayMetrics(android::content::Context const &context); /*! * Wrapper for the getDisplayRefreshRate static method * * Java prototype: - * `public static float getDisplayRefreshRate(android.app.Activity);` + * `public static float getDisplayRefreshRate(android.content.Context);` * - * JNI signature: (Landroid/app/Activity;)F; + * JNI signature: (Landroid/content/Context;)F; * */ static float - getDisplayRefreshRate(android::app::Activity const &activity); + getDisplayRefreshRate(android::content::Context const &context); /*! * Wrapper for the getNativePointer method diff --git a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.impl.hpp b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.impl.hpp index bc0f44339..682e4f48b 100644 --- a/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.impl.hpp +++ b/src/xrt/auxiliary/android/org.freedesktop.monado.auxiliary.impl.hpp @@ -11,6 +11,7 @@ #pragma once #include "wrap/android.app.h" +#include "wrap/android.content.h" #include "wrap/android.view.h" @@ -32,15 +33,15 @@ namespace org::freedesktop::monado::auxiliary { } inline jni::Object - MonadoView::getDisplayMetrics(android::app::Activity const &activity) + MonadoView::getDisplayMetrics(android::content::Context const &context) { - return Meta::data().clazz().call(Meta::data().getDisplayMetrics, activity.object()); + return Meta::data().clazz().call(Meta::data().getDisplayMetrics, context.object()); } inline float - MonadoView::getDisplayRefreshRate(android::app::Activity const &activity) + MonadoView::getDisplayRefreshRate(android::content::Context const &context) { - return Meta::data().clazz().call(Meta::data().getDisplayRefreshRate, activity.object()); + return Meta::data().clazz().call(Meta::data().getDisplayRefreshRate, context.object()); } inline void * diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java index 83f68924f..96b832811 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java @@ -11,22 +11,17 @@ package org.freedesktop.monado.auxiliary; import android.app.Activity; import android.content.Context; -import android.os.Build; import android.util.DisplayMetrics; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; -import android.view.View; import android.view.WindowManager; import androidx.annotation.GuardedBy; import androidx.annotation.Keep; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.Calendar; @Keep @@ -100,15 +95,17 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S @NonNull @Keep - public static DisplayMetrics getDisplayMetrics(Activity activity) { + public static DisplayMetrics getDisplayMetrics(@NonNull Context context) { DisplayMetrics displayMetrics = new DisplayMetrics(); - activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + wm.getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics; } @Keep - public static float getDisplayRefreshRate(Activity activity) { - return activity.getWindowManager().getDefaultDisplay().getRefreshRate(); + public static float getDisplayRefreshRate(@NonNull Context context) { + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + return wm.getDefaultDisplay().getRefreshRate(); } @Keep diff --git a/src/xrt/drivers/android/android_sensors.c b/src/xrt/drivers/android/android_sensors.c index e8f0e2b46..8e1fd7826 100644 --- a/src/xrt/drivers/android/android_sensors.c +++ b/src/xrt/drivers/android/android_sensors.c @@ -235,7 +235,7 @@ android_device_create() } struct xrt_android_display_metrics metrics; - if (!android_custom_surface_get_display_metrics(android_globals_get_vm(), android_globals_get_activity(), + if (!android_custom_surface_get_display_metrics(android_globals_get_vm(), android_globals_get_context(), &metrics)) { U_LOG_E("Could not get Android display metrics."); /* Fallback to default values (Pixel 3) */