1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.research;
18
19import android.app.IntentService;
20import android.content.Intent;
21import android.util.Log;
22
23import com.android.inputmethod.research.MotionEventReader.ReplayData;
24
25import java.io.File;
26import java.util.concurrent.TimeUnit;
27
28/**
29 * Provide a mechanism to invoke the replayer from outside.
30 *
31 * In particular, makes access from a host possible through {@code adb am startservice}.
32 */
33public class ReplayerService extends IntentService {
34    private static final String TAG = ReplayerService.class.getSimpleName();
35    private static final String EXTRA_FILENAME = "com.android.inputmethod.research.extra.FILENAME";
36    private static final long MAX_REPLAY_TIME = TimeUnit.SECONDS.toMillis(60);
37
38    public ReplayerService() {
39        super(ReplayerService.class.getSimpleName());
40    }
41
42    @Override
43    protected void onHandleIntent(final Intent intent) {
44        final String filename = intent.getStringExtra(EXTRA_FILENAME);
45        if (filename == null) return;
46
47        final ReplayData replayData = new MotionEventReader().readMotionEventData(
48                new File(filename));
49        synchronized (this) {
50            Replayer.getInstance().replay(replayData, new Runnable() {
51                @Override
52                public void run() {
53                    synchronized (ReplayerService.this) {
54                        ReplayerService.this.notify();
55                    }
56                }
57            });
58            try {
59                wait(MAX_REPLAY_TIME);
60            } catch (InterruptedException e) {
61                Log.e(TAG, "Timeout while replaying.", e);
62            }
63        }
64    }
65}
66