@Test public void lockOrientation_toLandscapeWhenReverseLandscape_shouldBeReverseLandscape() { testResources.getConfiguration().orientation = Configuration.ORIENTATION_LANDSCAPE; MoPubShadowDisplay.setStaticRotation(Surface.ROTATION_180); DeviceUtils.lockOrientation(testActivity, CreativeOrientation.LANDSCAPE); assertThat(testActivity.getRequestedOrientation()).isEqualTo(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); }
public static void lockOrientation(@NonNull Activity activity, @NonNull CreativeOrientation creativeOrientation) { if (!Preconditions.NoThrow.checkNotNull(creativeOrientation) || !Preconditions.NoThrow.checkNotNull(activity)) { return; } Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); final int currentRotation = display.getRotation(); final int deviceOrientation = activity.getResources().getConfiguration().orientation; final int currentOrientation = getScreenOrientationFromRotationAndOrientation(currentRotation, deviceOrientation); int requestedOrientation; if (CreativeOrientation.PORTRAIT == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } } else if (CreativeOrientation.LANDSCAPE == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } } else { return; } activity.setRequestedOrientation(requestedOrientation); }
DeviceUtils { public static void lockOrientation(@NonNull Activity activity, @NonNull CreativeOrientation creativeOrientation) { if (!Preconditions.NoThrow.checkNotNull(creativeOrientation) || !Preconditions.NoThrow.checkNotNull(activity)) { return; } Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); final int currentRotation = display.getRotation(); final int deviceOrientation = activity.getResources().getConfiguration().orientation; final int currentOrientation = getScreenOrientationFromRotationAndOrientation(currentRotation, deviceOrientation); int requestedOrientation; if (CreativeOrientation.PORTRAIT == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } } else if (CreativeOrientation.LANDSCAPE == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } } else { return; } activity.setRequestedOrientation(requestedOrientation); } }
DeviceUtils { public static void lockOrientation(@NonNull Activity activity, @NonNull CreativeOrientation creativeOrientation) { if (!Preconditions.NoThrow.checkNotNull(creativeOrientation) || !Preconditions.NoThrow.checkNotNull(activity)) { return; } Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); final int currentRotation = display.getRotation(); final int deviceOrientation = activity.getResources().getConfiguration().orientation; final int currentOrientation = getScreenOrientationFromRotationAndOrientation(currentRotation, deviceOrientation); int requestedOrientation; if (CreativeOrientation.PORTRAIT == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } } else if (CreativeOrientation.LANDSCAPE == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } } else { return; } activity.setRequestedOrientation(requestedOrientation); } private  DeviceUtils(); }
DeviceUtils { public static void lockOrientation(@NonNull Activity activity, @NonNull CreativeOrientation creativeOrientation) { if (!Preconditions.NoThrow.checkNotNull(creativeOrientation) || !Preconditions.NoThrow.checkNotNull(activity)) { return; } Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); final int currentRotation = display.getRotation(); final int deviceOrientation = activity.getResources().getConfiguration().orientation; final int currentOrientation = getScreenOrientationFromRotationAndOrientation(currentRotation, deviceOrientation); int requestedOrientation; if (CreativeOrientation.PORTRAIT == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } } else if (CreativeOrientation.LANDSCAPE == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } } else { return; } activity.setRequestedOrientation(requestedOrientation); } private  DeviceUtils(); static String getIpAddress(IP ip); static String getHashedUdid(final Context context); static boolean isNetworkAvailable(@Nullable final Context context); static int memoryCacheSizeBytes(final Context context); static long diskCacheSizeBytes(File dir, long minSize); static long diskCacheSizeBytes(File dir); static int getScreenOrientation(@NonNull final Activity activity); static void lockOrientation(@NonNull Activity activity, @NonNull CreativeOrientation creativeOrientation); @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) static Point getDeviceDimensions(@NonNull final Context context); }
DeviceUtils { public static void lockOrientation(@NonNull Activity activity, @NonNull CreativeOrientation creativeOrientation) { if (!Preconditions.NoThrow.checkNotNull(creativeOrientation) || !Preconditions.NoThrow.checkNotNull(activity)) { return; } Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); final int currentRotation = display.getRotation(); final int deviceOrientation = activity.getResources().getConfiguration().orientation; final int currentOrientation = getScreenOrientationFromRotationAndOrientation(currentRotation, deviceOrientation); int requestedOrientation; if (CreativeOrientation.PORTRAIT == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } } else if (CreativeOrientation.LANDSCAPE == creativeOrientation) { if (ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE == currentOrientation) { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; } else { requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } } else { return; } activity.setRequestedOrientation(requestedOrientation); } private  DeviceUtils(); static String getIpAddress(IP ip); static String getHashedUdid(final Context context); static boolean isNetworkAvailable(@Nullable final Context context); static int memoryCacheSizeBytes(final Context context); static long diskCacheSizeBytes(File dir, long minSize); static long diskCacheSizeBytes(File dir); static int getScreenOrientation(@NonNull final Activity activity); static void lockOrientation(@NonNull Activity activity, @NonNull CreativeOrientation creativeOrientation); @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) static Point getDeviceDimensions(@NonNull final Context context);  }