// Copyright (c) 2013 Intel Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.app.template;

import android.graphics.Color;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.View;
import android.widget.TextView;

import org.xwalk.app.XWalkRuntimeActivityBase;

public class AppTemplateActivity extends XWalkRuntimeActivityBase {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void didTryLoadRuntimeView(View runtimeView) {
        if (runtimeView != null) {
            setContentView(runtimeView);
            getRuntimeView().loadAppFromUrl("file:///android_asset/www/index.html");
        } else {
            TextView msgText = new TextView(this);
            msgText.setText("Crosswalk failed to initialize.");
            msgText.setTextSize(36);
            msgText.setTextColor(Color.BLACK);
            setContentView(msgText);
        }
    }

    private void enterFullscreen() {
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT &&
                ((getWindow().getAttributes().flags &
                        WindowManager.LayoutParams.FLAG_FULLSCREEN) != 0)) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }

    public void setIsFullscreen(boolean isFullscreen) {
        if (isFullscreen) {
            enterFullscreen();
        }
    }

}

@@ -247,24 +247,16 @@
 os.Build
-.VERSION
 ;%0Aimport
@@ -273,25 +273,37 @@
 s.Bu
-ild.VERSION_CODES
+ndle;%0Aimport android.util.Log
 ;%0Aim
@@ -311,33 +311,35 @@
 ort android.
-os.Bundle
+view.Window
 ;%0Aimport and
@@ -534,24 +534,415 @@
 ivityBase %7B%0A
+    private final int SYSTEM_UI_OPTIONS = View.SYSTEM_UI_FLAG_LAYOUT_STABLE %7C%0A            View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION %7C%0A            View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN %7C%0A            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION %7C%0A            View.SYSTEM_UI_FLAG_FULLSCREEN %7C%0A            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;%0A    private final String TAG = this.getClass().getName();%0A%0A
     @Overrid
@@ -1044,24 +1044,127 @@
 te);%0A    %7D%0A%0A
+    @Override%0A    public void onResume() %7B%0A        super.onResume();%0A        enterFullscreen();%0A    %7D%0A%0A
     @Overrid
@@ -1710,66 +1710,30 @@
 if (
-VERSION.SDK_INT %3E= VERSION_CODES.KITKAT &&%0A               
+isNewerThanKitkat() &&
  ((g
@@ -1767,24 +1767,16 @@
 flags &%0A
-        
         
@@ -1937,16 +1937,35 @@
 ibility(
+SYSTEM_UI_OPTIONS);
 %0A       
@@ -1969,238 +1969,692 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-    View.SYSTEM_UI_FLAG_LAYOUT_STABLE %7C%0A                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION %7C%0A                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN %7C%0A                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION %7C
+public void setIsFullscreen(boolean isFullscreen) %7B%0A        if (!isFullscreen) return;%0A        if (!isNewerThanKitkat()) %7B%0A            Log.w(TAG, %22setIsFullscreen() should not be called if %22 +%0A                    %22android version is older than Kitkat.%22);%0A            return;%0A        %7D%0A%0A        setSystemUiVisibilityChangeListener();%0A    %7D%0A%0A    private void setSystemUiVisibilityChangeListener() %7B%0A        final View decorView = getWindow().getDecorView();%0A        decorView.setOnSystemUiVisibilityChangeListener(%0A            new View.OnSystemUiVisibilityChangeListener() %7B%0A                @Override%0A                public void onSystemUiVisibilityChange(int visibility) %7B
 %0A   
@@ -2661,32 +2661,50 @@
                 
+ if ((visibility &
  View.SYSTEM_UI_
@@ -2710,34 +2710,41 @@
 _FLAG_FULLSCREEN
- %7C
+) == 0) %7B
 %0A               
@@ -2752,55 +2752,76 @@
     
-View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);%0A
+    decorView.setSystemUiVisibility(SYSTEM_UI_OPTIONS);%0A    
         
 %7D%0A  
@@ -2820,100 +2820,49 @@
     
-%7D%0A
+  
     
+  
 %7D
-%0A
 %0A    
-public void setIsFullscreen(boolean isFullscreen) %7B%0A        if (isFullscreen) %7B
+            %7D%0A            %7D
 %0A   
@@ -2870,46 +2870,132 @@
     
+);%0A
     
-enterFullscreen();%0A        %7D
+%7D%0A%0A    private boolean isNewerThanKitkat() %7B%0A        return Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.KITKAT;
 %0A    %7D%0A
-%0A
 %7D%0A

Fix immersive full screen mode doesn't recover.