Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Keep the screen at its fastest refresh rate while the app is open (#85)

Lelonio498a18c412d3
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt b/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt
index 823bc0d..0bc5b9e 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainActivity.kt
@@ -65,14 +65,9 @@ class MainActivity : ComponentActivity() {
*/
private var link by mutableStateOf<LinkRequest?>(null)
- /** The screen's fastest mode while a finger is on the app; see the class. */
+ /** The screen's fastest mode while the app is on screen; see the class. */
private val touchRefreshRate = TouchRefreshRate(this)
- override fun dispatchTouchEvent(event: android.view.MotionEvent): Boolean {
- touchRefreshRate.onTouch(event)
- return super.dispatchTouchEvent(event)
- }
-
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Transparent bars; SquareTheme sets the icon colour, because it is the
@@ -278,6 +273,7 @@ class MainActivity : ComponentActivity() {
getBooleanExtra(dev.lelonio.square.playback.EXTRA_OPEN_PLAYER, false)
override fun onStart() {
+ touchRefreshRate.hold()
dev.lelonio.square.data.RemoteConnect.onAppOpened()
androidx.core.content.ContextCompat.registerReceiver(
this,
diff --git a/app/src/main/java/dev/lelonio/square/ui/TouchRefreshRate.kt b/app/src/main/java/dev/lelonio/square/ui/TouchRefreshRate.kt
index 294dc56..da137d7 100644
--- a/app/src/main/java/dev/lelonio/square/ui/TouchRefreshRate.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/TouchRefreshRate.kt
@@ -2,54 +2,38 @@ package dev.lelonio.square.ui
import android.app.Activity
import android.os.Build
-import android.os.Handler
-import android.os.Looper
import android.view.Display
-import android.view.MotionEvent
/**
- * Asks for the screen's fastest mode while a finger is on the app, and gives
- * it back shortly after.
+ * Asks for the screen's fastest mode for as long as the app is on screen.
*
* Some phones decide the rate app by app. The Nothing Phone (3a) held this app
* at 90 Hz while other apps scrolled at 120: it gives an app that asks only
* for "fast" its middle rate, and a frame rate requested from a view is
* clamped to that too. What it does honour is a window naming the display mode
- * it wants. Named for good, though, that mode keeps the panel at its top rate
- * over a picture that is not moving, which is battery spent on nothing. So it
- * is named when a finger comes down and released a little after the last one
- * lifts, which leaves time for a fling to settle and for the bar to finish
- * folding.
+ * it wants.
+ *
+ * It used to be named only while a finger was on the app and let go two
+ * seconds after, to spare the battery over a still picture. Everything that
+ * moves on its own then ran at the phone's lower rate (the lyrics scrolling,
+ * the player opening on a tap that had already lifted, a cover playing), and
+ * on Xiaomi's phones, which drop to sixty for an app that does not ask, the
+ * app felt like it ran at sixty (#85). The phone still lowers the rate itself
+ * when nothing on screen changes, wherever its panel can.
*
* Where the phone already runs at its top rate, or has only one, asking
* changes nothing.
*/
internal class TouchRefreshRate(private val activity: Activity) {
- private val handler = Handler(Looper.getMainLooper())
- private val release = Runnable { request(NO_PREFERENCE) }
-
- /** The mode last written to the window, so a tap does not rewrite it. */
+ /** The mode last written to the window, so it is not rewritten. */
private var requested = NO_PREFERENCE
- fun onTouch(event: MotionEvent) {
- when (event.actionMasked) {
- MotionEvent.ACTION_DOWN -> {
- handler.removeCallbacks(release)
- request(fastestMode())
- }
- MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
- handler.removeCallbacks(release)
- handler.postDelayed(release, HOLD_MS)
- }
- }
- }
+ /** Asks for the fastest mode; called as the app comes on screen. */
+ fun hold() = request(fastestMode())
- /** Lets the mode go at once, for an activity that is leaving the screen. */
- fun reset() {
- handler.removeCallbacks(release)
- request(NO_PREFERENCE)
- }
+ /** Lets the mode go, for an activity that is leaving the screen. */
+ fun reset() = request(NO_PREFERENCE)
/**
* The fastest mode at the resolution the screen is using now: a mode at
@@ -87,8 +71,5 @@ internal class TouchRefreshRate(private val activity: Activity) {
private companion object {
const val NO_PREFERENCE = 0
-
- /** Long enough for a fling and the bar's fold after the finger lifts. */
- const val HOLD_MS = 2_000L
}
}