1/*
2 * Copyright (C) 2010 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 */
16package com.android.monkeyrunner.recorder;
17
18import com.android.chimpchat.ChimpChat;
19import com.android.chimpchat.core.IChimpDevice;
20import com.android.monkeyrunner.MonkeyDevice;
21
22import java.awt.event.WindowAdapter;
23import java.awt.event.WindowEvent;
24import java.util.logging.Level;
25import java.util.logging.Logger;
26
27import javax.swing.WindowConstants;
28
29/**
30 * Helper entry point for MonkeyRecorder.
31 */
32public class MonkeyRecorder {
33    private static final Logger LOG = Logger.getLogger(MonkeyRecorder.class.getName());
34    // This lock is used to keep the python process blocked while the frame is runing.
35    private static final Object LOCK = new Object();
36
37    /**
38     * Jython entry point for MonkeyRecorder.  Meant to be called like this:
39     *
40     * <code>
41     * from com.android.monkeyrunner import MonkeyRunner as mr
42     * from com.android.monkeyrunner import MonkeyRecorder
43     * MonkeyRecorder.start(mr.waitForConnection())
44     * </code>
45     *
46     * @param device
47     */
48    public static void start(final MonkeyDevice device) {
49        start(device.getImpl());
50    }
51
52    /* package */static void start(final IChimpDevice device) {
53        MonkeyRecorderFrame frame = new MonkeyRecorderFrame(device);
54        // TODO: this is a hack until the window listener works.
55        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
56        frame.addWindowListener(new WindowAdapter() {
57            @Override
58            public void windowClosed(WindowEvent e) {
59                device.dispose();
60                synchronized (LOCK) {
61                    LOCK.notifyAll();
62                }
63            }
64        });
65
66        frame.setVisible(true);
67        synchronized (LOCK) {
68            try {
69                LOCK.wait();
70            } catch (InterruptedException e) {
71                LOG.log(Level.SEVERE, "Unexpected Exception", e);
72            }
73        }
74    }
75
76    public static void main(String[] args) {
77        ChimpChat chimp = ChimpChat.getInstance();
78        MonkeyRecorder.start(chimp.waitForConnection());
79    }
80}
81