Implement battery alerts in Reader framework

This commit is contained in:
Nicolas Mailloux 2021-04-23 19:22:53 -04:00
parent 99676137e4
commit 9163689cac
6 changed files with 84 additions and 51 deletions

View file

@ -60,7 +60,7 @@ alert::alert(QWidget *parent) :
ui->warningLabel->setText("Please charge your eReader.");
ui->securityLabel->setText("The battery's charge level is critical.");
ui->messageLabel->setText("To prevent filesystem damage, your device has been turned off.\nPlease consider charging it.");
poweroff(false);
//poweroff(false);
}
ui->warningLabel->setStyleSheet("QLabel { background-color : black; color : white; font-size: 16pt}");

View file

@ -20,6 +20,7 @@ namespace global {
inline int pageNumber;
inline int bookNumber;
inline bool skipOpenDialog;
inline bool startBatteryWatchdog;
}
namespace mainwindow {
inline bool updateDialog;

View file

@ -58,6 +58,10 @@ int main(int argc, char *argv[])
}
// If we're waking from sleep and we have the lockscreen enabled, we'll "resume" the book from scratch
if(checkconfig("/tmp/suspendBook") == true) {
// Start the low/critical battery alert timer from the Reader framework since MainWindow is not going to be shown
global::reader::startBatteryWatchdog = true;
global::reader::skipOpenDialog = true;
string_writeconfig("/inkbox/skip_opendialog", "true");
QApplication a(argc, argv);
reader w;

View file

@ -294,6 +294,7 @@ MainWindow::MainWindow(QWidget *parent)
t->start();
}
// Battery watchdog
QTimer *batteryWatchdog = new QTimer(this);
batteryWatchdog->setInterval(2000);
connect(batteryWatchdog, &QTimer::timeout, [&]() {

View file

@ -24,6 +24,7 @@ reader::reader(QWidget *parent) :
{
// Variables
global::battery::showLowBatteryDialog = true;
global::battery::showCriticalBatteryAlert = true;
ui->setupUi(this);
ui->previousBtn->setProperty("type", "borderless");
@ -218,20 +219,6 @@ reader::reader(QWidget *parent) :
get_battery_level();
ui->batteryLabel->setText(batt_level);
ui->timeLabel->setText(time);
if(global::battery::showLowBatteryDialog != true) {
// Do nothing, since a dialog should already have been displayed and (probably) dismissed
;
}
else {
if(global::battery::batteryAlertLock == true) {
;
}
else {
if(isBatteryLow() == true) {
openLowBatteryDialog();
}
}
}
} );
t->start();
}
@ -243,20 +230,6 @@ reader::reader(QWidget *parent) :
get_battery_level();
ui->batteryLabel->setText(batt_level);
ui->timeLabel->setText(time);
if(global::battery::showLowBatteryDialog != true) {
// Do nothing, since a dialog should already have been displayed and (probably) dismissed
;
}
else {
if(global::battery::batteryAlertLock == true) {
;
}
else {
if(isBatteryLow() == true) {
openLowBatteryDialog();
}
}
}
} );
t->start();
}
@ -301,28 +274,30 @@ reader::reader(QWidget *parent) :
select_t->start();
// We have to get the file's path
if(checkconfig("/tmp/suspendBook") == true) {
wakeFromSleep = true;
// Prevent from opening the Reader framework next time unless the condition is reset
string_writeconfig("/tmp/suspendBook", "false");
book_file = "/inkbox/book/book.txt";
}
if(global::reader::skipOpenDialog == true) {
if(global::reader::bookNumber == 1) {
string_checkconfig(".config/08-recent_books/1");
book_file = checkconfig_str_val;
if(checkconfig("/tmp/suspendBook") == true) {
wakeFromSleep = true;
// Prevent from opening the Reader framework next time unless the condition is reset
string_writeconfig("/tmp/suspendBook", "false");
book_file = "/inkbox/book/book.txt";
}
if(global::reader::bookNumber == 2) {
string_checkconfig(".config/08-recent_books/2");
book_file = checkconfig_str_val;
}
if(global::reader::bookNumber == 3) {
string_checkconfig(".config/08-recent_books/3");
book_file = checkconfig_str_val;
}
if(global::reader::bookNumber == 4) {
string_checkconfig(".config/08-recent_books/4");
book_file = checkconfig_str_val;
else {
if(global::reader::bookNumber == 1) {
string_checkconfig(".config/08-recent_books/1");
book_file = checkconfig_str_val;
}
if(global::reader::bookNumber == 2) {
string_checkconfig(".config/08-recent_books/2");
book_file = checkconfig_str_val;
}
if(global::reader::bookNumber == 3) {
string_checkconfig(".config/08-recent_books/3");
book_file = checkconfig_str_val;
}
if(global::reader::bookNumber == 4) {
string_checkconfig(".config/08-recent_books/4");
book_file = checkconfig_str_val;
}
}
}
else {
@ -471,6 +446,47 @@ reader::reader(QWidget *parent) :
string_writeconfig(".config/08-recent_books/3", str_book_2);
string_writeconfig(".config/08-recent_books/4", str_book_3);
}
// Battery watchdog
if(global::reader::startBatteryWatchdog == true) {
QTimer *t = new QTimer(this);
t->setInterval(2000);
connect(t, &QTimer::timeout, [&]() {
// Checking if battery level is low
if(global::battery::showCriticalBatteryAlert != true) {
;
}
else {
if(isBatteryCritical() == true) {
qDebug() << "Warning! Battery is at a critical charge level!";
openCriticalBatteryAlertWindow();
}
}
if(global::battery::showLowBatteryDialog != true) {
// Do nothing, since a dialog should already have been displayed and (probably) dismissed
;
}
else {
if(isBatteryLow() == true) {
if(global::battery::batteryAlertLock == true) {
;
}
else {
qDebug() << "Warning! Battery is low!";
string_checkconfig_ro("/sys/devices/platform/pmic_battery.1/power_supply/mc13892_bat/status");
if(checkconfig_str_val == "Charging\n") {
;
}
else {
openLowBatteryDialog();
}
}
}
}
} );
t->start();
}
}
reader::~reader()
@ -1065,11 +1081,21 @@ void reader::quit_restart() {
}
void reader::openLowBatteryDialog() {
global::battery::batteryAlertLock = true;
global::battery::showLowBatteryDialog = false;
global::mainwindow::lowBatteryDialog = true;
global::battery::batteryAlertLock = true;
generalDialogWindow = new generalDialog(this);
generalDialogWindow->setAttribute(Qt::WA_DeleteOnClose);
generalDialogWindow->show();
QApplication::processEvents();
}
void reader::openCriticalBatteryAlertWindow() {
global::battery::showCriticalBatteryAlert = true;
global::battery::showLowBatteryDialog = false;
alertWindow = new alert();
alertWindow->setAttribute(Qt::WA_DeleteOnClose);
alertWindow->setGeometry(QRect(QPoint(0,0), screen()->geometry ().size()));
alertWindow->show();
}

View file

@ -68,6 +68,7 @@ public:
void wordwidget_show();
void wordwidget_hide();
void openLowBatteryDialog();
void openCriticalBatteryAlertWindow();
private slots:
void on_nextBtn_clicked();