Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mgrmobi.kitwts.common.utils;
- import android.app.Activity;
- import android.content.Context;
- import android.hardware.SensorManager;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.view.OrientationEventListener;
- import android.view.Surface;
- import android.view.WindowManager;
- import timber.log.Timber;
- /**
- * Use it as last resort,
- * for example if you need handle rotation from landscape to reverseLandscape
- *
- * @author Anton Vlasov - whalemare
- * @since 2017
- */
- public class RotationDelegate {
- @NonNull
- private final Context appContext;
- @Nullable
- protected Listener listener;
- @Nullable
- private OrientationEventListener orientationEventListener;
- protected int lastRotation;
- protected WindowManager windowManager;
- public RotationDelegate(@NonNull Context appContext) {
- this.appContext = appContext;
- }
- public RotationDelegate(@NonNull Context appContext, @NonNull ListenerImpl listener) {
- this(appContext);
- this.listener = listener;
- }
- public void enable() {
- if (listener == null) {
- throw new IllegalArgumentException("You must set listener with #setRotationListener() or constructor");
- }
- windowManager = (WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE);
- orientationEventListener = new OrientationEventListener(appContext, SensorManager.SENSOR_DELAY_NORMAL) {
- @Override
- public void onOrientationChanged(int orientation) {
- if (windowManager != null && listener != null) {
- int newRotation = windowManager.getDefaultDisplay().getRotation();
- if (newRotation != lastRotation) {
- executeRotationChanges();
- }
- }
- }
- };
- orientationEventListener.enable();
- }
- /**
- * Disabled {@link OrientationEventListener}. Use it in {@link Activity#onStop()}
- */
- public void disable() {
- if (orientationEventListener != null) {
- orientationEventListener.disable();
- orientationEventListener = null;
- } else {
- Timber.w("disable: orientationEventListener already disabled");
- }
- }
- public void setRotationListener(Listener listener) {
- this.listener = listener;
- }
- /**
- * Call it for immediately check configuration and apply changes from listeners methods.
- * Use it after call {@link #enable()} and {@link #setRotationListener(Listener)} methods
- */
- public void executeRotationChanges() {
- int newRotation = windowManager.getDefaultDisplay().getRotation();
- listener.onRotationChanges(lastRotation, newRotation);
- if (newRotation == Surface.ROTATION_90) {
- listener.onLandscape();
- } else if (newRotation == Surface.ROTATION_270) {
- listener.onLandscapeReverse();
- } else if (newRotation == Surface.ROTATION_0) {
- listener.onPortrait();
- } else if (newRotation == Surface.ROTATION_180) {
- listener.onPortraitReverse();
- }
- lastRotation = newRotation;
- }
- public interface Listener {
- void onPortrait();
- void onPortraitReverse();
- void onLandscape();
- void onLandscapeReverse();
- void onRotationChanges(int oldOrientation, int newOrientation);
- }
- }
Add Comment
Please, Sign In to add comment