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