DumpCommand.java revision 23296fc6448cd265fbb45c1fd9041976ae0da274
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.commands.uiautomator;
18
19import android.app.UiAutomation;
20import android.graphics.Point;
21import android.hardware.display.DisplayManagerGlobal;
22import android.os.Environment;
23import android.view.Display;
24import android.view.accessibility.AccessibilityNodeInfo;
25
26import com.android.commands.uiautomator.Launcher.Command;
27import com.android.uiautomator.core.AccessibilityNodeInfoDumper;
28import com.android.uiautomator.core.UiAutomationShellWrapper;
29
30import java.io.File;
31import java.util.concurrent.TimeoutException;
32
33/**
34 * Implementation of the dump subcommand
35 *
36 * This creates an XML dump of current UI hierarchy
37 */
38public class DumpCommand extends Command {
39
40    private static final File DEFAULT_DUMP_FILE = new File(
41            Environment.getLegacyExternalStorageDirectory(), "window_dump.xml");
42
43    public DumpCommand() {
44        super("dump");
45    }
46
47    @Override
48    public String shortHelp() {
49        return "creates an XML dump of current UI hierarchy";
50    }
51
52    @Override
53    public String detailedOptions() {
54        return "    dump [file]\n"
55            + "      [file]: the location where the dumped XML should be stored, default is\n      "
56            + DEFAULT_DUMP_FILE.getAbsolutePath() + "\n";
57    }
58
59    @Override
60    public void run(String[] args) {
61        File dumpFile = DEFAULT_DUMP_FILE;
62        if (args.length > 0) {
63            dumpFile = new File(args[0]);
64        }
65        UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
66        automationWrapper.connect();
67        // It appears that the bridge needs time to be ready. Making calls to the
68        // bridge immediately after connecting seems to cause exceptions. So let's also
69        // do a wait for idle in case the app is busy.
70        try {
71            UiAutomation uiAutomation = automationWrapper.getUiAutomation();
72            uiAutomation.waitForIdle(1000, 1000 * 10);
73            AccessibilityNodeInfo info = uiAutomation.getRootInActiveWindow();
74            if (info == null) {
75                System.err.println("ERROR: null root node returned by UiTestAutomationBridge.");
76                return;
77            }
78
79            Display display =
80                    DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
81            int rotation = display.getRotation();
82            Point size = new Point();
83            display.getSize(size);
84            AccessibilityNodeInfoDumper.dumpWindowToFile(info, dumpFile, rotation, size.x, size.y);
85        } catch (TimeoutException re) {
86            System.err.println("ERROR: could not get idle state.");
87            return;
88        } finally {
89            automationWrapper.disconnect();
90        }
91        System.out.println(
92                String.format("UI hierchary dumped to: %s", dumpFile.getAbsolutePath()));
93    }
94}
95