Skip to content

Commit

Permalink
feat(android): disable battery optimization for better playback
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Jun 26, 2023
1 parent 4077fac commit fe5b429
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
47 changes: 47 additions & 0 deletions lib/hooks/use_disable_battery_optimizations.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:disable_battery_optimization/disable_battery_optimization.dart';
import 'package:flutter_desktop_tools/flutter_desktop_tools.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/hooks/use_async_effect.dart';

bool _asked = false;
void useDisableBatterOptimizations() {
useAsyncEffect(() async {
if (!DesktopTools.platform.isAndroid || _asked) return;
final localStorage = await SharedPreferences.getInstance();

final rawIsBatteryOptimizationDisabled =
localStorage.getBool("isBatteryOptimizationDisabled");
final isBatteryOptimizationDisabled =
await DisableBatteryOptimization.isBatteryOptimizationDisabled;
if (rawIsBatteryOptimizationDisabled != false &&
isBatteryOptimizationDisabled == false) {
final hasDisabled = await DisableBatteryOptimization
.showDisableBatteryOptimizationSettings();

localStorage.setBool(
"isBatteryOptimizationDisabled",
hasDisabled == true,
);
}

final rawIsManBatteryOptimizationDisabled =
localStorage.getBool("isManufacturerBatteryOptimizationDisabled");
final isManBatteryOptimizationDisabled = await DisableBatteryOptimization
.isManufacturerBatteryOptimizationDisabled;

if (rawIsManBatteryOptimizationDisabled != false &&
isManBatteryOptimizationDisabled == false) {
final hasDisabled = await DisableBatteryOptimization
.showDisableManufacturerBatteryOptimizationSettings(
"Your device has additional battery optimization",
"Follow the steps and disable the optimizations to allow smooth functioning of this app",
);

localStorage.setBool(
"isManufacturerBatteryOptimizationDisabled",
hasDisabled == true,
);
}
_asked = true;
}, null, []);
}
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:spotube/collections/env.dart';
import 'package:spotube/collections/routes.dart';
import 'package:spotube/collections/intents.dart';
import 'package:spotube/hooks/use_disable_battery_optimizations.dart';
import 'package:spotube/l10n/l10n.dart';
import 'package:spotube/models/logger.dart';
import 'package:spotube/models/matched_track.dart';
Expand Down Expand Up @@ -189,6 +190,8 @@ class SpotubeState extends ConsumerState<Spotube> {
};
}, []);

useDisableBatterOptimizations();

final lightTheme = useMemoized(
() => theme(paletteColor ?? accentMaterialColor, Brightness.light),
[paletteColor, accentMaterialColor],
Expand Down
32 changes: 8 additions & 24 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "7.4.0"
badges:
dependency: "direct main"
description:
name: badges
sha256: "727580d938b7a1ff47ea42df730d581415606b4224cfa708671c10287f8d3fe6"
url: "https://pub.dev"
source: hosted
version: "2.0.3"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -466,6 +458,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.1.2"
disable_battery_optimization:
dependency: "direct main"
description:
name: disable_battery_optimization
sha256: b3441975ab2a3ab0c19ed78e909a88d245ce689d43d17f9b23582b1ed41c047b
url: "https://pub.dev"
source: hosted
version: "1.1.0+1"
dots_indicator:
dependency: transitive
description:
Expand Down Expand Up @@ -1439,14 +1439,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.22.0"
queue:
dependency: "direct main"
description:
name: queue
sha256: "9a41ecadc15db79010108c06eae229a45c56b18db699760f34e8c9ac9b831ff9"
url: "https://pub.dev"
source: hosted
version: "3.1.0+2"
quiver:
dependency: transitive
description:
Expand Down Expand Up @@ -1631,14 +1623,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1"
simple_circular_progress_bar:
dependency: "direct main"
description:
name: simple_circular_progress_bar
sha256: e661ca942fbc617298e975b41fde19003d995de73ca6c2a1526c54d52f07151b
url: "https://pub.dev"
source: hosted
version: "1.0.2"
skeleton_text:
dependency: "direct main"
description:
Expand Down
4 changes: 1 addition & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dependencies:
audio_service: ^0.18.9
audio_session: ^0.1.13
auto_size_text: ^3.0.0
badges: ^2.0.3
buttons_tabbar: ^1.3.6
cached_network_image: ^3.2.2
catcher:
Expand Down Expand Up @@ -70,11 +69,9 @@ dependencies:
path_provider: ^2.0.8
permission_handler: ^10.2.0
popover: ^0.2.6+3
queue: ^3.1.0+1
scroll_to_index: ^3.0.1
shared_preferences: ^2.0.11
sidebarx: ^0.15.0
simple_circular_progress_bar: ^1.0.2
skeleton_text: ^3.0.0
smtc_windows:
git:
Expand Down Expand Up @@ -104,6 +101,7 @@ dependencies:
dbus: ^0.7.8
background_downloader: ^7.4.0
duration: ^3.0.12
disable_battery_optimization: ^1.1.0+1

dev_dependencies:
build_runner: ^2.3.2
Expand Down

0 comments on commit fe5b429

Please sign in to comment.