// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What the game asks of Android's Java side, through JNI, on the main //! thread: the system's accent color, the part of the window clear of //! cutouts and system bars, and hiding those bars. const std = @import("std"); const view = @import("view"); const jni = @import("jni.zig"); const ndk = @import("ndk.zig"); /// The system's accent color, from Android 12 (API 31) on, where it is /// drawn from the wallpaper; null before that, or if it cannot be had. /// The shade is the one Material uses for the primary color in a light /// theme, which is what the game's white board is. pub fn accent(activity: *ndk.ANativeActivity) ?view.draw.Color { if (activity.sdk_version < 31) return null; const env = activity.env; const res = env.callObject(activity.clazz, "getResources", "()Landroid/content/res/Resources;", &.{}) orelse return null; defer env.deleteLocalRef(res); const name = env.newStringUtf("system_accent1_600") orelse return null; defer env.deleteLocalRef(name); const kind = env.newStringUtf("color") orelse return null; defer env.deleteLocalRef(kind); const package = env.newStringUtf("android") orelse return null; defer env.deleteLocalRef(package); const id = env.callInt(res, "getIdentifier", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)I", &.{ .{ .l = name }, .{ .l = kind }, .{ .l = package }, }) orelse return null; if (id == 0) return null; const argb: u32 = @bitCast(env.callInt(activity.clazz, "getColor", "(I)I", &.{.{ .i = id }}) orelse return null); return .{ @truncate(argb >> 16), @truncate(argb >> 8), @truncate(argb), 0xff }; } /// How far in from each edge of the window the board has to keep, in /// pixels. pub const Insets = struct { left: i32 = 0, top: i32 = 0, right: i32 = 0, bottom: i32 = 0, }; /// What the camera cutout and any system bars on screen cover, or null if /// the window cannot say yet. pub fn insets(activity: *ndk.ANativeActivity) ?Insets { const env = activity.env; const window = env.callObject(activity.clazz, "getWindow", "()Landroid/view/Window;", &.{}) orelse return null; defer env.deleteLocalRef(window); const decor = env.callObject(window, "getDecorView", "()Landroid/view/View;", &.{}) orelse return null; defer env.deleteLocalRef(decor); const root = env.callObject(decor, "getRootWindowInsets", "()Landroid/view/WindowInsets;", &.{}) orelse return null; defer env.deleteLocalRef(root); if (activity.sdk_version < 30) return .{ .left = env.callInt(root, "getSystemWindowInsetLeft", "()I", &.{}) orelse 0, .top = env.callInt(root, "getSystemWindowInsetTop", "()I", &.{}) orelse 0, .right = env.callInt(root, "getSystemWindowInsetRight", "()I", &.{}) orelse 0, .bottom = env.callInt(root, "getSystemWindowInsetBottom", "()I", &.{}) orelse 0, }; const bars = env.callStaticInt("android/view/WindowInsets$Type", "systemBars", "()I", &.{}) orelse return null; const cutout = env.callStaticInt("android/view/WindowInsets$Type", "displayCutout", "()I", &.{}) orelse return null; // Bars that are hidden cover nothing: `getInsets` counts only those on // screen. const covered = env.callObject(root, "getInsets", "(I)Landroid/graphics/Insets;", &.{.{ .i = bars | cutout }}) orelse return null; defer env.deleteLocalRef(covered); return .{ .left = env.intField(covered, "left") orelse 0, .top = env.intField(covered, "top") orelse 0, .right = env.intField(covered, "right") orelse 0, .bottom = env.intField(covered, "bottom") orelse 0, }; } /// Hides the status and navigation bars, letting a swipe in from the edge /// show them for a moment. pub fn hideBars(activity: *ndk.ANativeActivity) void { const env = activity.env; const window = env.callObject(activity.clazz, "getWindow", "()Landroid/view/Window;", &.{}) orelse return; defer env.deleteLocalRef(window); if (activity.sdk_version < 30) { const decor = env.callObject(window, "getDecorView", "()Landroid/view/View;", &.{}) orelse return; defer env.deleteLocalRef(decor); // IMMERSIVE_STICKY | LAYOUT_FULLSCREEN | LAYOUT_HIDE_NAVIGATION | // LAYOUT_STABLE | FULLSCREEN | HIDE_NAVIGATION, from View. _ = env.callVoid(decor, "setSystemUiVisibility", "(I)V", &.{.{ .i = 0x1706 }}); return; } const controller = env.callObject(window, "getInsetsController", "()Landroid/view/WindowInsetsController;", &.{}) orelse return; defer env.deleteLocalRef(controller); const bars = env.callStaticInt("android/view/WindowInsets$Type", "systemBars", "()I", &.{}) orelse return; // BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE, from WindowInsetsController. _ = env.callVoid(controller, "setSystemBarsBehavior", "(I)V", &.{.{ .i = 2 }}); _ = env.callVoid(controller, "hide", "(I)V", &.{.{ .i = bars }}); }