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.OnAccessibilityEventListener;
20import android.view.accessibility.AccessibilityEvent;
21
22import com.android.commands.uiautomator.Launcher.Command;
23import com.android.uiautomator.core.UiAutomationShellWrapper;
24
25import java.text.SimpleDateFormat;
26import java.util.Date;
27
28/**
29 * Implementation of the events subcommand
30 *
31 * Prints out accessibility events until process is stopped.
32 */
33public class EventsCommand extends Command {
34
35    private Object mQuitLock = new Object();
36
37    public EventsCommand() {
38        super("events");
39    }
40
41    @Override
42    public String shortHelp() {
43        return "prints out accessibility events until terminated";
44    }
45
46    @Override
47    public String detailedOptions() {
48        return null;
49    }
50
51    @Override
52    public void run(String[] args) {
53        UiAutomationShellWrapper automationWrapper = new UiAutomationShellWrapper();
54        automationWrapper.connect();
55        automationWrapper.getUiAutomation().setOnAccessibilityEventListener(
56                new OnAccessibilityEventListener() {
57            @Override
58            public void onAccessibilityEvent(AccessibilityEvent event) {
59                SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss.SSS");
60                System.out.println(String.format("%s %s",
61                        formatter.format(new Date()), event.toString()));
62            }
63        });
64        // there's really no way to stop, essentially we just block indefinitely here and wait
65        // for user to press Ctrl+C
66        synchronized (mQuitLock) {
67            try {
68                mQuitLock.wait();
69            } catch (InterruptedException e) {
70                e.printStackTrace();
71            }
72        }
73        automationWrapper.disconnect();
74    }
75}
76