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