2
* Copyright 2014 Canonical Ltd.
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation; version 2.1.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Author: Benjamin Zeller <benjamin.zeller@canonical.com>
18
#include "ubuntuemulatornotifier.h"
21
#include <QRegularExpression>
27
* \class UbuntuEmulatorNotifier
28
* Polls adb if a specific
31
UbuntuEmulatorNotifier::UbuntuEmulatorNotifier(QObject *parent) :
32
IUbuntuDeviceNotifier(parent)
34
m_pollTimout = new QTimer(this);
35
m_pollProcess = new QProcess(this);
37
connect(m_pollTimout,SIGNAL(timeout()),this,SLOT(pollTimeout()));
38
connect(m_pollProcess,SIGNAL(readyRead()),this,SLOT(pollProcessReadyRead()));
39
connect(m_pollProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(pollProcessFinished(int,QProcess::ExitStatus)));
42
void UbuntuEmulatorNotifier::startMonitoring(const QString &serialNumber)
44
m_serial = serialNumber;
45
m_pollTimout->setInterval(5000);
46
m_pollTimout->start();
49
void UbuntuEmulatorNotifier::stopMonitoring()
52
m_pollProcess->kill();
55
bool UbuntuEmulatorNotifier::isConnected() const
57
return (m_lastState == QLatin1String("device"));
60
void UbuntuEmulatorNotifier::pollTimeout()
63
m_pollProcess->start(QLatin1String("adb"),
67
<<QLatin1String("get-state"));
70
void UbuntuEmulatorNotifier::pollProcessReadyRead()
72
m_buffer.append(m_pollProcess->readAllStandardOutput());
75
void UbuntuEmulatorNotifier::pollProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
77
if (exitCode == 0 && exitStatus == QProcess::NormalExit) {
78
QString str = QString::fromLocal8Bit(m_buffer.data());
80
QRegularExpression expr(QLatin1String("(device|unknown|offline|bootloader)"));
81
QRegularExpressionMatch match = expr.match(str);
82
if(match.hasMatch()) {
83
bool wasConnected = isConnected();
84
QString newState = match.captured(1);
85
if(newState != m_lastState) {
86
m_lastState = newState;
87
if(isConnected() && !wasConnected)
88
emit deviceConnected();
89
else if(!isConnected() && wasConnected)
90
emit deviceDisconnected();
97
} // namespace Internal