PowerStatus: Implemented sound cues for low battery and charging

* Added two beep events ("Low battery" and "Battery charged") to Sounds
* "Battery charged" is activated when the battery is fully charged to 100 percent
* "Low battery" is activated when the battery is less than 15 percent
  or has less than 30 minutes of usage time left, whichever comes first

Change-Id: If95f812be7a0aa15d668a42fdcfaccd2995d2b4f
Reviewed-on: https://review.haiku-os.org/c/haiku/+/6481
Reviewed-by: Niels Sascha Reedijk <niels.reedijk@gmail.com>
This commit is contained in:
Theodore Wang 2023-05-30 23:00:02 +00:00 committed by Niels Sascha Reedijk
parent 1676a41ec5
commit 0f293bd7b6
2 changed files with 30 additions and 2 deletions

View File

@ -21,6 +21,7 @@
#include <AboutWindow.h>
#include <Application.h>
#include <Bitmap.h>
#include <Beep.h>
#include <Catalog.h>
#include <DataIO.h>
#include <Deskbar.h>
@ -61,6 +62,9 @@ const uint32 kMsgToggleExtInfo = 'texi';
const double kLowBatteryPercentage = 0.15;
const double kNoteBatteryPercentage = 0.3;
const double kFullBatteryPercentage = 1.0;
const time_t kLowBatteryTimeLeft = 30 * 60;
PowerStatusView::PowerStatusView(PowerStatusDriverInterface* interface,
@ -116,6 +120,11 @@ PowerStatusView::_Init()
fPercent = 1.0;
fOnline = true;
fTimeLeft = 0;
fHasNotifiedLowBattery = false;
add_system_beep_event("Low battery");
add_system_beep_event("Battery charged");
}
@ -298,6 +307,7 @@ PowerStatusView::Draw(BRect updateRect)
DrawTo(this, Bounds());
}
void
PowerStatusView::DrawTo(BView* view, BRect rect)
{
@ -491,9 +501,23 @@ PowerStatusView::Update(bool force, bool notify)
Invalidate();
}
if (!fOnline && fHasBattery && previousPercent > kLowBatteryPercentage
&& fPercent <= kLowBatteryPercentage && notify) {
if (fPercent > kLowBatteryPercentage && fTimeLeft > kLowBatteryTimeLeft)
fHasNotifiedLowBattery = false;
bool justTurnedLowBattery = (previousPercent > kLowBatteryPercentage
&& fPercent <= kLowBatteryPercentage)
|| (fTimeLeft <= kLowBatteryTimeLeft
&& previousTimeLeft > kLowBatteryTimeLeft);
if (!fOnline && notify && fHasBattery
&& !fHasNotifiedLowBattery && justTurnedLowBattery) {
_NotifyLowBattery();
fHasNotifiedLowBattery = true;
}
if (fOnline && fPercent >= kFullBatteryPercentage
&& previousPercent < kFullBatteryPercentage) {
system_beep("Battery charged");
}
}
@ -585,6 +609,8 @@ PowerStatusView::_NotifyLowBattery()
BNotification notification(
fHasBattery ? B_INFORMATION_NOTIFICATION : B_ERROR_NOTIFICATION);
system_beep("Low battery");
if (fHasBattery) {
notification.SetTitle(B_TRANSLATE("Battery low"));
notification.SetContent(B_TRANSLATE(

View File

@ -68,6 +68,8 @@ protected:
time_t fTimeLeft;
bool fOnline;
bool fHasBattery;
bool fHasNotifiedLowBattery;
};