Skip to content

Commit c1ad0ef

Browse files
committed
Add battery_plus_linux
1 parent 0411065 commit c1ad0ef

File tree

13 files changed

+345
-1
lines changed

13 files changed

+345
-1
lines changed

packages/battery_plus/lib/battery_plus.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:io' show Platform;
67

8+
import 'package:flutter/foundation.dart' show kIsWeb;
79
import 'package:battery_plus_platform_interface/battery_plus_platform_interface.dart';
10+
import 'package:battery_plus_platform_interface/src/method_channel_battery_plus.dart';
11+
import 'package:battery_plus_linux/battery_plus_linux.dart';
812

913
// Export enums from the platform_interface so plugin users can use them directly.
1014
export 'package:battery_plus_platform_interface/battery_plus_platform_interface.dart'
@@ -29,7 +33,24 @@ class Battery {
2933

3034
static Battery _singleton;
3135

32-
static BatteryPlatform get _platform => BatteryPlatform.instance;
36+
static bool _manualDartRegistrationNeeded = true;
37+
38+
static BatteryPlatform get _platform {
39+
// This is to manually endorse Dart implementations until automatic
40+
// registration of Dart plugins is implemented. For details see
41+
// https://github.com/flutter/flutter/issues/52267.
42+
if (_manualDartRegistrationNeeded) {
43+
// Only do the initial registration if it hasn't already been overridden
44+
// with a non-default instance.
45+
if (!kIsWeb && BatteryPlatform.instance is MethodChannelBattery) {
46+
if (Platform.isLinux) {
47+
BatteryPlatform.instance = BatteryLinux();
48+
}
49+
}
50+
_manualDartRegistrationNeeded = false;
51+
}
52+
return BatteryPlatform.instance;
53+
}
3354

3455
/// get battery level
3556
Future<int> get batteryLevel {

packages/battery_plus/pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ flutter:
1212
pluginClass: BatteryPlugin
1313
ios:
1414
pluginClass: FLTBatteryPlugin
15+
linux:
16+
default_package: battery_plus_linux
1517
macos:
1618
default_package: battery_plus_macos
1719
web:
@@ -24,6 +26,7 @@ dependencies:
2426
sdk: flutter
2527
meta: ^1.0.5
2628
battery_plus_platform_interface: ^0.1.0
29+
battery_plus_linux: ^0.1.0
2730
battery_plus_macos: ^0.1.0
2831
battery_plus_web: ^0.1.0
2932
battery_plus_windows: ^0.1.0
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Miscellaneous
2+
*.class
3+
*.log
4+
*.pyc
5+
*.swp
6+
.DS_Store
7+
.atom/
8+
.buildlog/
9+
.history
10+
.svn/
11+
12+
# IntelliJ related
13+
*.iml
14+
*.ipr
15+
*.iws
16+
.idea/
17+
18+
# The .vscode folder contains launch configuration and tasks you configure in
19+
# VS Code which you may wish to be included in version control, so this line
20+
# is commented out by default.
21+
#.vscode/
22+
23+
# Flutter/Dart/Pub related
24+
**/doc/api/
25+
.dart_tool/
26+
.flutter-plugins
27+
.flutter-plugins-dependencies
28+
.packages
29+
.pub-cache/
30+
.pub/
31+
build/
32+
33+
# Android related
34+
**/android/**/gradle-wrapper.jar
35+
**/android/.gradle
36+
**/android/captures/
37+
**/android/gradlew
38+
**/android/gradlew.bat
39+
**/android/local.properties
40+
**/android/**/GeneratedPluginRegistrant.java
41+
42+
# iOS/XCode related
43+
**/ios/**/*.mode1v3
44+
**/ios/**/*.mode2v3
45+
**/ios/**/*.moved-aside
46+
**/ios/**/*.pbxuser
47+
**/ios/**/*.perspectivev3
48+
**/ios/**/*sync/
49+
**/ios/**/.sconsign.dblite
50+
**/ios/**/.tags*
51+
**/ios/**/.vagrant/
52+
**/ios/**/DerivedData/
53+
**/ios/**/Icon?
54+
**/ios/**/Pods/
55+
**/ios/**/.symlinks/
56+
**/ios/**/profile
57+
**/ios/**/xcuserdata
58+
**/ios/.generated/
59+
**/ios/Flutter/App.framework
60+
**/ios/Flutter/Flutter.framework
61+
**/ios/Flutter/Flutter.podspec
62+
**/ios/Flutter/Generated.xcconfig
63+
**/ios/Flutter/app.flx
64+
**/ios/Flutter/app.zip
65+
**/ios/Flutter/flutter_assets/
66+
**/ios/Flutter/flutter_export_environment.sh
67+
**/ios/ServiceDefinitions.json
68+
**/ios/Runner/GeneratedPluginRegistrant.*
69+
70+
# Exceptions to above rules.
71+
!**/ios/**/default.mode1v3
72+
!**/ios/**/default.mode2v3
73+
!**/ios/**/default.pbxuser
74+
!**/ios/**/default.perspectivev3
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This file tracks properties of this Flutter project.
2+
# Used by Flutter tool to assess capabilities and perform upgrades etc.
3+
#
4+
# This file should be version controlled and should not be manually edited.
5+
6+
version:
7+
revision: 34541c30735bc1086335e13d69a7e0acdc0538c5
8+
channel: master
9+
10+
project_type: package
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 0.1.0
2+
3+
- Initial Linux support.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2020 The Chromium Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Battery Plus Linux
2+
3+
[![Flutter Community: battery_plus_linux](https://fluttercommunity.dev/_github/header/battery_plus_linux)](https://github.com/fluttercommunity/community)
4+
5+
[![pub package](https://img.shields.io/pub/v/battery_plus_linux.svg)](https://pub.dev/packages/battery_plus_linux)
6+
7+
The Linux implementation of [`battery_plus`](https://pub.dev/packages/battery_plus).
8+
9+
## Usage
10+
11+
This package is already included as part of the `battery_plus` package dependency, and will
12+
be included when using `battery_plus` as normal.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include: package:pedantic/analysis_options.1.9.0.yaml
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// The Linux implementation of `battery_plus`.
2+
library battery_plus_linux;
3+
4+
export 'src/battery_linux.dart';
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'dart:async';
2+
3+
import 'package:battery_plus_platform_interface/battery_plus_platform_interface.dart';
4+
import 'package:meta/meta.dart';
5+
6+
import 'upower_device.dart';
7+
8+
// ### TODO: introduce an 'unknown' battery state for workstations?
9+
// https://github.com/fluttercommunity/plus_plugins/issues/61
10+
extension _ToBatteryState on UPowerBatteryState {
11+
BatteryState toBatteryState() {
12+
switch (this) {
13+
case UPowerBatteryState.charging:
14+
return BatteryState.charging;
15+
case UPowerBatteryState.discharging:
16+
return BatteryState.discharging;
17+
default:
18+
return BatteryState.full;
19+
}
20+
}
21+
}
22+
23+
@visibleForTesting
24+
typedef UPowerDeviceFactory = UPowerDevice Function();
25+
26+
/// The Linux implementation of BatteryPlatform.
27+
class BatteryLinux extends BatteryPlatform {
28+
/// Returns the current battery level in percent.
29+
@override
30+
Future<int> get batteryLevel {
31+
final device = createDevice();
32+
return device
33+
.getPercentage()
34+
.then((value) => value.round())
35+
.whenComplete(() => device.dispose());
36+
}
37+
38+
/// Fires whenever the battery state changes.
39+
@override
40+
Stream<BatteryState> get onBatteryStateChanged {
41+
_stateController ??= StreamController<BatteryState>(
42+
onListen: _startListenState,
43+
onCancel: _stopListenState,
44+
);
45+
return _stateController.stream.asBroadcastStream();
46+
}
47+
48+
UPowerDevice _stateDevice;
49+
StreamController<BatteryState> _stateController;
50+
51+
@visibleForTesting
52+
UPowerDeviceFactory createDevice = () => UPowerDevice.display();
53+
54+
void _addState(UPowerBatteryState value) {
55+
_stateController.add(value.toBatteryState());
56+
}
57+
58+
void _startListenState() {
59+
_stateDevice ??= createDevice();
60+
_stateDevice.getState().then((value) => _addState(value));
61+
_stateDevice.subscribeStateChanged().listen((value) => _addState(value));
62+
}
63+
64+
void _stopListenState() {
65+
_stateDevice?.dispose();
66+
_stateDevice = null;
67+
}
68+
}

0 commit comments

Comments
 (0)