Apply IMMERSIVE mode to gallery (works only in Android 4.4)
authorDavid A. Velasco <dvelasco@solidgear.es>
Thu, 7 Aug 2014 15:50:47 +0000 (17:50 +0200)
committerDavid A. Velasco <dvelasco@solidgear.es>
Thu, 7 Aug 2014 15:50:47 +0000 (17:50 +0200)
src/com/owncloud/android/ui/preview/PreviewImageActivity.java

index 3c9d9cf..86df4e3 100644 (file)
@@ -16,6 +16,7 @@
  */
 package com.owncloud.android.ui.preview;
 
+import android.annotation.SuppressLint;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
@@ -24,9 +25,12 @@ import android.content.IntentFilter;
 import android.content.ServiceConnection;
 import android.content.SharedPreferences;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
+import android.os.Message;
 import android.preference.PreferenceManager;
 import android.support.v4.view.ViewPager;
+import android.view.View;
 
 import com.actionbarsherlock.app.ActionBar;
 import com.actionbarsherlock.view.MenuItem;
@@ -70,7 +74,9 @@ ViewPager.OnPageChangeListener, OnRemoteOperationListener {
     
     public static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
     private static final String KEY_WAITING_FOR_BINDER = "WAITING_FOR_BINDER";
-    
+
+    private static final int INITIAL_HIDE_DELAY = 0; // immediate hide
+
     private ExtendedViewPager mViewPager;
     private PreviewImagePagerAdapter mPreviewImagePagerAdapter;    
     
@@ -78,7 +84,9 @@ ViewPager.OnPageChangeListener, OnRemoteOperationListener {
     
     private DownloadFinishReceiver mDownloadFinishReceiver;
 
-    private boolean mFullScreen;
+    //private boolean mFullScreen;
+    
+    private View mFullScreenAnchorView;
     
     
     @Override
@@ -98,7 +106,26 @@ ViewPager.OnPageChangeListener, OnRemoteOperationListener {
             requestPinCode();
         }         
         
-        mFullScreen = true;
+        //mFullScreen = true;
+        mFullScreenAnchorView = getWindow().getDecorView(); 
+        // to keep our UI controls visibility in line with system bars visibility
+        mFullScreenAnchorView.setOnSystemUiVisibilityChangeListener(
+            new View.OnSystemUiVisibilityChangeListener() {
+                @SuppressLint("InlinedApi")
+                               @Override
+                public void onSystemUiVisibilityChange(int flags)   {
+                    boolean visible = (flags &
+                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
+                    ActionBar actionBar = getSupportActionBar();
+                    if (visible) {
+                        actionBar.show();
+                    } else {
+                        actionBar.hide();
+                    }
+                }
+            }
+        );
+            
         if (savedInstanceState != null) {
             mRequestWaitingForBinder = savedInstanceState.getBoolean(KEY_WAITING_FOR_BINDER);
         } else {
@@ -129,6 +156,47 @@ ViewPager.OnPageChangeListener, OnRemoteOperationListener {
     }
     
     
+    protected void onPostCreate(Bundle savedInstanceState)  {
+        super.onPostCreate(savedInstanceState);
+        
+        // Trigger the initial hide() shortly after the activity has been 
+        // created, to briefly hint to the user that UI controls 
+        // are available
+        delayedHide(INITIAL_HIDE_DELAY);
+        
+    }
+    
+    Handler mHideSystemUiHandler = new Handler() {
+        @Override
+        public void handleMessage(Message msg) {
+            hideSystemUI(mFullScreenAnchorView);
+            getSupportActionBar().hide();
+        }
+    };
+    
+    private void delayedHide(int delayMillis)   {
+        mHideSystemUiHandler.removeMessages(0);
+        mHideSystemUiHandler.sendEmptyMessageDelayed(0, delayMillis);
+    }
+    
+    
+    /// handle Window Focus changes
+    @Override
+    public void onWindowFocusChanged(boolean hasFocus) {
+        super.onWindowFocusChanged(hasFocus);
+        
+        // When the window loses focus (e.g. the action overflow is shown),
+        // cancel any pending hide action. When the window gains focus,
+        // hide the system UI.
+        if (hasFocus) {
+            delayedHide(INITIAL_HIDE_DELAY);
+        } else {
+            mHideSystemUiHandler.removeMessages(0);
+        }
+    }
+    
+    
+    
     @Override
     public void onStart() {
         super.onStart();
@@ -390,8 +458,23 @@ ViewPager.OnPageChangeListener, OnRemoteOperationListener {
 
     }
 
-    public void toggleFullScreen() {
-        ActionBar actionBar = getSupportActionBar();
+    @SuppressLint("InlinedApi")
+       public void toggleFullScreen() {
+        //ActionBar actionBar = getSupportActionBar();
+        
+        boolean visible = (
+                mFullScreenAnchorView.getSystemUiVisibility() 
+                & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
+            ) == 0;
+        
+        if (visible) {
+            hideSystemUI(mFullScreenAnchorView);
+            //actionBar.hide(); // propagated through OnSystemUiVisibilityChangeListener()
+        } else {
+            showSystemUI(mFullScreenAnchorView);
+            //actionBar.show(); // propagated through OnSystemUiVisibilityChangeListener()
+        }
+        /*
         if (mFullScreen) {
             actionBar.show();
             
@@ -400,6 +483,7 @@ ViewPager.OnPageChangeListener, OnRemoteOperationListener {
             
         }
         mFullScreen = !mFullScreen;
+        */
     }
 
     @Override
@@ -460,5 +544,29 @@ ViewPager.OnPageChangeListener, OnRemoteOperationListener {
         // TODO Auto-generated method stub
         
     }
+    
+    
+    @SuppressLint("InlinedApi")
+       private void hideSystemUI(View anchorView) {
+        anchorView.setSystemUiVisibility(
+                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION         // hides NAVIGATION BAR; Android >= 4.0
+            |   View.SYSTEM_UI_FLAG_FULLSCREEN              // hides STATUS BAR;     Android >= 4.1
+            |   View.SYSTEM_UI_FLAG_IMMERSIVE               // stays interactive;    Android >= 4.4
+            |   View.SYSTEM_UI_FLAG_LAYOUT_STABLE           // draw full window;     Android >= 4.1
+            |   View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN       // draw full window;     Android >= 4.1
+            |   View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  // draw full window;     Android >= 4.1
+        );
+    }
+    
+    @SuppressLint("InlinedApi")
+    private void showSystemUI(View anchorView) {
+        anchorView.setSystemUiVisibility(
+                View.SYSTEM_UI_FLAG_LAYOUT_STABLE           // draw full window;     Android >= 4.1
+            |   View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN       // draw full window;     Android >= 4.1
+            |   View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION  // draw full window;     Android >= 4.1
+        );
+    }
+    
+    
 
 }