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.
5 package org.chromium.content.browser;
7 import android.app.Activity;
8 import android.content.pm.ActivityInfo;
9 import android.content.pm.PackageManager;
10 import android.util.Log;
12 import org.chromium.base.ApplicationStatus;
13 import org.chromium.base.CalledByNative;
14 import org.chromium.base.JNINamespace;
15 import org.chromium.base.ThreadUtils;
16 import org.chromium.content.common.ScreenOrientationValues;
19 * This is the implementation of the C++ counterpart ScreenOrientationProvider.
21 @JNINamespace("content")
22 class ScreenOrientationProvider {
23 private static final String TAG = "ScreenOrientationProvider";
25 private static 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;
44 Log.w(TAG, "Trying to lock to unsupported orientation!");
45 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
50 static void lockOrientation(byte orientations) {
51 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
52 if (activity == null) {
56 int orientation = getOrientationFromWebScreenOrientations(orientations);
57 if (orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
61 activity.setRequestedOrientation(orientation);
65 static void unlockOrientation() {
66 Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
67 if (activity == null) {
71 int defaultOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
74 ActivityInfo info = activity.getPackageManager().getActivityInfo(
75 activity.getComponentName(), PackageManager.GET_META_DATA);
76 defaultOrientation = info.screenOrientation;
77 } catch (PackageManager.NameNotFoundException e) {
78 // Do nothing, defaultOrientation should be SCREEN_ORIENTATION_UNSPECIFIED.
80 activity.setRequestedOrientation(defaultOrientation);
85 static void startAccurateListening() {
86 ThreadUtils.runOnUiThread(new Runnable() {
89 ScreenOrientationListener.getInstance().startAccurateListening();
95 static void stopAccurateListening() {
96 ThreadUtils.runOnUiThread(new Runnable() {
99 ScreenOrientationListener.getInstance().stopAccurateListening();
104 private ScreenOrientationProvider() {