|
| 1 | +package com.performancetoolkit |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Build |
| 5 | +import android.util.Log |
| 6 | +import android.view.WindowManager |
| 7 | +import com.facebook.react.bridge.ReactApplicationContext |
| 8 | +import kotlin.math.roundToInt |
| 9 | + |
| 10 | +object DeviceUtils { |
| 11 | + private const val TAG = "PerformanceToolkit" |
| 12 | + |
| 13 | + fun getDeviceMaxRefreshRate(context: ReactApplicationContext): Double { |
| 14 | + return try { |
| 15 | + val display = getDisplay(context) |
| 16 | + |
| 17 | + // Get maximum refresh rate from supported modes (API 23+) |
| 18 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 19 | + display?.supportedModes |
| 20 | + ?.maxOfOrNull { it.refreshRate } |
| 21 | + ?.roundToInt()?.toDouble() ?: 60.0 |
| 22 | + } else { |
| 23 | + // Fall back to current refresh rate on older devices |
| 24 | + display?.refreshRate?.roundToInt()?.toDouble() ?: 60.0 |
| 25 | + } |
| 26 | + } catch (e: Exception) { |
| 27 | + Log.e(TAG, "Error getting max device refresh rate, defaulting to 60", e) |
| 28 | + 60.0 |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + fun getDeviceCurrentRefreshRate(context: ReactApplicationContext): Double { |
| 33 | + return try { |
| 34 | + val display = getDisplay(context) |
| 35 | + |
| 36 | + // Get current refresh rate |
| 37 | + display?.refreshRate?.roundToInt()?.toDouble() ?: 60.0 |
| 38 | + } catch (e: Exception) { |
| 39 | + Log.e(TAG, "Error getting current device refresh rate, defaulting to 60", e) |
| 40 | + 60.0 |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private fun getDisplay(context: ReactApplicationContext): android.view.Display? { |
| 45 | + // Try to get display from current activity first |
| 46 | + val display = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { |
| 47 | + context.currentActivity?.display |
| 48 | + } else { |
| 49 | + null |
| 50 | + } |
| 51 | + |
| 52 | + // Fall back to WindowManager's default display |
| 53 | + return display ?: run { |
| 54 | + val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as? WindowManager |
| 55 | + @Suppress("DEPRECATION") |
| 56 | + windowManager?.defaultDisplay |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
0 commit comments