Commit 67e4b170 authored by Karel Slaný's avatar Karel Slaný
Browse files

Added inactivity timer support into Locker class.

parent c5e4d4b8
......@@ -21,16 +21,83 @@
* the two.
*/
#include <QDebug>
#include <QEvent>
#include "src/locker.h"
/*
* Application behaviour on supported platforms:
*
* Android:
* on close (not terminate): Active -(immediately)-> Inactive -(short time)-> Suspended
* on attachment open: Active -(immediately)-> Inactive -(short time)-> Suspended
*
* iOS:
* on close (not terminate): Active -(immediately)-> Inactive -(short time)-> Suspended
* on attachment open: remains Active
*
* Don't know how to achieve hidden state.
*/
Locker::Locker(QObject *parent)
: QObject(parent)
: QObject(parent),
m_inactivityTimer(),
m_ignoreImmediateSuspension(false)
{
m_inactivityTimer.setInterval(10000); /* 10 s */
connect(&m_inactivityTimer, SIGNAL(timeout()),
this, SLOT(inactivityTimeOut()));
}
void Locker::ignoreNextSuspension(void)
{
m_ignoreImmediateSuspension = true;
}
void Locker::processNewState(Qt::ApplicationState state)
{
qDebug() << "New state" << state;
switch (state) {
case Qt::ApplicationSuspended:
case Qt::ApplicationHidden:
if (!m_ignoreImmediateSuspension) {
emit lockApp();
} else {
/* Was ignored once. */
m_ignoreImmediateSuspension = false;
}
return;
break;
case Qt::ApplicationInactive:
case Qt::ApplicationActive:
/* Let the timer handle the locking. */
break;
default:
Q_ASSERT(0);
return;
break;
}
}
void Locker::inactivityTimeOut(void)
{
/* Inactivity timer timed out. */
emit lockApp();
}
bool Locker::eventFilter(QObject *watched, QEvent *event)
{
switch (event->type()) {
case QEvent::MouseMove:
case QEvent::KeyPress:
case QEvent::TouchBegin:
m_inactivityTimer.stop(); /* Reset the timer. */
m_inactivityTimer.start();
break;
default:
/* Do nothing. */
break;
}
/* Standard event handling. */
return QObject::eventFilter(watched, event);
}
......@@ -25,17 +25,69 @@
#define _LOCKER_H_
#include <QObject>
#include <QTimer>
/*!
* @brief Application locker class.
*/
class Locker : public QObject {
Q_OBJECT
public:
/*!
* @brief Constructor.
*
* @param[in] parent Parent object.
*/
Locker(QObject *parent = Q_NULLPTR);
/*!
* @brief Sets internal flag. Following immediate application
* suspension call will be ignored.
*
* @note This is useful when the control is handed over to another
* application (e.g. when opening an attachment in associated
* application).
*/
Q_INVOKABLE
void ignoreNextSuspension(void);
public slots:
/*!
* @brief Processes supplied application state. Emits lockApp signal
* if suspension state achieved that should not be ignored.
*/
void processNewState(Qt::ApplicationState state);
private slots:
/*!
* @brief Emits lockApp signal.
*/
void inactivityTimeOut(void);
signals:
/*!
* @brief Signal emitted when the application should be locked.
*/
void lockApp(void);
protected:
/*!
* @brief Watches for MouseMove, KeyPress and TouchBegin events. Resets
* inactivity timer on those events.
*
* @param[in] watched Watched object.
* @param[in] event Incomming event.
* @return Returns whatever the parent class would return.
*/
virtual
bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE;
private:
QTimer m_inactivityTimer; /*!< Brief inactivity timer. */
bool m_ignoreImmediateSuspension; /*!<
* Set true if next suspension should
* be ignored.
*/
};
#endif /* _LOCKER_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment