From fc3af6f711d45909951c3a04e888eb40bbb29b8d Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Mon, 17 Apr 2023 10:54:32 -0500 Subject: [PATCH] a/android: Run spotlessApply to format Java and Kotlin code --- .../monado/auxiliary/IServiceNotification.kt | 4 +- .../monado/auxiliary/MonadoView.java | 128 ++++++++---------- .../monado/auxiliary/NameAndLogoProvider.kt | 7 +- .../monado/auxiliary/NativeCounterpart.java | 45 +++--- .../monado/auxiliary/SystemUiController.kt | 51 +++---- .../monado/auxiliary/UiProvider.kt | 5 +- 6 files changed, 114 insertions(+), 126 deletions(-) diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/IServiceNotification.kt b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/IServiceNotification.kt index 654366fa1..62c723430 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/IServiceNotification.kt +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/IServiceNotification.kt @@ -26,8 +26,6 @@ interface IServiceNotification { */ fun buildNotification(context: Context, pendingShutdownIntent: PendingIntent): Notification - /** - * Return the notification ID to use - */ + /** Return the notification ID to use */ fun getNotificationId(): Int } 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 ce4003159..fe48efac6 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 @@ -24,18 +24,15 @@ import androidx.annotation.Keep; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import java.util.Calendar; - @Keep -public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, SurfaceHolder.Callback2 { +public class MonadoView extends SurfaceView + implements SurfaceHolder.Callback, SurfaceHolder.Callback2 { private static final String TAG = "MonadoView"; - @NonNull - private final Context context; + @NonNull private final Context context; /// The activity we've connected to. - @Nullable - private final Activity activity; + @Nullable private final Activity activity; private final Object currentSurfaceHolderSync = new Object(); public int width = -1; @@ -44,9 +41,7 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S private NativeCounterpart nativeCounterpart; - @GuardedBy("currentSurfaceHolderSync") - @Nullable - private SurfaceHolder currentSurfaceHolder = null; + @GuardedBy("currentSurfaceHolderSync") @Nullable private SurfaceHolder currentSurfaceHolder = null; private SystemUiController systemUiController = null; @@ -80,29 +75,27 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S /** * Construct and start attaching a MonadoView to a client application. * - * @param activity The activity to attach to. + * @param activity The activity to attach to. * @param nativePointer The native android_custom_surface pointer, cast to a long. * @return The MonadoView instance created and asynchronously attached. */ - @NonNull - @Keep + @NonNull @Keep @SuppressWarnings("deprecation") - public static MonadoView attachToActivity(@NonNull final Activity activity, long nativePointer) { + public static MonadoView attachToActivity( + @NonNull final Activity activity, long nativePointer) { final MonadoView view = new MonadoView(activity, nativePointer); view.createSurfaceInActivity(); return view; } - @NonNull - @Keep + @NonNull @Keep public static MonadoView attachToActivity(@NonNull final Activity activity) { final MonadoView view = new MonadoView(activity); view.createSurfaceInActivity(); return view; } - @NonNull - @Keep + @NonNull @Keep public static DisplayMetrics getDisplayMetrics(@NonNull Context context) { DisplayMetrics displayMetrics = new DisplayMetrics(); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); @@ -128,62 +121,61 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S createSurfaceInActivity(false); } - /** - * @param focusable Indicates MonadoView should be focusable or not - */ + /** @param focusable Indicates MonadoView should be focusable or not */ private void createSurfaceInActivity(boolean focusable) { Log.i(TAG, "Starting to add a new surface!"); - activity.runOnUiThread(() -> { - Log.i(TAG, "Starting runOnUiThread"); - activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); + activity.runOnUiThread( + () -> { + Log.i(TAG, "Starting runOnUiThread"); + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - WindowManager windowManager = activity.getWindowManager(); - WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); - if (focusable) { - lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN; - } else { - // There are 2 problems if view is focusable on all-in-one device: - // 1. Navigation bar won't go away because view gets focus. - // 2. Underlying activity lost focus and cannot receive input. - lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | - WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; - } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - lp.layoutInDisplayCutoutMode = - WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; - } - windowManager.addView(this, lp); - if (focusable) { - requestFocus(); - } - SurfaceHolder surfaceHolder = getHolder(); - surfaceHolder.addCallback(this); - Log.i(TAG, "Registered callbacks!"); - }); + WindowManager windowManager = activity.getWindowManager(); + WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); + if (focusable) { + lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN; + } else { + // There are 2 problems if view is focusable on all-in-one device: + // 1. Navigation bar won't go away because view gets focus. + // 2. Underlying activity lost focus and cannot receive input. + lp.flags = + WindowManager.LayoutParams.FLAG_FULLSCREEN + | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; + } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + lp.layoutInDisplayCutoutMode = + WindowManager.LayoutParams + .LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; + } + windowManager.addView(this, lp); + if (focusable) { + requestFocus(); + } + SurfaceHolder surfaceHolder = getHolder(); + surfaceHolder.addCallback(this); + Log.i(TAG, "Registered callbacks!"); + }); } /** * Block up to a specified amount of time, waiting for the surfaceCreated callback to be fired * and populate the currentSurfaceHolder. - *

- * If it returns a SurfaceHolder, the `usedByNativeCode` flag will be set. - *

- * Called by native code! + * + *

If it returns a SurfaceHolder, the `usedByNativeCode` flag will be set. + * + *

Called by native code! * * @param wait_ms Max duration you prefer to wait, in milliseconds. Spurious wakeups mean this - * not be totally precise. + * not be totally precise. * @return A SurfaceHolder or null. */ @Keep - public @Nullable - SurfaceHolder waitGetSurfaceHolder(int wait_ms) { + public @Nullable SurfaceHolder waitGetSurfaceHolder(int wait_ms) { long currentTime = SystemClock.uptimeMillis(); long timeout = currentTime + wait_ms; SurfaceHolder ret = null; synchronized (currentSurfaceHolderSync) { ret = currentSurfaceHolder; - while (currentSurfaceHolder == null - && SystemClock.uptimeMillis() < timeout) { + while (currentSurfaceHolder == null && SystemClock.uptimeMillis() < timeout) { try { currentSurfaceHolderSync.wait(wait_ms, 0); ret = currentSurfaceHolder; @@ -194,8 +186,7 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S } } if (ret != null) { - if (nativeCounterpart != null) - nativeCounterpart.markAsUsedByNativeCode(); + if (nativeCounterpart != null) nativeCounterpart.markAsUsedByNativeCode(); } return ret; } @@ -203,13 +194,12 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S /** * Change the flag and notify those waiting on it, to indicate that native code is done with * this object. - *

- * Called by native code! + * + *

Called by native code! */ @Keep public void markAsDiscardedByNative() { - if (nativeCounterpart != null) - nativeCounterpart.markAsDiscardedByNative(TAG); + if (nativeCounterpart != null) nativeCounterpart.markAsDiscardedByNative(TAG); } @Override @@ -222,7 +212,8 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S } @Override - public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int format, int width, int height) { + public void surfaceChanged( + @NonNull SurfaceHolder surfaceHolder, int format, int width, int height) { synchronized (currentSurfaceHolderSync) { currentSurfaceHolder = surfaceHolder; @@ -245,18 +236,19 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S } } if (lost) { - //! @todo this function should notify native code that the surface is gone. + // ! @todo this function should notify native code that the surface is gone. if (nativeCounterpart != null && !nativeCounterpart.blockUntilNativeDiscard(TAG)) { - Log.i(TAG, - "Interrupted in surfaceDestroyed while waiting for native code to finish up."); + Log.i( + TAG, + "Interrupted in surfaceDestroyed while waiting for native code to finish" + + " up."); } } } @Override public void surfaceRedrawNeeded(@NonNull SurfaceHolder surfaceHolder) { -// currentSurfaceHolder = surfaceHolder; + // currentSurfaceHolder = surfaceHolder; Log.i(TAG, "surfaceRedrawNeeded"); } - } diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/NameAndLogoProvider.kt b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/NameAndLogoProvider.kt index 5c4bce56d..7d2ae6e33 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/NameAndLogoProvider.kt +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/NameAndLogoProvider.kt @@ -17,13 +17,12 @@ import android.graphics.drawable.Drawable * Intended for use in dependency injection. */ interface NameAndLogoProvider { - /** - * Gets a localized runtime name string for the runtime/Monado-incorporating target. - */ + /** Gets a localized runtime name string for the runtime/Monado-incorporating target. */ fun getLocalizedRuntimeName(): CharSequence /** - * Gets a drawable for use in the about activity and elsewhere, for the runtime/Monado-incorporating target. + * Gets a drawable for use in the about activity and elsewhere, for the + * runtime/Monado-incorporating target. */ fun getLogoDrawable(): Drawable? } diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/NativeCounterpart.java b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/NativeCounterpart.java index 1b9c976cc..0bde99f56 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/NativeCounterpart.java +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/NativeCounterpart.java @@ -20,11 +20,13 @@ import java.security.InvalidParameterException; * Object that tracks the native counterpart object for a type. Must be initialized on construction, * and may have its native code destroyed/discarded, but may not "re-seat" it to new native code * pointer. + * + *

Use as a member of any type with a native counterpart (a native-allocated-and-owned object + * that holds a reference to the owning class). Include the following field and delegating method to + * use (note: assumes you have a tag for logging purposes as TAG) + * *

- * Use as a member of any type with a native counterpart (a native-allocated-and-owned object that - * holds a reference to the owning class). Include the following field and delegating method to use - * (note: assumes you have a tag for logging purposes as TAG) - *

+ * *

  * private final NativeCounterpart nativeCounterpart;
  *
@@ -33,36 +35,33 @@ import java.security.InvalidParameterException;
  *     nativeCounterpart.markAsDiscardedByNative(TAG);
  * }
  * 
+ * * Then, initialize it in your constructor, call {@code markAsUsedByNativeCode()} where desired - * (often in your constructor), and call {@code getNativePointer()} and - * {@code blockUntilNativeDiscard()} as needed. - *

- * Your native code can use this to turn a void* into a jlong: - * {@code static_cast(reinterpret_cast(nativePointer))} + * (often in your constructor), and call {@code getNativePointer()} and {@code + * blockUntilNativeDiscard()} as needed. + * + *

Your native code can use this to turn a void* into a jlong: {@code static_cast(reinterpret_cast(nativePointer))} */ public final class NativeCounterpart { - /** - * Guards the usedByNativeCodeSync. - */ + /** Guards the usedByNativeCodeSync. */ private final Object usedByNativeCodeSync = new Object(); /** * Indicates if the containing object is in use by native code. - *

- * Guarded by usedByNativeCodeSync. + * + *

Guarded by usedByNativeCodeSync. */ private boolean usedByNativeCode = false; - /** - * Contains the pointer to the native counterpart object. - */ + /** Contains the pointer to the native counterpart object. */ private long nativePointer = 0; /** * Constructor * * @param nativePointer The native pointer, cast appropriately. Must be non-zero. Can cast like: - * {@code static_cast(reinterpret_cast(nativePointer))} + * {@code static_cast(reinterpret_cast(nativePointer))} */ public NativeCounterpart(long nativePointer) throws InvalidParameterException { if (nativePointer == 0) { @@ -92,8 +91,10 @@ public final class NativeCounterpart { public void markAsDiscardedByNative(String TAG) { synchronized (usedByNativeCodeSync) { if (!usedByNativeCode) { - Log.w(TAG, - "This should not have happened: Discarding by native code, but not marked as used!"); + Log.w( + TAG, + "This should not have happened: Discarding by native code, but not marked" + + " as used!"); } usedByNativeCode = false; nativePointer = 0; @@ -130,10 +131,8 @@ public final class NativeCounterpart { } } catch (InterruptedException e) { e.printStackTrace(); - Log.i(TAG, - "Interrupted while waiting for native code to finish up: " + e); + Log.i(TAG, "Interrupted while waiting for native code to finish up: " + e); return false; } - } } diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/SystemUiController.kt b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/SystemUiController.kt index 59f0c5404..3a2636a6c 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/SystemUiController.kt +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/SystemUiController.kt @@ -15,19 +15,16 @@ import android.view.WindowInsets import android.view.WindowInsetsController import androidx.annotation.RequiresApi -/** - * Helper class that handles system ui visibility. - */ +/** Helper class that handles system ui visibility. */ class SystemUiController(activity: Activity) { - private val impl: Impl = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - WindowInsetsControllerImpl(activity) - } else { - SystemUiVisibilityImpl(activity) - } + private val impl: Impl = + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + WindowInsetsControllerImpl(activity) + } else { + SystemUiVisibilityImpl(activity) + } - /** - * Hide system ui and make fullscreen. - */ + /** Hide system ui and make fullscreen. */ fun hide() { impl.hide() } @@ -51,11 +48,13 @@ class SystemUiController(activity: Activity) { private const val FLAG_FULL_SCREEN_IMMERSIVE_STICKY = // Give us a stable view of content insets (View.SYSTEM_UI_FLAG_LAYOUT_STABLE // Be able to do fullscreen and hide navigation - or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - or View.SYSTEM_UI_FLAG_FULLSCREEN - or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // we want sticky immersive - or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) + or + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or + View.SYSTEM_UI_FLAG_FULLSCREEN or + View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // we want sticky immersive + or + View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) } init { @@ -75,9 +74,11 @@ class SystemUiController(activity: Activity) { override fun hide() { activity.runOnUiThread { val controller = activity.window.insetsController - controller!!.hide(WindowInsets.Type.displayCutout() - or WindowInsets.Type.statusBars() - or WindowInsets.Type.navigationBars()) + controller!!.hide( + WindowInsets.Type.displayCutout() or + WindowInsets.Type.statusBars() or + WindowInsets.Type.navigationBars() + ) controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE } @@ -85,10 +86,13 @@ class SystemUiController(activity: Activity) { init { runOnUiThread { - activity.window.insetsController!!.addOnControllableInsetsChangedListener { _: WindowInsetsController?, typeMask: Int -> - if (typeMask and WindowInsets.Type.displayCutout() == 1 - || typeMask and WindowInsets.Type.statusBars() == 1 - || typeMask and WindowInsets.Type.navigationBars() == 1 + activity.window.insetsController!!.addOnControllableInsetsChangedListener { + _: WindowInsetsController?, + typeMask: Int -> + if ( + typeMask and WindowInsets.Type.displayCutout() == 1 || + typeMask and WindowInsets.Type.statusBars() == 1 || + typeMask and WindowInsets.Type.navigationBars() == 1 ) { hide() } @@ -96,5 +100,4 @@ class SystemUiController(activity: Activity) { } } } - } diff --git a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/UiProvider.kt b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/UiProvider.kt index b46f3341a..e032694e7 100644 --- a/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/UiProvider.kt +++ b/src/xrt/auxiliary/android/src/main/java/org/freedesktop/monado/auxiliary/UiProvider.kt @@ -24,14 +24,11 @@ interface UiProvider { */ fun getNotificationIcon(): Icon? = null - /** - * Make a {@code PendingIntent} to launch an "About" activity for the runtime/target. - */ + /** Make a {@code PendingIntent} to launch an "About" activity for the runtime/target. */ fun makeAboutActivityPendingIntent(): PendingIntent /** * Make a {@code PendingIntent} to launch a configuration activity, if provided by the target. */ fun makeConfigureActivityPendingIntent(): PendingIntent? = null - }