package retrobox.utils;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.view.Window;

public abstract class ImmersiveModeSetter {
	public static ImmersiveModeSetter get() {
		if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.KITKAT) {
			return ImmersiveModeSetterKitKat.instance;
		} else if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.JELLY_BEAN) {
			return ImmersiveModeSetterJellyBean.instance;
		}
		return ImmersiveModeSetterLegacy.instance;
	}
	
	public abstract void setImmersiveMode(Window window, boolean stable);
	
	public static void postImmersiveMode(Handler handler, final Window window, final boolean stable) {
		handler.postDelayed(new Runnable(){
			@Override
			public void run() {
				get().setImmersiveMode(window, stable);
			}
		}, 1000);
	}
	
	private static class ImmersiveModeSetterLegacy extends ImmersiveModeSetter {
		private static final ImmersiveModeSetterLegacy instance = new ImmersiveModeSetterLegacy();
		@SuppressWarnings("deprecation")
		@Override
		public void setImmersiveMode(Window window, boolean stable) {
			window.getDecorView().setSystemUiVisibility(
		            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
		            | View.STATUS_BAR_HIDDEN
		            );
		}
	}
	private static class ImmersiveModeSetterJellyBean extends ImmersiveModeSetter {
		private static final ImmersiveModeSetterJellyBean instance = new ImmersiveModeSetterJellyBean();
		@Override
		@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
		public void setImmersiveMode(Window window, boolean stable) {
			window.getDecorView().setSystemUiVisibility(
		            (stable?View.SYSTEM_UI_FLAG_LAYOUT_STABLE
		            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
		            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:0)
		            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
		            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
		            );
		}
	}
	private static class ImmersiveModeSetterKitKat extends ImmersiveModeSetter {
		private static final ImmersiveModeSetterKitKat instance = new ImmersiveModeSetterKitKat();
		@Override
		@TargetApi(Build.VERSION_CODES.KITKAT)
		public void setImmersiveMode(Window window, boolean stable) {
			window.getDecorView().setSystemUiVisibility(
		            (stable?View.SYSTEM_UI_FLAG_LAYOUT_STABLE:0)
		            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
		            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
		            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
		            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
		            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
		}
	}

}

@@ -107,16 +107,41 @@
 andler;%0A
+import android.util.Log;%0A
 import a
@@ -158,16 +158,16 @@
 w.View;%0A
-
 import a
@@ -231,16 +231,99 @@
 etter %7B%0A
+%09private static final String LOGTAG = ImmersiveModeSetter.class.getSimpleName();%0A%09%0A
 %09public 
@@ -975,16 +975,16 @@
 );%0A%09%09%09%7D%0A
-
 %09%09%7D, 100
@@ -992,16 +992,548 @@
 );%0A%09%7D%0A%09%0A
+%09private static boolean postPeriodic = false;%0A%09%0A%09public static void stop() %7B%0A%09%09postPeriodic = false;%0A%09%7D%0A%09%0A%09public static void postImmersiveModePeriodic(final Handler handler, final Window window, final boolean stable, final long period) %7B%0A%09%09postPeriodic = true;%0A%09%09final Runnable task = new Runnable()%7B%0A%09%09%09@Override%0A%09%09%09public void run() %7B%0A%09%09%09%09get().setImmersiveMode(window, stable);%0A%09%09%09%09if (postPeriodic) %7B%0A%09%09%09%09%09postImmersiveModePeriodic(handler, window, stable, period);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09%0A%09%09handler.postDelayed(task, period);%0A%09%7D%0A%09%0A
 %09private

Allow for periodic Immersive mode