simplest_power_utilities 0.0.1
simplest_power_utilities: ^0.0.1 copied to clipboard
Plugin which exposes various power-related utilities for Android and iOS, including low power mode detection
simplest_power_utilities #
A Flutter plugin for detecting low power mode status on Android and iOS devices. This plugin provides both synchronous queries and real-time stream updates for low power mode state changes, as well as the ability to launch the device's battery settings screen.
Features #
- Query current low power mode status synchronously
- Subscribe to real-time low power mode state changes via stream
- Launch device battery settings screen on supported platforms
- Supports Android SDK 28+ (Android 9.0+) and iOS 15.0+
- Comprehensive error handling
- Fully tested with unit and integration tests
Platform Support #
- Android: SDK 28-36 (Android 9.0+)
- iOS: 15.0+
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
simplest_power_utilities: ^0.0.1
Then run:
flutter pub get
Usage #
Basic Usage #
import 'package:simplest_power_utilities/simplest_power_utilities.dart';
final plugin = SimplestPowerUtilities();
// Check current low power mode status
final isEnabled = await plugin.isLowPowerModeEnabled();
print('Low power mode enabled: $isEnabled');
Real-time Updates #
// Subscribe to low power mode changes
final subscription = plugin.onLowPowerModeChanged.listen((isEnabled) {
print('Low power mode changed: $isEnabled');
});
// Don't forget to cancel the subscription when done
await subscription.cancel();
Launch Battery Settings #
// Launch the device's battery settings screen
final success = await plugin.launchBatterySettings();
if (success) {
print('Battery settings opened successfully');
} else {
print('Failed to open battery settings');
}
Complete Example #
import 'package:flutter/material.dart';
import 'package:simplest_power_utilities/simplest_power_utilities.dart';
import 'dart:async';
class LowPowerModeWidget extends StatefulWidget {
@override
State<LowPowerModeWidget> createState() => _LowPowerModeWidgetState();
}
class _LowPowerModeWidgetState extends State<LowPowerModeWidget> {
final _plugin = SimplestPowerUtilities();
bool? _isLowPowerModeEnabled;
StreamSubscription<bool>? _subscription;
@override
void initState() {
super.initState();
_loadStatus();
_subscribeToChanges();
}
@override
void dispose() {
_subscription?.cancel();
super.dispose();
}
Future<void> _loadStatus() async {
final isEnabled = await _plugin.isLowPowerModeEnabled();
setState(() {
_isLowPowerModeEnabled = isEnabled;
});
}
void _subscribeToChanges() {
_subscription = _plugin.onLowPowerModeChanged.listen((isEnabled) {
setState(() {
_isLowPowerModeEnabled = isEnabled;
});
});
}
@override
Widget build(BuildContext context) {
return Text(
_isLowPowerModeEnabled == true
? 'Low Power Mode: Enabled'
: 'Low Power Mode: Disabled',
);
}
}
API Reference #
SimplestPowerUtilities #
The main plugin class.
Methods
Future<bool> isLowPowerModeEnabled()
Returns the current low power mode state.
- Returns
trueif low power mode is currently enabled - Returns
falseif low power mode is disabled - Throws
PlatformExceptionif the platform call fails
Future<bool> launchBatterySettings()
Launches the device's battery settings screen.
- Returns
trueif the settings screen was successfully launched - Returns
falseif the operation failed or is unsupported - Gracefully handles platform-specific restrictions or permissions
Properties
Stream<bool> get onLowPowerModeChanged
Returns a stream of low power mode state changes.
- Emits
truewhen low power mode is enabled - Emits
falsewhen low power mode is disabled - Emits the current state immediately upon subscription
Platform-Specific Details #
Android #
The plugin uses Android's PowerManager service to detect power save mode:
- Uses
PowerManager.isPowerSaveModefor current state queries - Registers a
BroadcastReceiverforACTION_POWER_SAVE_MODE_CHANGEDto detect changes - Uses
Settings.ACTION_BATTERY_SAVER_SETTINGSIntent to launch battery settings (API 21+)
iOS #
The plugin uses iOS's ProcessInfo to detect low power mode:
- Uses
ProcessInfo.processInfo.isLowPowerModeEnabledfor current state queries - Observes
NSProcessInfoPowerStateDidChangenotification for changes - Uses
prefs:root=BATTERY_USAGEURL scheme to launch battery settings (with fallback to app settings)
Error Handling #
The plugin may throw PlatformException in the following cases:
- Platform call fails (e.g., service unavailable)
- Invalid platform state
- Stream errors
Always wrap calls in try-catch blocks:
try {
final isEnabled = await plugin.isLowPowerModeEnabled();
// Handle success
} on PlatformException catch (e) {
// Handle platform-specific error
print('Error: ${e.message}');
}
Testing #
The plugin includes comprehensive tests:
- Unit tests: Mock platform interface tests for Dart code
- Android tests: Mocked PowerManager tests for Android native code
- Integration tests: Real device verification tests
Run tests with:
flutter test
Example App #
See the example/ directory for a complete Material 3 demo application that demonstrates:
- Current low power mode status display
- Real-time updates when status changes
- Launch battery settings functionality
- Error handling
- Clean, responsive UI
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
See the LICENSE file for details.