diff --git a/android/capacitor/src/main/java/com/getcapacitor/plugin/Device.java b/android/capacitor/src/main/java/com/getcapacitor/plugin/Device.java index 457843503..9858f974b 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/plugin/Device.java +++ b/android/capacitor/src/main/java/com/getcapacitor/plugin/Device.java @@ -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); } diff --git a/core/src/core-plugin-definitions.ts b/core/src/core-plugin-definitions.ts index 547b973a2..330ff53f5 100644 --- a/core/src/core-plugin-definitions.ts +++ b/core/src/core-plugin-definitions.ts @@ -395,6 +395,10 @@ export interface DevicePlugin extends Plugin { * Return information about the underlying device/os/platform */ getInfo(): Promise; + /** + * Return information about the battery + */ + getBatteryInfo(): Promise; /** * Get the device's current language locale code */ @@ -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 */ diff --git a/core/src/web/device.ts b/core/src/web/device.ts index f173d4ed4..4889b5808 100644 --- a/core/src/web/device.ts +++ b/core/src/web/device.ts @@ -1,6 +1,7 @@ import { WebPlugin } from './index'; import { + DeviceBatteryInfo, DeviceInfo, DevicePlugin, DeviceLanguageCodeResult @@ -21,13 +22,6 @@ export class DevicePluginWeb extends WebPlugin implements DevicePlugin { async getInfo(): Promise { 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, @@ -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 { + 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 { return { value: navigator.language diff --git a/electron/src/electron/device.ts b/electron/src/electron/device.ts index 200d333ff..0c4ea7cff 100644 --- a/electron/src/electron/device.ts +++ b/electron/src/electron/device.ts @@ -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(); @@ -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 { + var batInfo = await webDevice.getBatteryInfo(); + + return { + batteryLevel: batInfo.batteryLevel, + isCharging: batInfo.isCharging + }; + } + async getLanguageCode(): Promise { return webDevice.getLanguageCode(); } diff --git a/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m b/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m index ed38413ac..8d8b4e895 100644 --- a/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m +++ b/ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m @@ -40,6 +40,7 @@ CAP_PLUGIN(CAPDevicePlugin, "Device", CAP_PLUGIN_METHOD(getInfo, CAPPluginReturnPromise); + CAP_PLUGIN_METHOD(getBatteryInfo, CAPPluginReturnPromise); CAP_PLUGIN_METHOD(getLanguageCode, CAPPluginReturnPromise); ) diff --git a/ios/Capacitor/Capacitor/Plugins/Device.swift b/ios/Capacitor/Capacitor/Plugins/Device.swift index 04f7cfd83..8444939cc 100644 --- a/ios/Capacitor/Capacitor/Plugins/Device.swift +++ b/ios/Capacitor/Capacitor/Plugins/Device.swift @@ -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, @@ -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([