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 [--verbose][file]\n"
55            + "      [--compressed]: dumps compressed layout information.\n"
56            + "      [file]: the location where the dumped XML should be stored, default is\n      "
57            + DEFAULT_DUMP_FILE.getAbsolutePath() + "\n";
58    }
59
60    @Override
61    public void run(String[] args) {
62        File dumpFile = DEFAULT_DUMP_FILE;
63        boolean verboseMode = true;
64
65        for (String arg : args) {
66            if (arg.equals("--compressed"))
67                verboseMode = false;
68            else if (!arg.startsWith("-")) {
69                dumpFile = new File(arg);
70            }
71        }
72
73        UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
74        automationWrapper.connect();
75        if (verboseMode) {
76            // default
77            automationWrapper.setCompressedLayoutHierarchy(false);
78        } else {
79            automationWrapper.setCompressedLayoutHierarchy(true);
80        }
81
82        // It appears that the bridge needs time to be ready. Making calls to the
83        // bridge immediately after connecting seems to cause exceptions. So let's also
84        // do a wait for idle in case the app is busy.
85        try {
86            UiAutomation uiAutomation = automationWrapper.getUiAutomation();
87            uiAutomation.waitForIdle(1000, 1000 * 10);
88            AccessibilityNodeInfo info = uiAutomation.getRootInActiveWindow();
89            if (info == null) {
90                System.err.println("ERROR: null root node returned by UiTestAutomationBridge.");
91                return;
92            }
93
94            Display display =
95                    DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
96            int rotation = display.getRotation();
97            Point size = new Point();
98            display.getSize(size);
99            AccessibilityNodeInfoDumper.dumpWindowToFile(info, dumpFile, rotation, size.x, size.y);
100        } catch (TimeoutException re) {
101            System.err.println("ERROR: could not get idle state.");
102            return;
103        } finally {
104            automationWrapper.disconnect();
105        }
106        System.out.println(
107                String.format("UI hierchary dumped to: %s", dumpFile.getAbsolutePath()));
108    }
109}
110