Guest User

Untitled

a guest
Dec 13th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. package com.mgrmobi.kitwts.common.utils;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.hardware.SensorManager;
  6. import android.support.annotation.NonNull;
  7. import android.support.annotation.Nullable;
  8. import android.view.OrientationEventListener;
  9. import android.view.Surface;
  10. import android.view.WindowManager;
  11.  
  12. import timber.log.Timber;
  13.  
  14. /**
  15. * Use it as last resort,
  16. * for example if you need handle rotation from landscape to reverseLandscape
  17. *
  18. * @author Anton Vlasov - whalemare
  19. * @since 2017
  20. */
  21. public class RotationDelegate {
  22. @NonNull
  23. private final Context appContext;
  24.  
  25. @Nullable
  26. protected Listener listener;
  27.  
  28. @Nullable
  29. private OrientationEventListener orientationEventListener;
  30.  
  31. protected int lastRotation;
  32.  
  33. protected WindowManager windowManager;
  34.  
  35.  
  36. public RotationDelegate(@NonNull Context appContext) {
  37. this.appContext = appContext;
  38. }
  39.  
  40. public RotationDelegate(@NonNull Context appContext, @NonNull ListenerImpl listener) {
  41. this(appContext);
  42. this.listener = listener;
  43. }
  44.  
  45. public void enable() {
  46. if (listener == null) {
  47. throw new IllegalArgumentException("You must set listener with #setRotationListener() or constructor");
  48. }
  49.  
  50. windowManager = (WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE);
  51.  
  52. orientationEventListener = new OrientationEventListener(appContext, SensorManager.SENSOR_DELAY_NORMAL) {
  53. @Override
  54. public void onOrientationChanged(int orientation) {
  55. if (windowManager != null && listener != null) {
  56. int newRotation = windowManager.getDefaultDisplay().getRotation();
  57. if (newRotation != lastRotation) {
  58. executeRotationChanges();
  59. }
  60. }
  61. }
  62. };
  63. orientationEventListener.enable();
  64. }
  65.  
  66. /**
  67. * Disabled {@link OrientationEventListener}. Use it in {@link Activity#onStop()}
  68. */
  69. public void disable() {
  70. if (orientationEventListener != null) {
  71. orientationEventListener.disable();
  72. orientationEventListener = null;
  73. } else {
  74. Timber.w("disable: orientationEventListener already disabled");
  75. }
  76. }
  77.  
  78. public void setRotationListener(Listener listener) {
  79. this.listener = listener;
  80. }
  81.  
  82. /**
  83. * Call it for immediately check configuration and apply changes from listeners methods.
  84. * Use it after call {@link #enable()} and {@link #setRotationListener(Listener)} methods
  85. */
  86. public void executeRotationChanges() {
  87. int newRotation = windowManager.getDefaultDisplay().getRotation();
  88. listener.onRotationChanges(lastRotation, newRotation);
  89.  
  90. if (newRotation == Surface.ROTATION_90) {
  91. listener.onLandscape();
  92. } else if (newRotation == Surface.ROTATION_270) {
  93. listener.onLandscapeReverse();
  94. } else if (newRotation == Surface.ROTATION_0) {
  95. listener.onPortrait();
  96. } else if (newRotation == Surface.ROTATION_180) {
  97. listener.onPortraitReverse();
  98. }
  99.  
  100. lastRotation = newRotation;
  101. }
  102.  
  103. public interface Listener {
  104. void onPortrait();
  105.  
  106. void onPortraitReverse();
  107.  
  108. void onLandscape();
  109.  
  110. void onLandscapeReverse();
  111.  
  112. void onRotationChanges(int oldOrientation, int newOrientation);
  113. }
  114. }
Add Comment
Please, Sign In to add comment