From b79d2e9c18ffcaaa73df03ff908a62fc7603bba5 Mon Sep 17 00:00:00 2001 From: Matt Porter Date: Sat, 11 Nov 2017 23:36:34 -0500 Subject: Move call timer functionality from QML into the C++ Phone class Removes the QML call timer implementation in favor of a C++ implementation in the Phone class. This allows the call timer to be started even if the QML application is in the background (QSG RenderThread not scheduled). Bug-AGL: SPEC-1083 Change-Id: I0cb9087d73862992d25b105f97b830eef5c83ef0 Signed-off-by: Matt Porter --- app/Dialer.qml | 25 +++++-------------------- app/main.cpp | 1 + app/phone.cpp | 21 +++++++++++++++++++-- app/phone.h | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/app/Dialer.qml b/app/Dialer.qml index afeea18..0bf51cf 100644 --- a/app/Dialer.qml +++ b/app/Dialer.qml @@ -25,24 +25,12 @@ import 'models' Item { id: root - function getTime() { - return new Date().getTime() - } - - // Elapsed time in hh:mm:ss format - function getElapsedTimeString(startTime) { - var seconds = Math.floor((getTime() - startTime) / 1000); - var time = new Date(null); - time.setSeconds(seconds); - return time.toISOString().substr(11, 8); - } + Connections { + target: phone - Timer { - id: callTimer - interval: 1000 - repeat: true - property var startTime - onTriggered: callStateLabel.text = getElapsedTimeString(startTime) + onElapsedTimeChanged: { + callStateLabel.text = phone.elapsedTime + } } Connections { @@ -60,12 +48,9 @@ Item { callStateLabel.text = "Dialing " + telephony.callColp } else if (telephony.callState == "active") { rejectButton.active = false - callTimer.startTime = getTime() - callTimer.restart() } else if (telephony.callState == "disconnected") { rejectButton.active = false callButton.checked = false - callTimer.stop() callStateLabel.text = "" } } diff --git a/app/main.cpp b/app/main.cpp index e2eaf7c..09e501f 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -70,6 +70,7 @@ int main(int argc, char *argv[]) Telephony *telephony = new Telephony(bindingAddress); context->setContextProperty("telephony", telephony); Phone *phone = new Phone(telephony); + context->setContextProperty("phone", phone); QObject::connect(telephony, &Telephony::callStateChanged, phone, &Phone::onCallStateChanged); } diff --git a/app/phone.cpp b/app/phone.cpp index 4eca02a..254fe24 100644 --- a/app/phone.cpp +++ b/app/phone.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include "phone.h" @@ -28,12 +29,28 @@ Phone::Phone(Telephony *telephony, QObject *parent) : m_ringtone.setLoopCount(QSoundEffect::Infinite); QObject::connect(telephony, &Telephony::callStateChanged, this, &Phone::onCallStateChanged); + + m_call_timer.setInterval(1000); + m_call_timer.setSingleShot(false); + connect(&m_call_timer, SIGNAL(timeout()), this, SLOT(updateElapsedTime())); } void Phone::onCallStateChanged(QString callState) { - if ((callState == "disconnected") || (callState == "active")) + if (callState == "disconnected") { + m_ringtone.stop(); + m_call_timer.stop(); + } else if (callState == "active") { m_ringtone.stop(); - else if (callState == "incoming") + m_date_time = m_date_time.fromSecsSinceEpoch(0); + setElapsedTime("00:00:00"); + m_call_timer.start(); + } else if (callState == "incoming") { m_ringtone.play(); + } +} + +void Phone::updateElapsedTime() { + m_date_time = m_date_time.addSecs(1); + setElapsedTime(m_date_time.toString(Qt::ISODate).right(8)); } diff --git a/app/phone.h b/app/phone.h index 2975daf..489f1da 100644 --- a/app/phone.h +++ b/app/phone.h @@ -18,20 +18,38 @@ #define PHONE_H #include +#include #include class Phone : public QObject { Q_OBJECT + Q_PROPERTY(QString elapsedTime READ elapsedTime WRITE setElapsedTime NOTIFY elapsedTimeChanged) public: explicit Phone(Telephony *telephony, QObject *parent = Q_NULLPTR); void onCallStateChanged(QString); + QString elapsedTime() { return m_elapsed_time; } + void setElapsedTime(QString elapsedTime) + { + m_elapsed_time = elapsedTime; + emit elapsedTimeChanged(m_elapsed_time); + } + + public slots: + void updateElapsedTime(); + + signals: + void elapsedTimeChanged(QString elapsedTime); + private: Telephony *m_telephony; QSoundEffect m_ringtone; + QTimer m_call_timer; + QDateTime m_date_time; + QString m_elapsed_time; }; #endif // PHONE_H -- cgit 1.2.3-korg