/
batterynotifier.cpp
511 lines (474 loc) · 19.4 KB
/
batterynotifier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** Copyright (C) 2012-2019 Jolla Ltd.
** Copyright (c) 2019 Open Mobile Platform LLC.
**
** Contact: Robin Burchell <robin.burchell@jollamobile.com>
**
** This file is part of lipstick.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/
#include <QCoreApplication>
#include <QStringList>
#include <QTimer>
#include "notificationmanager.h"
#include "lipsticknotification.h"
#include "batterynotifier.h"
#include <time.h>
#include <keepalive/backgroundactivity.h>
// How much slack to include in keepalive wakeup ranges [s]
static const int HEARTBEAT_INTERVAL = 12;
// Delay range for repeating battery low warnings [s]
static const int LOW_BATTERY_INTERVAL_LOW = 30 * 60;
static const int LOW_BATTERY_INTERVAL_HIGH = (LOW_BATTERY_INTERVAL_LOW
+ HEARTBEAT_INTERVAL);
// Maximum expected delay between charger connect and start of charging [ms]
static const int CHARGING_FAILURE_DELAY = 3 * 1000;
// Delay between 1st property change notification and state evaluation [ms]
static const int STATE_EVALUATION_DELAY = 100;
BatteryNotifier::BatteryNotifier(QObject *parent)
: QObject(parent)
, m_lowBatteryRepeatLevel(0)
, m_notificationManager(NotificationManager::instance())
, m_mceChargerType(new QMceChargerType(this))
, m_mceChargerState(new QMceChargerState(this))
, m_mceBatteryStatus(new QMceBatteryStatus(this))
, m_mceBatteryLevel(new QMceBatteryLevel(this))
, m_mcePowerSaveMode(new QMcePowerSaveMode(this))
, m_mceDisplay(new QMceDisplay(this))
, m_mceTkLock(new QMceTkLock(this))
, m_mceCallState(new QMceCallState(this))
, m_usbModed(new QUsbModed(this))
, m_lowBatteryRepeatActivity(new BackgroundActivity(this))
{
connect(m_notificationManager, &NotificationManager::NotificationClosed,
this, &BatteryNotifier::onNotificationClosed);
connect(m_mceChargerType, &QMceChargerType::validChanged,
this, &BatteryNotifier::onChargerTypeChanged);
connect(m_mceChargerType, &QMceChargerType::typeChanged,
this, &BatteryNotifier::onChargerTypeChanged);
connect(m_mceChargerState, &QMceChargerState::validChanged,
this, &BatteryNotifier::onChargerStateChanged);
connect(m_mceChargerState, &QMceChargerState::chargingChanged,
this, &BatteryNotifier::onChargerStateChanged);
connect(m_mceBatteryStatus, &QMceBatteryStatus::validChanged,
this, &BatteryNotifier::onBatteryStatusChanged);
connect(m_mceBatteryStatus, &QMceBatteryStatus::statusChanged,
this, &BatteryNotifier::onBatteryStatusChanged);
connect(m_mceBatteryLevel, &QMceBatteryLevel::validChanged,
this, &BatteryNotifier::onBatteryLevelChanged);
connect(m_mceBatteryLevel, &QMceBatteryLevel::percentChanged,
this, &BatteryNotifier::onBatteryLevelChanged);
connect(m_mcePowerSaveMode, &QMcePowerSaveMode::validChanged,
this, &BatteryNotifier::onPowerSaveModeChanged);
connect(m_mcePowerSaveMode, &QMcePowerSaveMode::activeChanged,
this, &BatteryNotifier::onPowerSaveModeChanged);
connect(m_mceDisplay, &QMceDisplay::validChanged,
this, &BatteryNotifier::onDisplayChanged);
connect(m_mceDisplay, &QMceDisplay::stateChanged,
this, &BatteryNotifier::onDisplayChanged);
connect(m_mceTkLock, &QMceTkLock::validChanged,
this, &BatteryNotifier::onTkLockChanged);
connect(m_mceTkLock, &QMceTkLock::lockedChanged,
this, &BatteryNotifier::onTkLockChanged);
connect(m_mceCallState, &QMceCallState::validChanged,
this, &BatteryNotifier::onCallStateChanged);
connect(m_mceCallState, &QMceCallState::stateChanged,
this, &BatteryNotifier::onCallStateChanged);
connect(m_mceCallState, &QMceCallState::typeChanged,
this, &BatteryNotifier::onCallStateChanged);
connect(m_usbModed, &QUsbModed::targetModeChanged,
this, &BatteryNotifier::onTargetUsbModeChanged);
m_evaluateStateTimer.setInterval(STATE_EVALUATION_DELAY);
m_evaluateStateTimer.setSingleShot(true);
connect(&m_evaluateStateTimer, &QTimer::timeout,
this, &BatteryNotifier::onEvaluateStateTimeout);
m_chargingFailureTimer.setInterval(CHARGING_FAILURE_DELAY);
m_chargingFailureTimer.setSingleShot(true);
connect(&m_chargingFailureTimer, &QTimer::timeout,
this, &BatteryNotifier::onChargingFailureTimeout);
m_lowBatteryRepeatActivity->setWakeupRange(LOW_BATTERY_INTERVAL_LOW,
LOW_BATTERY_INTERVAL_HIGH);
connect(m_lowBatteryRepeatActivity, &BackgroundActivity::running,
this, &BatteryNotifier::onBatteryLowTimeout);
scheduleStateEvaluation();
}
BatteryNotifier::~BatteryNotifier()
{
}
void BatteryNotifier::onNotificationClosed(uint id, uint reason)
{
Q_UNUSED(reason);
for (QList<QueuedNotification>::iterator queuedNotification = m_notifications.begin();
queuedNotification != m_notifications.end();) {
if (queuedNotification->m_id == id)
queuedNotification = m_notifications.erase(queuedNotification);
else
++queuedNotification;
}
}
void BatteryNotifier::onChargerTypeChanged()
{
if (m_mceChargerType->valid()) {
m_currentState.m_chargerType = m_mceChargerType->type();
scheduleStateEvaluation();
}
}
void BatteryNotifier::onChargerStateChanged()
{
if (m_mceChargerState->valid()) {
m_currentState.m_chargerState = m_mceChargerState->charging();
scheduleStateEvaluation();
}
}
void BatteryNotifier::onBatteryStatusChanged()
{
if (m_mceBatteryStatus->valid()) {
m_currentState.m_batteryStatus = m_mceBatteryStatus->status();
scheduleStateEvaluation();
}
}
void BatteryNotifier::onBatteryLevelChanged()
{
if (m_mceBatteryLevel->valid()) {
m_currentState.m_batteryLevel = m_mceBatteryLevel->percent();
scheduleStateEvaluation();
}
}
void BatteryNotifier::onPowerSaveModeChanged()
{
if (m_mcePowerSaveMode->valid()) {
m_currentState.m_powerSaveMode = m_mcePowerSaveMode->active();
scheduleStateEvaluation();
}
}
void BatteryNotifier::onDisplayChanged()
{
if (m_mceDisplay->valid()) {
m_currentState.m_displayState = m_mceDisplay->state();
scheduleStateEvaluation();
}
}
void BatteryNotifier::onTkLockChanged()
{
if (m_mceTkLock->valid()) {
m_currentState.m_tkLock = m_mceTkLock->locked();
scheduleStateEvaluation();
}
}
void BatteryNotifier::onCallStateChanged()
{
if (m_mceCallState->valid()) {
m_currentState.m_callState = m_mceCallState->state();
m_currentState.m_callType = m_mceCallState->type();
scheduleStateEvaluation();
}
}
void BatteryNotifier::onTargetUsbModeChanged()
{
const QString mode(m_usbModed->targetMode());
if (m_currentState.m_usbMode != mode) {
m_currentState.m_usbMode = mode;
scheduleStateEvaluation();
}
}
void BatteryNotifier::onBatteryLowTimeout()
{
sendNotification(NotificationLowBattery);
m_lowBatteryRepeatLevel = m_currentState.m_batteryLevel - 2;
m_lowBatteryRepeatActivity->wait();
}
void BatteryNotifier::onChargingFailureTimeout()
{
sendNotification(NotificationChargingNotStarted);
}
void BatteryNotifier::onEvaluateStateTimeout()
{
NotificationTypeSet toRemove;
NotificationTypeList toSend;
updateDerivedProperties();
for (int type = NotificationFirst; type <= NotificationLast; ++type)
evaluateNotificationTriggering(static_cast<NotificationType>(type),
m_previousState, m_currentState,
toRemove, toSend);
removeNotifications(toRemove);
foreach(NotificationType type, toSend)
sendNotification(type);
m_previousState = m_currentState;
updateLowBatteryNotifier();
}
void BatteryNotifier::scheduleStateEvaluation()
{
if (!m_evaluateStateTimer.isActive())
m_evaluateStateTimer.start();
}
void BatteryNotifier::updateDerivedProperties()
{
/* Update minimum battery level we expect to see while charging.
*
* While discharging / doing battery full maintenance charging:
* -> track current battery level value
*
* While charging:
* -> track maximum battery level value
*
* Allow one to two percent drops to make false positives less likely.
*/
if (m_currentState.m_chargerState == false
|| m_currentState.m_batteryStatus == QMceBatteryStatus::Full
|| m_currentState.m_batteryLevel > m_currentState.m_minimumBatteryLevel)
m_currentState.m_minimumBatteryLevel = m_currentState.m_batteryLevel - 1;
/* Suppress / remove charging notifications when it is expected
* that usb mode selection specific notifications will pop up.
*
* Due to dynamic nature of available usb modes, the condition:
* "suppress on dynamic mode activation"
* needs to be implemented in sort of reversed logic:
* "suppress when target mode is not one of known built-in modes"
*
* Basically:
*
* - Undefined (disconnected), Charger (wall charger), Charging
* (connected to pc, for charging only): Notifications in
* these situatios are handled here -> no suppressing.
*
* - ChargingFallback: Shows up when device lock is preventing
* either Ask or automatic mode selection -> usb mode handling
* will issue "unlock first" notification -> suppress charging
* notification to make room for it.
*
* - Ask: Mode selection dialog is on screen, for now it is
* assumed that it is ok to show charging notification banner
* on top of it -> no suppressing
*
* - Everything else: Assume it is dynamic mode, that will have
* an associated notification from usb mode handling -> suppress
* charging notifications
*/
const QString mode(m_currentState.m_usbMode);
m_currentState.m_suppressCharging = (mode != QUsbMode::Mode::Undefined
&& mode != QUsbMode::Mode::Ask
&& mode != QUsbMode::Mode::Charging
&& mode != QUsbMode::Mode::Charger);
}
bool BatteryNotifier::notificationTriggeringEdge(BatteryNotifier::NotificationType type)
{
switch (type) {
case BatteryNotifier::NotificationRemoveCharger:
return false;
default:
return true;
}
}
bool BatteryNotifier::evaluateNotificationLevel(BatteryNotifier::NotificationType type,
const BatteryNotifier::State &state)
{
bool level = false;
switch (type) {
case BatteryNotifier::NotificationCharging:
/* Charging, battery is not full, and we are not expecting
* usb-mode related notifications. */
level = (state.m_chargerState == true
&& state.m_batteryStatus != QMceBatteryStatus::Full
&& state.m_suppressCharging == false);
break;
case BatteryNotifier::NotificationChargingComplete:
/* Battery is full (implies charging), and we are not
* expecting usb-mode related notifications. */
level = (state.m_batteryStatus == QMceBatteryStatus::Full
&& state.m_suppressCharging == false);
break;
case BatteryNotifier::NotificationRechargeBattery:
/* Battery empty (implies not charging) */
level = (state.m_batteryStatus == QMceBatteryStatus::Empty);
break;
case BatteryNotifier::NotificationLowBattery:
/* Battery low (implies not charging) */
level = (state.m_batteryStatus == QMceBatteryStatus::Low);
break;
case BatteryNotifier::NotificationRemoveCharger:
/* Condition is "connected to wall charger", but we trigger
* on falling edge i.e. on wall charger disconnect. */
level = (state.m_chargerType == QMceChargerType::DCP
|| state.m_chargerType == QMceChargerType::HVDCP);
break;
case BatteryNotifier::NotificationChargingNotStarted:
/* Charger is connected, but charging has not commenced
* within expected time frame. */
level = (state.m_chargerType != QMceChargerType::None
&& state.m_chargerState == false);
break;
case BatteryNotifier::NotificationEnteringPSM:
level = (state.m_powerSaveMode == true);
break;
case BatteryNotifier::NotificationExitingPSM:
level = (state.m_powerSaveMode == false);
break;
case BatteryNotifier::NotificationNotEnoughPower:
/* Battery level has dropped since charger was connected. */
level = (state.m_batteryLevel < state.m_minimumBatteryLevel);
break;
}
return level;
}
void BatteryNotifier::evaluateNotificationTriggering(NotificationType type,
const State &previousState,
const State ¤tState,
NotificationTypeSet &toRemove,
NotificationTypeList &toSend)
{
bool previousLevel = evaluateNotificationLevel(type, previousState);
bool currentLevel = evaluateNotificationLevel(type, currentState);
if (previousLevel != currentLevel) {
if (currentLevel == notificationTriggeringEdge(type)) {
switch (type) {
case NotificationLowBattery:
startLowBatteryNotifier();
break;
case NotificationChargingNotStarted:
m_chargingFailureTimer.start();
break;
case NotificationCharging:
/* Make sure 'disconnect charger' notification gets
* hidden also on connect to pc */
toRemove << NotificationRemoveCharger;
toSend << type;
break;
default:
toSend << type;
break;
}
} else {
switch (type) {
case NotificationLowBattery:
stopLowBatteryNotifier();
break;
case NotificationChargingNotStarted:
m_chargingFailureTimer.stop();
break;
default:
break;
}
toRemove << type;
}
}
}
void BatteryNotifier::sendNotification(BatteryNotifier::NotificationType type)
{
static const struct NotificationInfo {
QString category;
QString message;
QString icon;
} description[] = {
{"x-nemo.battery", // NotificationCharging
//% "Charging"
qtTrId("qtn_ener_charging"),
""},
{"x-nemo.battery.chargingcomplete", // NotificationChargingComplete
//% "Charging complete"
qtTrId("qtn_ener_charcomp"),
""},
{"x-nemo.battery.removecharger", // NotificationRemoveCharger
//% "Disconnect charger from power supply to save energy"
qtTrId("qtn_ener_remcha"),
""},
{"x-nemo.battery.chargingnotstarted", // NotificationChargingNotStarted
//% "Charging not started. Replace charger."
qtTrId("qtn_ener_repcharger"),
""},
{"x-nemo.battery.recharge", // NotificationRechargeBattery
//% "Recharge battery"
qtTrId("qtn_ener_rebatt"),
""},
{"x-nemo.battery.enterpsm", // NotificationEnteringPSM
//% "Entering power save mode"
qtTrId("qtn_ener_ent_psnote"),
""},
{"x-nemo.battery.exitpsm", // NotificationExitingPSM
//% "Exiting power save mode"
qtTrId("qtn_ener_exit_psnote"),
""},
{"x-nemo.battery.lowbattery", // NotificationLowBattery
//% "Low battery"
qtTrId("qtn_ener_lowbatt"),
""},
{"x-nemo.battery.notenoughpower", // NotificationNotEnoughPower
//% "Not enough power to charge"
qtTrId("qtn_ener_nopowcharge"),
"icon-m-energy-management-insufficient-power"}
};
Q_ASSERT(type < sizeof(description) / sizeof(description[0]));
NotificationInfo const &info = description[type];
/* Purge any existing notification items of the same type */
for (QList<QueuedNotification>::iterator queuedNotification = m_notifications.begin();
queuedNotification != m_notifications.end();) {
if (queuedNotification->m_type == type) {
uint id = queuedNotification->m_id;
queuedNotification = m_notifications.erase(queuedNotification);
m_notificationManager->CloseNotification(id);
} else {
++queuedNotification;
}
}
/* Add fresh notification item */
QVariantHash hints;
hints.insert(LipstickNotification::HINT_CATEGORY, info.category);
hints.insert(LipstickNotification::HINT_PREVIEW_BODY, info.message);
hints.insert(LipstickNotification::HINT_VISIBILITY, QLatin1String("public"));
QueuedNotification queuedNotification;
queuedNotification.m_type = type;
queuedNotification.m_id = m_notificationManager->Notify(m_notificationManager->systemApplicationName(),
0,
info.icon,
QString(),
QString(),
QStringList(),
hints,
-1);
m_notifications.push_back(queuedNotification);
}
void BatteryNotifier::removeNotifications(const NotificationTypeSet &toRemove)
{
for (QList<QueuedNotification>::iterator queuedNotification = m_notifications.begin();
queuedNotification != m_notifications.end();) {
if (toRemove.contains(queuedNotification->m_type)) {
uint id = queuedNotification->m_id;
queuedNotification = m_notifications.erase(queuedNotification);
m_notificationManager->CloseNotification(id);
} else {
++queuedNotification;
}
}
}
void BatteryNotifier::startLowBatteryNotifier()
{
m_lowBatteryRepeatActivity->run();
}
void BatteryNotifier::stopLowBatteryNotifier()
{
m_lowBatteryRepeatActivity->stop();
}
void BatteryNotifier::updateLowBatteryNotifier()
{
if (m_lowBatteryRepeatActivity->isWaiting()) {
/* We have ongoing battery low warning repeat cycle */
bool active = (m_currentState.m_displayState != QMceDisplay::DisplayOff
&& m_currentState.m_tkLock == false);
bool incall = m_currentState.m_callState != QMceCallState::None;
if (active || incall) {
/* Device is in "active use" */
if (m_currentState.m_batteryLevel <= m_lowBatteryRepeatLevel) {
/* Significant battery level drop since the last warning
* -> repeat the warning immediately. */
m_lowBatteryRepeatActivity->run();
}
}
}
}