Fixed UiDevice.dumpWindowHierarchy(..) to work under instrumentation

Filenames are now relative to the application's private internal storage
directory. The previous location /data/local/tmp is not writable when
running under instrumentation.

Also changed the function to support absolute paths.

Bug: 10788452
Change-Id: I2b797f3f6b820354b231b03395782f3e8e776519
diff --git a/core/com/android/uiautomator/core/AccessibilityNodeInfoDumper.java b/core/com/android/uiautomator/core/AccessibilityNodeInfoDumper.java
index 63c51e8..1884e06 100644
--- a/core/com/android/uiautomator/core/AccessibilityNodeInfoDumper.java
+++ b/core/com/android/uiautomator/core/AccessibilityNodeInfoDumper.java
@@ -43,28 +43,6 @@
 
     /**
      * Using {@link AccessibilityNodeInfo} this method will walk the layout hierarchy
-     * and generates an xml dump into the /data/local/window_dump.xml
-     * @param root The root accessibility node.
-     * @param rotation The rotaion of current display
-     * @param width The pixel width of current display
-     * @param height The pixel height of current display
-     */
-    public static void dumpWindowToFile(AccessibilityNodeInfo root, int rotation,
-            int width, int height) {
-        File baseDir = new File(Environment.getDataDirectory(), "local");
-        if (!baseDir.exists()) {
-            baseDir.mkdir();
-            baseDir.setExecutable(true, false);
-            baseDir.setWritable(true, false);
-            baseDir.setReadable(true, false);
-        }
-        dumpWindowToFile(root,
-                new File(new File(Environment.getDataDirectory(), "local"), "window_dump.xml"),
-                rotation, width, height);
-    }
-
-    /**
-     * Using {@link AccessibilityNodeInfo} this method will walk the layout hierarchy
      * and generates an xml dump to the location specified by <code>dumpFile</code>
      * @param root The root accessibility node.
      * @param dumpFile The file to dump to.
diff --git a/core/com/android/uiautomator/core/UiDevice.java b/core/com/android/uiautomator/core/UiDevice.java
index a930eb4..2a51109 100644
--- a/core/com/android/uiautomator/core/UiDevice.java
+++ b/core/com/android/uiautomator/core/UiDevice.java
@@ -58,7 +58,7 @@
     private boolean mInWatcherContext = false;
 
     // provides access the {@link QueryController} and {@link InteractionController}
-    private UiAutomatorBridge mUiAutomationBridge;
+    private InstrumentationUiAutomatorBridge mUiAutomationBridge;
 
     // reference to self
     private static UiDevice sDevice;
@@ -70,7 +70,7 @@
     /**
      * @hide
      */
-    public void initialize(UiAutomatorBridge uiAutomatorBridge) {
+    public void initialize(InstrumentationUiAutomatorBridge uiAutomatorBridge) {
         mUiAutomationBridge = uiAutomatorBridge;
     }
 
@@ -82,7 +82,7 @@
      * Provides access the {@link QueryController} and {@link InteractionController}
      * @return {@link ShellUiAutomatorBridge}
      */
-    UiAutomatorBridge getAutomatorBridge() {
+    InstrumentationUiAutomatorBridge getAutomatorBridge() {
         if (mUiAutomationBridge == null) {
             throw new RuntimeException("UiDevice not initialized");
         }
@@ -752,7 +752,7 @@
 
     /**
      * Helper method used for debugging to dump the current window's layout hierarchy.
-     * The file root location is /data/local/tmp
+     * Relative file paths are stored the application's internal private storage location.
      *
      * @param fileName
      * @since API Level 16
@@ -765,9 +765,14 @@
             Display display = getAutomatorBridge().getDefaultDisplay();
             Point size = new Point();
             display.getSize(size);
-            AccessibilityNodeInfoDumper.dumpWindowToFile(root,
-                    new File(new File(Environment.getDataDirectory(), "local/tmp"), fileName),
-                    display.getRotation(), size.x, size.y);
+            File dumpFile = new File(fileName);
+            if (!dumpFile.isAbsolute()) {
+                dumpFile = getAutomatorBridge().getContext().getFileStreamPath(fileName);
+            }
+            AccessibilityNodeInfoDumper.dumpWindowToFile(root, dumpFile, display.getRotation(),
+                    size.x, size.y);
+            Log.d(LOG_TAG, String.format("Saved window hierarchy to %s",
+                    dumpFile.getAbsolutePath()));
         }
     }
 
diff --git a/testrunner/com/android/uiautomator/core/InstrumentationUiAutomatorBridge.java b/testrunner/com/android/uiautomator/core/InstrumentationUiAutomatorBridge.java
index 1d4fb93..34e6d23 100644
--- a/testrunner/com/android/uiautomator/core/InstrumentationUiAutomatorBridge.java
+++ b/testrunner/com/android/uiautomator/core/InstrumentationUiAutomatorBridge.java
@@ -57,4 +57,8 @@
     public long getSystemLongPressTime() {
         return ViewConfiguration.getLongPressTimeout();
     }
+
+    Context getContext() {
+        return mContext;
+    }
 }