1/*
2 * Copyright (C) 2008 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.monkey;
18
19import android.app.IActivityManager;
20import android.content.ComponentName;
21import android.content.Intent;
22import android.os.Bundle;
23import android.os.RemoteException;
24import android.view.IWindowManager;
25
26/**
27 * monkey activity event
28 */
29public class MonkeyActivityEvent extends MonkeyEvent {
30    private ComponentName mApp;
31    long mAlarmTime = 0;
32
33    public MonkeyActivityEvent(ComponentName app) {
34        super(EVENT_TYPE_ACTIVITY);
35        mApp = app;
36    }
37
38    public MonkeyActivityEvent(ComponentName app, long arg) {
39        super(EVENT_TYPE_ACTIVITY);
40        mApp = app;
41        mAlarmTime = arg;
42    }
43
44    /**
45     * @return Intent for the new activity
46     */
47    private Intent getEvent() {
48        Intent intent = new Intent(Intent.ACTION_MAIN);
49        intent.addCategory(Intent.CATEGORY_LAUNCHER);
50        intent.setComponent(mApp);
51        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
52        return intent;
53    }
54
55    @Override
56    public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
57        Intent intent = getEvent();
58        if (verbose > 0) {
59            System.out.println(":Switch: " + intent.toUri(0));
60        }
61
62        if (mAlarmTime != 0){
63            Bundle args = new Bundle();
64            args.putLong("alarmTime", mAlarmTime);
65            intent.putExtras(args);
66        }
67
68        try {
69            iam.startActivity(null, intent, null, null, null, 0,
70                    0, null, null, null);
71        } catch (RemoteException e) {
72            System.err.println("** Failed talking with activity manager!");
73            return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
74        } catch (SecurityException e) {
75            if (verbose > 0) {
76                System.out.println("** Permissions error starting activity "
77                        + intent.toUri(0));
78            }
79            return MonkeyEvent.INJECT_ERROR_SECURITY_EXCEPTION;
80        }
81        return MonkeyEvent.INJECT_SUCCESS;
82    }
83}
84