1/*
2 * Copyright (C) 2012 Google Inc.
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.os.RemoteException;
21import android.view.IWindowManager;
22/**
23 * monkey screen rotation event
24 */
25public class MonkeyRotationEvent extends MonkeyEvent {
26
27    private final int mRotationDegree;
28    private final boolean mPersist;
29
30    /**
31     * Construct a rotation Event.
32     *
33     * @param degree Possible rotation degrees, see constants in
34     * anroid.view.Suface.
35     * @param persist Should we keep the rotation lock after the orientation
36     * change.
37     */
38    public MonkeyRotationEvent(int degree, boolean persist) {
39        super(EVENT_TYPE_ROTATION);
40        mRotationDegree = degree;
41        mPersist = persist;
42    }
43
44    @Override
45    public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
46        if (verbose > 0) {
47            System.out.println(":Sending rotation degree=" + mRotationDegree +
48                               ", persist=" + mPersist);
49        }
50
51        // inject rotation event
52        try {
53            iwm.freezeRotation(mRotationDegree);
54            if (!mPersist) {
55                iwm.thawRotation();
56            }
57            return MonkeyEvent.INJECT_SUCCESS;
58        } catch (RemoteException ex) {
59            return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
60        }
61    }
62}
63