simplest_power_utilities 0.0.1 copy "simplest_power_utilities: ^0.0.1" to clipboard
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 true if low power mode is currently enabled
  • Returns false if low power mode is disabled
  • Throws PlatformException if the platform call fails
Future<bool> launchBatterySettings()

Launches the device's battery settings screen.

  • Returns true if the settings screen was successfully launched
  • Returns false if 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 true when low power mode is enabled
  • Emits false when 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.isPowerSaveMode for current state queries
  • Registers a BroadcastReceiver for ACTION_POWER_SAVE_MODE_CHANGED to detect changes
  • Uses Settings.ACTION_BATTERY_SAVER_SETTINGS Intent to launch battery settings (API 21+)

iOS #

The plugin uses iOS's ProcessInfo to detect low power mode:

  • Uses ProcessInfo.processInfo.isLowPowerModeEnabled for current state queries
  • Observes NSProcessInfoPowerStateDidChange notification for changes
  • Uses prefs:root=BATTERY_USAGE URL 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.

0
likes
160
points
34
downloads

Publisher

verified publisherbizjak.dev

Weekly Downloads

Plugin which exposes various power-related utilities for Android and iOS, including low power mode detection

Repository (GitLab)

Documentation

API reference

License

AGPL-3.0 (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on simplest_power_utilities

Packages that implement simplest_power_utilities