diff --git a/src/android/app/src/main/java/org/citra/citra_emu/activities/EmulationActivity.kt b/src/android/app/src/main/java/org/citra/citra_emu/activities/EmulationActivity.kt index da3a0c5f1..9ede0865d 100644 --- a/src/android/app/src/main/java/org/citra/citra_emu/activities/EmulationActivity.kt +++ b/src/android/app/src/main/java/org/citra/citra_emu/activities/EmulationActivity.kt @@ -48,7 +48,7 @@ import org.citra.citra_emu.utils.FileBrowserHelper import org.citra.citra_emu.utils.EmulationLifecycleUtil import org.citra.citra_emu.utils.EmulationMenuSettings import org.citra.citra_emu.utils.Log -import org.citra.citra_emu.utils.MaxRefreshRate +import org.citra.citra_emu.utils.RefreshRateUtil import org.citra.citra_emu.utils.ThemeUtil import org.citra.citra_emu.viewmodel.EmulationViewModel @@ -87,7 +87,7 @@ class EmulationActivity : AppCompatActivity() { ThemeUtil.setTheme(this) settingsViewModel.settings.loadSettings() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - MaxRefreshRate.set(this, forceSixtyHrz = true) + RefreshRateUtil.enforceRefreshRate(this, sixtyHz = true) } super.onCreate(savedInstanceState) diff --git a/src/android/app/src/main/java/org/citra/citra_emu/features/settings/ui/SettingsActivity.kt b/src/android/app/src/main/java/org/citra/citra_emu/features/settings/ui/SettingsActivity.kt index e954f8e9e..1ae781920 100644 --- a/src/android/app/src/main/java/org/citra/citra_emu/features/settings/ui/SettingsActivity.kt +++ b/src/android/app/src/main/java/org/citra/citra_emu/features/settings/ui/SettingsActivity.kt @@ -38,7 +38,7 @@ import org.citra.citra_emu.features.settings.utils.SettingsFile import org.citra.citra_emu.utils.SystemSaveGame import org.citra.citra_emu.utils.DirectoryInitialization import org.citra.citra_emu.utils.InsetsHelper -import org.citra.citra_emu.utils.MaxRefreshRate +import org.citra.citra_emu.utils.RefreshRateUtil import org.citra.citra_emu.utils.ThemeUtil class SettingsActivity : AppCompatActivity(), SettingsActivityView { @@ -58,7 +58,7 @@ class SettingsActivity : AppCompatActivity(), SettingsActivityView { binding = ActivitySettingsBinding.inflate(layoutInflater) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - MaxRefreshRate.set(this, forceSixtyHrz = false) + RefreshRateUtil.enforceRefreshRate(this) } setContentView(binding.root) diff --git a/src/android/app/src/main/java/org/citra/citra_emu/ui/main/MainActivity.kt b/src/android/app/src/main/java/org/citra/citra_emu/ui/main/MainActivity.kt index 408e22187..9426b55cd 100644 --- a/src/android/app/src/main/java/org/citra/citra_emu/ui/main/MainActivity.kt +++ b/src/android/app/src/main/java/org/citra/citra_emu/ui/main/MainActivity.kt @@ -52,7 +52,7 @@ import org.citra.citra_emu.utils.CitraDirectoryUtils import org.citra.citra_emu.utils.DirectoryInitialization import org.citra.citra_emu.utils.FileBrowserHelper import org.citra.citra_emu.utils.InsetsHelper -import org.citra.citra_emu.utils.MaxRefreshRate +import org.citra.citra_emu.utils.RefreshRateUtil import org.citra.citra_emu.utils.PermissionsHandler import org.citra.citra_emu.utils.ThemeUtil import org.citra.citra_emu.viewmodel.GamesViewModel @@ -90,7 +90,7 @@ class MainActivity : AppCompatActivity(), ThemeProvider { binding = ActivityMainBinding.inflate(layoutInflater) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - MaxRefreshRate.set(this, forceSixtyHrz = false) + RefreshRateUtil.enforceRefreshRate(this) } setContentView(binding.root) diff --git a/src/android/app/src/main/java/org/citra/citra_emu/utils/RefreshRateUtil.kt b/src/android/app/src/main/java/org/citra/citra_emu/utils/RefreshRateUtil.kt new file mode 100644 index 000000000..5b1c09cb9 --- /dev/null +++ b/src/android/app/src/main/java/org/citra/citra_emu/utils/RefreshRateUtil.kt @@ -0,0 +1,42 @@ +// Copyright Citra Emulator Project / Azahar Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +package org.citra.citra_emu.utils +import android.app.Activity +import android.os.Build +import androidx.annotation.RequiresApi + +object RefreshRateUtil { + // Since Android 15, the OS automatically sets apps categorized as games to run with a 60hz refresh rate. + // This functions sets the refresh rate to either the maximum allowed refresh rate or 60hz. + @RequiresApi(Build.VERSION_CODES.R) + fun enforceRefreshRate(activity: Activity, sixtyHz: Boolean = false) { + val display = activity.display + val window = activity.window + + display?.let { + // Get all supported modes and find the one with the highest refresh rate + val supportedModes = it.supportedModes + val maxRefreshRate = supportedModes.maxByOrNull { mode -> mode.refreshRate } + + if (maxRefreshRate == null) { + return + } + + var newModeId: Int? + if (sixtyHz) { + newModeId = supportedModes.firstOrNull { mode -> mode.refreshRate == 60f }?.modeId + } else { + // Set the preferred display mode to the one with the highest refresh rate + newModeId = maxRefreshRate.modeId + } + + if (newModeId == null) { + return + } + + window.attributes.preferredDisplayModeId = newModeId + } + } +} \ No newline at end of file diff --git a/src/android/app/src/main/java/org/citra/citra_emu/utils/SetMaxRefreshRate.kt b/src/android/app/src/main/java/org/citra/citra_emu/utils/SetMaxRefreshRate.kt deleted file mode 100644 index ea2e0fd03..000000000 --- a/src/android/app/src/main/java/org/citra/citra_emu/utils/SetMaxRefreshRate.kt +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright Citra Emulator Project / Azahar Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -package org.citra.citra_emu.utils -import android.app.Activity -import android.os.Build -import androidx.annotation.RequiresApi - -object MaxRefreshRate { - //Since Android 15, google automatically forces "games" to be 60 hrz. - // This functions sets the refresh rate to max supported rate, unless forced to 60 hrz. - @RequiresApi(Build.VERSION_CODES.R) - fun set(activity: Activity, forceSixtyHrz: Boolean) { - val display = activity.display - val window = activity.window - display?.let { - // Get all supported modes and find the one with the highest refresh rate - val supportedModes = it.supportedModes - val maxRefreshRate = supportedModes.maxByOrNull { mode -> mode.refreshRate } - - if (maxRefreshRate != null) { - val layoutParams = window.attributes - val modeId = if (forceSixtyHrz) { - supportedModes.firstOrNull { mode -> mode.refreshRate == 60f }?.modeId - } else { - // Set the preferred display mode to the one with the highest refresh rate - maxRefreshRate.modeId - } - - if (modeId != null) { - layoutParams.preferredDisplayModeId = modeId - window.attributes = layoutParams - } - } - } - } -} \ No newline at end of file