From c1b2ed036fc9ca2b14bc9d39c2d79d7ed858a12a Mon Sep 17 00:00:00 2001
From: Alessandro Portale <alessandro.portale@digia.com>
Date: Tue, 4 Mar 2014 23:38:21 +0100
Subject: [PATCH] Android: More accurate Android Sdk location error messages

If the user installs a fresh Android SDK and selects its path in
the Android options, Qt Creator will still say that this is not
a valid Android SDK.

The reason is that Qt Creator also checks for the platform tools
to be installed. Installing those is a separate step which needs
to be done after installing the SDK.

This patch enables Qt Creator to tell the user if the platform
tools are missing, but the SDK is otherwise fine.

Change-Id: I3557fb93d46e8677498843250302d12c8babb1df
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@digia.com>
---
 src/plugins/android/androidsettingswidget.cpp | 37 ++++++++++++-------
 src/plugins/android/androidsettingswidget.h   |  2 +
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/src/plugins/android/androidsettingswidget.cpp b/src/plugins/android/androidsettingswidget.cpp
index 8d1beec12e9..de2894c5dfc 100644
--- a/src/plugins/android/androidsettingswidget.cpp
+++ b/src/plugins/android/androidsettingswidget.cpp
@@ -155,20 +155,10 @@ void AndroidSettingsWidget::check(AndroidSettingsWidget::Mode mode)
 {
     if (mode & Sdk) {
         m_sdkState = Okay;
-        if (m_androidConfig.sdkLocation().isEmpty()) {
+        if (m_androidConfig.sdkLocation().isEmpty())
             m_sdkState = NotSet;
-        } else {
-            Utils::FileName adb = m_androidConfig.sdkLocation();
-            Utils::FileName androidExe = m_androidConfig.sdkLocation();
-            Utils::FileName androidBat = m_androidConfig.sdkLocation();
-            Utils::FileName emulator = m_androidConfig.sdkLocation();
-            if (!adb.appendPath(QLatin1String("platform-tools/adb" QTC_HOST_EXE_SUFFIX)).toFileInfo().exists()
-                    || (!androidExe.appendPath(QLatin1String("/tools/android" QTC_HOST_EXE_SUFFIX)).toFileInfo().exists()
-                        && !androidBat.appendPath(QLatin1String("/tools/android" ANDROID_BAT_SUFFIX)).toFileInfo().exists())
-                    || !emulator.appendPath(QLatin1String("/tools/emulator" QTC_HOST_EXE_SUFFIX)).toFileInfo().exists()) {
-                m_sdkState = Error;
-            }
-        }
+        else if (!(sdkLocationIsValid() && sdkPlatformToolsInstalled()))
+            m_sdkState = Error;
     }
 
     if (mode & Ndk) {
@@ -241,7 +231,10 @@ void AndroidSettingsWidget::applyToUi(AndroidSettingsWidget::Mode mode)
             m_ui->sdkWarningIconLabel->setVisible(true);
             m_ui->sdkWarningLabel->setVisible(true);
             Utils::FileName location = Utils::FileName::fromUserInput(m_ui->SDKLocationLineEdit->text());
-            m_ui->sdkWarningLabel->setText(tr("\"%1\" does not seem to be an Android SDK top folder.").arg(location.toUserOutput()));
+            if (sdkLocationIsValid())
+                m_ui->sdkWarningLabel->setText(tr("The Platform tools are missing. Please use the Android SDK Manager to install them."));
+            else
+                m_ui->sdkWarningLabel->setText(tr("\"%1\" does not seem to be an Android SDK top folder.").arg(location.toUserOutput()));
         } else {
             m_ui->sdkWarningIconLabel->setVisible(false);
             m_ui->sdkWarningLabel->setVisible(false);
@@ -300,6 +293,22 @@ void AndroidSettingsWidget::applyToUi(AndroidSettingsWidget::Mode mode)
     }
 }
 
+bool AndroidSettingsWidget::sdkLocationIsValid() const
+{
+    Utils::FileName androidExe = m_androidConfig.sdkLocation();
+    Utils::FileName androidBat = m_androidConfig.sdkLocation();
+    Utils::FileName emulator = m_androidConfig.sdkLocation();
+    return (androidExe.appendPath(QLatin1String("/tools/android" QTC_HOST_EXE_SUFFIX)).toFileInfo().exists()
+            || androidBat.appendPath(QLatin1String("/tools/android" ANDROID_BAT_SUFFIX)).toFileInfo().exists())
+            && emulator.appendPath(QLatin1String("/tools/emulator" QTC_HOST_EXE_SUFFIX)).toFileInfo().exists();
+}
+
+bool AndroidSettingsWidget::sdkPlatformToolsInstalled() const
+{
+    Utils::FileName adb = m_androidConfig.sdkLocation();
+    return adb.appendPath(QLatin1String("platform-tools/adb" QTC_HOST_EXE_SUFFIX)).toFileInfo().exists();
+}
+
 void AndroidSettingsWidget::saveSettings()
 {
     sdkLocationEditingFinished();
diff --git a/src/plugins/android/androidsettingswidget.h b/src/plugins/android/androidsettingswidget.h
index eebd20d1637..5e6fb1238d1 100644
--- a/src/plugins/android/androidsettingswidget.h
+++ b/src/plugins/android/androidsettingswidget.h
@@ -98,6 +98,8 @@ private:
     enum State { NotSet = 0, Okay = 1, Error = 2 };
     void check(Mode mode);
     void applyToUi(Mode mode);
+    bool sdkLocationIsValid() const;
+    bool sdkPlatformToolsInstalled() const;
 
     State m_sdkState;
     State m_ndkState;
-- 
GitLab