a/android: Add ActivityLifecycleListener class

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/1655>
This commit is contained in:
Jarvis Huang 2022-02-23 15:44:45 +08:00 committed by Rylie Pavlik
parent 449016be51
commit 84efd4d6fb
4 changed files with 195 additions and 0 deletions

View file

@ -32,5 +32,11 @@ namespace org::freedesktop::monado::auxiliary {
getDisplayModeIdHeight(
classRef().getStaticMethod("getDisplayModeIdHeight", "(Landroid/content/Context;II)I"))
{}
ActivityLifecycleListener::Meta::Meta(jni::jclass clazz)
: MetaBase(ActivityLifecycleListener::getTypeName(), clazz), init(classRef().getMethod("<init>", "(J)V")),
registerCallback(classRef().getMethod("registerCallback", "(Landroid/app/Activity;)V")),
unregisterCallback(classRef().getMethod("unregisterCallback", "(Landroid/app/Activity;)V"))
{}
} // namespace org::freedesktop::monado::auxiliary
} // namespace wrap

View file

@ -213,6 +213,93 @@ namespace org::freedesktop::monado::auxiliary {
};
};
class ActivityLifecycleListener : public ObjectWrapperBase
{
public:
using ObjectWrapperBase::ObjectWrapperBase;
static constexpr const char *
getTypeName() noexcept
{
return "org/freedesktop/monado/auxiliary/ActivityLifecycleListener";
}
static constexpr const char *
getFullyQualifiedTypeName() noexcept
{
return "org.freedesktop.monado.auxiliary.ActivityLifecycleListener";
}
/*!
* Initialize the static metadata of this wrapper with a known
* (non-null) Java class.
*/
static void
staticInitClass(jni::jclass clazz)
{
Meta::data(clazz);
}
/*!
* Wrapper for a constructor
*
* Java prototype:
* `public org.freedesktop.monado.auxiliary.ActivityLifecycleListener(long);`
*
* JNI signature: (J)V
*
*/
static ActivityLifecycleListener
construct(void *nativePointer);
/*!
* Wrapper for the registerCallback method
*
* Java prototype:
* `public void registerCallback(android.app.Activity);`
*
* JNI signature: (Landroid/app/Activity;)V
*
*/
void
registerCallback(android::app::Activity const &activity);
/*!
* Wrapper for the unregisterCallback method
*
* Java prototype:
* `public void unregisterCallback(android.app.Activity);`
*
* JNI signature: (Landroid/app/Activity;)V
*
*/
void
unregisterCallback(android::app::Activity const &activity);
/*!
* Class metadata
*/
struct Meta : public MetaBase
{
jni::method_t init;
jni::method_t registerCallback;
jni::method_t unregisterCallback;
/*!
* Singleton accessor
*/
static Meta &
data(jni::jclass clazz = nullptr)
{
static Meta instance{clazz};
return instance;
}
private:
explicit Meta(jni::jclass clazz);
};
};
} // namespace org::freedesktop::monado::auxiliary
} // namespace wrap
#include "org.freedesktop.monado.auxiliary.impl.hpp"

View file

@ -86,5 +86,23 @@ namespace org::freedesktop::monado::auxiliary {
object().call<jni::Object>(Meta::data().waitGetSurfaceHolder, wait_ms));
}
inline ActivityLifecycleListener
ActivityLifecycleListener::construct(void *nativePointer)
{
return ActivityLifecycleListener(Meta::data().clazz().newInstance(
Meta::data().init, static_cast<long long>(reinterpret_cast<uintptr_t>(nativePointer))));
}
inline void
ActivityLifecycleListener::registerCallback(android::app::Activity const &activity)
{
object().call<void>(Meta::data().registerCallback, activity.object());
}
inline void
ActivityLifecycleListener::unregisterCallback(android::app::Activity const &activity)
{
object().call<void>(Meta::data().unregisterCallback, activity.object());
}
} // namespace org::freedesktop::monado::auxiliary
} // namespace wrap

View file

@ -0,0 +1,84 @@
// Copyright 2022, Qualcomm Innovation Center, Inc.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Class that listens to activity lifecycle change event.
* @author Jarvis Huang
* @ingroup aux_android_java
*/
package org.freedesktop.monado.auxiliary
import android.app.Activity
import android.app.Application
import android.os.Bundle
import android.util.Log
import androidx.annotation.Keep
/** Monitor activity lifecycle of application. */
@Keep
class ActivityLifecycleListener(private val nativePtr: Long) :
Application.ActivityLifecycleCallbacks {
/** Register callback with [Application] from given [activity] object. */
fun registerCallback(activity: Activity) {
activity.application.registerActivityLifecycleCallbacks(this)
}
/** Unregister callback with [Application] from given [activity] object. */
fun unregisterCallback(activity: Activity) {
activity.application.unregisterActivityLifecycleCallbacks(this)
}
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
Log.i(TAG, "$activity onActivityCreated")
nativeOnActivityCreated(nativePtr, activity)
}
override fun onActivityStarted(activity: Activity) {
Log.i(TAG, "$activity onActivityStarted")
nativeOnActivityStarted(nativePtr, activity)
}
override fun onActivityResumed(activity: Activity) {
Log.i(TAG, "$activity onActivityResumed")
nativeOnActivityResumed(nativePtr, activity)
}
override fun onActivityPaused(activity: Activity) {
Log.i(TAG, "$activity onActivityPaused")
nativeOnActivityPaused(nativePtr, activity)
}
override fun onActivityStopped(activity: Activity) {
Log.i(TAG, "$activity onActivityStopped")
nativeOnActivityStopped(nativePtr, activity)
}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
Log.i(TAG, "$activity onActivitySaveInstanceState")
nativeOnActivitySaveInstanceState(nativePtr, activity)
}
override fun onActivityDestroyed(activity: Activity) {
Log.i(TAG, "$activity onActivityDestroyed")
nativeOnActivityDestroyed(nativePtr, activity)
}
private external fun nativeOnActivityCreated(nativePtr: Long, activity: Activity)
private external fun nativeOnActivityStarted(nativePtr: Long, activity: Activity)
private external fun nativeOnActivityResumed(nativePtr: Long, activity: Activity)
private external fun nativeOnActivityPaused(nativePtr: Long, activity: Activity)
private external fun nativeOnActivityStopped(nativePtr: Long, activity: Activity)
private external fun nativeOnActivitySaveInstanceState(nativePtr: Long, activity: Activity)
private external fun nativeOnActivityDestroyed(nativePtr: Long, activity: Activity)
companion object {
const val TAG = "ActivityLifecycleListener"
}
}