androidconfigurations.cpp 22.5 KB
Newer Older
BogDan Vatra's avatar
BogDan Vatra committed
1 2 3 4 5 6
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 BogDan Vatra <bog_dan_ro@yahoo.com>
**
Eike Ziller's avatar
Eike Ziller committed
7
** Contact: http://www.qt-project.org/
BogDan Vatra's avatar
BogDan Vatra committed
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
**
**
** GNU Lesser General Public License Usage
**
** This file may be used 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.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**************************************************************************/

#include "androidconfigurations.h"
#include "androidconstants.h"
#include "ui_addnewavddialog.h"

#include <coreplugin/icore.h>
#include <utils/persistentsettings.h>

#include <QDateTime>
#include <QSettings>
#include <QStringList>
#include <QProcess>
#include <QFileInfo>
#include <QDirIterator>
#include <QMetaObject>

#include <QStringListModel>
#include <QMessageBox>

#if defined(_WIN32)
#include <iostream>
#include <windows.h>
#define sleep(_n) Sleep(1000 * (_n))
Thiago Macieira's avatar
Thiago Macieira committed
53 54
#else
#include <unistd.h>
BogDan Vatra's avatar
BogDan Vatra committed
55 56
#endif

hjk's avatar
hjk committed
57
using namespace ProjectExplorer;
BogDan Vatra's avatar
BogDan Vatra committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
using namespace Utils;

namespace Android {
namespace Internal {

namespace {
    const QLatin1String SettingsGroup("AndroidConfigurations");
    const QLatin1String SDKLocationKey("SDKLocation");
    const QLatin1String NDKLocationKey("NDKLocation");
    const QLatin1String NDKToolchainVersionKey("NDKToolchainVersion");
    const QLatin1String AntLocationKey("AntLocation");
    const QLatin1String ArmGdbLocationKey("GdbLocation");
    const QLatin1String ArmGdbserverLocationKey("GdbserverLocation");
    const QLatin1String X86GdbLocationKey("X86GdbLocation");
    const QLatin1String X86GdbserverLocationKey("X86GdbserverLocation");
    const QLatin1String OpenJDKLocationKey("OpenJDKLocation");
    const QLatin1String KeystoreLocationKey("KeystoreLocation");
    const QLatin1String PartitionSizeKey("PartitionSize");
76
    const QLatin1String NDKGccVersionRegExp("-\\d[\\.\\d]+");
BogDan Vatra's avatar
BogDan Vatra committed
77 78 79 80 81 82 83 84 85
    const QLatin1String ArmToolchainPrefix("arm-linux-androideabi");
    const QLatin1String X86ToolchainPrefix("x86");
    const QLatin1String ArmToolsPrefix("arm-linux-androideabi");
    const QLatin1String X86ToolsPrefix("i686-android-linux");
    const QLatin1String Unknown("unknown");
    const QLatin1String keytoolName("keytool");
    const QLatin1String jarsignerName("jarsigner");
    const QLatin1String changeTimeStamp("ChangeTimeStamp");

86
    static Utils::FileName settingsFileName()
BogDan Vatra's avatar
BogDan Vatra committed
87
    {
88 89
        return Utils::FileName::fromString(QString::fromLatin1("%1/qtcreator/android.xml")
                                           .arg(QFileInfo(Core::ICore::settings(QSettings::SystemScope)->fileName()).absolutePath()));
BogDan Vatra's avatar
BogDan Vatra committed
90 91
    }

92
    bool androidDevicesLessThan(const AndroidDeviceInfo &dev1, const AndroidDeviceInfo &dev2)
BogDan Vatra's avatar
BogDan Vatra committed
93 94 95 96 97
    {
        return dev1.sdk < dev2.sdk;
    }
}

hjk's avatar
hjk committed
98
QLatin1String AndroidConfigurations::toolchainPrefix(Abi::Architecture architecture)
BogDan Vatra's avatar
BogDan Vatra committed
99 100
{
    switch (architecture) {
hjk's avatar
hjk committed
101
    case Abi::ArmArchitecture:
BogDan Vatra's avatar
BogDan Vatra committed
102
        return ArmToolchainPrefix;
hjk's avatar
hjk committed
103
    case Abi::X86Architecture:
BogDan Vatra's avatar
BogDan Vatra committed
104 105 106 107 108 109
        return X86ToolchainPrefix;
    default:
        return Unknown;
    }
}

hjk's avatar
hjk committed
110
QLatin1String AndroidConfigurations::toolsPrefix(Abi::Architecture architecture)
BogDan Vatra's avatar
BogDan Vatra committed
111 112
{
    switch (architecture) {
hjk's avatar
hjk committed
113
    case Abi::ArmArchitecture:
BogDan Vatra's avatar
BogDan Vatra committed
114
        return ArmToolsPrefix;
hjk's avatar
hjk committed
115
    case Abi::X86Architecture:
BogDan Vatra's avatar
BogDan Vatra committed
116 117 118 119 120 121 122 123 124
        return X86ToolsPrefix;
    default:
        return Unknown;
    }
}

AndroidConfig::AndroidConfig(const QSettings &settings)
{
    // user settings
hjk's avatar
hjk committed
125 126 127 128
    armGdbLocation = FileName::fromString(settings.value(ArmGdbLocationKey).toString());
    armGdbserverLocation = FileName::fromString(settings.value(ArmGdbserverLocationKey).toString());
    x86GdbLocation = FileName::fromString(settings.value(X86GdbLocationKey).toString());
    x86GdbserverLocation = FileName::fromString(settings.value(X86GdbserverLocationKey).toString());
BogDan Vatra's avatar
BogDan Vatra committed
129
    partitionSize = settings.value(PartitionSizeKey, 1024).toInt();
hjk's avatar
hjk committed
130 131 132 133 134
    sdkLocation = FileName::fromString(settings.value(SDKLocationKey).toString());
    ndkLocation = FileName::fromString(settings.value(NDKLocationKey).toString());
    antLocation = FileName::fromString(settings.value(AntLocationKey).toString());
    openJDKLocation = FileName::fromString(settings.value(OpenJDKLocationKey).toString());
    keystoreLocation = FileName::fromString(settings.value(KeystoreLocationKey).toString());
BogDan Vatra's avatar
BogDan Vatra committed
135 136 137 138 139 140

    QRegExp versionRegExp(NDKGccVersionRegExp);
    const QString &value = settings.value(NDKToolchainVersionKey).toString();
    if (versionRegExp.exactMatch(value))
        ndkToolchainVersion = value;
    else
141
        ndkToolchainVersion = value.mid(versionRegExp.indexIn(value)+1);
BogDan Vatra's avatar
BogDan Vatra committed
142 143 144
    // user settings

    PersistentSettingsReader reader;
145 146 147
    Utils::FileName fn = settingsFileName();
    if (reader.load(fn)
            && settings.value(changeTimeStamp).toInt() != fn.toFileInfo().lastModified().toMSecsSinceEpoch() / 1000) {
BogDan Vatra's avatar
BogDan Vatra committed
148
        // persisten settings
hjk's avatar
hjk committed
149 150 151 152 153
        sdkLocation = FileName::fromString(reader.restoreValue(SDKLocationKey).toString());
        ndkLocation = FileName::fromString(reader.restoreValue(NDKLocationKey).toString());
        antLocation = FileName::fromString(reader.restoreValue(AntLocationKey).toString());
        openJDKLocation = FileName::fromString(reader.restoreValue(OpenJDKLocationKey).toString());
        keystoreLocation = FileName::fromString(reader.restoreValue(KeystoreLocationKey).toString());
BogDan Vatra's avatar
BogDan Vatra committed
154 155 156 157 158 159

        QRegExp versionRegExp(NDKGccVersionRegExp);
        const QString &value = reader.restoreValue(NDKToolchainVersionKey).toString();
        if (versionRegExp.exactMatch(value))
            ndkToolchainVersion = value;
        else
160
            ndkToolchainVersion = value.mid(versionRegExp.indexIn(value)+1);
BogDan Vatra's avatar
BogDan Vatra committed
161

Tobias Hunger's avatar
Tobias Hunger committed
162
        if (armGdbLocation.isEmpty())
hjk's avatar
hjk committed
163
            armGdbLocation = FileName::fromString(reader.restoreValue(ArmGdbLocationKey).toString());
BogDan Vatra's avatar
BogDan Vatra committed
164

Tobias Hunger's avatar
Tobias Hunger committed
165
        if (armGdbserverLocation.isEmpty())
hjk's avatar
hjk committed
166
            armGdbserverLocation = FileName::fromString(reader.restoreValue(ArmGdbserverLocationKey).toString());
BogDan Vatra's avatar
BogDan Vatra committed
167

Tobias Hunger's avatar
Tobias Hunger committed
168
        if (x86GdbLocation.isEmpty())
hjk's avatar
hjk committed
169
            x86GdbLocation = FileName::fromString(reader.restoreValue(X86GdbLocationKey).toString());
BogDan Vatra's avatar
BogDan Vatra committed
170

Tobias Hunger's avatar
Tobias Hunger committed
171
        if (x86GdbserverLocation.isEmpty())
hjk's avatar
hjk committed
172
            x86GdbserverLocation = FileName::fromString(reader.restoreValue(X86GdbserverLocationKey).toString());
BogDan Vatra's avatar
BogDan Vatra committed
173 174 175 176 177 178 179 180 181 182 183 184
        // persistent settings
    }

}

AndroidConfig::AndroidConfig()
{
    partitionSize = 1024;
}

void AndroidConfig::save(QSettings &settings) const
{
Tobias Hunger's avatar
Tobias Hunger committed
185
    QFileInfo fileInfo = settingsFileName().toFileInfo();
BogDan Vatra's avatar
BogDan Vatra committed
186 187 188 189
    if (fileInfo.exists())
        settings.setValue(changeTimeStamp, fileInfo.lastModified().toMSecsSinceEpoch() / 1000);

    // user settings
Tobias Hunger's avatar
Tobias Hunger committed
190 191
    settings.setValue(SDKLocationKey, sdkLocation.toString());
    settings.setValue(NDKLocationKey, ndkLocation.toString());
BogDan Vatra's avatar
BogDan Vatra committed
192
    settings.setValue(NDKToolchainVersionKey, ndkToolchainVersion);
Tobias Hunger's avatar
Tobias Hunger committed
193 194 195 196 197 198 199
    settings.setValue(AntLocationKey, antLocation.toString());
    settings.setValue(OpenJDKLocationKey, openJDKLocation.toString());
    settings.setValue(KeystoreLocationKey, keystoreLocation.toString());
    settings.setValue(ArmGdbLocationKey, armGdbLocation.toString());
    settings.setValue(ArmGdbserverLocationKey, armGdbserverLocation.toString());
    settings.setValue(X86GdbLocationKey, x86GdbLocation.toString());
    settings.setValue(X86GdbserverLocationKey, x86GdbserverLocation.toString());
BogDan Vatra's avatar
BogDan Vatra committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213
    settings.setValue(PartitionSizeKey, partitionSize);
}

void AndroidConfigurations::setConfig(const AndroidConfig &devConfigs)
{
    m_config = devConfigs;
    save();
    updateAvailablePlatforms();
    emit updated();
}

void AndroidConfigurations::updateAvailablePlatforms()
{
    m_availablePlatforms.clear();
hjk's avatar
hjk committed
214
    FileName path = m_config.ndkLocation;
BogDan Vatra's avatar
BogDan Vatra committed
215
    QDirIterator it(path.appendPath(QLatin1String("platforms")).toString(), QStringList() << QLatin1String("android-*"), QDir::Dirs);
BogDan Vatra's avatar
BogDan Vatra committed
216 217 218 219 220 221 222 223 224 225 226
    while (it.hasNext()) {
        const QString &fileName = it.next();
        m_availablePlatforms.push_back(fileName.mid(fileName.lastIndexOf(QLatin1Char('-')) + 1).toInt());
    }
    qSort(m_availablePlatforms.begin(), m_availablePlatforms.end(), qGreater<int>());
}

QStringList AndroidConfigurations::sdkTargets(int minApiLevel) const
{
    QStringList targets;
    QProcess proc;
Tobias Hunger's avatar
Tobias Hunger committed
227
    proc.start(androidToolPath().toString(), QStringList() << QLatin1String("list") << QLatin1String("target")); // list avaialbe AVDs
BogDan Vatra's avatar
BogDan Vatra committed
228 229 230 231
    if (!proc.waitForFinished(-1)) {
        proc.terminate();
        return targets;
    }
232
    while (proc.canReadLine()) {
233
        const QString line = QString::fromLocal8Bit(proc.readLine().trimmed());
BogDan Vatra's avatar
BogDan Vatra committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247
        int index = line.indexOf(QLatin1String("\"android-"));
        if (index == -1)
            continue;
        QString apiLevel = line.mid(index + 1, line.length() - index - 2);
        if (apiLevel.mid(apiLevel.lastIndexOf(QLatin1Char('-')) + 1).toInt() >= minApiLevel)
            targets.push_back(apiLevel);
    }
    return targets;
}

QStringList AndroidConfigurations::ndkToolchainVersions() const
{
    QRegExp versionRegExp(NDKGccVersionRegExp);
    QStringList result;
hjk's avatar
hjk committed
248
    FileName path = m_config.ndkLocation;
Tobias Hunger's avatar
Tobias Hunger committed
249
    QDirIterator it(path.appendPath(QLatin1String("toolchains")).toString(),
BogDan Vatra's avatar
BogDan Vatra committed
250 251 252 253 254 255
                    QStringList() << QLatin1String("*"), QDir::Dirs);
    while (it.hasNext()) {
        const QString &fileName = it.next();
        int idx = versionRegExp.indexIn(fileName);
        if (idx == -1)
            continue;
256
        QString version = fileName.mid(idx+1);
BogDan Vatra's avatar
BogDan Vatra committed
257 258 259 260 261 262
        if (!result.contains(version))
            result.append(version);
    }
    return result;
}

hjk's avatar
hjk committed
263
FileName AndroidConfigurations::adbToolPath() const
BogDan Vatra's avatar
BogDan Vatra committed
264
{
hjk's avatar
hjk committed
265
    FileName path = m_config.sdkLocation;
266
    return path.appendPath(QLatin1String("platform-tools/adb" ANDROID_EXE_SUFFIX));
BogDan Vatra's avatar
BogDan Vatra committed
267 268
}

hjk's avatar
hjk committed
269
FileName AndroidConfigurations::androidToolPath() const
BogDan Vatra's avatar
BogDan Vatra committed
270 271 272 273
{
#ifdef Q_OS_WIN32
    // I want to switch from using android.bat to using an executable. All it really does is call
    // Java and I've made some progress on it. So if android.exe exists, return that instead.
hjk's avatar
hjk committed
274
    FileName path = m_config.sdkLocation;
Tobias Hunger's avatar
Tobias Hunger committed
275 276 277 278 279
    path.appendPath(QLatin1String("tools/android"ANDROID_EXE_SUFFIX));
    if (path.toFileInfo().exists())
        return path;
    path = m_config.sdkLocation;
    return path.appendPath(QLatin1String("tools/android"ANDROID_BAT_SUFFIX));
BogDan Vatra's avatar
BogDan Vatra committed
280
#else
hjk's avatar
hjk committed
281
    FileName path = m_config.sdkLocation;
BogDan Vatra's avatar
BogDan Vatra committed
282
    return path.appendPath(QLatin1String("tools/android"));
BogDan Vatra's avatar
BogDan Vatra committed
283 284 285
#endif
}

hjk's avatar
hjk committed
286
FileName AndroidConfigurations::antToolPath() const
BogDan Vatra's avatar
BogDan Vatra committed
287
{
Tobias Hunger's avatar
Tobias Hunger committed
288
    if (!m_config.antLocation.isEmpty())
BogDan Vatra's avatar
BogDan Vatra committed
289 290
        return m_config.antLocation;
    else
hjk's avatar
hjk committed
291
        return FileName::fromString(QLatin1String("ant"));
BogDan Vatra's avatar
BogDan Vatra committed
292 293
}

hjk's avatar
hjk committed
294
FileName AndroidConfigurations::emulatorToolPath() const
BogDan Vatra's avatar
BogDan Vatra committed
295
{
hjk's avatar
hjk committed
296
    FileName path = m_config.sdkLocation;
297
    return path.appendPath(QLatin1String("tools/emulator" ANDROID_EXE_SUFFIX));
BogDan Vatra's avatar
BogDan Vatra committed
298 299
}

hjk's avatar
hjk committed
300
FileName AndroidConfigurations::toolPath(Abi::Architecture architecture) const
BogDan Vatra's avatar
BogDan Vatra committed
301
{
hjk's avatar
hjk committed
302
    FileName path = m_config.ndkLocation;
Tobias Hunger's avatar
Tobias Hunger committed
303
    return path.appendPath(QString::fromLatin1("toolchains/%1-%2/prebuilt/%3/bin/%4")
BogDan Vatra's avatar
BogDan Vatra committed
304 305 306
            .arg(toolchainPrefix(architecture))
            .arg(m_config.ndkToolchainVersion)
            .arg(ToolchainHost)
Tobias Hunger's avatar
Tobias Hunger committed
307
            .arg(toolsPrefix(architecture)));
BogDan Vatra's avatar
BogDan Vatra committed
308 309
}

hjk's avatar
hjk committed
310
FileName AndroidConfigurations::stripPath(Abi::Architecture architecture) const
BogDan Vatra's avatar
BogDan Vatra committed
311
{
312
    return toolPath(architecture).append(QLatin1String("-strip" ANDROID_EXE_SUFFIX));
BogDan Vatra's avatar
BogDan Vatra committed
313 314
}

hjk's avatar
hjk committed
315
FileName AndroidConfigurations::readelfPath(Abi::Architecture architecture) const
BogDan Vatra's avatar
BogDan Vatra committed
316
{
317
    return toolPath(architecture).append(QLatin1String("-readelf" ANDROID_EXE_SUFFIX));
BogDan Vatra's avatar
BogDan Vatra committed
318 319
}

hjk's avatar
hjk committed
320
FileName AndroidConfigurations::gccPath(Abi::Architecture architecture) const
BogDan Vatra's avatar
BogDan Vatra committed
321
{
322
    return toolPath(architecture).append(QLatin1String("-gcc" ANDROID_EXE_SUFFIX));
BogDan Vatra's avatar
BogDan Vatra committed
323 324
}

hjk's avatar
hjk committed
325
FileName AndroidConfigurations::gdbServerPath(Abi::Architecture architecture) const
BogDan Vatra's avatar
BogDan Vatra committed
326
{
hjk's avatar
hjk committed
327
    FileName gdbServerPath;
BogDan Vatra's avatar
BogDan Vatra committed
328
    switch (architecture) {
hjk's avatar
hjk committed
329
    case Abi::ArmArchitecture:
BogDan Vatra's avatar
BogDan Vatra committed
330 331
        gdbServerPath = m_config.armGdbserverLocation;
        break;
hjk's avatar
hjk committed
332
    case Abi::X86Architecture:
BogDan Vatra's avatar
BogDan Vatra committed
333 334 335
        gdbServerPath = m_config.x86GdbserverLocation;
        break;
    default:
hjk's avatar
hjk committed
336
        gdbServerPath = FileName::fromString(Unknown);
BogDan Vatra's avatar
BogDan Vatra committed
337 338 339
        break;
    }

Tobias Hunger's avatar
Tobias Hunger committed
340
    if (!gdbServerPath.isEmpty())
BogDan Vatra's avatar
BogDan Vatra committed
341
        return gdbServerPath;
hjk's avatar
hjk committed
342
    FileName path = m_config.ndkLocation;
Tobias Hunger's avatar
Tobias Hunger committed
343 344 345
    return path.appendPath(QString::fromLatin1("toolchains/%1-%2/prebuilt/gdbserver")
                           .arg(toolchainPrefix(architecture))
                           .arg(m_config.ndkToolchainVersion));
BogDan Vatra's avatar
BogDan Vatra committed
346 347
}

hjk's avatar
hjk committed
348
FileName AndroidConfigurations::gdbPath(Abi::Architecture architecture) const
BogDan Vatra's avatar
BogDan Vatra committed
349
{
hjk's avatar
hjk committed
350
    FileName gdbPath;
BogDan Vatra's avatar
BogDan Vatra committed
351
    switch (architecture) {
hjk's avatar
hjk committed
352
    case Abi::ArmArchitecture:
BogDan Vatra's avatar
BogDan Vatra committed
353 354
        gdbPath = m_config.armGdbLocation;
        break;
hjk's avatar
hjk committed
355
    case Abi::X86Architecture:
BogDan Vatra's avatar
BogDan Vatra committed
356 357 358
        gdbPath = m_config.x86GdbLocation;
        break;
    default:
hjk's avatar
hjk committed
359
        gdbPath = FileName::fromString(Unknown);
BogDan Vatra's avatar
BogDan Vatra committed
360 361 362 363
        break;
    }
    if (!gdbPath.isEmpty())
        return gdbPath;
364
    return toolPath(architecture).append(QLatin1String("-gdb" ANDROID_EXE_SUFFIX));
BogDan Vatra's avatar
BogDan Vatra committed
365 366
}

hjk's avatar
hjk committed
367
FileName AndroidConfigurations::openJDKPath() const
BogDan Vatra's avatar
BogDan Vatra committed
368 369 370 371
{
    return m_config.openJDKLocation;
}

hjk's avatar
hjk committed
372
FileName AndroidConfigurations::openJDKBinPath() const
BogDan Vatra's avatar
BogDan Vatra committed
373
{
hjk's avatar
hjk committed
374
    FileName path = m_config.openJDKLocation;
Tobias Hunger's avatar
Tobias Hunger committed
375 376 377
    if (!path.isEmpty())
        return path.appendPath(QLatin1String("bin"));
    return path;
BogDan Vatra's avatar
BogDan Vatra committed
378 379
}

hjk's avatar
hjk committed
380
FileName AndroidConfigurations::keytoolPath() const
BogDan Vatra's avatar
BogDan Vatra committed
381
{
Tobias Hunger's avatar
Tobias Hunger committed
382
    return openJDKBinPath().appendPath(keytoolName);
BogDan Vatra's avatar
BogDan Vatra committed
383 384
}

hjk's avatar
hjk committed
385
FileName AndroidConfigurations::jarsignerPath() const
BogDan Vatra's avatar
BogDan Vatra committed
386
{
Tobias Hunger's avatar
Tobias Hunger committed
387
    return openJDKBinPath().appendPath(jarsignerName);
BogDan Vatra's avatar
BogDan Vatra committed
388 389
}

hjk's avatar
hjk committed
390
FileName AndroidConfigurations::zipalignPath() const
391 392 393 394 395
{
    Utils::FileName path = m_config.sdkLocation;
    return path.appendPath(QLatin1String("tools/zipalign" ANDROID_EXE_SUFFIX));
}

BogDan Vatra's avatar
BogDan Vatra committed
396 397
QString AndroidConfigurations::getDeployDeviceSerialNumber(int *apiLevel) const
{
398
    QVector<AndroidDeviceInfo> devices = connectedDevices();
BogDan Vatra's avatar
BogDan Vatra committed
399

400
    foreach (AndroidDeviceInfo device, devices) {
BogDan Vatra's avatar
BogDan Vatra committed
401 402 403 404
        if (device.sdk >= *apiLevel) {
            *apiLevel = device.sdk;
            return device.serialNumber;
        }
Tobias Hunger's avatar
Tobias Hunger committed
405
    }
BogDan Vatra's avatar
BogDan Vatra committed
406 407 408
    return startAVD(apiLevel);
}

409
QVector<AndroidDeviceInfo> AndroidConfigurations::connectedDevices(int apiLevel) const
BogDan Vatra's avatar
BogDan Vatra committed
410
{
411
    QVector<AndroidDeviceInfo> devices;
BogDan Vatra's avatar
BogDan Vatra committed
412
    QProcess adbProc;
Tobias Hunger's avatar
Tobias Hunger committed
413
    adbProc.start(adbToolPath().toString(), QStringList() << QLatin1String("devices"));
BogDan Vatra's avatar
BogDan Vatra committed
414 415 416 417 418 419
    if (!adbProc.waitForFinished(-1)) {
        adbProc.terminate();
        return devices;
    }
    QList<QByteArray> adbDevs = adbProc.readAll().trimmed().split('\n');
    adbDevs.removeFirst();
420
    AndroidDeviceInfo dev;
BogDan Vatra's avatar
BogDan Vatra committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
    foreach (const QByteArray &device, adbDevs) {
        dev.serialNumber = QString::fromLatin1(device.left(device.indexOf('\t')).trimmed());
        dev.sdk = getSDKVersion(dev.serialNumber);
        if (apiLevel != -1 && dev.sdk != apiLevel)
            continue;
        devices.push_back(dev);
    }
    qSort(devices.begin(), devices.end(), androidDevicesLessThan);
    return devices;
}

bool AndroidConfigurations::createAVD(int minApiLevel) const
{
    QDialog d;
    Ui::AddNewAVDDialog avdDialog;
    avdDialog.setupUi(&d);
    QStringListModel model(sdkTargets(minApiLevel));
    avdDialog.targetComboBox->setModel(&model);
    if (!model.rowCount()) {
Friedemann Kleint's avatar
Friedemann Kleint committed
440 441 442
        QMessageBox::critical(0, tr("Error Creating AVD"),
                              tr("Cannot create a new AVD, no sufficiently recent Android SDK available.\n"
                                 "Please install an SDK of at least API version %1.").
Friedemann Kleint's avatar
Friedemann Kleint committed
443
                              arg(minApiLevel));
BogDan Vatra's avatar
BogDan Vatra committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457
        return false;
    }

    QRegExp rx(QLatin1String("\\S+"));
    QRegExpValidator v(rx, 0);
    avdDialog.nameLineEdit->setValidator(&v);
    if (d.exec() != QDialog::Accepted)
        return false;
    return createAVD(avdDialog.targetComboBox->currentText(), avdDialog.nameLineEdit->text(), avdDialog.sizeSpinBox->value());
}

bool AndroidConfigurations::createAVD(const QString &target, const QString &name, int sdcardSize ) const
{
    QProcess proc;
Tobias Hunger's avatar
Tobias Hunger committed
458
    proc.start(androidToolPath().toString(),
BogDan Vatra's avatar
BogDan Vatra committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
               QStringList() << QLatin1String("create") << QLatin1String("avd")
               << QLatin1String("-a") << QLatin1String("-t") << target
               << QLatin1String("-n") << name
               << QLatin1String("-c") << QString::fromLatin1("%1M").arg(sdcardSize));
    if (!proc.waitForStarted())
        return false;
    proc.write(QByteArray("no\n"));
    if (!proc.waitForFinished(-1)) {
        proc.terminate();
        return false;
    }
    return !proc.exitCode();
}

bool AndroidConfigurations::removeAVD(const QString &name) const
{
    QProcess proc;
Tobias Hunger's avatar
Tobias Hunger committed
476
    proc.start(androidToolPath().toString(),
BogDan Vatra's avatar
BogDan Vatra committed
477 478 479 480 481 482 483 484 485
               QStringList() << QLatin1String("delete") << QLatin1String("avd")
               << QLatin1String("-n") << name);
    if (!proc.waitForFinished(-1)) {
        proc.terminate();
        return false;
    }
    return !proc.exitCode();
}

486
QVector<AndroidDeviceInfo> AndroidConfigurations::androidVirtualDevices() const
BogDan Vatra's avatar
BogDan Vatra committed
487
{
488
    QVector<AndroidDeviceInfo> devices;
BogDan Vatra's avatar
BogDan Vatra committed
489
    QProcess proc;
Tobias Hunger's avatar
Tobias Hunger committed
490
    proc.start(androidToolPath().toString(),
BogDan Vatra's avatar
BogDan Vatra committed
491 492 493 494 495 496 497
               QStringList() << QLatin1String("list") << QLatin1String("avd")); // list available AVDs
    if (!proc.waitForFinished(-1)) {
        proc.terminate();
        return devices;
    }
    QList<QByteArray> avds = proc.readAll().trimmed().split('\n');
    avds.removeFirst();
498
    AndroidDeviceInfo dev;
BogDan Vatra's avatar
BogDan Vatra committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
    for (int i = 0; i < avds.size(); i++) {
        QString line = QLatin1String(avds[i]);
        if (!line.contains(QLatin1String("Name:")))
            continue;

        dev.serialNumber = line.mid(line.indexOf(QLatin1Char(':')) + 2).trimmed();
        ++i;
        for (; i < avds.size(); ++i) {
            line = QLatin1String(avds[i]);
            if (line.contains(QLatin1String("---------")))
                break;
            if (line.contains(QLatin1String("Target:")))
                dev.sdk = line.mid(line.lastIndexOf(QLatin1Char(' '))).remove(QLatin1Char(')')).toInt();
            if (line.contains(QLatin1String("ABI:")))
                dev.cpuABI = line.mid(line.lastIndexOf(QLatin1Char(' '))).trimmed();
        }
        devices.push_back(dev);
    }
    qSort(devices.begin(), devices.end(), androidDevicesLessThan);

    return devices;
}

QString AndroidConfigurations::startAVD(int *apiLevel, const QString &name) const
{
    QProcess *m_avdProcess = new QProcess();
    connect(this, SIGNAL(destroyed()), m_avdProcess, SLOT(deleteLater()));
    connect(m_avdProcess, SIGNAL(finished(int)), m_avdProcess, SLOT(deleteLater()));

    QString avdName = name;
529
    QVector<AndroidDeviceInfo> devices;
BogDan Vatra's avatar
BogDan Vatra committed
530 531 532 533
    bool createAVDOnce = false;
    while (true) {
        if (avdName.isEmpty()) {
            devices = androidVirtualDevices();
534
            foreach (AndroidDeviceInfo device, devices)
BogDan Vatra's avatar
BogDan Vatra committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
                if (device.sdk >= *apiLevel) { // take first emulator how supports this package
                    *apiLevel = device.sdk;
                    avdName = device.serialNumber;
                    break;
                }
        }
        // if no emulators found try to create one once
        if (avdName.isEmpty() && !createAVDOnce) {
            createAVDOnce = true;
            QMetaObject::invokeMethod(const_cast<QObject*>(static_cast<const QObject*>(this)), "createAVD", Qt::AutoConnection,
                                      Q_ARG(int, *apiLevel));
        } else {
            break;
        }
    }

    if (avdName.isEmpty())// stop here if no emulators found
        return avdName;

    // start the emulator
Tobias Hunger's avatar
Tobias Hunger committed
555
    m_avdProcess->start(emulatorToolPath().toString(),
BogDan Vatra's avatar
BogDan Vatra committed
556 557 558 559 560 561 562 563 564
                        QStringList() << QLatin1String("-partition-size") << QString::number(config().partitionSize)
                        << QLatin1String("-avd") << avdName);
    if (!m_avdProcess->waitForStarted(-1)) {
        delete m_avdProcess;
        return QString();
    }

    // wait until the emulator is online
    QProcess proc;
Tobias Hunger's avatar
Tobias Hunger committed
565
    proc.start(adbToolPath().toString(), QStringList() << QLatin1String("-e") << QLatin1String("wait-for-device"));
BogDan Vatra's avatar
BogDan Vatra committed
566 567 568 569 570 571 572
    if (!proc.waitForFinished(-1)) {
        proc.terminate();
        return QString();
    }
    sleep(5);// wait for pm to start

    // workaround for stupid adb bug
Tobias Hunger's avatar
Tobias Hunger committed
573
    proc.start(adbToolPath().toString(), QStringList() << QLatin1String("devices"));
BogDan Vatra's avatar
BogDan Vatra committed
574 575 576 577 578 579 580
    if (!proc.waitForFinished(-1)) {
        proc.terminate();
        return QString();
    }

    // get connected devices
    devices = connectedDevices(*apiLevel);
581
    foreach (AndroidDeviceInfo device, devices)
BogDan Vatra's avatar
BogDan Vatra committed
582 583 584 585 586 587 588 589 590 591
        if (device.sdk == *apiLevel)
            return device.serialNumber;
    // this should not happen, but ...
    return QString();
}

int AndroidConfigurations::getSDKVersion(const QString &device) const
{

    QProcess adbProc;
Tobias Hunger's avatar
Tobias Hunger committed
592
    adbProc.start(adbToolPath().toString(),
BogDan Vatra's avatar
BogDan Vatra committed
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
                  QStringList() << QLatin1String("-s") << device
                  << QLatin1String("shell") << QLatin1String("getprop")
                  << QLatin1String("ro.build.version.sdk"));
    if (!adbProc.waitForFinished(-1)) {
        adbProc.terminate();
        return -1;
    }
    return adbProc.readAll().trimmed().toInt();
}

QString AndroidConfigurations::bestMatch(const QString &targetAPI) const
{
    int target = targetAPI.mid(targetAPI.lastIndexOf(QLatin1Char('-')) + 1).toInt();
    foreach (int apiLevel, m_availablePlatforms) {
        if (apiLevel <= target)
            return QString::fromLatin1("android-%1").arg(apiLevel);
    }
    return QLatin1String("android-8");
}

AndroidConfigurations &AndroidConfigurations::instance(QObject *parent)
{
    if (m_instance == 0)
        m_instance = new AndroidConfigurations(parent);
    return *m_instance;
}

void AndroidConfigurations::save()
{
    QSettings *settings = Core::ICore::instance()->settings();
    settings->beginGroup(SettingsGroup);
    m_config.save(*settings);
    settings->endGroup();
}

AndroidConfigurations::AndroidConfigurations(QObject *parent)
    : QObject(parent)
{
    load();
    updateAvailablePlatforms();
}

void AndroidConfigurations::load()
{
    QSettings *settings = Core::ICore::instance()->settings();
    settings->beginGroup(SettingsGroup);
    m_config = AndroidConfig(*settings);
    settings->endGroup();
}

AndroidConfigurations *AndroidConfigurations::m_instance = 0;

} // namespace Internal
} // namespace Android