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.accessibilityservice.UiTestAutomationBridge; 20import android.os.Environment; 21import android.view.accessibility.AccessibilityNodeInfo; 22 23import com.android.commands.uiautomator.Launcher.Command; 24import com.android.uiautomator.core.AccessibilityNodeInfoDumper; 25 26import java.io.File; 27 28/** 29 * Implementation of the dump subcommand 30 * 31 * This creates an XML dump of current UI hierarchy 32 */ 33public class DumpCommand extends Command { 34 35 private static final File DEFAULT_DUMP_FILE = new File( 36 Environment.getLegacyExternalStorageDirectory(), "window_dump.xml"); 37 38 public DumpCommand() { 39 super("dump"); 40 } 41 42 @Override 43 public String shortHelp() { 44 return "creates an XML dump of current UI hierarchy"; 45 } 46 47 @Override 48 public String detailedOptions() { 49 return " dump [file]\n" 50 + " [file]: the location where the dumped XML should be stored, default is\n " 51 + DEFAULT_DUMP_FILE.getAbsolutePath() + "\n"; 52 } 53 54 @Override 55 public void run(String[] args) { 56 File dumpFile = DEFAULT_DUMP_FILE; 57 if (args.length > 0) { 58 dumpFile = new File(args[0]); 59 } 60 UiTestAutomationBridge bridge = new UiTestAutomationBridge(); 61 bridge.connect(); 62 // It appears that the bridge needs time to be ready. Making calls to the 63 // bridge immediately after connecting seems to cause exceptions. So let's also 64 // do a wait for idle in case the app is busy. 65 bridge.waitForIdle(1000, 1000 * 10); 66 AccessibilityNodeInfo info = bridge.getRootAccessibilityNodeInfoInActiveWindow(); 67 if (info == null) { 68 System.err.println("ERROR: null root node returned by UiTestAutomationBridge."); 69 return; 70 } 71 AccessibilityNodeInfoDumper.dumpWindowToFile(info, dumpFile); 72 bridge.disconnect(); 73 System.out.println( 74 String.format("UI hierchary dumped to: %s", dumpFile.getAbsolutePath())); 75 } 76 77} 78