Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.os.SystemClock
import android.view.Choreographer
import android.view.MotionEvent
Expand Down Expand Up @@ -224,6 +225,7 @@ internal class LockscreenCapsuleImmersiveHost(
fun destroy() {
if (registered) runCatching { context.unregisterReceiver(receiver) }
registered = false
layer.stopAnimation()
handler.removeCallbacksAndMessages(null)
runCatching { host.removeView(layer) }
}
Expand Down Expand Up @@ -264,17 +266,65 @@ private class MapImmersiveLayer(context: Context) : View(context) {
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val route = Path()
private var phase = 0f
private var animating = false
private val tick = object : Runnable {
override fun run() {
if (!shouldAnimate()) {
animating = false
return
}
phase = (phase + .035f) % 1f
invalidate()
postDelayed(this, 16L)
}
}

init {
setBackgroundColor(Color.argb(235, 8, 18, 26))
isClickable = true
post(object : Runnable {
override fun run() {
phase = (phase + .035f) % 1f
invalidate()
postDelayed(this, 16L)
}

private fun isScreenInteractive(): Boolean =
context.getSystemService(PowerManager::class.java)?.isInteractive != false

private fun shouldAnimate(): Boolean =
isAttachedToWindow && windowVisibility == VISIBLE && visibility == VISIBLE && isScreenInteractive()

private fun syncAnimationState() {
if (shouldAnimate()) {
if (!animating) {
animating = true
removeCallbacks(tick)
postDelayed(tick, 16L)
}
})
} else {
stopAnimation()
}
}

fun stopAnimation() {
animating = false
removeCallbacks(tick)
}

override fun setVisibility(visibility: Int) {
super.setVisibility(visibility)
syncAnimationState()
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
syncAnimationState()
}

override fun onDetachedFromWindow() {
stopAnimation()
super.onDetachedFromWindow()
}

override fun onWindowVisibilityChanged(visibility: Int) {
super.onWindowVisibilityChanged(visibility)
syncAnimationState()
}

override fun onDraw(canvas: Canvas) {
Expand Down
63 changes: 57 additions & 6 deletions app/src/main/java/btm/m/os4/systemuihook/LockscreenWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ package btm.m.os4.systemuihook

import android.app.KeyguardManager
import android.app.WallpaperManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.res.ColorStateList
import android.graphics.Bitmap
import android.graphics.Canvas
Expand All @@ -17,8 +20,10 @@ import android.net.Uri
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.os.BatteryManager
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.provider.Settings
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
Expand Down Expand Up @@ -1144,8 +1149,28 @@ internal class LockscreenWidgetController(
// contribution per root so their own AOD and gesture translations remain untouched.
private val appliedClockAvoidanceOffsets = WeakHashMap<View, ClockTranslationTransform>()
private val appliedClockAvoidanceScales = WeakHashMap<View, ClockScaleTransform>()
private var screenReceiverRegistered = false
private fun isScreenInteractive(): Boolean =
context.getSystemService(PowerManager::class.java)?.isInteractive != false

private val screenStateReceiver = object : BroadcastReceiver() {
override fun onReceive(ctx: Context?, intent: Intent?) {
when (intent?.action) {
Intent.ACTION_SCREEN_OFF -> {
handler.removeCallbacks(refreshRunnable)
host.removeCallbacks(animationFollower)
followerPosted = false
}
Intent.ACTION_SCREEN_ON -> {
handler.removeCallbacks(refreshRunnable)
handler.post(refreshRunnable)
}
}
}
}
private val refreshRunnable = object : Runnable {
override fun run() {
if (!isScreenInteractive()) return
refresh()
handler.postDelayed(this, 30_000L)
}
Expand All @@ -1160,15 +1185,17 @@ internal class LockscreenWidgetController(
}
private val clockAvoidancePreDrawListener = ViewTreeObserver.OnPreDrawListener {
val widget = view
if (targetVisible && widget?.isAttachedToWindow == true && widget.visibility == View.VISIBLE) {
if (targetVisible && widget?.isAttachedToWindow == true && widget.isShown) {
adjustClockForWidget(widget)
}
true
}
private val animationFollower = object : Runnable {
override fun run() {
val widget = view
if (targetVisible && widget?.visibility == View.VISIBLE) {
val keyguard = context.getSystemService(KeyguardManager::class.java)
val locked = keyguard?.let { it.isKeyguardLocked || it.isDeviceLocked } ?: false
if (targetVisible && locked && isScreenInteractive() && widget != null && widget.isShown) {
position()
host.postOnAnimation(this)
} else {
Expand All @@ -1187,10 +1214,30 @@ internal class LockscreenWidgetController(
host.rootView.viewTreeObserver.addOnPreDrawListener(clockAvoidancePreDrawListener)
leftShortcut.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> schedulePosition() }
rightShortcut.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> schedulePosition() }
handler.post(refreshRunnable)
val screenFilter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
}
runCatching {
val app = context.applicationContext ?: context
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
app.registerReceiver(screenStateReceiver, screenFilter, Context.RECEIVER_NOT_EXPORTED)
} else {
@Suppress("DEPRECATION")
app.registerReceiver(screenStateReceiver, screenFilter)
}
screenReceiverRegistered = true
}
if (isScreenInteractive()) {
handler.post(refreshRunnable)
}
}

fun destroy() {
if (screenReceiverRegistered) {
runCatching { (context.applicationContext ?: context).unregisterReceiver(screenStateReceiver) }
screenReceiverRegistered = false
}
LockscreenWidgetSceneState.unregister(this)
LockscreenMediaPresentationBridge.unregister(this)
runCatching { preferences.unregisterOnSharedPreferenceChangeListener(preferenceListener) }
Expand Down Expand Up @@ -1663,9 +1710,13 @@ internal class LockscreenWidgetController(
return null
}

private fun viewIdName(view: View): String = runCatching {
view.resources.getResourceEntryName(view.id)
}.getOrDefault("")
private fun viewIdName(view: View): String {
val id = view.id
if (id == View.NO_ID || id == 0) return ""
return runCatching {
view.resources.getResourceEntryName(id)
}.getOrDefault("")
}

private fun schedulePosition() {
if (positionPosted) return
Expand Down
54 changes: 51 additions & 3 deletions app/src/main/java/btm/m/os4/systemuihook/StatusBarSecondsHook.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
package btm.m.os4.systemuihook

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.SharedPreferences
import android.os.Build
import android.os.PowerManager
import android.os.SystemClock
import android.widget.TextView
import btm.m.os4.systemuihook.ModuleLog as Log
Expand All @@ -19,6 +25,43 @@ internal class StatusBarSecondsHook(
private val clocks = WeakHashMap<TextView, ClockState>()
private var settings = StatusBarSecondsSettings()
private var preferenceListener: SharedPreferences.OnSharedPreferenceChangeListener? = null
@Volatile private var screenReceiverRegistered = false

private fun isScreenInteractive(context: Context): Boolean =
context.getSystemService(PowerManager::class.java)?.isInteractive != false

private fun ensureScreenReceiver(context: Context) {
if (screenReceiverRegistered) return
val app = context.applicationContext ?: return
synchronized(this) {
if (screenReceiverRegistered) return
val receiver = object : BroadcastReceiver() {
override fun onReceive(ctx: Context?, intent: Intent?) {
when (intent?.action) {
Intent.ACTION_SCREEN_OFF -> {
synchronized(clocks) { clocks.values.toList() }.forEach { it.stopTicker() }
}
Intent.ACTION_SCREEN_ON -> {
synchronized(clocks) { clocks.values.toList() }.forEach { it.renderAfterOemUpdate() }
}
}
}
}
val filter = IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
}
runCatching {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
app.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED)
} else {
@Suppress("DEPRECATION")
app.registerReceiver(receiver, filter)
}
screenReceiverRegistered = true
}
}
}

fun install(classLoader: ClassLoader, preferences: SharedPreferences) {
runCatching {
Expand Down Expand Up @@ -75,13 +118,18 @@ internal class StatusBarSecondsHook(
}
}

fun stopTicker() {
tickerOn = false
view.removeCallbacks(tick)
}

fun renderAfterOemUpdate() {
ensureScreenReceiver(view.context)
val enabled = settings.mode == STATUS_BAR_SECONDS_MODE_ALWAYS ||
(settings.mode == STATUS_BAR_SECONDS_MODE_LONG_PRESS &&
activeUntil > SystemClock.elapsedRealtime())
if (!enabled || !view.isAttachedToWindow || !view.isShown) {
tickerOn = false
view.removeCallbacks(tick)
if (!enabled || !view.isAttachedToWindow || !view.isShown || !isScreenInteractive(view.context)) {
stopTicker()
return
}
val base = view.text?.toString().orEmpty()
Expand Down
Loading