ScreenOrientationProvider.java revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.content.browser;
6
7import android.app.Activity;
8import android.content.pm.ActivityInfo;
9import android.util.Log;
10
11import com.google.common.annotations.VisibleForTesting;
12
13import org.chromium.base.ApplicationStatus;
14import org.chromium.base.CalledByNative;
15import org.chromium.base.JNINamespace;
16import org.chromium.content.common.ScreenOrientationValues;
17
18/**
19 * This is the implementation of the C++ counterpart ScreenOrientationProvider.
20 */
21@JNINamespace("content")
22class ScreenOrientationProvider {
23    private static final String TAG = "ScreenOrientationProvider";
24
25    private int getOrientationFromWebScreenOrientations(byte orientations) {
26        switch (orientations) {
27            case ScreenOrientationValues.DEFAULT:
28                return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
29            case ScreenOrientationValues.PORTRAIT_PRIMARY:
30                return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
31            case ScreenOrientationValues.PORTRAIT_SECONDARY:
32                return ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
33            case ScreenOrientationValues.LANDSCAPE_PRIMARY:
34                return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
35            case ScreenOrientationValues.LANDSCAPE_SECONDARY:
36                return ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
37            case ScreenOrientationValues.PORTRAIT:
38                return ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;
39            case ScreenOrientationValues.LANDSCAPE:
40                return ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
41            case ScreenOrientationValues.ANY:
42                return ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR;
43            default:
44                Log.w(TAG, "Trying to lock to unsupported orientation!");
45                return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
46        }
47    }
48
49    @VisibleForTesting
50    @CalledByNative
51    static ScreenOrientationProvider create() {
52        return new ScreenOrientationProvider();
53    }
54
55    @CalledByNative
56    void lockOrientation(byte orientations) {
57        Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
58        if (activity == null) {
59            return;
60        }
61
62        int orientation = getOrientationFromWebScreenOrientations(orientations);
63        if (orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
64            return;
65        }
66
67        activity.setRequestedOrientation(orientation);
68    }
69
70    @CalledByNative
71    void unlockOrientation() {
72        Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
73        if (activity == null) {
74            return;
75        }
76
77        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
78    }
79
80    private ScreenOrientationProvider() {
81    }
82}
83