2016-03-07 01:48:20 +08:00
|
|
|
import {Plugin} from './plugin';
|
|
|
|
import {Observable} from "rxjs/Observable";
|
|
|
|
|
|
|
|
/**
|
2016-03-11 04:14:55 +08:00
|
|
|
* @name Battery Status
|
2016-03-13 07:30:16 +08:00
|
|
|
* @description
|
2016-03-07 01:48:20 +08:00
|
|
|
* Requires Cordova plugin: cordova-plugin-batterystatus. For more info, please see the [BatteryStatus plugin docs](https://github.com/apache/cordova-plugin-battery-status).
|
|
|
|
*
|
|
|
|
* @usage
|
|
|
|
* ```js
|
2016-03-11 04:14:55 +08:00
|
|
|
* // watch change in battery status
|
|
|
|
* let subscription = BatteryStatus.onChange().subscribe(
|
2016-03-07 01:48:20 +08:00
|
|
|
* status => {
|
2016-03-11 04:24:04 +08:00
|
|
|
* console.log(status.level, status.isPlugged);
|
2016-03-07 01:48:20 +08:00
|
|
|
* }
|
|
|
|
* );
|
|
|
|
*
|
2016-03-11 04:14:55 +08:00
|
|
|
* // stop watch
|
|
|
|
* subscription.unsubscribe();
|
|
|
|
*
|
2016-03-07 01:48:20 +08:00
|
|
|
* ```
|
|
|
|
*/
|
|
|
|
@Plugin({
|
2016-03-13 08:08:47 +08:00
|
|
|
plugin: 'cordova-plugin-batterystatus',
|
2016-03-15 01:38:35 +08:00
|
|
|
repo: 'https://github.com/apache/cordova-plugin-battery-status',
|
|
|
|
platforms: ['Amazon Fire OS', 'iOS', 'Android', 'BlackBerry 10', 'Windows Phone 7', 'Windows Phone 8', 'Windows', 'Firefox OS', 'Browser']
|
2016-03-07 01:48:20 +08:00
|
|
|
})
|
|
|
|
export class BatteryStatus {
|
|
|
|
|
|
|
|
/**
|
2016-03-11 04:14:55 +08:00
|
|
|
* Watch the change in battery level
|
2016-03-11 04:17:51 +08:00
|
|
|
* @returns {Observable} Returns an observable that pushes a status object
|
2016-03-07 01:48:20 +08:00
|
|
|
*/
|
2016-03-11 04:17:51 +08:00
|
|
|
static onChange () : Observable<StatusObject> {
|
2016-03-11 04:27:01 +08:00
|
|
|
return getEventObservable("batterylevel");
|
2016-03-11 04:24:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch when the battery level goes low
|
|
|
|
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
|
|
|
|
*/
|
|
|
|
static onLow () : Observable<StatusObject> {
|
2016-03-11 04:27:01 +08:00
|
|
|
return getEventObservable("batterylow");
|
2016-03-11 04:24:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch when the battery level goes to critial
|
|
|
|
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
|
|
|
|
*/
|
|
|
|
static onCritical () : Observable<StatusObject> {
|
2016-03-11 04:27:01 +08:00
|
|
|
return getEventObservable("batterycritical");
|
2016-03-07 01:48:20 +08:00
|
|
|
}
|
|
|
|
|
2016-03-11 04:17:51 +08:00
|
|
|
}
|
|
|
|
|
2016-03-13 07:35:58 +08:00
|
|
|
export interface StatusObject {
|
2016-03-11 04:17:51 +08:00
|
|
|
/**
|
|
|
|
* The battery charge percentage
|
|
|
|
*/
|
|
|
|
level : number,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A boolean that indicates whether the device is plugged in
|
|
|
|
*/
|
|
|
|
isPlugged : boolean
|
2016-03-11 04:27:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrap the event with an observable
|
|
|
|
* @param event
|
|
|
|
* @returns {Observable}
|
|
|
|
*/
|
|
|
|
function getEventObservable (event : string) : Observable<StatusObject> {
|
|
|
|
return new Observable(observer => {
|
|
|
|
let callback = (status : any) => observer.next(status);
|
|
|
|
window.addEventListener(event, callback, false);
|
|
|
|
return () => window.removeEventListener(event, callback, false);
|
|
|
|
});
|
2016-03-11 04:14:55 +08:00
|
|
|
}
|