androidmanager.cpp 34.1 KB
Newer Older
BogDan Vatra's avatar
BogDan Vatra committed
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
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 BogDan Vatra <bog_dan_ro@yahoo.com>
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** 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.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

Tobias Hunger's avatar
Tobias Hunger committed
33
#include "androidmanager.h"
BogDan Vatra's avatar
BogDan Vatra committed
34 35 36 37 38 39 40
#include "androiddeployconfiguration.h"
#include "androidconfigurations.h"
#include "androidrunconfiguration.h"
#include "androiddeploystep.h"
#include "androidglobal.h"
#include "androidpackagecreationstep.h"

Tobias Hunger's avatar
Tobias Hunger committed
41 42 43
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
BogDan Vatra's avatar
BogDan Vatra committed
44 45 46 47
#include <qt4projectmanager/qt4nodes.h>
#include <qt4projectmanager/qt4project.h>
#include <qt4projectmanager/qt4projectmanagerconstants.h>
#include <qt4projectmanager/qt4buildconfiguration.h>
48
#include <qtsupport/customexecutablerunconfiguration.h>
Tobias Hunger's avatar
Tobias Hunger committed
49 50
#include <qtsupport/qtprofileinformation.h>
#include <qtsupport/qtsupportconstants.h>
BogDan Vatra's avatar
BogDan Vatra committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

#include <QDir>
#include <QFileSystemWatcher>
#include <QList>
#include <QProcess>
#include <QMessageBox>
#include <QApplication>
#include <QDomDocument>

namespace {
    const QLatin1String AndroidDirName("android");
    const QLatin1String AndroidManifestName("AndroidManifest.xml");
    const QLatin1String AndroidLibsFileName("/res/values/libs.xml");
    const QLatin1String AndroidStringsFileName("/res/values/strings.xml");
    const QLatin1String AndroidDefaultPropertiesName("project.properties");

    QString cleanPackageName(QString packageName)
    {
69
        QRegExp legalChars(QLatin1String("[a-zA-Z0-9_\\.]"));
BogDan Vatra's avatar
BogDan Vatra committed
70 71 72 73 74 75 76 77 78 79 80 81

        for (int i = 0; i < packageName.length(); ++i)
            if (!legalChars.exactMatch(packageName.mid(i, 1)))
                packageName[i] = QLatin1Char('_');

        return packageName;
    }
} // anonymous namespace

namespace Android {
namespace Internal {

Tobias Hunger's avatar
Tobias Hunger committed
82 83 84 85 86 87 88 89
AndroidManager *AndroidManager::m_instance = 0;

AndroidManager *AndroidManager::instance()
{
    return m_instance;
}

AndroidManager::~AndroidManager()
BogDan Vatra's avatar
BogDan Vatra committed
90 91 92
{
}

Tobias Hunger's avatar
Tobias Hunger committed
93
bool AndroidManager::supportsAndroid(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
94
{
Tobias Hunger's avatar
Tobias Hunger committed
95 96 97 98 99
    if (!qobject_cast<Qt4ProjectManager::Qt4Project *>(target->project()))
        return false;
    QtSupport::BaseQtVersion *version = QtSupport::QtProfileInformation::qtVersion(target->profile());
    return version && version->platformName() == QtSupport::Constants::ANDROID_PLATFORM;
}
BogDan Vatra's avatar
BogDan Vatra committed
100

Tobias Hunger's avatar
Tobias Hunger committed
101 102 103 104 105 106 107
QString AndroidManager::packageName(ProjectExplorer::Target *target)
{
    QDomDocument doc;
    if (!openManifest(target, doc))
        return QString();
    QDomElement manifestElem = doc.documentElement();
    return manifestElem.attribute(QLatin1String("package"));
BogDan Vatra's avatar
BogDan Vatra committed
108 109
}

Tobias Hunger's avatar
Tobias Hunger committed
110
bool AndroidManager::setPackageName(ProjectExplorer::Target *target, const QString &name)
BogDan Vatra's avatar
BogDan Vatra committed
111
{
Tobias Hunger's avatar
Tobias Hunger committed
112 113 114 115 116 117
    QDomDocument doc;
    if (!openManifest(target, doc))
        return false;
    QDomElement manifestElem = doc.documentElement();
    manifestElem.setAttribute(QLatin1String("package"), cleanPackageName(name));
    return saveManifest(target, doc);
BogDan Vatra's avatar
BogDan Vatra committed
118 119
}

Tobias Hunger's avatar
Tobias Hunger committed
120
QString AndroidManager::applicationName(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
121
{
Tobias Hunger's avatar
Tobias Hunger committed
122 123 124 125 126 127 128 129 130 131 132
    QDomDocument doc;
    if (!openXmlFile(target, doc, stringsPath(target)))
        return QString();
    QDomElement metadataElem = doc.documentElement().firstChildElement(QLatin1String("string"));
    while (!metadataElem.isNull()) {
        if (metadataElem.attribute(QLatin1String("name")) == QLatin1String("app_name"))
            return metadataElem.text();
        metadataElem = metadataElem.nextSiblingElement(QLatin1String("string"));
    }
    return QString();
}
BogDan Vatra's avatar
BogDan Vatra committed
133

Tobias Hunger's avatar
Tobias Hunger committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
bool AndroidManager::setApplicationName(ProjectExplorer::Target *target, const QString &name)
{
    QDomDocument doc;
    Utils::FileName path = stringsPath(target);
    if (!openXmlFile(target, doc, path))
        return false;
    QDomElement metadataElem = doc.documentElement().firstChildElement(QLatin1String("string"));
    while (!metadataElem.isNull()) {
        if (metadataElem.attribute(QLatin1String("name")) == QLatin1String("app_name")) {
            metadataElem.removeChild(metadataElem.firstChild());
            metadataElem.appendChild(doc.createTextNode(name));
            break;
        }
        metadataElem = metadataElem.nextSiblingElement(QLatin1String("string"));
    }
    return saveXmlFile(target, doc, path);
}
BogDan Vatra's avatar
BogDan Vatra committed
151

Tobias Hunger's avatar
Tobias Hunger committed
152 153 154 155 156 157 158 159 160 161 162 163 164
QStringList AndroidManager::permissions(ProjectExplorer::Target *target)
{
    QStringList per;
    QDomDocument doc;
    if (!openManifest(target, doc))
        return per;
    QDomElement permissionElem = doc.documentElement().firstChildElement(QLatin1String("uses-permission"));
    while (!permissionElem.isNull()) {
        per << permissionElem.attribute(QLatin1String("android:name"));
        permissionElem = permissionElem.nextSiblingElement(QLatin1String("uses-permission"));
    }
    return per;
}
BogDan Vatra's avatar
BogDan Vatra committed
165

Tobias Hunger's avatar
Tobias Hunger committed
166 167 168 169 170 171 172 173 174 175 176
bool AndroidManager::setPermissions(ProjectExplorer::Target *target, const QStringList &permissions)
{
    QDomDocument doc;
    if (!openManifest(target, doc))
        return false;
    QDomElement docElement = doc.documentElement();
    QDomElement permissionElem = docElement.firstChildElement(QLatin1String("uses-permission"));
    while (!permissionElem.isNull()) {
        docElement.removeChild(permissionElem);
        permissionElem = docElement.firstChildElement(QLatin1String("uses-permission"));
    }
BogDan Vatra's avatar
BogDan Vatra committed
177

Tobias Hunger's avatar
Tobias Hunger committed
178 179 180 181
    foreach (const QString &permission, permissions ) {
        permissionElem = doc.createElement(QLatin1String("uses-permission"));
        permissionElem.setAttribute(QLatin1String("android:name"), permission);
        docElement.appendChild(permissionElem);
BogDan Vatra's avatar
BogDan Vatra committed
182
    }
Tobias Hunger's avatar
Tobias Hunger committed
183 184

    return saveManifest(target, doc);
BogDan Vatra's avatar
BogDan Vatra committed
185 186
}

Tobias Hunger's avatar
Tobias Hunger committed
187
QString AndroidManager::intentName(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
188
{
Tobias Hunger's avatar
Tobias Hunger committed
189
    return packageName(target) + QLatin1Char('/') + activityName(target);
BogDan Vatra's avatar
BogDan Vatra committed
190 191
}

Tobias Hunger's avatar
Tobias Hunger committed
192
QString AndroidManager::activityName(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
193
{
Tobias Hunger's avatar
Tobias Hunger committed
194 195 196 197 198
    QDomDocument doc;
    if (!openManifest(target, doc))
        return QString();
    QDomElement activityElem = doc.documentElement().firstChildElement(QLatin1String("application")).firstChildElement(QLatin1String("activity"));
    return activityElem.attribute(QLatin1String("android:name"));
BogDan Vatra's avatar
BogDan Vatra committed
199 200
}

Tobias Hunger's avatar
Tobias Hunger committed
201
int AndroidManager::versionCode(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
202
{
Tobias Hunger's avatar
Tobias Hunger committed
203 204 205 206 207 208
    QDomDocument doc;
    if (!openManifest(target, doc))
        return 0;
    QDomElement manifestElem = doc.documentElement();
    return manifestElem.attribute(QLatin1String("android:versionCode")).toInt();
}
BogDan Vatra's avatar
BogDan Vatra committed
209

Tobias Hunger's avatar
Tobias Hunger committed
210 211 212 213 214 215 216 217 218
bool AndroidManager::setVersionCode(ProjectExplorer::Target *target, int version)
{
    QDomDocument doc;
    if (!openManifest(target, doc))
        return false;
    QDomElement manifestElem = doc.documentElement();
    manifestElem.setAttribute(QLatin1String("android:versionCode"), version);
    return saveManifest(target, doc);
}
BogDan Vatra's avatar
BogDan Vatra committed
219

Tobias Hunger's avatar
Tobias Hunger committed
220 221 222 223 224 225 226 227
QString AndroidManager::versionName(ProjectExplorer::Target *target)
{
    QDomDocument doc;
    if (!openManifest(target, doc))
        return QString();
    QDomElement manifestElem = doc.documentElement();
    return manifestElem.attribute(QLatin1String("android:versionName"));
}
BogDan Vatra's avatar
BogDan Vatra committed
228

Tobias Hunger's avatar
Tobias Hunger committed
229 230 231 232 233 234 235 236
bool AndroidManager::setVersionName(ProjectExplorer::Target *target, const QString &version)
{
    QDomDocument doc;
    if (!openManifest(target, doc))
        return false;
    QDomElement manifestElem = doc.documentElement();
    manifestElem.setAttribute(QLatin1String("android:versionName"), version);
    return saveManifest(target, doc);
BogDan Vatra's avatar
BogDan Vatra committed
237 238
}

Tobias Hunger's avatar
Tobias Hunger committed
239
QString AndroidManager::targetSDK(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
240
{
Tobias Hunger's avatar
Tobias Hunger committed
241 242 243 244 245 246 247 248 249 250 251 252
    if (!createAndroidTemplatesIfNecessary(target))
        return AndroidConfigurations::instance().bestMatch(QLatin1String("android-8"));
    QFile file(defaultPropertiesPath(target).toString());
    if (!file.open(QIODevice::ReadOnly))
        return AndroidConfigurations::instance().bestMatch(QLatin1String("android-8"));
    while (!file.atEnd()) {
        QByteArray line = file.readLine();
        if (line.startsWith("target="))
            return QString::fromLatin1(line.trimmed().mid(7));
    }
    return AndroidConfigurations::instance().bestMatch(QLatin1String("android-8"));
}
BogDan Vatra's avatar
BogDan Vatra committed
253

Tobias Hunger's avatar
Tobias Hunger committed
254 255 256 257 258
bool AndroidManager::setTargetSDK(ProjectExplorer::Target *target, const QString &sdk)
{
    updateTarget(target, sdk, applicationName(target));
    return true;
}
BogDan Vatra's avatar
BogDan Vatra committed
259

Tobias Hunger's avatar
Tobias Hunger committed
260 261 262
QIcon AndroidManager::highDpiIcon(ProjectExplorer::Target *target)
{
    return icon(target, HighDPI);
BogDan Vatra's avatar
BogDan Vatra committed
263 264
}

Tobias Hunger's avatar
Tobias Hunger committed
265
bool AndroidManager::setHighDpiIcon(ProjectExplorer::Target *target, const QString &iconFilePath)
BogDan Vatra's avatar
BogDan Vatra committed
266
{
Tobias Hunger's avatar
Tobias Hunger committed
267
    return setIcon(target, HighDPI, iconFilePath);
BogDan Vatra's avatar
BogDan Vatra committed
268 269
}

Tobias Hunger's avatar
Tobias Hunger committed
270
QIcon AndroidManager::mediumDpiIcon(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
271
{
Tobias Hunger's avatar
Tobias Hunger committed
272
    return icon(target, MediumDPI);
BogDan Vatra's avatar
BogDan Vatra committed
273 274
}

Tobias Hunger's avatar
Tobias Hunger committed
275
bool AndroidManager::setMediumDpiIcon(ProjectExplorer::Target *target, const QString &iconFilePath)
BogDan Vatra's avatar
BogDan Vatra committed
276
{
Tobias Hunger's avatar
Tobias Hunger committed
277
    return setIcon(target, MediumDPI, iconFilePath);
BogDan Vatra's avatar
BogDan Vatra committed
278 279
}

Tobias Hunger's avatar
Tobias Hunger committed
280
QIcon AndroidManager::lowDpiIcon(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
281
{
Tobias Hunger's avatar
Tobias Hunger committed
282
    return icon(target, LowDPI);
BogDan Vatra's avatar
BogDan Vatra committed
283 284
}

Tobias Hunger's avatar
Tobias Hunger committed
285
bool AndroidManager::setLowDpiIcon(ProjectExplorer::Target *target, const QString &iconFilePath)
BogDan Vatra's avatar
BogDan Vatra committed
286
{
Tobias Hunger's avatar
Tobias Hunger committed
287
    return setIcon(target, LowDPI, iconFilePath);
BogDan Vatra's avatar
BogDan Vatra committed
288 289
}

Tobias Hunger's avatar
Tobias Hunger committed
290
Utils::FileName AndroidManager::dirPath(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
291
{
Tobias Hunger's avatar
Tobias Hunger committed
292
    return Utils::FileName::fromString(target->project()->projectDirectory()).appendPath(AndroidDirName);
BogDan Vatra's avatar
BogDan Vatra committed
293 294
}

Tobias Hunger's avatar
Tobias Hunger committed
295
Utils::FileName AndroidManager::manifestPath(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
296
{
Tobias Hunger's avatar
Tobias Hunger committed
297
    return dirPath(target).appendPath(AndroidManifestName);
BogDan Vatra's avatar
BogDan Vatra committed
298 299
}

Tobias Hunger's avatar
Tobias Hunger committed
300
Utils::FileName AndroidManager::libsPath(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
301
{
Tobias Hunger's avatar
Tobias Hunger committed
302 303
    return dirPath(target).appendPath(AndroidLibsFileName);
}
BogDan Vatra's avatar
BogDan Vatra committed
304

Tobias Hunger's avatar
Tobias Hunger committed
305 306 307
Utils::FileName AndroidManager::stringsPath(ProjectExplorer::Target *target)
{
    return dirPath(target).append(AndroidStringsFileName);
BogDan Vatra's avatar
BogDan Vatra committed
308 309
}

Tobias Hunger's avatar
Tobias Hunger committed
310
Utils::FileName AndroidManager::defaultPropertiesPath(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
311
{
Tobias Hunger's avatar
Tobias Hunger committed
312 313
    return dirPath(target).appendPath(AndroidDefaultPropertiesName);
}
BogDan Vatra's avatar
BogDan Vatra committed
314

Tobias Hunger's avatar
Tobias Hunger committed
315 316 317 318
Utils::FileName AndroidManager::srcPath(ProjectExplorer::Target *target)
{
    return dirPath(target).appendPath(QLatin1String("/src"));
}
BogDan Vatra's avatar
BogDan Vatra committed
319

Tobias Hunger's avatar
Tobias Hunger committed
320 321 322 323 324 325 326 327 328 329 330 331
Utils::FileName AndroidManager::apkPath(ProjectExplorer::Target *target, BuildType buildType)
{
    return dirPath(target)
            .appendPath(QLatin1String("bin"))
            .appendPath(QString::fromLatin1("%1-%2.apk")
                        .arg(applicationName(target))
                        .arg(buildType == DebugBuild
                             ? QLatin1String("debug")
                             : (buildType == ReleaseBuildUnsigned)
                               ? QLatin1String("release-unsigned")
                               : QLatin1String("signed")));
}
BogDan Vatra's avatar
BogDan Vatra committed
332

Tobias Hunger's avatar
Tobias Hunger committed
333 334 335 336 337 338 339 340 341 342 343
QStringList AndroidManager::availableTargetApplications(ProjectExplorer::Target *target)
{
    QStringList apps;
    Qt4ProjectManager::Qt4Project *qt4Project = qobject_cast<Qt4ProjectManager::Qt4Project *>(target->project());
    foreach (Qt4ProjectManager::Qt4ProFileNode *proFile, qt4Project->applicationProFiles()) {
        if (proFile->projectType() == Qt4ProjectManager::ApplicationTemplate) {
            if (proFile->targetInformation().target.startsWith(QLatin1String("lib"))
                    && proFile->targetInformation().target.endsWith(QLatin1String(".so")))
                apps << proFile->targetInformation().target.mid(3, proFile->targetInformation().target.lastIndexOf(QLatin1Char('.')) - 3);
            else
                apps << proFile->targetInformation().target;
BogDan Vatra's avatar
BogDan Vatra committed
344 345
        }
    }
Tobias Hunger's avatar
Tobias Hunger committed
346 347 348
    apps.sort();
    return apps;
}
BogDan Vatra's avatar
BogDan Vatra committed
349

Tobias Hunger's avatar
Tobias Hunger committed
350 351 352 353 354 355 356 357 358 359 360 361 362
QString AndroidManager::targetApplication(ProjectExplorer::Target *target)
{
    QDomDocument doc;
    if (!openManifest(target, doc))
        return QString();
    QDomElement metadataElem = doc.documentElement().firstChildElement(QLatin1String("application")).firstChildElement(QLatin1String("activity")).firstChildElement(QLatin1String("meta-data"));
    while (!metadataElem.isNull()) {
        if (metadataElem.attribute(QLatin1String("android:name")) == QLatin1String("android.app.lib_name"))
            return metadataElem.attribute(QLatin1String("android:value"));
        metadataElem = metadataElem.nextSiblingElement(QLatin1String("meta-data"));
    }
    return QString();
}
BogDan Vatra's avatar
BogDan Vatra committed
363

Tobias Hunger's avatar
Tobias Hunger committed
364 365 366 367 368 369 370 371 372 373
bool AndroidManager::setTargetApplication(ProjectExplorer::Target *target, const QString &name)
{
    QDomDocument doc;
    if (!openManifest(target, doc))
        return false;
    QDomElement metadataElem = doc.documentElement().firstChildElement(QLatin1String("application")).firstChildElement(QLatin1String("activity")).firstChildElement(QLatin1String("meta-data"));
    while (!metadataElem.isNull()) {
        if (metadataElem.attribute(QLatin1String("android:name")) == QLatin1String("android.app.lib_name")) {
            metadataElem.setAttribute(QLatin1String("android:value"), name);
            return saveManifest(target, doc);
BogDan Vatra's avatar
BogDan Vatra committed
374
        }
Tobias Hunger's avatar
Tobias Hunger committed
375
        metadataElem = metadataElem.nextSiblingElement(QLatin1String("meta-data"));
BogDan Vatra's avatar
BogDan Vatra committed
376
    }
Tobias Hunger's avatar
Tobias Hunger committed
377
    return false;
BogDan Vatra's avatar
BogDan Vatra committed
378 379
}

Tobias Hunger's avatar
Tobias Hunger committed
380
QString AndroidManager::targetApplicationPath(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
381
{
Tobias Hunger's avatar
Tobias Hunger committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
    QString selectedApp = targetApplication(target);
    if (selectedApp.isEmpty())
        return QString();
    Qt4ProjectManager::Qt4Project *qt4Project = qobject_cast<Qt4ProjectManager::Qt4Project *>(target->project());
    foreach (Qt4ProjectManager::Qt4ProFileNode *proFile, qt4Project->applicationProFiles()) {
        if (proFile->projectType() == Qt4ProjectManager::ApplicationTemplate) {
            if (proFile->targetInformation().target.startsWith(QLatin1String("lib"))
                    && proFile->targetInformation().target.endsWith(QLatin1String(".so"))) {
                if (proFile->targetInformation().target.mid(3, proFile->targetInformation().target.lastIndexOf(QLatin1Char('.')) - 3)
                        == selectedApp)
                    return proFile->targetInformation().buildDir + QLatin1String("/") + proFile->targetInformation().target;
            } else {
                if (proFile->targetInformation().target == selectedApp)
                    return proFile->targetInformation().buildDir + QLatin1String("/lib") + proFile->targetInformation().target + QLatin1String(".so");
            }
        }
    }
    return QString();
BogDan Vatra's avatar
BogDan Vatra committed
400 401
}

Tobias Hunger's avatar
Tobias Hunger committed
402
bool AndroidManager::createAndroidTemplatesIfNecessary(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
403
{
Tobias Hunger's avatar
Tobias Hunger committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 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 481 482 483 484 485 486 487 488 489 490 491
    QtSupport::BaseQtVersion *version = QtSupport::QtProfileInformation::qtVersion(target->profile());
    Qt4ProjectManager::Qt4Project *qt4Project = qobject_cast<Qt4ProjectManager::Qt4Project*>(target->project());
    if (!qt4Project || !qt4Project->rootProjectNode() || !version)
        return false;

    Utils::FileName javaSrcPath
            = Utils::FileName::fromString(version->versionInfo()[QLatin1String("QT_INSTALL_PREFIX")])
            .append(QLatin1String("src/android/java"));
    QDir projectDir(qt4Project->projectDirectory());
    Utils::FileName androidPath = dirPath(target);

    QStringList m_ignoreFiles;
    bool forceUpdate = false;
    QDomDocument srcVersionDoc;
    if (openXmlFile(target, srcVersionDoc, javaSrcPath.append(QLatin1String("version.xml")), false)) {
        QDomDocument dstVersionDoc;
        if (openXmlFile(target, dstVersionDoc, androidPath.append(QLatin1String("version.xml")), false))
            forceUpdate = (srcVersionDoc.documentElement().attribute(QLatin1String("value")).toDouble()
                           > dstVersionDoc.documentElement().attribute(QLatin1String("value")).toDouble());
        else
            forceUpdate = true;

        if (forceUpdate && androidPath.toFileInfo().exists()) {
            QDomElement ignoreFile = srcVersionDoc.documentElement().firstChildElement(QLatin1String("ignore")).firstChildElement(QLatin1String("file"));
            while (!ignoreFile.isNull()) {
                m_ignoreFiles << ignoreFile.text();
                ignoreFile = ignoreFile.nextSiblingElement();
            }
        }
    }

    if (!forceUpdate && androidPath.toFileInfo().exists()
            && manifestPath(target).toFileInfo().exists()
            && androidPath.append(QLatin1String("/src")).toFileInfo().exists()
            && androidPath.append(QLatin1String("/res")).toFileInfo().exists())
        return true;

    forceUpdate &= androidPath.toFileInfo().exists();

    if (!dirPath(target).toFileInfo().exists() && !projectDir.mkdir(AndroidDirName)) {
        raiseError(tr("Error creating Android directory '%1'.").arg(AndroidDirName));
        return false;
    }

    QStringList androidFiles;
    QDirIterator it(javaSrcPath.toString(), QDirIterator::Subdirectories);
    int pos = it.path().size();
    while (it.hasNext()) {
        it.next();
        if (it.fileInfo().isDir()) {
            projectDir.mkpath(AndroidDirName + it.filePath().mid(pos));
        } else {
            const Utils::FileName dstFile = androidPath.append(it.filePath().mid(pos));
            if (m_ignoreFiles.contains(it.fileName())) {
                continue;
            } else {
                if (dstFile.toFileInfo().exists())
                    QFile::remove(dstFile.toString());
                else
                    androidFiles << dstFile.toString();
            }
            QFile::copy(it.filePath(), dstFile.toString());
        }
    }
    if (!androidFiles.isEmpty())
        qt4Project->rootProjectNode()->addFiles(ProjectExplorer::UnknownFileType, androidFiles);

    QStringList sdks = AndroidConfigurations::instance().sdkTargets();
    if (sdks.isEmpty()) {
        raiseError(tr("No Qt for Android SDKs were found.\nPlease install at least one SDK."));
        return false;
    }
    updateTarget(target, AndroidConfigurations::instance().sdkTargets().at(0));
    QStringList apps = availableTargetApplications(target);
    if (!apps.isEmpty())
        setTargetApplication(target, apps.at(0));

    QString applicationName = target->project()->displayName();
    if (!applicationName.isEmpty()) {
        setPackageName(target, packageName(target) + QLatin1Char('.') + applicationName);
        applicationName[0] = applicationName[0].toUpper();
        setApplicationName(target, applicationName);
    }

    if (forceUpdate)
        QMessageBox::warning(0, tr("Warning"), tr("Android files have been updated automatically"));

    return true;
BogDan Vatra's avatar
BogDan Vatra committed
492 493
}

Tobias Hunger's avatar
Tobias Hunger committed
494
void AndroidManager::updateTarget(ProjectExplorer::Target *target, const QString &targetSDK, const QString &name)
BogDan Vatra's avatar
BogDan Vatra committed
495
{
Tobias Hunger's avatar
Tobias Hunger committed
496
    QString androidDir = dirPath(target).toString();
BogDan Vatra's avatar
BogDan Vatra committed
497 498 499 500

    // clean previous build
    QProcess androidProc;
    androidProc.setWorkingDirectory(androidDir);
Tobias Hunger's avatar
Tobias Hunger committed
501 502
    androidProc.start(AndroidConfigurations::instance().antToolPath().toString(),
                      QStringList() << QLatin1String("clean"));
BogDan Vatra's avatar
BogDan Vatra committed
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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
    if (!androidProc.waitForFinished(-1))
        androidProc.terminate();
    // clean previous build

    int targetSDKNumber = targetSDK.mid(targetSDK.lastIndexOf(QLatin1Char('-')) + 1).toInt();
    bool commentLines = false;
    QDirIterator it(androidDir, QStringList() << QLatin1String("*.java"), QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        it.next();
        QFile file(it.filePath());
        if (!file.open(QIODevice::ReadWrite))
            continue;
        QList<QByteArray> lines = file.readAll().trimmed().split('\n');

        bool modified = false;
        bool comment = false;
        for (int i = 0; i < lines.size(); i++) {
            if (lines[i].contains("@ANDROID-")) {
                commentLines = targetSDKNumber < lines[i].mid(lines[i].lastIndexOf('-') + 1).toInt();
                comment = !comment;
                continue;
            }
            if (!comment)
                continue;
            if (commentLines) {
                if (!lines[i].trimmed().startsWith("//QtCreator")) {
                    lines[i] = "//QtCreator " + lines[i];
                    modified = true;
                }
            } else { if (lines[i].trimmed().startsWith("//QtCreator")) {
                    lines[i] = lines[i].mid(12);
                    modified = true;
                }
            }
        }
        if (modified) {
            file.resize(0);
            foreach (const QByteArray &line, lines) {
                file.write(line);
                file.write("\n");
            }
        }
        file.close();
    }

    QStringList params;
    params << QLatin1String("update") << QLatin1String("project") << QLatin1String("-p") << androidDir;
    if (!targetSDK.isEmpty())
        params << QLatin1String("-t") << targetSDK;
    if (!name.isEmpty())
        params << QLatin1String("-n") << name;
Tobias Hunger's avatar
Tobias Hunger committed
554
    androidProc.start(AndroidConfigurations::instance().androidToolPath().toString(), params);
BogDan Vatra's avatar
BogDan Vatra committed
555 556 557 558
    if (!androidProc.waitForFinished(-1))
        androidProc.terminate();
}

Tobias Hunger's avatar
Tobias Hunger committed
559
Utils::FileName AndroidManager::localLibsRulesFilePath(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
560
{
Tobias Hunger's avatar
Tobias Hunger committed
561 562 563 564 565
    QtSupport::BaseQtVersion *version = QtSupport::QtProfileInformation::qtVersion(target->profile());
    if (!version)
        return Utils::FileName();
    return Utils::FileName::fromString(version->versionInfo()[QLatin1String("QT_INSTALL_LIBS")] + QLatin1String("/rules.xml"));
}
BogDan Vatra's avatar
BogDan Vatra committed
566

Tobias Hunger's avatar
Tobias Hunger committed
567 568 569 570
QString AndroidManager::loadLocalLibs(ProjectExplorer::Target *target, int apiLevel)
{
    return loadLocal(target, apiLevel, Lib);
}
BogDan Vatra's avatar
BogDan Vatra committed
571

Tobias Hunger's avatar
Tobias Hunger committed
572 573 574 575
QString AndroidManager::loadLocalJars(ProjectExplorer::Target *target, int apiLevel)
{
    return loadLocal(target, apiLevel, Jar);
}
BogDan Vatra's avatar
BogDan Vatra committed
576

Tobias Hunger's avatar
Tobias Hunger committed
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
QStringList AndroidManager::availableQtLibs(ProjectExplorer::Target *target)
{
    QtSupport::BaseQtVersion *version = QtSupport::QtProfileInformation::qtVersion(target->profile());
    if (!target->activeRunConfiguration())
        return QStringList();

    Utils::FileName readelfPath = AndroidConfigurations::instance().readelfPath(target->activeRunConfiguration()->abi().architecture());
    QStringList libs;
    const Qt4ProjectManager::Qt4Project *const qt4Project
            = qobject_cast<const Qt4ProjectManager::Qt4Project *>(target->project());
    if (!qt4Project || !version)
        return libs;
    QString qtLibsPath = version->versionInfo()[QLatin1String("QT_INSTALL_LIBS")];
    if (!readelfPath.toFileInfo().exists()) {
        QDirIterator libsIt(qtLibsPath, QStringList() << QLatin1String("libQt*.so"));
        while (libsIt.hasNext()) {
            libsIt.next();
            libs << libsIt.fileName().mid(3, libsIt.fileName().indexOf(QLatin1Char('.')) - 3);
BogDan Vatra's avatar
BogDan Vatra committed
595
        }
Tobias Hunger's avatar
Tobias Hunger committed
596 597
        libs.sort();
        return libs;
BogDan Vatra's avatar
BogDan Vatra committed
598
    }
Tobias Hunger's avatar
Tobias Hunger committed
599 600 601 602 603 604 605
    LibrariesMap mapLibs;
    QDir libPath;
    QDirIterator it(qtLibsPath, QStringList() << QLatin1String("*.so"), QDir::Files, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        libPath = it.next();
        const QString library = libPath.absolutePath().mid(libPath.absolutePath().lastIndexOf(QLatin1Char('/')) + 1);
        mapLibs[library].dependencies = dependencies(readelfPath, libPath.absolutePath());
BogDan Vatra's avatar
BogDan Vatra committed
606 607
    }

Tobias Hunger's avatar
Tobias Hunger committed
608 609 610 611 612 613 614 615 616
    // clean dependencies
    foreach (const QString &key, mapLibs.keys()) {
        int it = 0;
        while (it < mapLibs[key].dependencies.size()) {
            const QString &dependName = mapLibs[key].dependencies[it];
            if (!mapLibs.keys().contains(dependName) && dependName.startsWith(QLatin1String("lib")) && dependName.endsWith(QLatin1String(".so"))) {
                mapLibs[key].dependencies.removeAt(it);
            } else {
                ++it;
BogDan Vatra's avatar
BogDan Vatra committed
617 618
            }
        }
Tobias Hunger's avatar
Tobias Hunger committed
619 620
        if (!mapLibs[key].dependencies.size())
            mapLibs[key].level = 0;
BogDan Vatra's avatar
BogDan Vatra committed
621 622
    }

Tobias Hunger's avatar
Tobias Hunger committed
623 624 625 626 627
    QVector<Library> qtLibraries;
    // calculate the level for every library
    foreach (const QString &key, mapLibs.keys()) {
        if (mapLibs[key].level < 0)
           setLibraryLevel(key, mapLibs);
BogDan Vatra's avatar
BogDan Vatra committed
628

Tobias Hunger's avatar
Tobias Hunger committed
629 630
        if (!mapLibs[key].name.length() && key.startsWith(QLatin1String("lib")) && key.endsWith(QLatin1String(".so")))
            mapLibs[key].name = key.mid(3, key.length() - 6);
BogDan Vatra's avatar
BogDan Vatra committed
631

Tobias Hunger's avatar
Tobias Hunger committed
632 633 634 635 636 637
        for (int it = 0; it < mapLibs[key].dependencies.size(); it++) {
            const QString &libName = mapLibs[key].dependencies[it];
            if (libName.startsWith(QLatin1String("lib")) && libName.endsWith(QLatin1String(".so")))
                mapLibs[key].dependencies[it] = libName.mid(3, libName.length() - 6);
        }
        qtLibraries.push_back(mapLibs[key]);
BogDan Vatra's avatar
BogDan Vatra committed
638
    }
Tobias Hunger's avatar
Tobias Hunger committed
639 640 641
    qSort(qtLibraries.begin(), qtLibraries.end(), qtLibrariesLessThan);
    foreach (Library lib, qtLibraries) {
        libs.push_back(lib.name);
BogDan Vatra's avatar
BogDan Vatra committed
642
    }
Tobias Hunger's avatar
Tobias Hunger committed
643
    return libs;
BogDan Vatra's avatar
BogDan Vatra committed
644 645
}

Tobias Hunger's avatar
Tobias Hunger committed
646
QStringList AndroidManager::qtLibs(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
647
{
Tobias Hunger's avatar
Tobias Hunger committed
648
    return libsXml(target, QLatin1String("qt_libs"));
BogDan Vatra's avatar
BogDan Vatra committed
649 650
}

Tobias Hunger's avatar
Tobias Hunger committed
651
bool AndroidManager::setQtLibs(ProjectExplorer::Target *target, const QStringList &libs)
BogDan Vatra's avatar
BogDan Vatra committed
652
{
Tobias Hunger's avatar
Tobias Hunger committed
653
    return setLibsXml(target, libs, QLatin1String("qt_libs"));
BogDan Vatra's avatar
BogDan Vatra committed
654 655
}

Tobias Hunger's avatar
Tobias Hunger committed
656
QStringList AndroidManager::availablePrebundledLibs(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
657
{
Tobias Hunger's avatar
Tobias Hunger committed
658 659 660 661
    QStringList libs;
    Qt4ProjectManager::Qt4Project *qt4Project = qobject_cast<Qt4ProjectManager::Qt4Project *>(target->project());
    if (!qt4Project)
        return libs;
BogDan Vatra's avatar
BogDan Vatra committed
662

Tobias Hunger's avatar
Tobias Hunger committed
663 664 665 666
    foreach (Qt4ProjectManager::Qt4ProFileNode *node, qt4Project->allProFiles())
        if (node->projectType() == Qt4ProjectManager::LibraryTemplate)
            libs << QLatin1String("lib") + node->targetInformation().target + QLatin1String(".so");
    return libs;
BogDan Vatra's avatar
BogDan Vatra committed
667 668
}

Tobias Hunger's avatar
Tobias Hunger committed
669
QStringList AndroidManager::prebundledLibs(ProjectExplorer::Target *target)
BogDan Vatra's avatar
BogDan Vatra committed
670
{
Tobias Hunger's avatar
Tobias Hunger committed
671
    return libsXml(target, QLatin1String("bundled_libs"));
BogDan Vatra's avatar
BogDan Vatra committed
672 673
}

Tobias Hunger's avatar
Tobias Hunger committed
674
bool AndroidManager::setPrebundledLibs(ProjectExplorer::Target *target, const QStringList &libs)
BogDan Vatra's avatar
BogDan Vatra committed
675
{
Tobias Hunger's avatar
Tobias Hunger committed
676
    return setLibsXml(target, libs, QLatin1String("bundled_libs"));
BogDan Vatra's avatar
BogDan Vatra committed
677 678
}

Tobias Hunger's avatar
Tobias Hunger committed
679
bool AndroidManager::openLibsXml(ProjectExplorer::Target *target, QDomDocument &doc)
BogDan Vatra's avatar
BogDan Vatra committed
680
{
Tobias Hunger's avatar
Tobias Hunger committed
681
    return openXmlFile(target, doc, libsPath(target));
BogDan Vatra's avatar
BogDan Vatra committed
682 683
}

Tobias Hunger's avatar
Tobias Hunger committed
684
bool AndroidManager::saveLibsXml(ProjectExplorer::Target *target, QDomDocument &doc)
BogDan Vatra's avatar
BogDan Vatra committed
685
{
Tobias Hunger's avatar
Tobias Hunger committed
686
    return saveXmlFile(target, doc, libsPath(target));
BogDan Vatra's avatar
BogDan Vatra committed
687 688
}

Tobias Hunger's avatar
Tobias Hunger committed
689 690
AndroidManager::AndroidManager(QObject *parent) :
    QObject(parent)
BogDan Vatra's avatar
BogDan Vatra committed
691
{
Tobias Hunger's avatar
Tobias Hunger committed
692
    m_instance = this;
BogDan Vatra's avatar
BogDan Vatra committed
693

Tobias Hunger's avatar
Tobias Hunger committed
694 695 696
//    ProjectExplorer::SessionManager *sm = ProjectExplorerPlugin::instance()->session();
//    connect(sm, SIGNAL(projectAdded(ProjectExplorer::Project*)),
//            this, SLOT(handleProjectAdditions(ProjectExplorer::Project*)));
BogDan Vatra's avatar
BogDan Vatra committed
697 698
}

Tobias Hunger's avatar
Tobias Hunger committed
699
void AndroidManager::raiseError(const QString &reason)
BogDan Vatra's avatar
BogDan Vatra committed
700
{
Tobias Hunger's avatar
Tobias Hunger committed
701
    QMessageBox::critical(0, tr("Error creating Android templates"), reason);
BogDan Vatra's avatar
BogDan Vatra committed
702 703
}

Tobias Hunger's avatar
Tobias Hunger committed
704
QString AndroidManager::loadLocal(ProjectExplorer::Target *target, int apiLevel, ItemType item)
BogDan Vatra's avatar
BogDan Vatra committed
705
{
Tobias Hunger's avatar
Tobias Hunger committed
706 707 708 709 710 711 712
    QString itemType;
    if (item == Lib)
        itemType = QLatin1String("lib");
    else
        itemType = QLatin1String("jar");

    QString localLibs;
BogDan Vatra's avatar
BogDan Vatra committed
713 714

    QDomDocument doc;
Tobias Hunger's avatar
Tobias Hunger committed
715 716 717 718 719 720 721 722 723 724 725
    if (!openXmlFile(target, doc, localLibsRulesFilePath(target)))
        return localLibs;

    QStringList libs;
    libs << qtLibs(target) << prebundledLibs(target);
    QDomElement element = doc.documentElement().firstChildElement(QLatin1String("platforms")).firstChildElement(itemType + QLatin1Char('s')).firstChildElement(QLatin1String("version"));
    while (!element.isNull()) {
        if (element.attribute(QLatin1String("value")).toInt() == apiLevel) {
            if (element.hasAttribute(QLatin1String("symlink")))
                apiLevel = element.attribute(QLatin1String("symlink")).toInt();
            break;
BogDan Vatra's avatar
BogDan Vatra committed
726
        }
Tobias Hunger's avatar
Tobias Hunger committed
727
        element = element.nextSiblingElement(QLatin1String("version"));
BogDan Vatra's avatar
BogDan Vatra committed
728 729
    }

Tobias Hunger's avatar
Tobias Hunger committed
730 731 732 733 734 735 736 737 738 739 740 741 742
    element = doc.documentElement().firstChildElement(QLatin1String("dependencies")).firstChildElement(QLatin1String("lib"));
    while (!element.isNull()) {
        if (libs.contains(element.attribute(QLatin1String("name")))) {
            QDomElement libElement = element.firstChildElement(QLatin1String("depends")).firstChildElement(itemType);
            while (!libElement.isNull()) {
                localLibs += libElement.attribute(QLatin1String("file")).arg(apiLevel) + QLatin1Char(':');
                libElement = libElement.nextSiblingElement(itemType);
            }

            libElement = element.firstChildElement(QLatin1String("replaces")).firstChildElement(itemType);
            while (!libElement.isNull()) {
                localLibs.replace(libElement.attribute(QLatin1String("file")).arg(apiLevel) + QLatin1Char(':'), QString());
                libElement = libElement.nextSiblingElement(itemType);
BogDan Vatra's avatar
BogDan Vatra committed
743 744
            }
        }
Tobias Hunger's avatar
Tobias Hunger committed
745
        element = element.nextSiblingElement(QLatin1String("lib"));
BogDan Vatra's avatar
BogDan Vatra committed
746
    }
Tobias Hunger's avatar
Tobias Hunger committed
747
    return localLibs;
BogDan Vatra's avatar
BogDan Vatra committed
748 749
}

Tobias Hunger's avatar
Tobias Hunger committed
750 751
bool AndroidManager::openXmlFile(ProjectExplorer::Target *target, QDomDocument &doc,
                                 const Utils::FileName &fileName, bool createAndroidTemplates)
BogDan Vatra's avatar
BogDan Vatra committed
752
{
Tobias Hunger's avatar
Tobias Hunger committed
753
    if (createAndroidTemplates && !createAndroidTemplatesIfNecessary(target))
BogDan Vatra's avatar
BogDan Vatra committed
754 755
        return false;

Tobias Hunger's avatar
Tobias Hunger committed
756 757
    QFile f(fileName.toString());
    if (!f.open(QIODevice::ReadOnly))
BogDan Vatra's avatar
BogDan Vatra committed
758 759
        return false;

Tobias Hunger's avatar
Tobias Hunger committed
760 761
    if (!doc.setContent(f.readAll())) {
        raiseError(tr("Can't parse '%1'").arg(fileName.toUserOutput()));
BogDan Vatra's avatar
BogDan Vatra committed
762 763
        return false;
    }
Tobias Hunger's avatar
Tobias Hunger committed
764
    return true;
BogDan Vatra's avatar
BogDan Vatra committed
765 766
}

Tobias Hunger's avatar
Tobias Hunger committed
767
bool AndroidManager::saveXmlFile(ProjectExplorer::Target *target, QDomDocument &doc, const Utils::FileName &fileName)
BogDan Vatra's avatar
BogDan Vatra committed
768
{
Tobias Hunger's avatar
Tobias Hunger committed
769 770
    if (!createAndroidTemplatesIfNecessary(target))
        return false;
BogDan Vatra's avatar
BogDan Vatra committed
771

Tobias Hunger's avatar
Tobias Hunger committed
772 773 774 775
    QFile f(fileName.toString());
    if (!f.open(QIODevice::WriteOnly)) {
        raiseError(tr("Can't open '%1'").arg(fileName.toUserOutput()));
        return false;
BogDan Vatra's avatar
BogDan Vatra committed
776
    }
Tobias Hunger's avatar
Tobias Hunger committed
777
    return f.write(doc.toByteArray(4)) >= 0;
BogDan Vatra's avatar
BogDan Vatra committed
778 779
}

Tobias Hunger's avatar
Tobias Hunger committed
780
bool AndroidManager::openManifest(ProjectExplorer::Target *target, QDomDocument &doc)
BogDan Vatra's avatar
BogDan Vatra committed
781
{
Tobias Hunger's avatar
Tobias Hunger committed
782
    return openXmlFile(target, doc, manifestPath(target));
BogDan Vatra's avatar
BogDan Vatra committed
783 784
}

Tobias Hunger's avatar
Tobias Hunger committed
785
bool AndroidManager::saveManifest(ProjectExplorer::Target *target, QDomDocument &doc)
BogDan Vatra's avatar
BogDan Vatra committed
786
{
Tobias Hunger's avatar
Tobias Hunger committed
787
    return saveXmlFile(target, doc, manifestPath(target));
BogDan Vatra's avatar
BogDan Vatra committed
788 789
}

Tobias Hunger's avatar
Tobias Hunger committed
790
QString AndroidManager::iconPath(ProjectExplorer::Target *target, AndroidManager::IconType type)
BogDan Vatra's avatar
BogDan Vatra committed
791 792 793
{
    switch (type) {
    case HighDPI:
Tobias Hunger's avatar
Tobias Hunger committed
794
        return dirPath(target).appendPath(QLatin1String("res/drawable-hdpi/icon.png")).toString();
BogDan Vatra's avatar
BogDan Vatra committed
795
    case MediumDPI:
Tobias Hunger's avatar
Tobias Hunger committed
796
        return dirPath(target).appendPath(QLatin1String("res/drawable-mdpi/icon.png")).toString();
BogDan Vatra's avatar
BogDan Vatra committed
797
    case LowDPI:
Tobias Hunger's avatar
Tobias Hunger committed
798 799 800
        return dirPath(target).appendPath(QLatin1String("res/drawable-ldpi/icon.png")).toString();
    default:
        return QString();
BogDan Vatra's avatar
BogDan Vatra committed
801 802 803
    }
}

Tobias Hunger's avatar
Tobias Hunger committed
804
QStringList AndroidManager::libsXml(ProjectExplorer::Target *target, const QString &tag)
BogDan Vatra's avatar
BogDan Vatra committed
805 806 807
{
    QStringList libs;
    QDomDocument doc;
Tobias Hunger's avatar
Tobias Hunger committed
808
    if (!openLibsXml(target, doc))
BogDan Vatra's avatar
BogDan Vatra committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
        return libs;
    QDomElement arrayElem = doc.documentElement().firstChildElement(QLatin1String("array"));
    while (!arrayElem.isNull()) {
        if (arrayElem.attribute(QLatin1String("name")) == tag) {
            arrayElem = arrayElem.firstChildElement(QLatin1String("item"));
            while (!arrayElem.isNull()) {
                libs << arrayElem.text();
                arrayElem = arrayElem.nextSiblingElement(QLatin1String("item"));
            }
            return libs;
        }
        arrayElem = arrayElem.nextSiblingElement(QLatin1String("array"));
    }
    return libs;
}

Tobias Hunger's avatar
Tobias Hunger committed
825
bool AndroidManager::setLibsXml(ProjectExplorer::Target *target, const QStringList &libs, const QString &tag)
BogDan Vatra's avatar
BogDan Vatra committed
826 827
{
    QDomDocument doc;
Tobias Hunger's avatar
Tobias Hunger committed
828
    if (!openLibsXml(target, doc))
BogDan Vatra's avatar
BogDan Vatra committed
829 830 831 832 833 834 835 836 837 838 839 840 841
        return false;
    QDomElement arrayElem = doc.documentElement().firstChildElement(QLatin1String("array"));
    while (!arrayElem.isNull()) {
        if (arrayElem.attribute(QLatin1String("name")) == tag) {
            doc.documentElement().removeChild(arrayElem);
            arrayElem = doc.createElement(QLatin1String("array"));
            arrayElem.setAttribute(QLatin1String("name"), tag);
            foreach (const QString &lib, libs) {
                QDomElement item = doc.createElement(QLatin1String("item"));
                item.appendChild(doc.createTextNode(lib));
                arrayElem.appendChild(item);
            }
            doc.documentElement().appendChild(arrayElem);
Tobias Hunger's avatar
Tobias Hunger committed
842
            return saveLibsXml(target, doc);
BogDan Vatra's avatar
BogDan Vatra committed
843 844 845 846 847 848 849
        }
        arrayElem = arrayElem.nextSiblingElement(QLatin1String("array"));
    }
    return false;
}


Tobias Hunger's avatar
Tobias Hunger committed
850
QIcon AndroidManager::icon(ProjectExplorer::Target *target, IconType type)
BogDan Vatra's avatar
BogDan Vatra committed
851
{
Tobias Hunger's avatar
Tobias Hunger committed
852
    return QIcon(iconPath(target, type));
BogDan Vatra's avatar
BogDan Vatra committed
853 854
}

Tobias Hunger's avatar
Tobias Hunger committed
855
bool AndroidManager::setIcon(ProjectExplorer::Target *target, IconType type, const QString &iconFileName)
BogDan Vatra's avatar
BogDan Vatra committed
856
{
Tobias Hunger's avatar
Tobias Hunger committed
857 858
    if (!QFileInfo(iconFileName).exists())
        return false;
BogDan Vatra's avatar
BogDan Vatra committed
859

Tobias Hunger's avatar
Tobias Hunger committed
860 861 862
    const QString path = iconPath(target, type);
    QFile::remove(path);
    return QFile::copy(iconFileName, path);
BogDan Vatra's avatar
BogDan Vatra committed
863 864
}

Tobias Hunger's avatar
Tobias Hunger committed
865
QStringList AndroidManager::dependencies(const Utils::FileName &readelfPath, const QString &lib)
BogDan Vatra's avatar
BogDan Vatra committed
866
{
Tobias Hunger's avatar
Tobias Hunger committed
867
    QStringList libs;
BogDan Vatra's avatar
BogDan Vatra committed
868

Tobias Hunger's avatar
Tobias Hunger committed
869 870
    QProcess readelfProc;
    readelfProc.start(readelfPath.toString(), QStringList() << QLatin1String("-d") << QLatin1String("-W") << lib);
BogDan Vatra's avatar
BogDan Vatra committed
871

Tobias Hunger's avatar
Tobias Hunger committed
872 873 874 875
    if (!readelfProc.waitForFinished(-1)) {
        readelfProc.terminate();
        return libs;
    }
BogDan Vatra's avatar
BogDan Vatra committed
876

Tobias Hunger's avatar
Tobias Hunger committed
877 878 879 880 881 882 883 884
    QList<QByteArray> lines = readelfProc.readAll().trimmed().split('\n');
    foreach (const QByteArray &line, lines) {
        if (line.contains("(NEEDED)") && line.contains("Shared library:") ) {
            const int pos = line.lastIndexOf('[') + 1;
            libs << QString::fromLatin1(line.mid(pos, line.lastIndexOf(']') - pos));
        }
    }
    return libs;
BogDan Vatra's avatar
BogDan Vatra committed
885 886
}

Tobias Hunger's avatar
Tobias Hunger committed
887
int AndroidManager::setLibraryLevel(const QString &library, LibrariesMap &mapLibs)
BogDan Vatra's avatar
BogDan Vatra committed
888
{
Tobias Hunger's avatar
Tobias Hunger committed
889 890 891 892 893 894 895 896 897
    int maxlevel = mapLibs[library].level;
    if (maxlevel > 0)
        return maxlevel;
    foreach (QString lib, mapLibs[library].dependencies) {
        foreach (const QString &key, mapLibs.keys()) {
            if (library == key)
                continue;
            if (key == lib) {
                int libLevel = mapLibs[key].level;
BogDan Vatra's avatar
BogDan Vatra committed
898

Tobias Hunger's avatar
Tobias Hunger committed
899 900
                if (libLevel < 0)
                    libLevel = setLibraryLevel(key, mapLibs);
BogDan Vatra's avatar
BogDan Vatra committed
901

Tobias Hunger's avatar
Tobias Hunger committed
902 903 904 905 906
                if (libLevel > maxlevel)
                    maxlevel = libLevel;
                break;
            }
        }
BogDan Vatra's avatar
BogDan Vatra committed
907
    }
Tobias Hunger's avatar
Tobias Hunger committed
908 909 910
    if (mapLibs[library].level < 0)
        mapLibs[library].level = maxlevel + 1;
    return maxlevel + 1;
BogDan Vatra's avatar
BogDan Vatra committed
911 912
}

Tobias Hunger's avatar
Tobias Hunger committed
913
bool AndroidManager::qtLibrariesLessThan(const Library &a, const Library &b)
BogDan Vatra's avatar
BogDan Vatra committed
914
{
Tobias Hunger's avatar
Tobias Hunger committed
915 916 917
    if (a.level == b.level)
        return a.name < b.name;
    return a.level < b.level;
BogDan Vatra's avatar
BogDan Vatra committed
918 919 920 921
}

} // namespace Internal
} // namespace Qt4ProjectManager