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
/**
* Return the notification ID to use
*/
/** Return the notification ID to use */
fun getNotificationId(): Int
}

View file

@ -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.
* <p>
* If it returns a SurfaceHolder, the `usedByNativeCode` flag will be set.
* <p>
* Called by native code!
*
* <p>If it returns a SurfaceHolder, the `usedByNativeCode` flag will be set.
*
* <p>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.
* <p>
* Called by native code!
*
* <p>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");
}
}

View file

@ -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?
}

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,
* and may have its native code destroyed/discarded, but may not "re-seat" it to new native code
* 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>
* 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>
* private final NativeCounterpart nativeCounterpart;
*
@ -33,36 +35,33 @@ import java.security.InvalidParameterException;
* nativeCounterpart.markAsDiscardedByNative(TAG);
* }
* </pre>
*
* Then, initialize it in your constructor, call {@code markAsUsedByNativeCode()} where desired
* (often in your constructor), and call {@code getNativePointer()} and
* {@code blockUntilNativeDiscard()} as needed.
* <p>
* Your native code can use this to turn a void* into a jlong:
* {@code static_cast<long long>(reinterpret_cast<intptr_t>(nativePointer))}
* (often in your constructor), and call {@code getNativePointer()} and {@code
* blockUntilNativeDiscard()} as needed.
*
* <p>Your native code can use this to turn a void* into a jlong: {@code static_cast<long
* long>(reinterpret_cast<intptr_t>(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.
* <p>
* Guarded by usedByNativeCodeSync.
*
* <p>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<long long>(reinterpret_cast<intptr_t>(nativePointer))}
* {@code static_cast<long long>(reinterpret_cast<intptr_t>(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;
}
}
}

View file

@ -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) {
}
}
}
}

View file

@ -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
}