Skip to content

Commit

Permalink
feat(Device): Add getBatteryInfo function (#2435)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Feb 10, 2020
1 parent d5efb05 commit 0deca04
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ public void getInfo(PluginCall call) {
r.put("platform", getPlatform());
r.put("manufacturer", android.os.Build.MANUFACTURER);
r.put("uuid", getUuid());
r.put("isVirtual", isVirtual());

call.success(r);
}

@PluginMethod()
public void getBatteryInfo(PluginCall call) {
JSObject r = new JSObject();

r.put("batteryLevel", getBatteryLevel());
r.put("isCharging", isCharging());
r.put("isVirtual", isVirtual());

call.success(r);
}
Expand Down
7 changes: 7 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ export interface DevicePlugin extends Plugin {
* Return information about the underlying device/os/platform
*/
getInfo(): Promise<DeviceInfo>;
/**
* Return information about the battery
*/
getBatteryInfo(): Promise<DeviceBatteryInfo>;
/**
* Get the device's current language locale code
*/
Expand Down Expand Up @@ -455,6 +459,9 @@ export interface DeviceInfo {
* The total size of the normal data storage path for the OS, in bytes
*/
diskTotal?: number;
}

export interface DeviceBatteryInfo {
/**
* A percentage (0 to 1) indicating how much the battery is charged
*/
Expand Down
25 changes: 16 additions & 9 deletions core/src/web/device.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WebPlugin } from './index';

import {
DeviceBatteryInfo,
DeviceInfo,
DevicePlugin,
DeviceLanguageCodeResult
Expand All @@ -21,13 +22,6 @@ export class DevicePluginWeb extends WebPlugin implements DevicePlugin {
async getInfo(): Promise<DeviceInfo> {
const ua = navigator.userAgent;
const uaFields = this.parseUa(ua);
let battery: any = {};

try {
battery = await navigator.getBattery();
} catch (e) {
// Let it fail, we don't care
}

return Promise.resolve({
model: uaFields.model,
Expand All @@ -38,12 +32,25 @@ export class DevicePluginWeb extends WebPlugin implements DevicePlugin {
osVersion: uaFields.osVersion,
manufacturer: navigator.vendor,
isVirtual: false,
batteryLevel: battery.level,
isCharging: battery.charging,
uuid: this.getUid()
});
}

async getBatteryInfo(): Promise<DeviceBatteryInfo> {
let battery: any = {};

try {
battery = await navigator.getBattery();
} catch (e) {
// Let it fail, we don't care
}

return Promise.resolve({
batteryLevel: battery.level,
isCharging: battery.charging
});
}

async getLanguageCode(): Promise<DeviceLanguageCodeResult> {
return {
value: navigator.language
Expand Down
13 changes: 10 additions & 3 deletions electron/src/electron/device.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeviceInfo, DeviceLanguageCodeResult, DevicePlugin, DevicePluginWeb, WebPlugin } from "@capacitor/core";
import { DeviceBatteryInfo, DeviceInfo, DeviceLanguageCodeResult, DevicePlugin, DevicePluginWeb, WebPlugin } from "@capacitor/core";

declare var navigator: any;
const webDevice = new DevicePluginWeb();
Expand All @@ -23,12 +23,19 @@ export class DevicePluginElectron extends WebPlugin implements DevicePlugin {
osVersion: info.osVersion,
manufacturer: navigator.vendor,
isVirtual: false,
batteryLevel: info.batteryLevel,
isCharging: info.isCharging,
uuid: info.uuid
};
}

async getBatteryInfo(): Promise<DeviceBatteryInfo> {
var batInfo = await webDevice.getBatteryInfo();

return {
batteryLevel: batInfo.batteryLevel,
isCharging: batInfo.isCharging
};
}

async getLanguageCode(): Promise<DeviceLanguageCodeResult> {
return webDevice.getLanguageCode();
}
Expand Down
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

CAP_PLUGIN(CAPDevicePlugin, "Device",
CAP_PLUGIN_METHOD(getInfo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getBatteryInfo, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getLanguageCode, CAPPluginReturnPromise);
)

Expand Down
23 changes: 14 additions & 9 deletions ios/Capacitor/Capacitor/Plugins/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ public typealias DeviceInfo = [String:Any]
@objc(CAPDevicePlugin)
public class CAPDevicePlugin: CAPPlugin {
let diagnostics: Diagnostics = Diagnostics()

@objc func getInfo(_ call: CAPPluginCall) {
var isSimulator = false
#if arch(i386) || arch(x86_64)
isSimulator = true
#endif

UIDevice.current.isBatteryMonitoringEnabled = true


let memUsed = diagnostics.getMemoryUsage()
let diskFree = diagnostics.getFreeDiskSize() ?? 0
let diskTotal = diagnostics.getTotalDiskSize() ?? 0

call.success([
"memUsed": memUsed,
"diskFree": diskFree,
Expand All @@ -30,14 +28,21 @@ public class CAPDevicePlugin: CAPPlugin {
"platform": "ios",
"manufacturer": "Apple",
"uuid": UIDevice.current.identifierForVendor!.uuidString,
"batteryLevel": UIDevice.current.batteryLevel,
"isCharging": UIDevice.current.batteryState == .charging || UIDevice.current.batteryState == .full,
"isVirtual": isSimulator
])

}

@objc func getBatteryInfo(_ call: CAPPluginCall) {
UIDevice.current.isBatteryMonitoringEnabled = true

call.success([
"batteryLevel": UIDevice.current.batteryLevel,
"isCharging": UIDevice.current.batteryState == .charging || UIDevice.current.batteryState == .full
])

UIDevice.current.isBatteryMonitoringEnabled = false
}

@objc func getLanguageCode(_ call: CAPPluginCall) {
let code = String(Locale.preferredLanguages[0].prefix(2))
call.success([
Expand Down

0 comments on commit 0deca04

Please sign in to comment.