From d0187cee9a66959a46fff2491879bb4f7d44eb4f Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Tue, 13 Oct 2020 15:10:38 -0500 Subject: [PATCH] aux/android: Add Java code --- build.gradle | 34 ++++++++++ doc/changes/auxiliary/mr.493.md | 3 + gradle.properties | 5 ++ settings.gradle | 6 ++ src/xrt/auxiliary/android/build.gradle | 31 +++++++++ src/xrt/auxiliary/android/proguard-rules.pro | 4 ++ .../android/src/main/AndroidManifest.xml | 8 +++ .../monado/auxiliary/MonadoView.java | 68 +++++++++++++++++++ 8 files changed, 159 insertions(+) create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 settings.gradle create mode 100644 src/xrt/auxiliary/android/build.gradle create mode 100644 src/xrt/auxiliary/android/proguard-rules.pro create mode 100644 src/xrt/auxiliary/android/src/main/AndroidManifest.xml create mode 100644 src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java diff --git a/build.gradle b/build.gradle new file mode 100644 index 000000000..08c46e3b2 --- /dev/null +++ b/build.gradle @@ -0,0 +1,34 @@ +// Copyright 2020, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 + +buildscript { + ext.kotlin_version = '1.4.10' + repositories { + google() + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:4.1.0' + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} +plugins { + // Used for getting the eigen dir from local.properties + id 'com.github.b3er.local.properties' version '1.1' +} + +ext { + ndk_version = '21.3.6528147' + sharedTargetSdk = 30 + sharedMinSdk = 26 + + // If you get an error here, make sure you have this set in local.properties + eigenIncludeDir = project.property('eigenIncludeDir') +} + +allprojects { + repositories { + google() + jcenter() + } +} diff --git a/doc/changes/auxiliary/mr.493.md b/doc/changes/auxiliary/mr.493.md index e3417060c..a1ef210a2 100644 --- a/doc/changes/auxiliary/mr.493.md +++ b/doc/changes/auxiliary/mr.493.md @@ -1 +1,4 @@ +--- +- mr.547 +--- aux/android: New Android utility library added. diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 000000000..29c20d3d7 --- /dev/null +++ b/gradle.properties @@ -0,0 +1,5 @@ +# Copyright 2020, Collabora, Ltd. +# SPDX-License-Identifier: BSL-1.0 + +android.useAndroidX = true + diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 000000000..b286e35e6 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,6 @@ +// Copyright 2020, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 + +rootProject.name = 'monado' + +include ":src:xrt:auxiliary:android" diff --git a/src/xrt/auxiliary/android/build.gradle b/src/xrt/auxiliary/android/build.gradle new file mode 100644 index 000000000..1e6d38d57 --- /dev/null +++ b/src/xrt/auxiliary/android/build.gradle @@ -0,0 +1,31 @@ +// Copyright 2020, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 + +apply plugin: 'com.android.library' + +android { + compileSdkVersion project.sharedTargetSdk + buildToolsVersion '30.0.2' + + defaultConfig { + minSdkVersion 20 + targetSdkVersion project.sharedTargetSdk + } + + buildTypes { + release { + minifyEnabled false + // Gradle plugin produces proguard-android-optimize.txt from @Keep annotations + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'androidx.annotation:annotation:1.1.0' +} diff --git a/src/xrt/auxiliary/android/proguard-rules.pro b/src/xrt/auxiliary/android/proguard-rules.pro new file mode 100644 index 000000000..686f8abdb --- /dev/null +++ b/src/xrt/auxiliary/android/proguard-rules.pro @@ -0,0 +1,4 @@ +# Copyright 2020, Collabora, Ltd. +# SPDX-License-Identifier: BSL-1.0 +# see http://developer.android.com/guide/developing/tools/proguard.html +# Trying to keep most of them in source code annotations and let Gradle do the work diff --git a/src/xrt/auxiliary/android/src/main/AndroidManifest.xml b/src/xrt/auxiliary/android/src/main/AndroidManifest.xml new file mode 100644 index 000000000..bdbf534a0 --- /dev/null +++ b/src/xrt/auxiliary/android/src/main/AndroidManifest.xml @@ -0,0 +1,8 @@ + + + + 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 new file mode 100644 index 000000000..9f0ff9648 --- /dev/null +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/MonadoView.java @@ -0,0 +1,68 @@ +// Copyright 2020, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief Class to inject a custom surface into an activity. + * @author Ryan Pavlik + * @ingroup aux_android_java + */ + +package org.freedesktop.monado.auxiliary; + +import android.app.Activity; +import android.content.Context; +import android.util.Log; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.WindowManager; + +import androidx.annotation.NonNull; +import androidx.annotation.Keep; + +@Keep +public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, SurfaceHolder.Callback2 { + private static final String TAG = "MonadoView"; + public static MonadoView attachToActivity(@NonNull Activity activity) { + Log.i(TAG, "Starting to add a new surface!"); + + MonadoView view = new MonadoView(activity); + WindowManager windowManager = activity.getWindowManager(); + windowManager.addView(view, new WindowManager.LayoutParams()); + view.requestFocus(); + return view; + } + + public SurfaceHolder currentSurfaceHolder; + + public MonadoView(Context context) { + super(context); + } + + @Override + public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) { + currentSurfaceHolder = surfaceHolder; + Log.i(TAG, "surfaceCreated: Got a surface holder!"); + } + + @Override + public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int format, int width, int height) { + Log.i(TAG, "surfaceChanged"); + + } + + @Override + public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) { + //! @todo this function should block until the surface is no longer used in the native code. + Log.i(TAG, "surfaceDestroyed: Lost our surface."); + if (surfaceHolder == currentSurfaceHolder) { + currentSurfaceHolder = null; + } + } + + @Override + public void surfaceRedrawNeeded(@NonNull SurfaceHolder surfaceHolder) { + Log.i(TAG, "surfaceRedrawNeeded"); + + } + +}