|
| 1 | +# Battery API |
| 2 | + |
| 3 | +<show-structure for="chapter" depth="2"/> |
| 4 | +<secondary-label ref="security-limitations"/> |
| 5 | +<secondary-label ref="macos-limitations"/> |
| 6 | + |
| 7 | +This API provides information about |
| 8 | +[device battery](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API). |
| 9 | + |
| 10 | +<warning> |
| 11 | +MacOS/WebKit does not support this API. In the future, it |
| 12 | +may be implemented using native syscalls. |
| 13 | +</warning> |
| 14 | + |
| 15 | +The API is available in the `WebView::$battery` property. |
| 16 | + |
| 17 | +```php |
| 18 | +$app = new Boson\Application(); |
| 19 | + |
| 20 | +$app->webview->battery; // Access to Battery API |
| 21 | +``` |
| 22 | + |
| 23 | +## Battery Level |
| 24 | +<secondary-label ref="read-only"/> |
| 25 | + |
| 26 | +To get the current battery charge level you can use the read-only |
| 27 | +`$level` property. The battery level contain a float value |
| 28 | +between `0.0` and `1.0`. |
| 29 | + |
| 30 | +```php |
| 31 | +$level = $app->webview->battery->level; |
| 32 | + |
| 33 | +echo 'Charge level is ' . (int) ($level * 100) . '%'; |
| 34 | +// |
| 35 | +// Expects: Charge level is 100% |
| 36 | +// |
| 37 | +``` |
| 38 | + |
| 39 | +<note> |
| 40 | +For non-mobile devices the charge level is always <code>1.0</code> |
| 41 | +</note> |
| 42 | + |
| 43 | +## Charging Status |
| 44 | +<secondary-label ref="read-only"/> |
| 45 | + |
| 46 | +To get the battery charging status you can use the read-only |
| 47 | +`$isCharging` property. |
| 48 | + |
| 49 | +```php |
| 50 | +$isCharging = $app->webview->battery->isCharging; |
| 51 | + |
| 52 | +echo 'The battery is ' . ($isCharging ? '' : 'not ') . 'charging now'; |
| 53 | +// |
| 54 | +// Expects: The battery is charging now |
| 55 | +// |
| 56 | +``` |
| 57 | + |
| 58 | +<note> |
| 59 | +For non-mobile devices the charging status is always <code>true</code> |
| 60 | +</note> |
| 61 | + |
| 62 | +## Charging Time |
| 63 | +<secondary-label ref="read-only"/> |
| 64 | + |
| 65 | +To get the time until the battery is fully charged, use the read-only |
| 66 | +`$chargingTime` property. The charging time property will contain an |
| 67 | +integer value in seconds. |
| 68 | + |
| 69 | +```php |
| 70 | +$chargingTime = $app->webview->battery->chargingTime; |
| 71 | + |
| 72 | +echo vsprintf('It takes another %d seconds to fully charge', [ |
| 73 | + $chargingTime, |
| 74 | +]); |
| 75 | +``` |
| 76 | + |
| 77 | +<note> |
| 78 | +For non-mobile devices the charging time is always <code>0</code> |
| 79 | +</note> |
| 80 | + |
| 81 | +## Discharging Time |
| 82 | +<secondary-label ref="read-only"/> |
| 83 | + |
| 84 | +To get the time until the battery is fully discharged, use the read-only |
| 85 | +`$dischargingTime` property. The discharging time property will |
| 86 | +contain an integer value in seconds or `null` in case the |
| 87 | +discharge time is not available. |
| 88 | + |
| 89 | +```php |
| 90 | +$dischargingTime = $app->webview->battery->dischargingTime; |
| 91 | + |
| 92 | +if ($dischargingTime === null) { |
| 93 | + echo 'Battery is missing'; |
| 94 | + |
| 95 | + return; |
| 96 | +} |
| 97 | + |
| 98 | +echo vsprintf('It takes another %d seconds to fully discharge', [ |
| 99 | + $dischargingTime, |
| 100 | +]); |
| 101 | +``` |
| 102 | + |
| 103 | +<note> |
| 104 | +For non-mobile devices the discharging time is always <code>null</code> |
| 105 | +</note> |
0 commit comments