Behave better when in immersive mode. Take up the entire screen, instead of leaving the navigation bar empty. Re-enter immersive mode if the volume buttons are pressed

This commit is contained in:
Cameron Gutman 2014-05-07 22:17:16 -04:00
parent 508b855e36
commit b82d74474a

View File

@ -20,6 +20,7 @@ import android.graphics.Point;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.InputDevice;
import android.view.KeyEvent;
@ -91,15 +92,24 @@ public class Game extends Activity implements SurfaceHolder.Callback, OnGenericM
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// We don't want a title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Full-screen and don't let the display go off
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// We don't want a title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// If we're going to use immersive mode, we want to have
// the entire screen
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
}
// Change volume button behavior
setVolumeControlStream(AudioManager.STREAM_MUSIC);
@ -162,8 +172,7 @@ public class Game extends Activity implements SurfaceHolder.Callback, OnGenericM
}
@SuppressLint("InlinedApi")
private void hideSystemUi() {
runOnUiThread(new Runnable() {
private Runnable hideSystemUi = new Runnable() {
@Override
public void run() {
// Use immersive mode on 4.4+ or standard low profile on previous builds
@ -182,7 +191,14 @@ public class Game extends Activity implements SurfaceHolder.Callback, OnGenericM
View.SYSTEM_UI_FLAG_LOW_PROFILE);
}
}
});
};
private void hideSystemUi() {
Handler h = getWindow().getDecorView().getHandler();
if (h != null) {
h.removeCallbacks(hideSystemUi);
h.postDelayed(hideSystemUi, 1000);
}
}
@Override
@ -235,6 +251,16 @@ public class Game extends Activity implements SurfaceHolder.Callback, OnGenericM
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// Pressing a volume button drops the immersive flag so the UI shows up again and doesn't
// go away. I'm not sure if that's a bug or a feature, but we're working around it here
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
Handler h = getWindow().getDecorView().getHandler();
if (h != null) {
h.removeCallbacks(hideSystemUi);
h.postDelayed(hideSystemUi, 2000);
}
}
if (event.getDevice() != null &&
(event.getDevice().getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC)) {
short translated = keybTranslator.translate(event.getKeyCode());