summaryrefslogtreecommitdiff
path: root/gui/androidutils.cpp
blob: ac0ca2630c636e5a7459212f129a9b3ba07c9d87 (plain)
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
#include "androidutils.h"
#include "androiddevicedialog.h"

#include <QCoreApplication>
#include <QByteArray>
#include <QFileInfo>
#include <QProcess>
#include <QSettings>
#include <QStringList>

typedef QLatin1String _;

static QString s_androidSerialNumber;

QStringList AndroidDeviceInfo::adbSelector(const QString &serialNumber)
{
    if (serialNumber.startsWith(_("????")))
        return QStringList() << _("-d");
    return QStringList() << _("-s") << serialNumber;
}

QString AndroidDeviceInfo::adbPath()
{
    QSettings s;
    s.beginGroup(_("android"));
    return s.value(_("adbPath"), _("adb")).toString();
}

AndroidUtils::FileInfo::FileInfo()
{
    type = File;
    size = 0;
}

AndroidUtils::AndroidUtils()
{
    m_wait = 30000;
    reloadAdb();
}

void AndroidUtils::reloadAdb()
{
    m_adbPath = AndroidDeviceInfo::adbPath();
}

QString AndroidUtils::serialNumber() const
{
    if (m_serialNumber.isEmpty())
        m_serialNumber = s_androidSerialNumber;

    if (m_serialNumber.isEmpty()) {
        AndroidDeviceDialog ad;
        if (ad.exec() != QDialog::Accepted)
            return QString();

        m_serialNumber = ad.device().serialNumber;
        if (ad.saveDeviceSelection())
            s_androidSerialNumber = m_serialNumber;
    }
    return m_serialNumber;
}

bool AndroidUtils::runAdb(const QStringList &params, QByteArray *output) const
{
    QProcess adbProcess;
    adbProcess.start(m_adbPath, AndroidDeviceInfo::adbSelector(serialNumber()) << params);
    if (!output)
        connect(&adbProcess, SIGNAL(readyReadStandardError()), SLOT(processReadyRead()));

    if (!adbProcess.waitForStarted())
        return false;

    if (!adbProcess.waitForFinished(m_wait > INT_MAX ? -1 : m_wait))
        return false;

    if (output)
        *output = adbProcess.readAll();
    return true;
}

AndroidUtils::FileInfoList AndroidUtils::ls(const QString &path) const
{
    m_wait = 10000;
    FileInfoList ret;
    QByteArray lsOutput;
    runAdb(QStringList(_("shell")) << _("ls") << _("-l") << path, &lsOutput);
    foreach (const QByteArray &line, lsOutput.split('\n')) {
        if (line.isEmpty() || line[10] != ' ')
            continue;

        int pos = line.indexOf(':');
        if (pos == -1)
            continue;
        pos += 3;
        if (pos >= line.size())
            continue;

        FileInfo f;
        // Assume all links are directories
        f.type = (line[0] == 'd' || line[0] == 'l') ? FileInfo::Directory : FileInfo::File;
        f.name = line.mid(pos).trimmed();
        if (f.type == FileInfo::File) {
            pos = line.lastIndexOf('-', pos);
            if (pos < 4)
                continue;
            pos = line.lastIndexOf('-', pos -1);
            if (pos < 3)
                continue;
            pos = line.lastIndexOf(' ', pos -1);
            if (pos < 2)
                continue;
            int startPos = line.lastIndexOf(' ', pos -1);
            if (startPos == -1)
                continue;
            f.size = line.mid(startPos, pos - startPos).trimmed().toLongLong();
        } else if (line[0] == 'l') {
            // Fix links
            pos = f.name.indexOf(_("->"));
            if (pos < 2)
                continue;
            f.name = f.name.left(pos - 1).trimmed();
        }
        ret << f;
    }
    return ret;
}

bool AndroidUtils::sameFile(const QString &localPath, const QString &devicePath)
{
    QFileInfo fi(localPath);
    QList<FileInfo> deviceFiles = ls(devicePath);
    return fi.exists() && (deviceFiles.size() == 1) && (deviceFiles.first().size == fi.size());
}

bool AndroidUtils::updateFile(const QString &localPath, const QString &devicePath, UpdateType updateType)
{
    if (sameFile(localPath, devicePath))
        return true;

    QFileInfo fi(localPath);
    QList<FileInfo> deviceFiles = ls(devicePath);

    switch (updateType) {
    case PushLocalToDevice:
        if (!fi.exists())
            return false;

        m_wait = std::min(fi.size() / 1024, qint64(30000));
        if (!runAdb(QStringList()<< _("push") << _("-p") << localPath << devicePath))
            return false;

        deviceFiles = ls(devicePath);

        if (deviceFiles.size() != 1 || deviceFiles.first().size != fi.size())
            return false; // pushing to device failed
        break;

    case PullFromDeviceToLocal:
        if (deviceFiles.size() != 1)
            return false;

        m_wait = std::min(deviceFiles.at(0).size / 1024, qint64(30000));
        if (!runAdb(QStringList()<< _("pull") << _("-p") << devicePath << localPath))
            return false;

        fi = QFileInfo(localPath);
        if (!fi.exists() || deviceFiles.first().size != fi.size())
            return false; // pulling from device failed
        break;
    }
    return true;
}

void AndroidUtils::processReadyRead()
{
    QProcess *proc = qobject_cast<QProcess *>(QObject::sender());
    if (proc) {
        QByteArray stdOutErr = proc->readAllStandardError();
        foreach (const QByteArray &line,  stdOutErr.split('\r')) {
            QString msg(line.trimmed());
            if (!msg.isEmpty())
                emit statusMessage(msg);
        }
    }
}