package com.miviclin.droidengine2d.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ConfigurationInfo;
import android.media.AudioManager;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;

import com.miviclin.droidengine2d.BuildConfig;

/**
 * ActivityUtilities.
 * 
 * @author Miguel Vicente Linares
 * 
 */
public class ActivityUtilities {

	/**
	 * Checks if the device supports OpenGL ES 2.0.<br>
	 * NOTE: If the app is running on the Android emulator, this method returns always true.
	 * 
	 * @param activity Activity.
	 * @return true if OpenGL ES 2.0 is supported, false otherwise
	 */
	public static boolean detectOpenGLES20(Activity activity) {
		ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
		ConfigurationInfo info = am.getDeviceConfigurationInfo();
		if (BuildConfig.DEBUG) {
			if (Build.FINGERPRINT.startsWith("generic")) {
				return true;
			}
		}
		return (info.reqGlEsVersion >= 0x20000);
	}

	/**
	 * Changes the orientation of the Activity to PORTRAIT.
	 * 
	 * @param activity Activity.
	 */
	public static void setOrientationToPortrait(Activity activity) {
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	/**
	 * Changes the orientation of the Activity to LANDSCAPE.
	 * 
	 * @param activity Activity.
	 */
	public static void setOrientationToLandscape(Activity activity) {
		activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	}

	/**
	 * Hides the title of the Activity.
	 * 
	 * @param activity Activity.
	 */
	public static void hideTitle(Activity activity) {
		activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	/**
	 * Sets the Activity to full screen.
	 * 
	 * @param activity Activity.
	 */
	public static void setToFullscreen(Activity activity) {
		activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
	}

	/**
	 * Sets the Activity to keep the screen on as long as it is visible to the user.
	 * 
	 * @param activity Activity.
	 */
	public static void keepScreenOn(Activity activity) {
		activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	/**
	 * Sets the Activity to allow the hardware buttons to control the volume of music playback.
	 * 
	 * @param activity Activity.
	 */
	public static void controlMusicPlayBackStream(Activity activity) {
		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
	}

}

@@ -158,24 +158,29 @@
 tent.pm.
-Activity
+Configuration
 Info;%0Aim
@@ -192,34 +192,35 @@
 android.content.
-pm
+res
 .ConfigurationIn
@@ -221,47 +221,8 @@
 tion
-Info;%0Aimport android.media.AudioManager
 ;%0Aim
@@ -264,22 +264,23 @@
 id.view.
-Window
+Display
 ;%0Aimport
@@ -293,29 +293,23 @@
 id.view.
-WindowManager
+Surface
 ;%0A%0Aimpor
@@ -430,16 +430,22 @@
 %0Apublic 
+final 
 class Ac
@@ -463,16 +463,148 @@
 ties %7B%0A%0A
+%09/**%0A%09 * Private constructor to prevent the instantiation of this utility class.%0A%09 */%0A%09private ActivityUtilities() %7B%0A%09%09super();%0A%09%7D%0A%0A
 %09/**%0A%09 *
@@ -1226,1538 +1226,1083 @@
  * C
-hanges the orientation of the Activity to PORTRAIT.%0A%09 * %0A%09 * @param activity Activity.%0A%09 */%0A%09public static void setOrientationToPortrait(Activity activity) %7B%0A%09%09activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);%0A%09%7D%0A%0A%09/**%0A%09 * Changes the orientation of the Activity to LANDSCAPE.%0A%09 * %0A%09 * @param activity Activity.%0A%09 */%0A%09public static void setOrientationToLandscape(Activity activity) %7B%0A%09%09activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);%0A%09%7D%0A%0A%09/**%0A%09 * Hides the title of the Activity.%0A%09 * %0A%09 * @param activity Activity.%0A%09 */%0A%09public static void hideTitle(Activity activity) %7B%0A%09%09activity.requestWindowFeature(Window.FEATURE_NO_TITLE);%0A%09%7D%0A%0A%09/**%0A%09 * Sets the Activity to full screen.%0A%09 * %0A%09 * @param activity Activity.%0A%09 */%0A%09public static void setToFullscreen(Activity activity) %7B%0A%09%09activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN
+alculates the default orientation of the device.%3Cbr%3E%0A%09 * The default orientation of most phones is portrait, and the default orientation of most tablets is landscape.%0A%09 * %0A%09 * @param activity Activity.%0A%09 * @return %7B@link Configuration#ORIENTATION_PORTRAIT%7D or %7B@link Configuration#ORIENTATION_LANDSCAPE%7D%0A%09 */%0A%09public static int getDefaultOrientationOfDevice(Activity activity) %7B%0A%09%09Configuration configuration = activity.getResources().getConfiguration();%0A%09%09boolean orientationLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
 );%0A
+%0A
 %09%09
-activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);%0A%09%7D%0A%0A%09/**%0A%09 * Sets the Activity to keep the screen on as long as it is visible to the user.%0A%09 * %0A%09 * @param activity Activity.%0A%09 */%0A%09public static void keepScreenOn(Activity activity) %7B%0A%09%09activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);%0A%09%7D%0A%0A%09/**%0A%09 * Sets the Activity to allow the hardware buttons to control the volume of music playback.%0A%09 * %0A%09 * @param activity Activity.%0A%09 */%0A%09public static void controlMusicPlayBackStream(Activity activity) %7B%0A%09%09activity.setVolumeControlStream(AudioManager.STREAM_MUSIC)
+Display display = activity.getWindowManager().getDefaultDisplay();%0A%09%09int rotation = display.getRotation();%0A%09%09boolean parallelToDefaultVerticalAxis = (rotation == Surface.ROTATION_0) %7C%7C (rotation == Surface.ROTATION_180);%0A%0A%09%09boolean defaultOrientationLandscape = (parallelToDefaultVerticalAxis && orientationLandscape) %7C%7C%0A%09%09%09%09(!parallelToDefaultVerticalAxis && !orientationLandscape);%0A%0A%09%09if (defaultOrientationLandscape) %7B%0A%09%09%09return Configuration.ORIENTATION_LANDSCAPE;%0A%09%09%7D%0A%09%09return Configuration.ORIENTATION_PORTRAIT
 ;%0A%09%7D

Add ActivityUtilities.getDefaultOrientationOfDevice(...)