From 744db1f614d9abe050ccd19b7bf4f28b11aa9a18 Mon Sep 17 00:00:00 2001 From: Rewtio Date: Fri, 17 Nov 2023 07:08:45 +0700 Subject: [PATCH] fix(android): add failsafe in setHighRefreshRate --- lib/main.dart | 9 ++------- lib/utils/android_utils.dart | 39 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 lib/utils/android_utils.dart diff --git a/lib/main.dart b/lib/main.dart index 9e71bd8d..943b5a52 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -33,7 +33,7 @@ import 'package:path_provider/path_provider.dart'; import 'package:spotube/hooks/configurators/use_init_sys_tray.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_native_splash/flutter_native_splash.dart'; -import 'package:flutter_displaymode/flutter_displaymode.dart'; +import 'package:spotube/utils/android_utils.dart'; Future main(List rawArgs) async { final arguments = await startCLI(rawArgs); @@ -46,12 +46,7 @@ Future main(List rawArgs) async { // force High Refresh Rate on some Android devices (like One Plus) if (DesktopTools.platform.isAndroid) { - DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); - AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo; - final buggyBrand = ['oppo', 'oplus', 'oneplus', 'realme']; - if (buggyBrand.any(androidDeviceInfo.brand.toLowerCase().contains)) { - await FlutterDisplayMode.setHighRefreshRate(); - } + await AndroidUtils.setHighRefreshRate(); } await DesktopTools.ensureInitialized( diff --git a/lib/utils/android_utils.dart b/lib/utils/android_utils.dart new file mode 100644 index 00000000..c7ef3d2e --- /dev/null +++ b/lib/utils/android_utils.dart @@ -0,0 +1,39 @@ +import 'package:flutter_displaymode/flutter_displaymode.dart'; + +abstract class AndroidUtils { + + /// Sets the device's display to the highest refresh rate available. + /// + /// This method retrieves the list of supported display modes and the currently active display mode. + /// It then selects the display mode with the highest refresh rate that matches the current resolution. + /// The selected display mode is set as the preferred mode using the FlutterDisplayMode plugin. + /// After setting the new mode, it checks if the system is using the new mode. + /// If the system is not using the new mode, it reverts back to the original mode and returns false. + /// Otherwise, it returns true to indicate that the high refresh rate has been successfully set. + /// + /// Returns true if the high refresh rate is set successfully, false otherwise. + static Future setHighRefreshRate() async { + final List modes = await FlutterDisplayMode.supported; + final DisplayMode activeMode = await FlutterDisplayMode.active; + + DisplayMode newMode = activeMode; + for (final DisplayMode mode in modes) { + if (mode.height == newMode.height && + mode.width == newMode.width && + mode.refreshRate > newMode.refreshRate) { + newMode = mode; + } + } + + await FlutterDisplayMode.setPreferredMode(newMode); + + final display = await FlutterDisplayMode.active; // possibly altered by system + + if (display.refreshRate < newMode.refreshRate) { + await FlutterDisplayMode.setPreferredMode(display); + return false; + } + + return true; + } +}