|
| 1 | +// Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:async'; |
| 6 | + |
| 7 | +import 'package:plugin_platform_interface/plugin_platform_interface.dart'; |
| 8 | + |
| 9 | +import 'method_channel/method_channel_battery.dart'; |
| 10 | +import 'enums/battery_state.dart'; |
| 11 | + |
| 12 | +export 'enums/battery_state.dart'; |
| 13 | + |
| 14 | +/// The interface that implementations of battery must implement. |
| 15 | +/// |
| 16 | +/// Platform implementations should extend this class rather than implement it as `battery` |
| 17 | +/// does not consider newly added methods to be breaking changes. Extending this class |
| 18 | +/// (using `extends`) ensures that the subclass will get the default implementation, while |
| 19 | +/// platform implementations that `implements` this interface will be broken by newly added |
| 20 | +/// [BatteryPlatform] methods. |
| 21 | +abstract class BatteryPlatform extends PlatformInterface { |
| 22 | + /// Constructs a BatteryPlatform. |
| 23 | + BatteryPlatform() : super(token: _token); |
| 24 | + |
| 25 | + static final Object _token = Object(); |
| 26 | + |
| 27 | + static BatteryPlatform _instance = MethodChannelBattery(); |
| 28 | + |
| 29 | + /// The default instance of [BatteryPlatform] to use. |
| 30 | + /// |
| 31 | + /// Defaults to [MethodChannelBattery]. |
| 32 | + static BatteryPlatform get instance => _instance; |
| 33 | + |
| 34 | + /// Platform-specific plugins should set this with their own platform-specific |
| 35 | + /// class that extends [BatteryPlatform] when they register themselves. |
| 36 | + static set instance(BatteryPlatform instance) { |
| 37 | + PlatformInterface.verifyToken(instance, _token); |
| 38 | + _instance = instance; |
| 39 | + } |
| 40 | + |
| 41 | + /// Gets the battery level from device. |
| 42 | + Future<int> batteryLevel() { |
| 43 | + throw UnimplementedError('batteryLevel() has not been implemented.'); |
| 44 | + } |
| 45 | + |
| 46 | + /// gets battery state from device. |
| 47 | + Stream<BatteryState> onBatteryStateChanged() { |
| 48 | + throw UnimplementedError( |
| 49 | + 'onBatteryStateChanged() has not been implemented.'); |
| 50 | + } |
| 51 | +} |
0 commit comments