am 26cdbb78: SDK Updater: Fix to allow install from directory URL (i.e. auto-guess the repository.xml correctly.)

Merge commit '26cdbb788a9137d04587bc678d7aa7f49e3671fc'

* commit '26cdbb788a9137d04587bc678d7aa7f49e3671fc':
  SDK Updater: Fix to allow install from directory URL (i.e. auto-guess the
diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java
index 61b348a..ef62f6e 100644
--- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java
+++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/AndroidVersion.java
@@ -174,7 +174,7 @@
         } else if (obj instanceof String) {
             // if we have a code name, this must match.
             if (mCodename != null) {
-                return mCodename.equals((String)obj);
+                return mCodename.equals(obj);
             }
 
             // else we try to convert to a int and compare to the api level
diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Archive.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Archive.java
index 3757b74..9fa614a 100755
--- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Archive.java
+++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Archive.java
@@ -23,6 +23,7 @@
 import org.apache.commons.compress.archivers.zip.ZipFile;

 

 import java.io.File;

+import java.io.FileNotFoundException;

 import java.io.FileOutputStream;

 import java.io.IOException;

 import java.io.InputStream;

@@ -572,6 +573,10 @@
 

             return true;

 

+        } catch (FileNotFoundException e) {

+            // The FNF message is just the URL. Make it a bit more useful.

+            monitor.setResult("File not found: %1$s", e.getMessage());

+

         } catch (Exception e) {

             monitor.setResult(e.getMessage());

 

@@ -723,8 +728,7 @@
 

             byte[] buf = new byte[65536];

 

-            Enumeration<ZipArchiveEntry> entries =

-                    (Enumeration<ZipArchiveEntry>)zipFile.getEntries();

+            Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();

             while (entries.hasMoreElements()) {

                 ZipArchiveEntry entry = entries.nextElement();

 

diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Package.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Package.java
index a637fde..8d19c0f 100755
--- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Package.java
+++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Package.java
@@ -332,7 +332,7 @@
      * current one, it's not an update.

      *

      * @param replacementPackage The potential replacement package.

-     * @return

+     * @return One of the {@link UpdateInfo} values.

      *

      * @see #sameItemAs(Package)

      */

diff --git a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/RepoSource.java b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/RepoSource.java
index 5de2e94..cd2bacb 100755
--- a/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/RepoSource.java
+++ b/tools/sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/RepoSource.java
@@ -47,7 +47,7 @@
  */

 public class RepoSource implements IDescription {

 

-    private final String mUrl;

+    private String mUrl;

     private final boolean mUserSource;

 

     private Package[] mPackages;

@@ -132,55 +132,51 @@
         monitor.setDescription("Fetching %1$s", url);

         monitor.incProgress(1);

 

-        ByteArrayInputStream xml = null;

+        mFetchError = null;

+        Exception[] exception = new Exception[] { null };

+        ByteArrayInputStream xml = fetchUrl(url, exception);

+        boolean validated = false;

+        if (xml != null) {

+            monitor.setDescription("Validate XML");

+            validated = validateXml(xml, url, monitor);

+        }

 

-        for (int tentative = 0; tentative < 2 && xml == null; tentative++) {

+        // If we failed the first time and the URL doesn't explicitly end with

+        // our filename, make another tentative after changing the URL.

+        if (!validated && !url.endsWith(SdkRepository.URL_DEFAULT_XML_FILE)) {

+            if (!url.endsWith("/")) {       //$NON-NLS-1$

+                url += "/";                 //$NON-NLS-1$

+            }

+            url += SdkRepository.URL_DEFAULT_XML_FILE;

 

-            // reset fetch error and fetch

-            mFetchError = null;

-            xml = fetchUrl(url, monitor);

+            xml = fetchUrl(url, exception);

+            if (xml != null) {

+                validated = validateXml(xml, url, monitor);

+            }

 

-            if (xml == null) {

-                mDescription += String.format("\nFailed to fetch URL %1$s", url);

-                mFetchError = "Failed to fetch URL";

-                monitor.setResult("Failed to fetch URL %1$s", url);

-            } else {

-                // We got a document. It might not be XML or it might not be valid.

+            if (validated) {

+                // If the second tentative succeeded, indicate it in the console

+                // with the URL that worked.

+                monitor.setResult("Repository found at %1$s", url);

 

-                monitor.setDescription("Validate XML");

+                // Keep the modified URL

+                mUrl = url;

+            }

+        }

 

-                if (validateXml(xml, url, monitor)) {

-                    // We got a valid XML, keep it and use it.

+        if (!validated) {

+            mFetchError = "Failed to fetch URL";

 

-                    if (tentative > 0) {

-                        // If the second tentative succeeded, indicate it in the console,

-                        // otherwise the user will only see the first failure

-                        // message and will think the whole thing failed. This also

-                        // indicates we modifed the URL.

-                        monitor.setResult("Repository found instead at %1$s", url);

-                    }

-                    break;

-                } else {

-                    mDescription += String.format("\nFailed to validate XML at %1$s", url);

-                    mFetchError = "Failed to validate XML";

-                    monitor.setResult("Failed to validate XML at %1$s", url);

-

-                    // forget this XML, it wasn't any good.

-                    xml = null;

+            String reason = "Unknown";

+            if (exception[0] != null) {

+                if (exception[0] instanceof FileNotFoundException) {

+                    reason = "File not found";

+                } else if (exception[0].getMessage() != null) {

+                    reason = exception[0].getMessage();

                 }

             }

 

-            // If we failed the first time and the URL doesn't explicitly end with

-            // our filename, make another tentative. Otherwise abort.

-            if (tentative == 0 && !url.endsWith(SdkRepository.URL_DEFAULT_XML_FILE)) {

-                if (!url.endsWith("/")) {       //$NON-NLS-1$

-                    url += "/";                 //$NON-NLS-1$

-                }

-                url += SdkRepository.URL_DEFAULT_XML_FILE;

-            } else {

-                break;

-            }

-

+            monitor.setResult("Failed to fetch URL %1$s, reason:", url, reason);

         }

 

         monitor.incProgress(1);

@@ -219,7 +215,7 @@
      * Java URL Reader: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html

      * Java set Proxy: http://java.sun.com/docs/books/tutorial/networking/urls/_setProxy.html

      */

-    private ByteArrayInputStream fetchUrl(String urlString, ITaskMonitor monitor) {

+    private ByteArrayInputStream fetchUrl(String urlString, Exception[] outException) {

         URL url;

         try {

             url = new URL(urlString);

@@ -255,12 +251,8 @@
                 }

             }

 

-        } catch (FileNotFoundException e) {

-            // The FNF message is just the URL. Make it a bit more useful.

-            monitor.setResult("File not found: %1$s", e.getMessage());

-

         } catch (IOException e) {

-            monitor.setResult(e.getMessage());

+            outException[0] = e;

         }

 

         return null;