Last active
December 11, 2017 13:27
-
-
Save whalemare/7d7e5550393104398dd314c71a274e54 to your computer and use it in GitHub Desktop.
Rotation changes helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add to manifest something like this: