Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / public / android / java / src / org / chromium / content / browser / ScreenOrientationProvider.java
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
5 package org.chromium.content.browser;
6
7 import android.app.Activity;
8 import android.content.pm.ActivityInfo;
9 import android.content.pm.PackageManager;
10 import android.util.Log;
11
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;
17
18 /**
19  * This is the implementation of the C++ counterpart ScreenOrientationProvider.
20  */
21 @JNINamespace("content")
22 class ScreenOrientationProvider {
23     private static final String TAG = "ScreenOrientationProvider";
24
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;
43             default:
44                 Log.w(TAG, "Trying to lock to unsupported orientation!");
45                 return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
46         }
47     }
48
49     @CalledByNative
50     static void lockOrientation(byte orientations) {
51         Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
52         if (activity == null) {
53             return;
54         }
55
56         int orientation = getOrientationFromWebScreenOrientations(orientations);
57         if (orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
58             return;
59         }
60
61         activity.setRequestedOrientation(orientation);
62     }
63
64     @CalledByNative
65     static void unlockOrientation() {
66         Activity activity = ApplicationStatus.getLastTrackedFocusedActivity();
67         if (activity == null) {
68             return;
69         }
70
71         int defaultOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
72
73         try {
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.
79         } finally {
80             activity.setRequestedOrientation(defaultOrientation);
81         }
82     }
83
84     @CalledByNative
85     static void startAccurateListening() {
86         ThreadUtils.runOnUiThread(new Runnable() {
87             @Override
88             public void run() {
89                 ScreenOrientationListener.getInstance().startAccurateListening();
90             }
91         });
92     }
93
94     @CalledByNative
95     static void stopAccurateListening() {
96         ThreadUtils.runOnUiThread(new Runnable() {
97             @Override
98             public void run() {
99                 ScreenOrientationListener.getInstance().stopAccurateListening();
100             }
101         });
102     }
103
104     private ScreenOrientationProvider() {
105     }
106 }