Last active
August 29, 2015 13:56
-
-
Save fada21/8935622 to your computer and use it in GitHub Desktop.
Detects orientation change
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.fada21.android.orientation; | |
import java.util.concurrent.locks.ReentrantLock; | |
import android.content.Context; | |
import android.content.res.Configuration; | |
import android.view.OrientationEventListener; | |
import android.view.Surface; | |
import android.view.WindowManager; | |
/** | |
* Detects orientation change. Implement method {@link SimpleOrientationListener#onSimpleOrientationChanged(int)} to react for changes. Remember to enable | |
* listener via {@link SimpleOrientationListener#enable()} and release via {@link SimpleOrientationListener#disable()} | |
* | |
* | |
*/ | |
public abstract class SimpleOrientationListener extends OrientationEventListener { | |
private Context ctx; | |
public int prevOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; | |
public static final int CONFIGURATION_ORIENTATION_UNDEFINED = Configuration.ORIENTATION_UNDEFINED; | |
private volatile int defaultScreenOrientation = CONFIGURATION_ORIENTATION_UNDEFINED; | |
private ReentrantLock lock = new ReentrantLock(true); | |
public SimpleOrientationListener(Context context) { | |
super(context); | |
ctx = context; | |
} | |
public SimpleOrientationListener(Context context, int rate) { | |
super(context, rate); | |
ctx = context; | |
} | |
@Override | |
public void onOrientationChanged(final int orientation) { | |
int currentOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; | |
if (orientation >= 330 || orientation < 30) { | |
currentOrientation = Surface.ROTATION_0; | |
} else if (orientation >= 60 && orientation < 120) { | |
currentOrientation = Surface.ROTATION_90; | |
} else if (orientation >= 150 && orientation < 210) { | |
currentOrientation = Surface.ROTATION_180; | |
} else if (orientation >= 240 && orientation < 300) { | |
currentOrientation = Surface.ROTATION_270; | |
} | |
if (prevOrientation != currentOrientation && orientation != OrientationEventListener.ORIENTATION_UNKNOWN) { | |
prevOrientation = currentOrientation; | |
if (currentOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) | |
reportOrientationChanged(currentOrientation); | |
} | |
} | |
private void reportOrientationChanged(final int currentOrientation) { | |
int defaultOrientation = getDeviceDefaultOrientation(); | |
int orthogonalOrientation = defaultOrientation == Configuration.ORIENTATION_LANDSCAPE ? Configuration.ORIENTATION_PORTRAIT | |
: Configuration.ORIENTATION_LANDSCAPE; | |
int toReportOrientation; | |
if (currentOrientation == Surface.ROTATION_0 || currentOrientation == Surface.ROTATION_180) | |
toReportOrientation = defaultOrientation; | |
else | |
toReportOrientation = orthogonalOrientation; | |
onSimpleOrientationChanged(toReportOrientation); | |
} | |
/** | |
* Must determine what is default device orientation (some tablets can have default landscape). Must be initialized when device orientation is defined. | |
* | |
* @return value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} | |
*/ | |
private int getDeviceDefaultOrientation() { | |
if (defaultScreenOrientation == CONFIGURATION_ORIENTATION_UNDEFINED) { | |
lock.lock(); | |
defaultScreenOrientation = initDeviceDefaultOrientation(ctx); | |
lock.unlock(); | |
} | |
return defaultScreenOrientation; | |
} | |
/** | |
* Provides device default orientation | |
* | |
* @return value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} | |
*/ | |
private int initDeviceDefaultOrientation(Context context) { | |
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
Configuration config = context.getResources().getConfiguration(); | |
int rotation = windowManager.getDefaultDisplay().getRotation(); | |
boolean isLand = config.orientation == Configuration.ORIENTATION_LANDSCAPE; | |
boolean isDefaultAxis = rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180; | |
int result = CONFIGURATION_ORIENTATION_UNDEFINED; | |
if ((isDefaultAxis && isLand) || (!isDefaultAxis && !isLand)) { | |
result = Configuration.ORIENTATION_LANDSCAPE; | |
} | |
else { | |
result = Configuration.ORIENTATION_PORTRAIT; | |
} | |
return result; | |
} | |
/** | |
* Fires when orientation changes from landscape to portrait and vice versa. | |
* | |
* @param orientation | |
* value of {@link Configuration#ORIENTATION_LANDSCAPE} or {@link Configuration#ORIENTATION_PORTRAIT} | |
*/ | |
public abstract void onSimpleOrientationChanged(int orientation); | |
} |
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
private void checkForceOrientation() { | |
if(Utils.isTablet() && getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){ | |
((ViewGroup)root).addView(forceOrientationView, ((ViewGroup)root).getChildCount(), new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); | |
}else{ | |
((ViewGroup)root).removeView(forceOrientationView); | |
} | |
} | |
@Override | |
public void onConfigurationChanged(Configuration newConfig) { | |
super.onConfigurationChanged(newConfig); | |
checkForceOrientation(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment