bb996d1304
Bug: 361850561 Test: Manual Flag: EXEMPT Dagger Integration Change-Id: Ice5dc3364f90d2b9b99a0837702ae1d49a356f88
93 lines
2.9 KiB
Java
93 lines
2.9 KiB
Java
/*
|
|
* Copyright (C) 2022 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package com.android.launcher3.util.window;
|
|
|
|
import static android.view.Display.DEFAULT_DISPLAY;
|
|
|
|
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
|
|
|
|
import android.content.Context;
|
|
import android.hardware.display.DisplayManager;
|
|
import android.hardware.display.DisplayManager.DisplayListener;
|
|
import android.view.Display;
|
|
|
|
import androidx.annotation.WorkerThread;
|
|
|
|
import com.android.launcher3.dagger.ApplicationContext;
|
|
import com.android.launcher3.dagger.LauncherAppComponent;
|
|
import com.android.launcher3.dagger.LauncherAppSingleton;
|
|
import com.android.launcher3.util.DaggerSingletonObject;
|
|
import com.android.launcher3.util.DaggerSingletonTracker;
|
|
import com.android.launcher3.util.SafeCloseable;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
/**
|
|
* Utility class to track refresh rate of the current device
|
|
*/
|
|
@LauncherAppSingleton
|
|
public class RefreshRateTracker implements DisplayListener, SafeCloseable {
|
|
|
|
private static final DaggerSingletonObject<RefreshRateTracker> INSTANCE =
|
|
new DaggerSingletonObject<>(LauncherAppComponent::getRefreshRateTracker);
|
|
|
|
private int mSingleFrameMs = 1;
|
|
|
|
private final DisplayManager mDM;
|
|
|
|
@Inject
|
|
RefreshRateTracker(@ApplicationContext Context context, DaggerSingletonTracker tracker) {
|
|
mDM = context.getSystemService(DisplayManager.class);
|
|
updateSingleFrameMs();
|
|
mDM.registerDisplayListener(this, UI_HELPER_EXECUTOR.getHandler());
|
|
tracker.addCloseable(this);
|
|
}
|
|
|
|
/**
|
|
* Returns the single frame time in ms
|
|
*/
|
|
public static int getSingleFrameMs(Context context) {
|
|
return INSTANCE.get(context).mSingleFrameMs;
|
|
}
|
|
|
|
@Override
|
|
public final void onDisplayAdded(int displayId) { }
|
|
|
|
@Override
|
|
public final void onDisplayRemoved(int displayId) { }
|
|
|
|
@WorkerThread
|
|
@Override
|
|
public final void onDisplayChanged(int displayId) {
|
|
if (displayId == DEFAULT_DISPLAY) {
|
|
updateSingleFrameMs();
|
|
}
|
|
}
|
|
|
|
private void updateSingleFrameMs() {
|
|
Display display = mDM.getDisplay(DEFAULT_DISPLAY);
|
|
if (display != null) {
|
|
float refreshRate = display.getRefreshRate();
|
|
mSingleFrameMs = refreshRate > 0 ? (int) (1000 / refreshRate) : 16;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void close() {
|
|
mDM.unregisterDisplayListener(this);
|
|
}
|
|
}
|