1 /*
2  * Copyright (C) 2022 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 package com.android.launcher3.util.window;
17 
18 import static android.view.Display.DEFAULT_DISPLAY;
19 
20 import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
21 
22 import android.content.Context;
23 import android.hardware.display.DisplayManager;
24 import android.hardware.display.DisplayManager.DisplayListener;
25 import android.view.Display;
26 
27 import androidx.annotation.WorkerThread;
28 
29 import com.android.launcher3.util.MainThreadInitializedObject;
30 import com.android.launcher3.util.SafeCloseable;
31 
32 /**
33  * Utility class to track refresh rate of the current device
34  */
35 public class RefreshRateTracker implements DisplayListener, SafeCloseable {
36 
37     private static final MainThreadInitializedObject<RefreshRateTracker> INSTANCE =
38             new MainThreadInitializedObject<>(RefreshRateTracker::new);
39 
40     private int mSingleFrameMs = 1;
41 
42     private final DisplayManager mDM;
43 
RefreshRateTracker(Context context)44     private RefreshRateTracker(Context context) {
45         mDM = context.getSystemService(DisplayManager.class);
46         updateSingleFrameMs();
47         mDM.registerDisplayListener(this, UI_HELPER_EXECUTOR.getHandler());
48     }
49 
50     /**
51      * Returns the single frame time in ms
52      */
getSingleFrameMs(Context context)53     public static int getSingleFrameMs(Context context) {
54         return INSTANCE.get(context).mSingleFrameMs;
55     }
56 
57     @Override
onDisplayAdded(int displayId)58     public final void onDisplayAdded(int displayId) { }
59 
60     @Override
onDisplayRemoved(int displayId)61     public final void onDisplayRemoved(int displayId) { }
62 
63     @WorkerThread
64     @Override
onDisplayChanged(int displayId)65     public final void onDisplayChanged(int displayId) {
66         if (displayId == DEFAULT_DISPLAY) {
67             updateSingleFrameMs();
68         }
69     }
70 
updateSingleFrameMs()71     private void updateSingleFrameMs() {
72         Display display = mDM.getDisplay(DEFAULT_DISPLAY);
73         if (display != null) {
74             float refreshRate = display.getRefreshRate();
75             mSingleFrameMs = refreshRate > 0 ? (int) (1000 / refreshRate) : 16;
76         }
77     }
78 
79     @Override
close()80     public void close() {
81         mDM.unregisterDisplayListener(this);
82     }
83 }
84