|
| 1 | +import { Application, Utils } from '@nativescript/core'; |
| 2 | +import { batteryState, formatValue } from './common'; |
| 3 | + |
| 4 | +// Just check current battery level |
| 5 | +export function checkBatteryLevel(): Promise<number> { |
| 6 | + return new Promise((resolve) => { |
| 7 | + // Android direct native access! |
| 8 | + Application.android.registerBroadcastReceiver( |
| 9 | + android.content.Intent.ACTION_BATTERY_CHANGED, |
| 10 | + (context, intent: android.content.Intent) => { |
| 11 | + const level = intent.getIntExtra( |
| 12 | + android.os.BatteryManager.EXTRA_LEVEL, |
| 13 | + -1 |
| 14 | + ); |
| 15 | + const scale = intent.getIntExtra( |
| 16 | + android.os.BatteryManager.EXTRA_SCALE, |
| 17 | + -1 |
| 18 | + ); |
| 19 | + resolve(formatValue((level / scale) * 100)); |
| 20 | + } |
| 21 | + ); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +// Listen for changes |
| 26 | +export function listenForBatteryChanges(callback?: (level: number) => void) { |
| 27 | + batteryState.clientCallback = callback; |
| 28 | + const action = android.content.Intent.ACTION_BATTERY_CHANGED; |
| 29 | + if (!batteryState.isListening) { |
| 30 | + batteryState.isListening = true; |
| 31 | + broadcastReceiverRegister(action, (context, intent) => { |
| 32 | + const level = intent.getIntExtra( |
| 33 | + android.os.BatteryManager.EXTRA_LEVEL, |
| 34 | + -1 |
| 35 | + ); |
| 36 | + const scale = intent.getIntExtra( |
| 37 | + android.os.BatteryManager.EXTRA_SCALE, |
| 38 | + -1 |
| 39 | + ); |
| 40 | + if (batteryState.clientCallback) { |
| 41 | + batteryState.clientCallback(formatValue((level / scale) * 100)); |
| 42 | + } |
| 43 | + }); |
| 44 | + } else { |
| 45 | + batteryState.isListening = false; |
| 46 | + broadcastReceiverUnRegister(action); |
| 47 | + } |
| 48 | + return batteryState.isListening; |
| 49 | +} |
| 50 | + |
| 51 | +interface Constructable<T> { |
| 52 | + new (...args: any): T; |
| 53 | +} |
| 54 | +type BroadcastReceiverType = Partial<android.content.BroadcastReceiver> & |
| 55 | + Constructable<android.content.BroadcastReceiver>; |
| 56 | +let BroadcastReceiverImpl: BroadcastReceiverType; |
| 57 | +let registeredReceivers: { |
| 58 | + [key: string]: android.content.BroadcastReceiver | undefined; |
| 59 | +}; |
| 60 | + |
| 61 | +function ensureBroadCastReceiver() { |
| 62 | + if (BroadcastReceiverImpl) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + @NativeClass() |
| 67 | + class BroadcastReceiver extends android.content.BroadcastReceiver { |
| 68 | + private _onReceiveCallback: ( |
| 69 | + context: android.content.Context, |
| 70 | + intent: android.content.Intent |
| 71 | + ) => void; |
| 72 | + |
| 73 | + constructor( |
| 74 | + onReceiveCallback: ( |
| 75 | + context: android.content.Context, |
| 76 | + intent: android.content.Intent |
| 77 | + ) => void |
| 78 | + ) { |
| 79 | + super(); |
| 80 | + this._onReceiveCallback = onReceiveCallback; |
| 81 | + |
| 82 | + return (<any>globalThis).__native(this); |
| 83 | + } |
| 84 | + |
| 85 | + public onReceive( |
| 86 | + context: android.content.Context, |
| 87 | + intent: android.content.Intent |
| 88 | + ) { |
| 89 | + if (this._onReceiveCallback) { |
| 90 | + this._onReceiveCallback(context, intent); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + BroadcastReceiverImpl = <BroadcastReceiverType>(<unknown>BroadcastReceiver); |
| 96 | +} |
| 97 | + |
| 98 | +function broadcastReceiverRegister( |
| 99 | + intentFilter: string, |
| 100 | + onReceiveCallback: ( |
| 101 | + context: android.content.Context, |
| 102 | + intent: android.content.Intent |
| 103 | + ) => void |
| 104 | +): void { |
| 105 | + ensureBroadCastReceiver(); |
| 106 | + const registerFunc = (context: android.content.Context) => { |
| 107 | + const receiver = new BroadcastReceiverImpl(onReceiveCallback); |
| 108 | + context.registerReceiver( |
| 109 | + receiver, |
| 110 | + new android.content.IntentFilter(intentFilter) |
| 111 | + ); |
| 112 | + if (!registeredReceivers) { |
| 113 | + registeredReceivers = {}; |
| 114 | + } |
| 115 | + registeredReceivers[intentFilter] = receiver; |
| 116 | + }; |
| 117 | + |
| 118 | + registerFunc(Utils.android.getApplicationContext()); |
| 119 | +} |
| 120 | + |
| 121 | +function broadcastReceiverUnRegister(intentFilter: string): void { |
| 122 | + if (!registeredReceivers) { |
| 123 | + registeredReceivers = {}; |
| 124 | + } |
| 125 | + const receiver = registeredReceivers[intentFilter]; |
| 126 | + if (receiver) { |
| 127 | + Utils.android.getApplicationContext().unregisterReceiver(receiver); |
| 128 | + registeredReceivers[intentFilter] = undefined; |
| 129 | + delete registeredReceivers[intentFilter]; |
| 130 | + } |
| 131 | +} |
0 commit comments