2020-04-26 11:30:30 +03:00
|
|
|
/**
|
|
|
|
* Battery Buddy, a Sailfish application to prolong battery lifetime
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019-2020 Matti Viljanen
|
|
|
|
*
|
|
|
|
* Battery Buddy is free software: you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Battery Buddy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU
|
|
|
|
* General Public License along with Battery Buddy. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Matti Viljanen
|
|
|
|
*/
|
|
|
|
#include "battery.h"
|
|
|
|
|
2020-04-26 18:20:28 +03:00
|
|
|
Battery::Battery(Settings *newSettings, QTimer *newUpdater, QTimer *newNotifier, Notification *newNotification, QObject *parent) : QObject(parent)
|
2020-04-26 11:30:30 +03:00
|
|
|
{
|
|
|
|
QString filename;
|
|
|
|
settings = newSettings;
|
2020-04-26 18:20:28 +03:00
|
|
|
updateTimer = newUpdater;
|
|
|
|
notifyTimer = newNotifier;
|
|
|
|
notification = newNotification;
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
// Number: charge percentage, e.g. 42
|
|
|
|
chargeFile = new QFile("/sys/class/power_supply/battery/capacity", this);
|
|
|
|
qInfo() << "Reading capacity from" << chargeFile->fileName();
|
|
|
|
|
|
|
|
// String: charging, discharging, full, empty, unknown (others?)
|
|
|
|
stateFile = new QFile("/sys/class/power_supply/battery/status", this);
|
|
|
|
qInfo() << "Reading charge state from" << stateFile->fileName();
|
|
|
|
|
|
|
|
// Number: 0 or 1
|
|
|
|
chargerConnectedFile = new QFile("/sys/class/power_supply/usb/present", this);
|
|
|
|
qInfo() << "Reading charger status from" << chargerConnectedFile->fileName();
|
|
|
|
|
|
|
|
// ENABLE/DISABLE CHARGING
|
|
|
|
if(QHostInfo::localHostName().contains("SailfishEmul")) {
|
2020-04-26 18:39:27 +03:00
|
|
|
qInfo() << "Sailfish SDK detected, not using charger control file";
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// e.g. for Sony Xperia XA2
|
|
|
|
filename = "/sys/class/power_supply/battery/input_suspend";
|
|
|
|
if(!chargingEnabledFile && QFile::exists(filename)) {
|
|
|
|
chargingEnabledFile = new QFile(filename, this);
|
|
|
|
enableChargingValue = 0;
|
|
|
|
disableChargingValue = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// e.g. for Sony Xperia Z3 Compact Tablet
|
|
|
|
filename = "/sys/class/power_supply/battery/charging_enabled";
|
|
|
|
if(!chargingEnabledFile && QFile::exists(filename)) {
|
|
|
|
chargingEnabledFile = new QFile(filename, this);
|
|
|
|
enableChargingValue = 1;
|
|
|
|
disableChargingValue = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// e.g. for Jolla Phone
|
|
|
|
filename = "/sys/class/power_supply/usb/charger_disable";
|
|
|
|
if(!chargingEnabledFile && QFile::exists(filename)) {
|
|
|
|
chargingEnabledFile = new QFile(filename, this);
|
|
|
|
enableChargingValue = 0;
|
|
|
|
disableChargingValue = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!chargingEnabledFile) {
|
|
|
|
qWarning() << "Charger control file not found!";
|
|
|
|
qWarning() << "Please contact the developer with your device model!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we found a usable file, check that it is writable
|
2020-06-12 14:16:46 +03:00
|
|
|
if(chargingEnabledFile) {
|
|
|
|
if(chargingEnabledFile->open(QIODevice::WriteOnly)) {
|
|
|
|
chargingEnabledFile->close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qWarning() << "Charger control file" << chargingEnabledFile->fileName() << "is not writable";
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
updateData();
|
2020-04-26 18:20:28 +03:00
|
|
|
|
|
|
|
connect(updateTimer, SIGNAL(timeout()), this, SLOT(updateData()));
|
|
|
|
connect(settings, SIGNAL(configChanged()), this, SLOT(updateConfig()));
|
|
|
|
connect(notifyTimer, SIGNAL(timeout()), this, SLOT(showNotification()));
|
|
|
|
|
|
|
|
updateConfig();
|
|
|
|
|
|
|
|
updateTimer->setInterval(5000);
|
|
|
|
updateTimer->start();
|
|
|
|
|
|
|
|
if(settings->getNotificationsEnabled()) {
|
|
|
|
showNotification();
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Battery::~Battery() { }
|
|
|
|
|
|
|
|
void Battery::updateData()
|
|
|
|
{
|
|
|
|
if(chargeFile->open(QIODevice::ReadOnly)) {
|
|
|
|
nextCharge = chargeFile->readLine().trimmed().toInt();
|
|
|
|
if(nextCharge != charge) {
|
|
|
|
charge = nextCharge;
|
|
|
|
emit chargeChanged(charge);
|
|
|
|
qDebug() << "Battery:" << charge;
|
|
|
|
}
|
|
|
|
chargeFile->close();
|
|
|
|
}
|
|
|
|
if(chargerConnectedFile->open(QIODevice::ReadOnly)) {
|
|
|
|
nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt();
|
|
|
|
if(nextChargerConnected != chargerConnected) {
|
|
|
|
chargerConnected = nextChargerConnected;
|
|
|
|
emit chargerConnectedChanged(chargerConnected);
|
|
|
|
qDebug() << "Charger is connected:" << chargerConnected;
|
|
|
|
}
|
|
|
|
chargerConnectedFile->close();
|
|
|
|
}
|
|
|
|
if(stateFile->open(QIODevice::ReadOnly)) {
|
|
|
|
nextState = (QString(stateFile->readLine().trimmed().toLower()));
|
|
|
|
if(nextState != state) {
|
|
|
|
state = nextState;
|
|
|
|
emit stateChanged(state);
|
|
|
|
qDebug() << "Charging status:" << state;
|
|
|
|
}
|
|
|
|
stateFile->close();
|
|
|
|
}
|
|
|
|
if(chargingEnabledFile && settings->getLimitEnabled()) {
|
|
|
|
if(chargingEnabled && charge >= settings->getHighLimit()) {
|
|
|
|
qDebug() << "Disabling";
|
|
|
|
setChargingEnabled(false);
|
|
|
|
}
|
|
|
|
else if(!chargingEnabled && charge <= settings->getLowLimit()) {
|
|
|
|
qDebug() << "Enabling";
|
|
|
|
setChargingEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:20:28 +03:00
|
|
|
void Battery::updateConfig() {
|
|
|
|
notifyTimer->stop();
|
|
|
|
notifyTimer->setInterval(settings->getInterval() * 1000);
|
|
|
|
if(settings->getNotificationsEnabled())
|
|
|
|
notifyTimer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Battery::showNotification() {
|
|
|
|
if(!settings->getNotificationsEnabled())
|
2020-06-12 01:04:11 +03:00
|
|
|
return;
|
2020-04-26 18:20:28 +03:00
|
|
|
|
2020-04-26 18:39:27 +03:00
|
|
|
qInfo() << "battery" << charge << "low" << settings->getLowAlert() << "high" << settings->getHighAlert() << "state" << state;
|
2020-04-26 18:20:28 +03:00
|
|
|
|
2020-06-01 18:56:03 +03:00
|
|
|
if(charge <= settings->getLowAlert() && state != "charging") {
|
2020-04-26 18:39:27 +03:00
|
|
|
qDebug() << "Battery notification timer: empty enough battery";
|
2020-04-26 18:20:28 +03:00
|
|
|
notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile());
|
|
|
|
}
|
2020-06-01 18:56:03 +03:00
|
|
|
else if((charge >= settings->getHighAlert() && state != "discharging")
|
|
|
|
&& !(charge == 100 && state == "idle")) {
|
2020-04-26 18:39:27 +03:00
|
|
|
qDebug() << "Battery notification timer: full enough battery";
|
2020-04-26 18:20:28 +03:00
|
|
|
notification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationHighText(), settings->getHighAlertFile());
|
|
|
|
}
|
|
|
|
else {
|
2020-04-26 18:39:27 +03:00
|
|
|
qDebug() << "Battery notification timer: close notification";
|
2020-04-26 18:20:28 +03:00
|
|
|
notification->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int Battery::getCharge() { return charge; }
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
QString Battery::getState() { return state; }
|
|
|
|
|
|
|
|
bool Battery::getChargingEnabled() { return chargingEnabled; }
|
|
|
|
|
2020-06-12 14:26:00 +03:00
|
|
|
bool Battery::setChargingEnabled(bool isEnabled) {
|
|
|
|
bool success = false;
|
2020-06-12 14:16:46 +03:00
|
|
|
if(chargingEnabledFile) {
|
|
|
|
if(chargingEnabledFile->open(QIODevice::WriteOnly)) {
|
|
|
|
if(chargingEnabledFile->write(QString("%1").arg(isEnabled ? enableChargingValue : disableChargingValue).toLatin1())) {
|
|
|
|
chargingEnabled = isEnabled;
|
|
|
|
emit chargingEnabledChanged(chargingEnabled);
|
2020-06-12 14:26:00 +03:00
|
|
|
success = true;
|
2020-06-12 14:16:46 +03:00
|
|
|
|
|
|
|
if(isEnabled) {
|
|
|
|
qInfo() << "Charging resumed";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qInfo() << "Charging paused";
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
else {
|
2020-06-12 14:16:46 +03:00
|
|
|
qWarning() << "Could not write new charger state";
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
2020-06-12 14:16:46 +03:00
|
|
|
chargingEnabledFile->close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qWarning() << "Could not open charger control file";
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-12 14:26:00 +03:00
|
|
|
return success;
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Battery::getChargerConnected() {
|
|
|
|
return chargerConnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Battery::shutdown() {
|
2020-04-26 18:20:28 +03:00
|
|
|
qDebug() << "\nPreparing for exit...";
|
|
|
|
blockSignals(true);
|
|
|
|
if(updateTimer) {
|
|
|
|
updateTimer->stop();
|
|
|
|
qDebug() << "Timer stopped";
|
|
|
|
}
|
2020-04-26 18:39:27 +03:00
|
|
|
notification->close();
|
|
|
|
if(notifyTimer) {
|
|
|
|
notifyTimer->stop();
|
|
|
|
qDebug() << "Notification stopped";
|
|
|
|
}
|
2020-06-12 14:32:25 +03:00
|
|
|
if(!setChargingEnabled(true)) {
|
|
|
|
qWarning() << "ERROR! Could not restore charger status! Your device" << endl
|
|
|
|
<< "may not start until reboot! If that doesn't help," << endl
|
|
|
|
<< "uninstall Battery Buddy and reboot your device.";
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|