Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(battery_plus): Added power source type of how the device is powered. #2612

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
result.error("UNAVAILABLE", "Battery level not available.", null)
}
}
"getPowerSourceType" -> {
result.success(getPowerSourceType())
}

"getBatteryState" -> {
val currentBatteryStatus = getBatteryStatus()
Expand Down Expand Up @@ -109,6 +112,20 @@ class BatteryPlusPlugin : MethodCallHandler, EventChannel.StreamHandler, Flutter
return convertBatteryStatus(status)
}

private fun getPowerSourceType(): String? {
val intent = ContextWrapper(applicationContext).registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
val source = intent!!.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
return when (source) {
-1 -> "unknown"
0 -> "battery"
BatteryManager.BATTERY_PLUGGED_AC -> "ac"
BatteryManager.BATTERY_PLUGGED_DOCK -> "dock"
BatteryManager.BATTERY_PLUGGED_USB -> "usb"
BatteryManager.BATTERY_PLUGGED_WIRELESS -> "wireless"
else -> null
}
}

private fun getBatteryLevel(): Int {
return if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
getBatteryProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
Expand Down
16 changes: 15 additions & 1 deletion packages/battery_plus/battery_plus/lib/battery_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:async';
import 'package:battery_plus_platform_interface/battery_plus_platform_interface.dart';

export 'package:battery_plus_platform_interface/battery_plus_platform_interface.dart'
show BatteryState;
show BatteryState, PowerSourceType;

export 'src/battery_plus_linux.dart';

Expand Down Expand Up @@ -44,6 +44,20 @@ class Battery {
return _platform.isInBatterySaveMode;
}

/// get the current power source type.
///
/// When the [BatteryState] is in a state of
/// [BatteryState.connectedNotCharging],[BatteryState.charging]
/// or [BatteryState.full], the [PowerSourceType] can be determined
/// and see how the device gets it's power.
///
/// when the device is mobile and has the [BatteryState] of
/// [BatteryState.discharging] or [BatteryState.unknown] the power source can
/// not be determined
Future<PowerSourceType> get powerSourceType {
return _platform.powerSourceType;
}

/// Get battery state
Future<BatteryState> get batteryState {
return _platform.batteryState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ abstract class BatteryPlatform extends PlatformInterface {
throw UnimplementedError('batteryState() has not been implemented.');
}

Future<PowerSourceType> get powerSourceType {
throw UnimplementedError('powerSourceType() has not been implemented.');
}

/// Returns a Stream of BatteryState changes.
Stream<BatteryState> get onBatteryStateChanged {
throw UnimplementedError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class MethodChannelBattery extends BatteryPlatform {
.invokeMethod<String>('getBatteryState')
.then<BatteryState>((dynamic result) => parseBatteryState(result));

@override
Future<PowerSourceType> get powerSourceType => methodChannel
.invokeMethod<String>('getPowerSourceType')
.then<PowerSourceType>(
(dynamic result) => PowerSourceType.parsePowerSource(result),
);

/// Fires whenever the battery state changes.
@override
Stream<BatteryState> get onBatteryStateChanged {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,42 @@ enum BatteryState {
/// The state of the battery is unknown.
unknown;
}

/// Indicates the current power source type.
///
/// When the [BatteryState] is in a state of
/// [connectedNotCharging],[charging] or [full].
///
/// We can get some extra info battery info on how it is powered.
enum PowerSourceType {
/// Power source is an AC charger.
ac('ac'),

/// Power source is dock.
dock('dock'),

/// Power source is a USB port.
usb('usb'),

/// Power source is wireless.
wireless('wireless'),

/// Power source is battery
battery('battery'),

/// Power source could not be determined.
unknown('unknown');

/// The known string value communicated from the channel.
final String _state;

const PowerSourceType(this._state);

/// Parse the power source string value to the given power source type.
static PowerSourceType parsePowerSource(String? value) {
return PowerSourceType.values.firstWhere(
(sourceType) => sourceType._state == value,
orElse: () => PowerSourceType.unknown,
);
}
}