1a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu/*
2a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * Copyright (C) 2012 The Android Open Source Project
3a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu *
4a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * Licensed under the Apache License, Version 2.0 (the "License");
5a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * you may not use this file except in compliance with the License.
6a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * You may obtain a copy of the License at
7a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu *
8a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu *      http://www.apache.org/licenses/LICENSE-2.0
9a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu *
10a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * Unless required by applicable law or agreed to in writing, software
11a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * distributed under the License is distributed on an "AS IS" BASIS,
12a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * See the License for the specific language governing permissions and
14a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * limitations under the License.
15a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu */
16a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
17a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhupackage com.android.commands.uiautomator;
18a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
19104ac2482e8b44fb5e5fb817b5724331e4ec7fbeSvetoslav Ganovimport android.app.UiAutomation.OnAccessibilityEventListener;
20a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhuimport android.view.accessibility.AccessibilityEvent;
21a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
22a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhuimport com.android.commands.uiautomator.Launcher.Command;
23104ac2482e8b44fb5e5fb817b5724331e4ec7fbeSvetoslav Ganovimport com.android.uiautomator.core.UiAutomationShellWrapper;
24a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
25a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhuimport java.text.SimpleDateFormat;
26a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhuimport java.util.Date;
27a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
28a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu/**
29a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * Implementation of the events subcommand
30a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu *
31a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu * Prints out accessibility events until process is stopped.
32a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu */
33a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhupublic class EventsCommand extends Command {
34a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
35a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    private Object mQuitLock = new Object();
36a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
37a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    public EventsCommand() {
38a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu        super("events");
39a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    }
40a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
41a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    @Override
42a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    public String shortHelp() {
43a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu        return "prints out accessibility events until terminated";
44a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    }
45a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
46a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    @Override
47a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    public String detailedOptions() {
48a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu        return null;
49a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    }
50a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu
51a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    @Override
52a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    public void run(String[] args) {
53104ac2482e8b44fb5e5fb817b5724331e4ec7fbeSvetoslav Ganov        UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
54104ac2482e8b44fb5e5fb817b5724331e4ec7fbeSvetoslav Ganov        automationWrapper.connect();
55104ac2482e8b44fb5e5fb817b5724331e4ec7fbeSvetoslav Ganov        automationWrapper.getUiAutomation().setOnAccessibilityEventListener(
56104ac2482e8b44fb5e5fb817b5724331e4ec7fbeSvetoslav Ganov                new OnAccessibilityEventListener() {
57a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu            @Override
58a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu            public void onAccessibilityEvent(AccessibilityEvent event) {
59a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu                SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
60a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu                System.out.println(String.format("%s %s",
61a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu                        formatter.format(new Date()), event.toString()));
62a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu            }
63104ac2482e8b44fb5e5fb817b5724331e4ec7fbeSvetoslav Ganov        });
64a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu        // there's really no way to stop, essentially we just block indefinitely here and wait
65a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu        // for user to press Ctrl+C
66a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu        synchronized (mQuitLock) {
67a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu            try {
68a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu                mQuitLock.wait();
69a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu            } catch (InterruptedException e) {
70a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu                e.printStackTrace();
71a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu            }
72a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu        }
73104ac2482e8b44fb5e5fb817b5724331e4ec7fbeSvetoslav Ganov        automationWrapper.disconnect();
74a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu    }
75a5c65de4744dca37b46de0acf8be11a9c24cc91bGuang Zhu}
76