SystemUI: FPSInfoService: extend LifecycleService and fix overlay not being visible in secondary user
Signed-off-by: jhonboy121 <alfredmathew05@gmail.com>
Signed-off-by: Dmitrii <bankersenator@gmail.com>
diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp
index 7e2f6e3..0b171a0 100644
--- a/packages/SystemUI/Android.bp
+++ b/packages/SystemUI/Android.bp
@@ -111,6 +111,7 @@
"androidx.lifecycle_lifecycle-common-java8",
"androidx.lifecycle_lifecycle-extensions",
"androidx.lifecycle_lifecycle-runtime-ktx",
+ "androidx.lifecycle_lifecycle-service",
"androidx.dynamicanimation_dynamicanimation",
"androidx-constraintlayout_constraintlayout",
"androidx.exifinterface_exifinterface",
diff --git a/packages/SystemUI/src/com/android/systemui/FPSInfoService.kt b/packages/SystemUI/src/com/android/systemui/FPSInfoService.kt
index 1282051..0879efe 100644
--- a/packages/SystemUI/src/com/android/systemui/FPSInfoService.kt
+++ b/packages/SystemUI/src/com/android/systemui/FPSInfoService.kt
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2021-2022 AOSP-Krypton Project
+ * Copyright (C) 2022 FlamingoOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,13 +16,11 @@
package com.android.systemui
-import android.app.Service
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.PixelFormat
import android.os.Binder
-import android.os.Handler
import android.os.IBinder
import android.util.Log
import android.view.Gravity
@@ -31,31 +29,34 @@
import android.widget.TextView
import androidx.core.graphics.ColorUtils
+import androidx.lifecycle.LifecycleService
+import androidx.lifecycle.lifecycleScope
-import com.android.systemui.dagger.qualifiers.Main
+import com.android.systemui.R
import com.android.systemui.keyguard.WakefulnessLifecycle
import java.io.RandomAccessFile
import javax.inject.Inject
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.cancel
+import kotlin.math.roundToInt
+
import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.Job
+import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
-import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
class FPSInfoService @Inject constructor(
- private val wakefulnessLifecycle: WakefulnessLifecycle,
- @Main private val handler: Handler,
-) : Service() {
+ private val wakefulnessLifecycle: WakefulnessLifecycle
+) : LifecycleService() {
private val layoutParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
- WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY,
+ WindowManager.LayoutParams.TYPE_DISPLAY_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or
@@ -77,8 +78,11 @@
}
}
- private lateinit var binder: IBinder
- private lateinit var coroutineScope: CoroutineScope
+ private val topInset: Int
+ get() = windowManager.currentWindowMetrics.windowInsets
+ .getInsetsIgnoringVisibility(WindowInsets.Type.statusBars()).top
+
+ private lateinit var binder: ServiceBinder
private lateinit var windowManager: WindowManager
private lateinit var fpsInfoView: TextView
private lateinit var fpsInfoNode: RandomAccessFile
@@ -87,10 +91,6 @@
private var registeredWakefulnessLifecycleObserver = false
- private val topInset: Int
- get() = windowManager.currentWindowMetrics.windowInsets
- .getInsetsIgnoringVisibility(WindowInsets.Type.statusBars()).top
-
val isReading: Boolean
get() = fpsReadJob?.isActive == true
@@ -98,43 +98,40 @@
super.onCreate()
logD("onCreate")
binder = ServiceBinder()
- coroutineScope = CoroutineScope(Dispatchers.IO)
windowManager = getSystemService(WindowManager::class.java)
layoutParams.y = topInset
- handler.post {
- fpsInfoView = TextView(this).apply {
- text = getString(R.string.fps_text_placeholder, 0)
- setBackgroundColor(ColorUtils.setAlphaComponent(Color.BLACK, BACKGROUND_ALPHA))
- setTextColor(Color.WHITE)
- val padding = resources.getDimensionPixelSize(R.dimen.fps_info_text_padding)
- setPadding(padding, padding, padding, padding)
- }
+ fpsInfoView = TextView(this).apply {
+ text = getString(R.string.fps_text_placeholder, 0)
+ setBackgroundColor(ColorUtils.setAlphaComponent(Color.BLACK, BACKGROUND_ALPHA))
+ setTextColor(Color.WHITE)
+ val padding = resources.getDimensionPixelSize(R.dimen.fps_info_text_padding)
+ setPadding(padding, padding, padding, padding)
}
val nodePath = getString(R.string.config_fpsInfoSysNode)
- val result = runCatching {
+ runCatching {
RandomAccessFile(nodePath, "r")
- }
- if (result.isFailure) {
- Log.e(TAG, "Unable to open $nodePath, ${result.exceptionOrNull()?.message}")
+ }.onFailure {
+ Log.e(TAG, "Unable to open $nodePath", it)
stopSelf()
- return
- } else {
- fpsInfoNode = result.getOrThrow()
+ }.onSuccess {
+ fpsInfoNode = it
}
}
- override fun onBind(intent: Intent?): IBinder = binder
+ override fun onBind(intent: Intent): IBinder {
+ super.onBind(intent)
+ return binder
+ }
override fun onConfigurationChanged(newConfig: Configuration) {
+ super.onConfigurationChanged(newConfig)
logD("onConfigurationChanged")
layoutParams.y = topInset
if (fpsInfoView.parent != null) {
- handler.post {
- windowManager.updateViewLayout(fpsInfoView, layoutParams)
- }
+ windowManager.updateViewLayout(fpsInfoView, layoutParams)
}
}
@@ -149,17 +146,12 @@
private fun startReadingInternal() {
logD("startReadingInternal, isReading = $isReading")
if (isReading) return
- if (fpsInfoView.parent == null) {
- handler.post {
+ fpsReadJob = lifecycleScope.launch {
+ if (fpsInfoView.parent == null) {
windowManager.addView(fpsInfoView, layoutParams)
}
- }
- fpsReadJob = coroutineScope.launch {
do {
- val fps = measureFps()
- handler.post {
- fpsInfoView.text = getString(R.string.fps_text_placeholder, fps)
- }
+ fpsInfoView.text = getString(R.string.fps_text_placeholder, measureFps())
delay(FPS_MEASURE_INTERVAL)
} while (isActive)
}
@@ -178,25 +170,26 @@
if (!isReading) return
fpsReadJob?.cancel()
fpsReadJob = null
- if (fpsInfoView.parent != null) {
- handler.post {
+ lifecycleScope.launch {
+ if (fpsInfoView.parent != null) {
windowManager.removeViewImmediate(fpsInfoView)
}
}
}
- private fun measureFps(): Int = runCatching {
+ private suspend fun measureFps(): Int = withContext(Dispatchers.IO) {
+ runCatching {
fpsInfoNode.seek(0L)
- fpsRegex.find(fpsInfoNode.readLine())?.value?.toInt() ?: 0
+ FpsRegex.find(fpsInfoNode.readLine())?.value?.toFloat()?.roundToInt() ?: 0
}.getOrElse {
Log.e(TAG, "Failed to parse fps, ${it.message}")
0
}
+ }
override fun onDestroy() {
logD("onDestroy")
stopReading()
- coroutineScope.cancel()
super.onDestroy()
}
@@ -217,6 +210,6 @@
private const val BACKGROUND_ALPHA = 120
- private val fpsRegex = Regex("[0-9]+")
+ private val FpsRegex = "[0-9]+".toRegex()
}
-}
\ No newline at end of file
+}