androidsettingswidget.cpp 22.8 KB
Newer Older
1
/****************************************************************************
BogDan Vatra's avatar
BogDan Vatra committed
2
**
3
4
** Copyright (C) 2016 BogDan Vatra <bog_dan_ro@yahoo.com>
** Contact: https://www.qt.io/licensing/
BogDan Vatra's avatar
BogDan Vatra committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
BogDan Vatra's avatar
BogDan Vatra committed
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12
13
14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
BogDan Vatra's avatar
BogDan Vatra committed
15
**
16
17
18
19
20
21
22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
BogDan Vatra's avatar
BogDan Vatra committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
BogDan Vatra's avatar
BogDan Vatra committed
25
26
27
28
29
30
31

#include "androidsettingswidget.h"

#include "ui_androidsettingswidget.h"

#include "androidconfigurations.h"
#include "androidconstants.h"
Daniel Teske's avatar
Daniel Teske committed
32
#include "androidtoolchain.h"
Vikas Pachdha's avatar
Vikas Pachdha committed
33
#include "androidavdmanager.h"
BogDan Vatra's avatar
BogDan Vatra committed
34

35
#include <utils/environment.h>
36
#include <utils/hostosinfo.h>
37
#include <utils/pathchooser.h>
Eike Ziller's avatar
Eike Ziller committed
38
#include <utils/runextensions.h>
Ulf Hermann's avatar
Ulf Hermann committed
39
#include <utils/utilsicons.h>
Daniel Teske's avatar
Daniel Teske committed
40
41
42
#include <projectexplorer/toolchainmanager.h>
#include <projectexplorer/kitmanager.h>
#include <projectexplorer/kitinformation.h>
43
#include <projectexplorer/projectexplorerconstants.h>
Daniel Teske's avatar
Daniel Teske committed
44
45
#include <qtsupport/qtkitinformation.h>
#include <qtsupport/qtversionmanager.h>
46

BogDan Vatra's avatar
BogDan Vatra committed
47
48
49
#include <QFile>
#include <QTextStream>
#include <QProcess>
50
51
#include <QTimer>
#include <QTime>
BogDan Vatra's avatar
BogDan Vatra committed
52

53
#include <QDesktopServices>
BogDan Vatra's avatar
BogDan Vatra committed
54
55
56
#include <QFileDialog>
#include <QMessageBox>
#include <QModelIndex>
57
#include <QtCore/QUrl>
BogDan Vatra's avatar
BogDan Vatra committed
58
59
60
61

namespace Android {
namespace Internal {

Vikas Pachdha's avatar
Vikas Pachdha committed
62
void AvdModel::setAvdList(const AndroidDeviceInfoList &list)
BogDan Vatra's avatar
BogDan Vatra committed
63
{
64
    beginResetModel();
BogDan Vatra's avatar
BogDan Vatra committed
65
    m_list = list;
66
    endResetModel();
BogDan Vatra's avatar
BogDan Vatra committed
67
68
}

69
70
71
72
73
74
75
76
77
QModelIndex AvdModel::indexForAvdName(const QString &avdName) const
{
    for (int i = 0; i < m_list.size(); ++i) {
        if (m_list.at(i).serialNumber == avdName)
            return index(i, 0);
    }
    return QModelIndex();
}

78
QString AvdModel::avdName(const QModelIndex &index) const
BogDan Vatra's avatar
BogDan Vatra committed
79
{
80
    return m_list.at(index.row()).avdname;
BogDan Vatra's avatar
BogDan Vatra committed
81
82
}

hjk's avatar
hjk committed
83
QVariant AvdModel::data(const QModelIndex &index, int role) const
BogDan Vatra's avatar
BogDan Vatra committed
84
85
86
87
88
{
    if (role != Qt::DisplayRole || !index.isValid())
        return QVariant();
    switch (index.column()) {
        case 0:
89
            return m_list[index.row()].avdname;
BogDan Vatra's avatar
BogDan Vatra committed
90
91
        case 1:
            return QString::fromLatin1("API %1").arg(m_list[index.row()].sdk);
92
93
94
95
        case 2: {
            QStringList cpuAbis = m_list[index.row()].cpuAbi;
            return cpuAbis.isEmpty() ? QVariant() : QVariant(cpuAbis.first());
        }
BogDan Vatra's avatar
BogDan Vatra committed
96
97
98
99
    }
    return QVariant();
}

hjk's avatar
hjk committed
100
QVariant AvdModel::headerData(int section, Qt::Orientation orientation, int role) const
BogDan Vatra's avatar
BogDan Vatra committed
101
102
103
104
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
            case 0:
Friedemann Kleint's avatar
Friedemann Kleint committed
105
                //: AVD - Android Virtual Device
BogDan Vatra's avatar
BogDan Vatra committed
106
107
108
109
110
111
112
113
114
115
                return tr("AVD Name");
            case 1:
                return tr("AVD Target");
            case 2:
                return tr("CPU/ABI");
        }
    }
    return QAbstractItemModel::headerData(section, orientation, role );
}

hjk's avatar
hjk committed
116
int AvdModel::rowCount(const QModelIndex &/*parent*/) const
BogDan Vatra's avatar
BogDan Vatra committed
117
118
119
120
{
    return m_list.size();
}

hjk's avatar
hjk committed
121
int AvdModel::columnCount(const QModelIndex &/*parent*/) const
BogDan Vatra's avatar
BogDan Vatra committed
122
123
124
125
126
127
{
    return 3;
}

AndroidSettingsWidget::AndroidSettingsWidget(QWidget *parent)
    : QWidget(parent),
Daniel Teske's avatar
Daniel Teske committed
128
129
130
      m_sdkState(NotSet),
      m_ndkState(NotSet),
      m_javaState(NotSet),
BogDan Vatra's avatar
BogDan Vatra committed
131
      m_ui(new Ui_AndroidSettingsWidget),
132
      m_androidConfig(AndroidConfigurations::currentConfig()),
Vikas Pachdha's avatar
Vikas Pachdha committed
133
      m_avdManager(new AndroidAvdManager(m_androidConfig))
BogDan Vatra's avatar
BogDan Vatra committed
134
{
Daniel Teske's avatar
Daniel Teske committed
135
    m_ui->setupUi(this);
BogDan Vatra's avatar
BogDan Vatra committed
136

Orgad Shaneh's avatar
Orgad Shaneh committed
137
138
    connect(&m_checkGdbWatcher, &QFutureWatcherBase::finished,
            this, &AndroidSettingsWidget::checkGdbFinished);
139

140
141
142
143
    m_ui->SDKLocationPathChooser->setFileName(m_androidConfig.sdkLocation());
    m_ui->SDKLocationPathChooser->setPromptDialogTitle(tr("Select Android SDK folder"));
    m_ui->NDKLocationPathChooser->setFileName(m_androidConfig.ndkLocation());
    m_ui->NDKLocationPathChooser->setPromptDialogTitle(tr("Select Android NDK folder"));
BogDan Vatra's avatar
BogDan Vatra committed
144

145
146
    m_ui->OpenJDKLocationPathChooser->setFileName(m_androidConfig.openJDKLocation());
    m_ui->OpenJDKLocationPathChooser->setPromptDialogTitle(tr("Select JDK Path"));
Daniel Teske's avatar
Daniel Teske committed
147
148
    m_ui->DataPartitionSizeSpinBox->setValue(m_androidConfig.partitionSize());
    m_ui->CreateKitCheckBox->setChecked(m_androidConfig.automaticKitCreation());
BogDan Vatra's avatar
BogDan Vatra committed
149
    m_ui->AVDTableView->setModel(&m_AVDModel);
150
151
    m_ui->AVDTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_ui->AVDTableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
Daniel Teske's avatar
Daniel Teske committed
152

153
    m_ui->downloadOpenJDKToolButton->setVisible(!Utils::HostOsInfo::isLinuxHost());
Daniel Teske's avatar
Daniel Teske committed
154

Ulf Hermann's avatar
Ulf Hermann committed
155
    const QPixmap warningPixmap = Utils::Icons::WARNING.pixmap();
156
157
158
    m_ui->jdkWarningIconLabel->setPixmap(warningPixmap);
    m_ui->kitWarningIconLabel->setPixmap(warningPixmap);

159
    const QPixmap errorPixmap = Utils::Icons::CRITICAL.pixmap();
160
161
162
163
    m_ui->sdkWarningIconLabel->setPixmap(errorPixmap);
    m_ui->gdbWarningIconLabel->setPixmap(errorPixmap);
    m_ui->ndkWarningIconLabel->setPixmap(errorPixmap);

Orgad Shaneh's avatar
Orgad Shaneh committed
164
165
    connect(m_ui->gdbWarningLabel, &QLabel::linkActivated,
            this, &AndroidSettingsWidget::showGdbWarningDialog);
166

Orgad Shaneh's avatar
Orgad Shaneh committed
167
168
    connect(&m_virtualDevicesWatcher, &QFutureWatcherBase::finished,
            this, &AndroidSettingsWidget::updateAvds);
169

Daniel Teske's avatar
Daniel Teske committed
170
171
    check(All);
    applyToUi(All);
172

Orgad Shaneh's avatar
Orgad Shaneh committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    connect(&m_futureWatcher, &QFutureWatcherBase::finished,
            this, &AndroidSettingsWidget::avdAdded);

    connect(m_ui->NDKLocationPathChooser, &Utils::PathChooser::rawPathChanged,
            this, &AndroidSettingsWidget::ndkLocationEditingFinished);
    connect(m_ui->SDKLocationPathChooser, &Utils::PathChooser::rawPathChanged,
            this, &AndroidSettingsWidget::sdkLocationEditingFinished);
    connect(m_ui->OpenJDKLocationPathChooser, &Utils::PathChooser::rawPathChanged,
            this, &AndroidSettingsWidget::openJDKLocationEditingFinished);
    connect(m_ui->AVDAddPushButton, &QAbstractButton::clicked,
            this, &AndroidSettingsWidget::addAVD);
    connect(m_ui->AVDRemovePushButton, &QAbstractButton::clicked,
            this, &AndroidSettingsWidget::removeAVD);
    connect(m_ui->AVDStartPushButton, &QAbstractButton::clicked,
            this, &AndroidSettingsWidget::startAVD);
    connect(m_ui->AVDTableView, &QAbstractItemView::activated,
            this, &AndroidSettingsWidget::avdActivated);
    connect(m_ui->AVDTableView, &QAbstractItemView::clicked,
            this, &AndroidSettingsWidget::avdActivated);
    connect(m_ui->DataPartitionSizeSpinBox, &QAbstractSpinBox::editingFinished,
            this, &AndroidSettingsWidget::dataPartitionSizeEditingFinished);
    connect(m_ui->manageAVDPushButton, &QAbstractButton::clicked,
            this, &AndroidSettingsWidget::manageAVD);
    connect(m_ui->CreateKitCheckBox, &QAbstractButton::toggled,
            this, &AndroidSettingsWidget::createKitToggled);
    connect(m_ui->downloadSDKToolButton, &QAbstractButton::clicked,
            this, &AndroidSettingsWidget::openSDKDownloadUrl);
    connect(m_ui->downloadNDKToolButton, &QAbstractButton::clicked,
            this, &AndroidSettingsWidget::openNDKDownloadUrl);
    connect(m_ui->downloadOpenJDKToolButton, &QAbstractButton::clicked,
            this, &AndroidSettingsWidget::openOpenJDKDownloadUrl);
BogDan Vatra's avatar
BogDan Vatra committed
204
205
}

Daniel Teske's avatar
Daniel Teske committed
206
AndroidSettingsWidget::~AndroidSettingsWidget()
BogDan Vatra's avatar
BogDan Vatra committed
207
{
Daniel Teske's avatar
Daniel Teske committed
208
    delete m_ui;
209
    m_futureWatcher.waitForFinished();
BogDan Vatra's avatar
BogDan Vatra committed
210
211
}

212
// NOTE: Will be run via QFuture
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
static QPair<QStringList, bool> checkGdbForBrokenPython(const QStringList &paths)
{
    foreach (const QString &path, paths) {
        QTime timer;
        timer.start();
        QProcess proc;
        proc.setProcessChannelMode(QProcess::MergedChannels);
        proc.start(path);
        proc.waitForStarted();

        QByteArray output;
        while (proc.waitForReadyRead(300)) {
            output += proc.readAll();
            if (output.contains("(gdb)"))
                break;
            if (timer.elapsed() > 7 * 1000)
229
                return qMakePair(paths, true); // Took too long, abort
230
        }
231

232
        output.clear();
233

234
235
236
237
238
239
        proc.write("python import string\n");
        proc.write("python print(string.ascii_uppercase)\n");
        proc.write("python import struct\n");
        proc.write("quit\n");
        while (proc.waitForFinished(300)) {
            if (timer.elapsed() > 9 * 1000)
240
                return qMakePair(paths, true); // Took too long, abort
241
242
        }
        proc.waitForFinished();
243

244
        output = proc.readAll();
245

246
247
248
249
250
251
252
253
        bool error = output.contains("_PyObject_Free")
                || output.contains("_PyExc_IOError")
                || output.contains("_sysconfigdata_nd ")
                || !output.contains("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        if (error)
            return qMakePair(paths, error);
    }
    return qMakePair(paths, false);
254
255
}

Daniel Teske's avatar
Daniel Teske committed
256
void AndroidSettingsWidget::check(AndroidSettingsWidget::Mode mode)
BogDan Vatra's avatar
BogDan Vatra committed
257
{
Daniel Teske's avatar
Daniel Teske committed
258
    if (mode & Sdk) {
259
        m_sdkState = verifySdkInstallation(&m_sdkInstallationError) ? Okay : Error;
260
    }
Daniel Teske's avatar
Daniel Teske committed
261
262
263
264
265
266

    if (mode & Ndk) {
        m_ndkState = Okay;
        Utils::FileName platformPath = m_androidConfig.ndkLocation();
        Utils::FileName toolChainPath = m_androidConfig.ndkLocation();
        Utils::FileName sourcesPath = m_androidConfig.ndkLocation();
267
268
        m_ui->gdbWarningIconLabel->setVisible(false);
        m_ui->gdbWarningLabel->setVisible(false);
Daniel Teske's avatar
Daniel Teske committed
269
270
        if (m_androidConfig.ndkLocation().isEmpty()) {
            m_ndkState = NotSet;
hjk's avatar
hjk committed
271
272
273
        } else if (!platformPath.appendPath(QLatin1String("platforms")).exists()
                || !toolChainPath.appendPath(QLatin1String("toolchains")).exists()
                || !sourcesPath.appendPath(QLatin1String("sources/cxx-stl")).exists()) {
Daniel Teske's avatar
Daniel Teske committed
274
            m_ndkState = Error;
275
276
            m_ndkErrorMessage = tr("\"%1\" does not seem to be an Android NDK top folder.")
                    .arg(m_androidConfig.ndkLocation().toUserOutput());
277
        } else if (platformPath.toString().contains(QLatin1Char(' '))) {
278
279
            m_ndkState = Error;
            m_ndkErrorMessage = tr("The Android NDK cannot be installed into a path with spaces.");
Daniel Teske's avatar
Daniel Teske committed
280
281
282
283
284
        } else {
            QList<AndroidToolChainFactory::AndroidToolChainInformation> compilerPaths
                    = AndroidToolChainFactory::toolchainPathsForNdk(m_androidConfig.ndkLocation());
            m_ndkCompilerCount = compilerPaths.count();

285
            // Check for a gdb with a broken python
286
287
            QStringList gdbPaths;
            foreach (const AndroidToolChainFactory::AndroidToolChainInformation &ati, compilerPaths) {
288
                if (ati.language == Core::Id(ProjectExplorer::Constants::C_LANGUAGE_ID))
Tobias Hunger's avatar
Tobias Hunger committed
289
                    continue;
290
                // we only check the arm gdbs, that's indicative enough
Daniel Teske's avatar
Daniel Teske committed
291
                if (ati.abi.architecture() != ProjectExplorer::Abi::ArmArchitecture)
292
                    continue;
Daniel Teske's avatar
Daniel Teske committed
293
                Utils::FileName gdbPath = m_androidConfig.gdbPath(ati.abi, ati.version);
hjk's avatar
hjk committed
294
                if (gdbPath.exists())
295
                    gdbPaths << gdbPath.toString();
296
297
            }

298
            if (!gdbPaths.isEmpty()) {
Eike Ziller's avatar
Eike Ziller committed
299
                m_checkGdbWatcher.setFuture(Utils::runAsync(&checkGdbForBrokenPython, gdbPaths));
300
301
                m_gdbCheckPaths = gdbPaths;
            }
Daniel Teske's avatar
Daniel Teske committed
302
303

            // See if we have qt versions for those toolchains
Daniel Teske's avatar
Daniel Teske committed
304
            QSet<ProjectExplorer::Abi> toolchainsForAbi;
Tobias Hunger's avatar
Tobias Hunger committed
305
            foreach (const AndroidToolChainFactory::AndroidToolChainInformation &ati, compilerPaths) {
306
                if (ati.language == Core::Id(ProjectExplorer::Constants::CXX_LANGUAGE_ID))
Tobias Hunger's avatar
Tobias Hunger committed
307
308
                    toolchainsForAbi.insert(ati.abi);
            }
Daniel Teske's avatar
Daniel Teske committed
309

310
            const QList<QtSupport::BaseQtVersion *> androidQts
311
                    = QtSupport::QtVersionManager::versions([](const QtSupport::BaseQtVersion *v) {
312
313
                return v->type() == QLatin1String(Constants::ANDROIDQT) && !v->qtAbis().isEmpty();
            });
Daniel Teske's avatar
Daniel Teske committed
314
            QSet<ProjectExplorer::Abi> qtVersionsForAbi;
315
            foreach (QtSupport::BaseQtVersion *qtVersion, androidQts)
Daniel Teske's avatar
Daniel Teske committed
316
                qtVersionsForAbi.insert(qtVersion->qtAbis().first());
Daniel Teske's avatar
Daniel Teske committed
317

Daniel Teske's avatar
Daniel Teske committed
318
            QSet<ProjectExplorer::Abi> missingQtArchs = toolchainsForAbi.subtract(qtVersionsForAbi);
Daniel Teske's avatar
Daniel Teske committed
319
320
321
322
323
324
            if (missingQtArchs.isEmpty()) {
                m_ndkMissingQtArchs.clear();
            } else {
                if (missingQtArchs.count() == 1) {
                    m_ndkMissingQtArchs = tr("Qt version for architecture %1 is missing.\n"
                                             "To add the Qt version, select Options > Build & Run > Qt Versions.")
Daniel Teske's avatar
Daniel Teske committed
325
                            .arg((*missingQtArchs.constBegin()).toString());
Daniel Teske's avatar
Daniel Teske committed
326
                } else {
Robert Loehning's avatar
Robert Loehning committed
327
328
329
                    m_ndkMissingQtArchs = tr("Qt versions for %n architectures are missing.\n"
                                             "To add the Qt versions, select Options > Build & Run > Qt Versions.",
                                             nullptr, missingQtArchs.size());
Daniel Teske's avatar
Daniel Teske committed
330
331
332
                }
            }
        }
BogDan Vatra's avatar
BogDan Vatra committed
333
334
    }

Daniel Teske's avatar
Daniel Teske committed
335
336
337
338
339
340
    if (mode & Java) {
        m_javaState = Okay;
        if (m_androidConfig.openJDKLocation().isEmpty()) {
            m_javaState = NotSet;
        } else {
            Utils::FileName bin = m_androidConfig.openJDKLocation();
341
            bin.appendPath(QLatin1String("bin/javac" QTC_HOST_EXE_SUFFIX));
hjk's avatar
hjk committed
342
            if (!m_androidConfig.openJDKLocation().exists() || !bin.exists())
Daniel Teske's avatar
Daniel Teske committed
343
344
                m_javaState = Error;
        }
Daniel Teske's avatar
Daniel Teske committed
345
346
347
    }
}

Daniel Teske's avatar
Daniel Teske committed
348
void AndroidSettingsWidget::applyToUi(AndroidSettingsWidget::Mode mode)
BogDan Vatra's avatar
BogDan Vatra committed
349
{
Daniel Teske's avatar
Daniel Teske committed
350
351
352
353
    if (mode & Sdk) {
        if (m_sdkState == Error) {
            m_ui->sdkWarningIconLabel->setVisible(true);
            m_ui->sdkWarningLabel->setVisible(true);
354
            m_ui->sdkWarningLabel->setText(m_sdkInstallationError);
Daniel Teske's avatar
Daniel Teske committed
355
356
357
358
        } else {
            m_ui->sdkWarningIconLabel->setVisible(false);
            m_ui->sdkWarningLabel->setVisible(false);
        }
BogDan Vatra's avatar
BogDan Vatra committed
359
    }
Daniel Teske's avatar
Daniel Teske committed
360

Daniel Teske's avatar
Daniel Teske committed
361
362
363
364
365
366
367
    if (mode & Ndk) {
        if (m_ndkState == NotSet) {
            m_ui->ndkWarningIconLabel->setVisible(false);
            m_ui->toolchainFoundLabel->setVisible(false);
            m_ui->kitWarningIconLabel->setVisible(false);
            m_ui->kitWarningLabel->setVisible(false);
        } else if (m_ndkState == Error) {
368
            m_ui->toolchainFoundLabel->setText(m_ndkErrorMessage);
Daniel Teske's avatar
Daniel Teske committed
369
370
            m_ui->toolchainFoundLabel->setVisible(true);
            m_ui->ndkWarningIconLabel->setVisible(true);
371
372
            m_ui->kitWarningIconLabel->setVisible(false);
            m_ui->kitWarningLabel->setVisible(false);
Daniel Teske's avatar
Daniel Teske committed
373
374
375
376
377
378
379
380
381
        } else {
            if (m_ndkCompilerCount > 0) {
                m_ui->ndkWarningIconLabel->setVisible(false);
                m_ui->toolchainFoundLabel->setText(tr("Found %n toolchains for this NDK.", 0, m_ndkCompilerCount));
                m_ui->toolchainFoundLabel->setVisible(true);
            } else {
                m_ui->ndkWarningIconLabel->setVisible(false);
                m_ui->toolchainFoundLabel->setVisible(false);
            }
Daniel Teske's avatar
Daniel Teske committed
382

Daniel Teske's avatar
Daniel Teske committed
383
384
385
386
387
388
389
390
391
392
            if (m_ndkMissingQtArchs.isEmpty()) {
                m_ui->kitWarningIconLabel->setVisible(false);
                m_ui->kitWarningLabel->setVisible(false);
            } else {
                m_ui->kitWarningIconLabel->setVisible(true);
                m_ui->kitWarningLabel->setVisible(true);
                m_ui->kitWarningLabel->setText(m_ndkMissingQtArchs);
            }
        }
    }
Daniel Teske's avatar
Daniel Teske committed
393

Daniel Teske's avatar
Daniel Teske committed
394
395
396
397
398
399
400
    if (mode & Java) {
        Utils::FileName location = m_androidConfig.openJDKLocation();
        bool error = m_javaState == Error;
        m_ui->jdkWarningIconLabel->setVisible(error);
        m_ui->jdkWarningLabel->setVisible(error);
        if (error)
            m_ui->jdkWarningLabel->setText(tr("\"%1\" does not seem to be a JDK folder.").arg(location.toUserOutput()));
Daniel Teske's avatar
Daniel Teske committed
401
402
    }

Daniel Teske's avatar
Daniel Teske committed
403
404
405
    if (mode & Sdk || mode & Java) {
        if (m_sdkState == Okay && m_javaState == Okay) {
            m_ui->AVDManagerFrame->setEnabled(true);
Daniel Teske's avatar
Daniel Teske committed
406
        } else {
Daniel Teske's avatar
Daniel Teske committed
407
            m_ui->AVDManagerFrame->setEnabled(false);
Daniel Teske's avatar
Daniel Teske committed
408
        }
Daniel Teske's avatar
Daniel Teske committed
409

410
        startUpdateAvd();
Daniel Teske's avatar
Daniel Teske committed
411
    }
Daniel Teske's avatar
Daniel Teske committed
412
}
Daniel Teske's avatar
Daniel Teske committed
413

414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
void AndroidSettingsWidget::disableAvdControls()
{
    m_ui->AVDAddPushButton->setEnabled(false);
    m_ui->AVDTableView->setEnabled(false);
    m_ui->AVDRemovePushButton->setEnabled(false);
    m_ui->AVDStartPushButton->setEnabled(false);
}

void AndroidSettingsWidget::enableAvdControls()
{
    m_ui->AVDTableView->setEnabled(true);
    m_ui->AVDAddPushButton->setEnabled(true);
    avdActivated(m_ui->AVDTableView->currentIndex());
}

void AndroidSettingsWidget::startUpdateAvd()
{
    disableAvdControls();
Vikas Pachdha's avatar
Vikas Pachdha committed
432
    m_virtualDevicesWatcher.setFuture(m_avdManager->avdList());
433
434
435
436
437
438
439
440
441
442
443
444
}

void AndroidSettingsWidget::updateAvds()
{
    m_AVDModel.setAvdList(m_virtualDevicesWatcher.result());
    if (!m_lastAddedAvd.isEmpty()) {
        m_ui->AVDTableView->setCurrentIndex(m_AVDModel.indexForAvdName(m_lastAddedAvd));
        m_lastAddedAvd.clear();
    }
    enableAvdControls();
}

445
bool AndroidSettingsWidget::verifySdkInstallation(QString *errorDetails) const
446
{
447
448
449
450
451
    if (m_androidConfig.sdkLocation().isEmpty()) {
        if (errorDetails)
            *errorDetails = tr("Android SDK path not set.");
        return false;
    }
452

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
    if (!m_androidConfig.sdkLocation().exists()) {
        if (errorDetails)
            *errorDetails = tr("Android SDK path does not exist.");
        return false;
    }

    if (m_androidConfig.sdkToolsVersion().isNull()) {
        if (errorDetails)
            *errorDetails = tr("The SDK path does not seem to be a valid Android SDK top folder.");
        return false;
    }

    QStringList missingComponents;
    if (!m_androidConfig.adbToolPath().exists())
        missingComponents << "Platform Tools";

    if (m_androidConfig.buildToolsVersion().isNull())
        missingComponents << "Build Tools";

    if (m_androidConfig.sdkTargets().isEmpty())
        missingComponents << "Platform SDK";

    if (!missingComponents.isEmpty() && errorDetails) {
        *errorDetails = tr("Android SDK components missing (%1).\nUse Android SDK Manager to "
                           "manage SDK components.").arg(missingComponents.join(", "));
    }

    return missingComponents.isEmpty();
481
482
}

Daniel Teske's avatar
Daniel Teske committed
483
484
485
486
487
488
489
490
void AndroidSettingsWidget::saveSettings()
{
    sdkLocationEditingFinished();
    ndkLocationEditingFinished();
    openJDKLocationEditingFinished();
    dataPartitionSizeEditingFinished();
    AndroidConfigurations::setConfig(m_androidConfig);
}
BogDan Vatra's avatar
BogDan Vatra committed
491
492
493

void AndroidSettingsWidget::sdkLocationEditingFinished()
{
494
    m_androidConfig.setSdkLocation(Utils::FileName::fromUserInput(m_ui->SDKLocationPathChooser->rawPath()));
Daniel Teske's avatar
Daniel Teske committed
495
496
497

    check(Sdk);
    applyToUi(Sdk);
BogDan Vatra's avatar
BogDan Vatra committed
498
499
500
501
}

void AndroidSettingsWidget::ndkLocationEditingFinished()
{
502
    m_androidConfig.setNdkLocation(Utils::FileName::fromUserInput(m_ui->NDKLocationPathChooser->rawPath()));
Daniel Teske's avatar
Daniel Teske committed
503
504
505

    check(Ndk);
    applyToUi(Ndk);
BogDan Vatra's avatar
BogDan Vatra committed
506
507
508
509
}

void AndroidSettingsWidget::openJDKLocationEditingFinished()
{
510
    m_androidConfig.setOpenJDKLocation(Utils::FileName::fromUserInput(m_ui->OpenJDKLocationPathChooser->rawPath()));
Daniel Teske's avatar
Daniel Teske committed
511
512
513

    check(Java);
    applyToUi(Java);
BogDan Vatra's avatar
BogDan Vatra committed
514
515
}

516
517
void AndroidSettingsWidget::openSDKDownloadUrl()
{
518
    QDesktopServices::openUrl(QUrl::fromUserInput("https://developer.android.com/studio/"));
519
520
521
522
}

void AndroidSettingsWidget::openNDKDownloadUrl()
{
523
    QDesktopServices::openUrl(QUrl::fromUserInput("https://developer.android.com/ndk/downloads/"));
524
525
526
527
}

void AndroidSettingsWidget::openOpenJDKDownloadUrl()
{
528
    QDesktopServices::openUrl(QUrl::fromUserInput("http://www.oracle.com/technetwork/java/javase/downloads/"));
529
530
}

BogDan Vatra's avatar
BogDan Vatra committed
531
532
void AndroidSettingsWidget::addAVD()
{
533
    disableAvdControls();
534
535
    AndroidConfig::CreateAvdInfo info = m_androidConfig.gatherCreateAVDInfo(this);

Vikas Pachdha's avatar
Vikas Pachdha committed
536
    if (!info.target.isValid()) {
537
        enableAvdControls();
538
539
540
        return;
    }

Vikas Pachdha's avatar
Vikas Pachdha committed
541
    m_futureWatcher.setFuture(m_avdManager->createAvd(info));
542
543
544
545
546
547
}

void AndroidSettingsWidget::avdAdded()
{
    AndroidConfig::CreateAvdInfo info = m_futureWatcher.result();
    if (!info.error.isEmpty()) {
548
        enableAvdControls();
549
550
551
552
        QMessageBox::critical(this, QApplication::translate("AndroidConfig", "Error Creating AVD"), info.error);
        return;
    }

553
554
    startUpdateAvd();
    m_lastAddedAvd = info.name;
BogDan Vatra's avatar
BogDan Vatra committed
555
556
557
558
}

void AndroidSettingsWidget::removeAVD()
{
559
    disableAvdControls();
560
561
562
563
    QString avdName = m_AVDModel.avdName(m_ui->AVDTableView->currentIndex());
    if (QMessageBox::question(this, tr("Remove Android Virtual Device"),
                              tr("Remove device \"%1\"? This cannot be undone.").arg(avdName),
                              QMessageBox::Yes | QMessageBox::No)
564
565
            == QMessageBox::No) {
        enableAvdControls();
566
        return;
567
    }
568

Vikas Pachdha's avatar
Vikas Pachdha committed
569
    m_avdManager->removeAvd(avdName);
570
    startUpdateAvd();
BogDan Vatra's avatar
BogDan Vatra committed
571
572
573
574
}

void AndroidSettingsWidget::startAVD()
{
Vikas Pachdha's avatar
Vikas Pachdha committed
575
    m_avdManager->startAvdAsync(m_AVDModel.avdName(m_ui->AVDTableView->currentIndex()));
BogDan Vatra's avatar
BogDan Vatra committed
576
577
}

578
void AndroidSettingsWidget::avdActivated(const QModelIndex &index)
BogDan Vatra's avatar
BogDan Vatra committed
579
580
581
582
583
584
585
{
    m_ui->AVDRemovePushButton->setEnabled(index.isValid());
    m_ui->AVDStartPushButton->setEnabled(index.isValid());
}

void AndroidSettingsWidget::dataPartitionSizeEditingFinished()
{
Daniel Teske's avatar
Daniel Teske committed
586
    m_androidConfig.setPartitionSize(m_ui->DataPartitionSizeSpinBox->value());
BogDan Vatra's avatar
BogDan Vatra committed
587
588
}

Daniel Teske's avatar
Daniel Teske committed
589
590
void AndroidSettingsWidget::createKitToggled()
{
Daniel Teske's avatar
Daniel Teske committed
591
    m_androidConfig.setAutomaticKitCreation(m_ui->CreateKitCheckBox->isChecked());
Daniel Teske's avatar
Daniel Teske committed
592
593
}

594
595
void AndroidSettingsWidget::checkGdbFinished()
{
596
597
    QPair<QStringList, bool> result = m_checkGdbWatcher.future().result();
    if (result.first != m_gdbCheckPaths) // no longer relevant
598
599
600
601
602
603
604
605
606
607
608
        return;
    m_ui->gdbWarningIconLabel->setVisible(result.second);
    m_ui->gdbWarningLabel->setVisible(result.second);
}

void AndroidSettingsWidget::showGdbWarningDialog()
{
    QMessageBox::warning(this,
                         tr("Unsupported GDB"),
                         tr("The GDB inside this NDK seems to not support Python. "
                            "The Qt Project offers fixed GDB builds at: "
609
610
                            "<a href=\"http://download.qt.io/official_releases/gdb/\">"
                            "http://download.qt.io/official_releases/gdb/</a>"));
611
612
}

BogDan Vatra's avatar
BogDan Vatra committed
613
614
void AndroidSettingsWidget::manageAVD()
{
Vikas Pachdha's avatar
Vikas Pachdha committed
615
616
617
618
619
620
621
622
623
    if (m_avdManager->avdManagerUiToolAvailable()) {
        m_avdManager->launchAvdManagerUiTool();
    } else {
        QMessageBox::warning(this, tr("AVD Manager Not Available"),
                             tr("AVD manager UI tool is not available in the installed SDK tools"
                                "(version %1). Use the command line tool \"avdmanager\" for "
                                "advanced AVD management.")
                             .arg(m_androidConfig.sdkToolsVersion().toString()));
    }
BogDan Vatra's avatar
BogDan Vatra committed
624
625
626
627
628
}


} // namespace Internal
} // namespace Android