clean up tmp files

screenshot and xml dumps are pulled from device and stored into
tmp directories, this leaves a lot of traces behind until the
system reboots. we now keep track of temp directories created
and delete them when the app exits.

Change-Id: I3190dd93c3b413afc0a513eaedf11409f3e5efa8
diff --git a/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/UiAutomatorModel.java b/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/UiAutomatorModel.java
index ce7379d..0a1fab0 100644
--- a/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/UiAutomatorModel.java
+++ b/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/UiAutomatorModel.java
@@ -28,6 +28,7 @@
 import org.eclipse.swt.graphics.Rectangle;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.List;
 
 public class UiAutomatorModel {
@@ -41,6 +42,7 @@
     private BasicTreeNode mSelectedNode;
     private Rectangle mCurrentDrawingRect;
     private List<Rectangle> mNafNodes;
+    private List<File> mTmpDirs;
 
     // determines whether we lookup the leaf UI node on mouse move of screenshot image
     private boolean mExploreMode = true;
@@ -49,6 +51,7 @@
 
     private UiAutomatorModel(UiAutomatorViewer view) {
         mView = view;
+        mTmpDirs = new ArrayList<File>();
     }
 
     public static UiAutomatorModel createInstance(UiAutomatorViewer view) {
@@ -216,4 +219,22 @@
     public boolean shouldShowNafNodes() {
         return mShowNafNodes;
     }
+
+    /**
+     * Registers a temporary directory for deletion when app exists
+     *
+     * @param tmpDir
+     */
+    public void registerTempDirectory(File tmpDir) {
+        mTmpDirs.add(tmpDir);
+    }
+
+    /**
+     * Performs cleanup tasks when the app is exiting
+     */
+    public void cleanUp() {
+        for (File dir : mTmpDirs) {
+            Utils.deleteRecursive(dir);
+        }
+    }
 }
diff --git a/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/UiAutomatorViewer.java b/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/UiAutomatorViewer.java
index bb3e893..d0d33cf 100644
--- a/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/UiAutomatorViewer.java
+++ b/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/UiAutomatorViewer.java
@@ -285,6 +285,7 @@
             UiAutomatorViewer window = new UiAutomatorViewer();
             window.setBlockOnOpen(true);
             window.open();
+            UiAutomatorModel.getModel().cleanUp();
         } catch (Exception e) {
             e.printStackTrace();
         }
diff --git a/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/Utils.java b/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/Utils.java
new file mode 100644
index 0000000..5306fe3
--- /dev/null
+++ b/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/Utils.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.uiautomator;
+
+import java.io.File;
+
+public class Utils {
+    public static void deleteRecursive(File file) {
+        if (file.isDirectory()) {
+            File[] children = file.listFiles();
+            for (File child : children) {
+                if (!child.getName().startsWith("."))
+                    deleteRecursive(child);
+            }
+        }
+        file.delete();
+    }
+}
diff --git a/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/actions/ScreenshotAction.java b/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/actions/ScreenshotAction.java
index 9bff372..7d1eaa3 100644
--- a/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/actions/ScreenshotAction.java
+++ b/uiautomator/utils/uiautomatorviewer/src/com/android/uiautomator/actions/ScreenshotAction.java
@@ -89,6 +89,7 @@
                         showError("Cannot get temp directory", e, monitor);
                         return;
                     }
+                    UiAutomatorModel.getModel().registerTempDirectory(tmpDir);
 
                     // boiler plates to do a bunch of adb stuff to take XML snapshot and screenshot
                     monitor.beginTask("Getting UI status dump from device...",