OSDN Git Service

SDK Manager: fix File.listfiles()
authorRaphael Moll <ralf@android.com>
Tue, 12 Oct 2010 20:06:48 +0000 (13:06 -0700)
committerRaphael Moll <ralf@android.com>
Tue, 12 Oct 2010 20:26:32 +0000 (13:26 -0700)
This fixes a couple instances of File.listfiles() that
were not validating that either a/ the file is a directory
or b/ the list is not null.

This also adds a couple toString() methods to some repo
classes, which are nice when debugging.

Change-Id: I8912d12c5344c8b511d84a58fe4693632315dff0

sdkmanager/libs/sdklib/src/com/android/sdklib/internal/avd/AvdManager.java
sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Archive.java
sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/LocalSdkParser.java
sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/Package.java
sdkmanager/libs/sdklib/src/com/android/sdklib/internal/repository/SdkSource.java
sdkmanager/libs/sdklib/src/com/android/sdklib/io/FolderWrapper.java
sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/ArchiveInfo.java

index 963fc3d..27d849c 100644 (file)
@@ -1062,16 +1062,19 @@ public final class AvdManager {
      * @throws SecurityException like {@link File#delete()} does if file/folder is not writable.
      */
     private boolean deleteContentOf(File folder) throws SecurityException {
-        for (File f : folder.listFiles()) {
-            if (f.isDirectory()) {
-                if (deleteContentOf(f) == false) {
+        File[] files = folder.listFiles();
+        if (files != null) {
+            for (File f : files) {
+                if (f.isDirectory()) {
+                    if (deleteContentOf(f) == false) {
+                        return false;
+                    }
+                }
+                if (f.delete() == false) {
                     return false;
                 }
-            }
-            if (f.delete() == false) {
-                return false;
-            }
 
+            }
         }
 
         return true;
index 86f041c..3f2f966 100755 (executable)
@@ -333,6 +333,22 @@ public class Archive implements IDescription {
     }\r
 \r
     /**\r
+     * Returns the short description of the source, if not null.\r
+     * Otherwise returns the default Object toString result.\r
+     * <p/>\r
+     * This is mostly helpful for debugging.\r
+     * For UI display, use the {@link IDescription} interface.\r
+     */\r
+    @Override\r
+    public String toString() {\r
+        String s = getShortDescription();\r
+        if (s != null) {\r
+            return s;\r
+        }\r
+        return super.toString();\r
+    }\r
+\r
+    /**\r
      * Generates a short description for this archive.\r
      */\r
     public String getShortDescription() {\r
index 51549f3..8d79ced 100755 (executable)
@@ -238,8 +238,11 @@ public class LocalSdkParser {
         // We're not going to check that all tools are present. At the very least\r
         // we should expect to find android and an emulator adapted to the current OS.\r
         Set<String> names = new HashSet<String>();\r
-        for (File file : toolFolder.listFiles()) {\r
-            names.add(file.getName());\r
+        File[] files = toolFolder.listFiles();\r
+        if (files != null) {\r
+            for (File file : files) {\r
+                names.add(file.getName());\r
+            }\r
         }\r
         if (!names.contains(SdkConstants.androidCmdName()) ||\r
                 !names.contains(SdkConstants.FN_EMULATOR)) {\r
index 48c3e79..a6c28a9 100755 (executable)
@@ -345,6 +345,22 @@ public abstract class Package implements IDescription, Comparable<Package> {
     }\r
 \r
     /**\r
+     * Returns the short description of the source, if not null.\r
+     * Otherwise returns the default Object toString result.\r
+     * <p/>\r
+     * This is mostly helpful for debugging.\r
+     * For UI display, use the {@link IDescription} interface.\r
+     */\r
+    @Override\r
+    public String toString() {\r
+        String s = getShortDescription();\r
+        if (s != null) {\r
+            return s;\r
+        }\r
+        return super.toString();\r
+    }\r
+\r
+    /**\r
      * Returns a short description for an {@link IDescription}.\r
      * Can be empty but not null.\r
      */\r
index cee7fd6..db6067b 100755 (executable)
@@ -171,6 +171,22 @@ public abstract class SdkSource implements IDescription {
         mPackages = null;\r
     }\r
 \r
+    /**\r
+     * Returns the short description of the source, if not null.\r
+     * Otherwise returns the default Object toString result.\r
+     * <p/>\r
+     * This is mostly helpful for debugging.\r
+     * For UI display, use the {@link IDescription} interface.\r
+     */\r
+    @Override\r
+    public String toString() {\r
+        String s = getShortDescription();\r
+        if (s != null) {\r
+            return s;\r
+        }\r
+        return super.toString();\r
+    }\r
+\r
     public String getShortDescription() {\r
 \r
         // TODO extract domain from URL and add to UiName if not present.\r
index d009b7f..a4601d3 100644 (file)
@@ -83,15 +83,17 @@ public class FolderWrapper extends File implements IAbstractFolder {
 
     public IAbstractResource[] listMembers() {
         File[] files = listFiles();
-        final int count = files.length;
+        final int count = files == null ? 0 : files.length;
         IAbstractResource[] afiles = new IAbstractResource[count];
 
-        for (int i = 0 ; i < count ; i++) {
-            File f = files[i];
-            if (f.isFile()) {
-                afiles[i] = new FileWrapper(f);
-            } else {
-                afiles[i] = new FolderWrapper(f);
+        if (files != null) {
+            for (int i = 0 ; i < count ; i++) {
+                File f = files[i];
+                if (f.isFile()) {
+                    afiles[i] = new FileWrapper(f);
+                } else {
+                    afiles[i] = new FolderWrapper(f);
+                }
             }
         }
 
@@ -135,7 +137,7 @@ public class FolderWrapper extends File implements IAbstractFolder {
 
     public String[] list(FilenameFilter filter) {
         File[] files = listFiles();
-        if (files.length > 0) {
+        if (files != null && files.length > 0) {
             ArrayList<String> list = new ArrayList<String>();
 
             for (File file : files) {
index 593f007..038b86a 100755 (executable)
@@ -177,4 +177,22 @@ class ArchiveInfo implements IDescription {
         }\r
         return "";\r
     }\r
+\r
+    /**\r
+     * Returns the short description of the parent package of the new archive, if not null.\r
+     * Otherwise returns the default Object toString result.\r
+     * <p/>\r
+     * This is mostly helpful for debugging. For UI display, use the {@link IDescription}\r
+     * interface.\r
+     */\r
+    @Override\r
+    public String toString() {\r
+        if (mNewArchive != null) {\r
+            Package p = mNewArchive.getParentPackage();\r
+            if (p != null) {\r
+                return p.getShortDescription();\r
+            }\r
+        }\r
+        return super.toString();\r
+    }\r
 }\r