Robustness on logging sandbox

Test: unit tests
Bug: 185680163
Change-Id: I8ba7a09108d830af6f402432d983edc6b8a1a326
diff --git a/src/com/android/tradefed/sandbox/TradefedSandbox.java b/src/com/android/tradefed/sandbox/TradefedSandbox.java
index b2de972..7c84e7a 100644
--- a/src/com/android/tradefed/sandbox/TradefedSandbox.java
+++ b/src/com/android/tradefed/sandbox/TradefedSandbox.java
@@ -557,12 +557,19 @@
 
     private void logAndCleanHeapDump(File heapDumpDir, ITestLogger logger) {
         try {
-            if (heapDumpDir != null && heapDumpDir.listFiles().length != 0) {
-                for (File f : heapDumpDir.listFiles()) {
-                    FileInputStreamSource fileInput = new FileInputStreamSource(f);
-                    logger.testLog(f.getName(), LogDataType.HPROF, fileInput);
-                    StreamUtil.cancel(fileInput);
-                }
+            if (heapDumpDir == null) {
+                return;
+            }
+            if (!heapDumpDir.isDirectory()) {
+                return;
+            }
+            if (heapDumpDir.listFiles().length == 0) {
+                return;
+            }
+            for (File f : heapDumpDir.listFiles()) {
+                FileInputStreamSource fileInput = new FileInputStreamSource(f);
+                logger.testLog(f.getName(), LogDataType.HPROF, fileInput);
+                StreamUtil.cancel(fileInput);
             }
         } finally {
             FileUtil.recursiveDelete(heapDumpDir);