From 526d66f6a91e0ed907db609a4adaa97f3239898b Mon Sep 17 00:00:00 2001 From: Traktores <144092020+Traktores@users.noreply.github.com> Date: Thu, 14 Sep 2023 01:11:49 +0000 Subject: [PATCH] feat(TU Dortmund): Add `Show on lockscreen` patch (#472) Co-authored-by: oSumAtrIX --- .../lockscreen/ShowOnLockscreenPatch.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 app/src/main/java/app/revanced/tudortmund/lockscreen/ShowOnLockscreenPatch.java diff --git a/app/src/main/java/app/revanced/tudortmund/lockscreen/ShowOnLockscreenPatch.java b/app/src/main/java/app/revanced/tudortmund/lockscreen/ShowOnLockscreenPatch.java new file mode 100644 index 00000000..2260de24 --- /dev/null +++ b/app/src/main/java/app/revanced/tudortmund/lockscreen/ShowOnLockscreenPatch.java @@ -0,0 +1,46 @@ +package app.revanced.tudortmund.lockscreen; + +import android.content.Context; +import android.hardware.display.DisplayManager; +import android.os.Build; +import android.view.Display; +import android.view.Window; +import androidx.appcompat.app.AppCompatActivity; + +import static android.view.WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD; +import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED; + +public class ShowOnLockscreenPatch { + /** + * @noinspection deprecation + */ + public static Window getWindow(AppCompatActivity activity, float brightness) { + Window window = activity.getWindow(); + + if (brightness >= 0) { + // High brightness set, therefore show on lockscreen. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) activity.setShowWhenLocked(true); + else window.addFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_DISMISS_KEYGUARD); + } else { + // Ignore brightness reset when the screen is turned off. + DisplayManager displayManager = (DisplayManager) activity.getSystemService(Context.DISPLAY_SERVICE); + + boolean isScreenOn = false; + for (Display display : displayManager.getDisplays()) { + if (display.getState() == Display.STATE_OFF) continue; + + isScreenOn = true; + break; + } + + if (isScreenOn) { + // Hide on lockscreen. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) activity.setShowWhenLocked(false); + else window.clearFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_DISMISS_KEYGUARD); + } + } + + return window; + } + +}