[go: nahoru, domu]

1/*
2 * Copyright (C) 2015 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 android.util;
18
19import android.app.Activity;
20import android.app.Instrumentation;
21import android.content.pm.ActivityInfo;
22
23import android.test.ActivityInstrumentationTestCase2;
24
25import com.android.internal.util.Preconditions;
26
27/**
28 * Utilities for manipulating screen orientation.
29 */
30public final class OrientationUtil {
31
32    private final Activity mActivity;
33    private final Instrumentation mInstrumentation;
34
35    private final Runnable mSetToPortrait = new Runnable() {
36        @Override
37        public void run() {
38            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
39        }
40    };
41
42    private final Runnable mSetToLandscape = new Runnable() {
43        @Override
44        public void run() {
45            mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
46        }
47    };
48
49    public static OrientationUtil initializeAndStartActivityIfNotStarted(
50            ActivityInstrumentationTestCase2 testCase) {
51        Preconditions.checkNotNull(testCase);
52        return new OrientationUtil(testCase.getActivity(), testCase.getInstrumentation());
53    }
54
55    private OrientationUtil(Activity activity, Instrumentation instrumentation) {
56        mActivity = activity;
57        mInstrumentation = instrumentation;
58    }
59
60    public void setPortraitOrientation() {
61        mInstrumentation.runOnMainSync(mSetToPortrait);
62    }
63
64    public void setLandscapeOrientation() {
65        mInstrumentation.runOnMainSync(mSetToLandscape);
66    }
67}
68