a/android: Run spotlessApply to format Java and Kotlin code

This commit is contained in:
Ryan Pavlik 2023-04-17 10:54:32 -05:00
parent b5b0c30936
commit fc3af6f711
6 changed files with 114 additions and 126 deletions

View file

@ -26,8 +26,6 @@ interface IServiceNotification {
*/ */
fun buildNotification(context: Context, pendingShutdownIntent: PendingIntent): Notification fun buildNotification(context: Context, pendingShutdownIntent: PendingIntent): Notification
/** /** Return the notification ID to use */
* Return the notification ID to use
*/
fun getNotificationId(): Int fun getNotificationId(): Int
} }

View file

@ -24,18 +24,15 @@ import androidx.annotation.Keep;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import java.util.Calendar;
@Keep @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"; private static final String TAG = "MonadoView";
@NonNull @NonNull private final Context context;
private final Context context;
/// The activity we've connected to. /// The activity we've connected to.
@Nullable @Nullable private final Activity activity;
private final Activity activity;
private final Object currentSurfaceHolderSync = new Object(); private final Object currentSurfaceHolderSync = new Object();
public int width = -1; public int width = -1;
@ -44,9 +41,7 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S
private NativeCounterpart nativeCounterpart; private NativeCounterpart nativeCounterpart;
@GuardedBy("currentSurfaceHolderSync") @GuardedBy("currentSurfaceHolderSync") @Nullable private SurfaceHolder currentSurfaceHolder = null;
@Nullable
private SurfaceHolder currentSurfaceHolder = null;
private SystemUiController systemUiController = 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. * 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. * @param nativePointer The native android_custom_surface pointer, cast to a long.
* @return The MonadoView instance created and asynchronously attached. * @return The MonadoView instance created and asynchronously attached.
*/ */
@NonNull @NonNull @Keep
@Keep
@SuppressWarnings("deprecation") @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); final MonadoView view = new MonadoView(activity, nativePointer);
view.createSurfaceInActivity(); view.createSurfaceInActivity();
return view; return view;
} }
@NonNull @NonNull @Keep
@Keep
public static MonadoView attachToActivity(@NonNull final Activity activity) { public static MonadoView attachToActivity(@NonNull final Activity activity) {
final MonadoView view = new MonadoView(activity); final MonadoView view = new MonadoView(activity);
view.createSurfaceInActivity(); view.createSurfaceInActivity();
return view; return view;
} }
@NonNull @NonNull @Keep
@Keep
public static DisplayMetrics getDisplayMetrics(@NonNull Context context) { public static DisplayMetrics getDisplayMetrics(@NonNull Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics(); DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
@ -128,62 +121,61 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S
createSurfaceInActivity(false); 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) { private void createSurfaceInActivity(boolean focusable) {
Log.i(TAG, "Starting to add a new surface!"); Log.i(TAG, "Starting to add a new surface!");
activity.runOnUiThread(() -> { activity.runOnUiThread(
Log.i(TAG, "Starting runOnUiThread"); () -> {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); Log.i(TAG, "Starting runOnUiThread");
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
WindowManager windowManager = activity.getWindowManager(); WindowManager windowManager = activity.getWindowManager();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
if (focusable) { if (focusable) {
lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN; lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN;
} else { } else {
// There are 2 problems if view is focusable on all-in-one device: // There are 2 problems if view is focusable on all-in-one device:
// 1. Navigation bar won't go away because view gets focus. // 1. Navigation bar won't go away because view gets focus.
// 2. Underlying activity lost focus and cannot receive input. // 2. Underlying activity lost focus and cannot receive input.
lp.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | lp.flags =
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; WindowManager.LayoutParams.FLAG_FULLSCREEN
} | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { }
lp.layoutInDisplayCutoutMode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; lp.layoutInDisplayCutoutMode =
} WindowManager.LayoutParams
windowManager.addView(this, lp); .LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
if (focusable) { }
requestFocus(); windowManager.addView(this, lp);
} if (focusable) {
SurfaceHolder surfaceHolder = getHolder(); requestFocus();
surfaceHolder.addCallback(this); }
Log.i(TAG, "Registered callbacks!"); 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 * Block up to a specified amount of time, waiting for the surfaceCreated callback to be fired
* and populate the currentSurfaceHolder. * and populate the currentSurfaceHolder.
* <p> *
* If it returns a SurfaceHolder, the `usedByNativeCode` flag will be set. * <p>If it returns a SurfaceHolder, the `usedByNativeCode` flag will be set.
* <p> *
* Called by native code! * <p>Called by native code!
* *
* @param wait_ms Max duration you prefer to wait, in milliseconds. Spurious wakeups mean this * @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. * @return A SurfaceHolder or null.
*/ */
@Keep @Keep
public @Nullable public @Nullable SurfaceHolder waitGetSurfaceHolder(int wait_ms) {
SurfaceHolder waitGetSurfaceHolder(int wait_ms) {
long currentTime = SystemClock.uptimeMillis(); long currentTime = SystemClock.uptimeMillis();
long timeout = currentTime + wait_ms; long timeout = currentTime + wait_ms;
SurfaceHolder ret = null; SurfaceHolder ret = null;
synchronized (currentSurfaceHolderSync) { synchronized (currentSurfaceHolderSync) {
ret = currentSurfaceHolder; ret = currentSurfaceHolder;
while (currentSurfaceHolder == null while (currentSurfaceHolder == null && SystemClock.uptimeMillis() < timeout) {
&& SystemClock.uptimeMillis() < timeout) {
try { try {
currentSurfaceHolderSync.wait(wait_ms, 0); currentSurfaceHolderSync.wait(wait_ms, 0);
ret = currentSurfaceHolder; ret = currentSurfaceHolder;
@ -194,8 +186,7 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S
} }
} }
if (ret != null) { if (ret != null) {
if (nativeCounterpart != null) if (nativeCounterpart != null) nativeCounterpart.markAsUsedByNativeCode();
nativeCounterpart.markAsUsedByNativeCode();
} }
return ret; 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 * Change the flag and notify those waiting on it, to indicate that native code is done with
* this object. * this object.
* <p> *
* Called by native code! * <p>Called by native code!
*/ */
@Keep @Keep
public void markAsDiscardedByNative() { public void markAsDiscardedByNative() {
if (nativeCounterpart != null) if (nativeCounterpart != null) nativeCounterpart.markAsDiscardedByNative(TAG);
nativeCounterpart.markAsDiscardedByNative(TAG);
} }
@Override @Override
@ -222,7 +212,8 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S
} }
@Override @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) { synchronized (currentSurfaceHolderSync) {
currentSurfaceHolder = surfaceHolder; currentSurfaceHolder = surfaceHolder;
@ -245,18 +236,19 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S
} }
} }
if (lost) { 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)) { if (nativeCounterpart != null && !nativeCounterpart.blockUntilNativeDiscard(TAG)) {
Log.i(TAG, Log.i(
"Interrupted in surfaceDestroyed while waiting for native code to finish up."); TAG,
"Interrupted in surfaceDestroyed while waiting for native code to finish"
+ " up.");
} }
} }
} }
@Override @Override
public void surfaceRedrawNeeded(@NonNull SurfaceHolder surfaceHolder) { public void surfaceRedrawNeeded(@NonNull SurfaceHolder surfaceHolder) {
// currentSurfaceHolder = surfaceHolder; // currentSurfaceHolder = surfaceHolder;
Log.i(TAG, "surfaceRedrawNeeded"); Log.i(TAG, "surfaceRedrawNeeded");
} }
} }

View file

@ -17,13 +17,12 @@ import android.graphics.drawable.Drawable
* Intended for use in dependency injection. * Intended for use in dependency injection.
*/ */
interface NameAndLogoProvider { 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 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? fun getLogoDrawable(): Drawable?
} }

View file

@ -20,11 +20,13 @@ import java.security.InvalidParameterException;
* Object that tracks the native counterpart object for a type. Must be initialized on construction, * 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 * and may have its native code destroyed/discarded, but may not "re-seat" it to new native code
* pointer. * pointer.
*
* <p>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)
*
* <p> * <p>
* 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)
* <p>
* <pre> * <pre>
* private final NativeCounterpart nativeCounterpart; * private final NativeCounterpart nativeCounterpart;
* *
@ -33,36 +35,33 @@ import java.security.InvalidParameterException;
* nativeCounterpart.markAsDiscardedByNative(TAG); * nativeCounterpart.markAsDiscardedByNative(TAG);
* } * }
* </pre> * </pre>
*
* Then, initialize it in your constructor, call {@code markAsUsedByNativeCode()} where desired * Then, initialize it in your constructor, call {@code markAsUsedByNativeCode()} where desired
* (often in your constructor), and call {@code getNativePointer()} and * (often in your constructor), and call {@code getNativePointer()} and {@code
* {@code blockUntilNativeDiscard()} as needed. * blockUntilNativeDiscard()} as needed.
* <p> *
* Your native code can use this to turn a void* into a jlong: * <p>Your native code can use this to turn a void* into a jlong: {@code static_cast<long
* {@code static_cast<long long>(reinterpret_cast<intptr_t>(nativePointer))} * long>(reinterpret_cast<intptr_t>(nativePointer))}
*/ */
public final class NativeCounterpart { public final class NativeCounterpart {
/** /** Guards the usedByNativeCodeSync. */
* Guards the usedByNativeCodeSync.
*/
private final Object usedByNativeCodeSync = new Object(); private final Object usedByNativeCodeSync = new Object();
/** /**
* Indicates if the containing object is in use by native code. * Indicates if the containing object is in use by native code.
* <p> *
* Guarded by usedByNativeCodeSync. * <p>Guarded by usedByNativeCodeSync.
*/ */
private boolean usedByNativeCode = false; private boolean usedByNativeCode = false;
/** /** Contains the pointer to the native counterpart object. */
* Contains the pointer to the native counterpart object.
*/
private long nativePointer = 0; private long nativePointer = 0;
/** /**
* Constructor * Constructor
* *
* @param nativePointer The native pointer, cast appropriately. Must be non-zero. Can cast like: * @param nativePointer The native pointer, cast appropriately. Must be non-zero. Can cast like:
* {@code static_cast<long long>(reinterpret_cast<intptr_t>(nativePointer))} * {@code static_cast<long long>(reinterpret_cast<intptr_t>(nativePointer))}
*/ */
public NativeCounterpart(long nativePointer) throws InvalidParameterException { public NativeCounterpart(long nativePointer) throws InvalidParameterException {
if (nativePointer == 0) { if (nativePointer == 0) {
@ -92,8 +91,10 @@ public final class NativeCounterpart {
public void markAsDiscardedByNative(String TAG) { public void markAsDiscardedByNative(String TAG) {
synchronized (usedByNativeCodeSync) { synchronized (usedByNativeCodeSync) {
if (!usedByNativeCode) { if (!usedByNativeCode) {
Log.w(TAG, Log.w(
"This should not have happened: Discarding by native code, but not marked as used!"); TAG,
"This should not have happened: Discarding by native code, but not marked"
+ " as used!");
} }
usedByNativeCode = false; usedByNativeCode = false;
nativePointer = 0; nativePointer = 0;
@ -130,10 +131,8 @@ public final class NativeCounterpart {
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
Log.i(TAG, Log.i(TAG, "Interrupted while waiting for native code to finish up: " + e);
"Interrupted while waiting for native code to finish up: " + e);
return false; return false;
} }
} }
} }

View file

@ -15,19 +15,16 @@ import android.view.WindowInsets
import android.view.WindowInsetsController import android.view.WindowInsetsController
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
/** /** Helper class that handles system ui visibility. */
* Helper class that handles system ui visibility.
*/
class SystemUiController(activity: Activity) { class SystemUiController(activity: Activity) {
private val impl: Impl = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { private val impl: Impl =
WindowInsetsControllerImpl(activity) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
} else { WindowInsetsControllerImpl(activity)
SystemUiVisibilityImpl(activity) } else {
} SystemUiVisibilityImpl(activity)
}
/** /** Hide system ui and make fullscreen. */
* Hide system ui and make fullscreen.
*/
fun hide() { fun hide() {
impl.hide() impl.hide()
} }
@ -51,11 +48,13 @@ class SystemUiController(activity: Activity) {
private const val FLAG_FULL_SCREEN_IMMERSIVE_STICKY = private const val FLAG_FULL_SCREEN_IMMERSIVE_STICKY =
// Give us a stable view of content insets // Give us a stable view of content insets
(View.SYSTEM_UI_FLAG_LAYOUT_STABLE // Be able to do fullscreen and hide navigation (View.SYSTEM_UI_FLAG_LAYOUT_STABLE // Be able to do fullscreen and hide navigation
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
or View.SYSTEM_UI_FLAG_FULLSCREEN View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // we want sticky immersive View.SYSTEM_UI_FLAG_FULLSCREEN or
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // we want sticky immersive
or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
} }
init { init {
@ -75,9 +74,11 @@ class SystemUiController(activity: Activity) {
override fun hide() { override fun hide() {
activity.runOnUiThread { activity.runOnUiThread {
val controller = activity.window.insetsController val controller = activity.window.insetsController
controller!!.hide(WindowInsets.Type.displayCutout() controller!!.hide(
or WindowInsets.Type.statusBars() WindowInsets.Type.displayCutout() or
or WindowInsets.Type.navigationBars()) WindowInsets.Type.statusBars() or
WindowInsets.Type.navigationBars()
)
controller.systemBarsBehavior = controller.systemBarsBehavior =
WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
} }
@ -85,10 +86,13 @@ class SystemUiController(activity: Activity) {
init { init {
runOnUiThread { runOnUiThread {
activity.window.insetsController!!.addOnControllableInsetsChangedListener { _: WindowInsetsController?, typeMask: Int -> activity.window.insetsController!!.addOnControllableInsetsChangedListener {
if (typeMask and WindowInsets.Type.displayCutout() == 1 _: WindowInsetsController?,
|| typeMask and WindowInsets.Type.statusBars() == 1 typeMask: Int ->
|| typeMask and WindowInsets.Type.navigationBars() == 1 if (
typeMask and WindowInsets.Type.displayCutout() == 1 ||
typeMask and WindowInsets.Type.statusBars() == 1 ||
typeMask and WindowInsets.Type.navigationBars() == 1
) { ) {
hide() hide()
} }
@ -96,5 +100,4 @@ class SystemUiController(activity: Activity) {
} }
} }
} }
} }

View file

@ -24,14 +24,11 @@ interface UiProvider {
*/ */
fun getNotificationIcon(): Icon? = null 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 fun makeAboutActivityPendingIntent(): PendingIntent
/** /**
* Make a {@code PendingIntent} to launch a configuration activity, if provided by the target. * Make a {@code PendingIntent} to launch a configuration activity, if provided by the target.
*/ */
fun makeConfigureActivityPendingIntent(): PendingIntent? = null fun makeConfigureActivityPendingIntent(): PendingIntent? = null
} }