• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.launcher3.util.window
18 
19 import android.content.Context
20 import android.hardware.display.DisplayManager
21 import android.hardware.display.DisplayManager.DisplayListener
22 import android.view.Display.DEFAULT_DISPLAY
23 import com.android.launcher3.dagger.ApplicationContext
24 import com.android.launcher3.dagger.LauncherAppSingleton
25 import com.android.launcher3.dagger.LauncherComponentProvider.appComponent
26 import com.android.launcher3.util.DaggerSingletonTracker
27 import com.android.launcher3.util.Executors
28 import javax.inject.Inject
29 
30 /** Utility class to track refresh rate of the current device */
31 interface RefreshRateTracker {
32 
33     val singleFrameMs: Int
34 
35     @LauncherAppSingleton
36     class RefreshRateTrackerImpl
37     @Inject
38     constructor(@ApplicationContext ctx: Context, tracker: DaggerSingletonTracker) :
39         RefreshRateTracker, DisplayListener {
40 
41         private val displayManager: DisplayManager =
<lambda>null42             ctx.getSystemService(DisplayManager::class.java)!!.also {
43                 it.registerDisplayListener(this, Executors.UI_HELPER_EXECUTOR.handler)
44                 tracker.addCloseable { it.unregisterDisplayListener(this) }
45             }
46 
47         override var singleFrameMs: Int = updateSingleFrameMs()
48 
updateSingleFrameMsnull49         private fun updateSingleFrameMs(): Int {
50             val refreshRate = displayManager.getDisplay(DEFAULT_DISPLAY)?.refreshRate
51             return if (refreshRate != null && refreshRate > 0) (1000 / refreshRate).toInt() else 16
52         }
53 
onDisplayChangednull54         override fun onDisplayChanged(displayId: Int) {
55             if (displayId == DEFAULT_DISPLAY) {
56                 singleFrameMs = updateSingleFrameMs()
57             }
58         }
59 
onDisplayAddednull60         override fun onDisplayAdded(displayId: Int) {}
61 
onDisplayRemovednull62         override fun onDisplayRemoved(displayId: Int) {}
63     }
64 
65     companion object {
66 
67         /** Returns the single frame time in ms */
getSingleFrameMsnull68         @JvmStatic fun Context.getSingleFrameMs() = appComponent.frameRateProvider.singleFrameMs
69     }
70 }
71