Work on issue #6475693 OnSystemUiVisibilityChangeListener reporting...

...incorrect visibility when the ActionBar overflow menu is opened

Improve API demos.

Change-Id: I3e831152a0dbb56270a80cf58e9441b55059c884
diff --git a/samples/ApiDemos/res/layout/content_browser.xml b/samples/ApiDemos/res/layout/content_browser.xml
index 1e1ba37..0770de3 100644
--- a/samples/ApiDemos/res/layout/content_browser.xml
+++ b/samples/ApiDemos/res/layout/content_browser.xml
@@ -26,28 +26,26 @@
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true"
+        android:animateLayoutChanges="true"
         >
         <TextView
+            android:id="@+id/title"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="top|center_horizontal"
             android:textColor="#ff000000"
-            android:background="#ffa0a0a0"
-            android:textSize="16dp"
-            android:textStyle="bold"
-            android:gravity="center"
-            android:text="You Could Put Controls Here"
+            android:background="#c0f0f0f0"
+            android:textAppearance="?android:attr/textAppearanceLarge"
+            android:gravity="left"
+            android:padding="16dp"
+            android:text="A title goes here"
             />
-        <TextView
+        <SeekBar
+            android:id="@+id/seekbar"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="bottom|center_horizontal"
-            android:textColor="#ff000000"
-            android:background="#ffa0a0a0"
-            android:textSize="16dp"
-            android:textStyle="bold"
-            android:gravity="center"
-            android:text="Or Put Them Here, Too"
+            android:layout_marginBottom="16dp"
             />
     </FrameLayout>
 </FrameLayout>
diff --git a/samples/ApiDemos/res/layout/video_player.xml b/samples/ApiDemos/res/layout/video_player.xml
index fb9479c..82cc1d0 100644
--- a/samples/ApiDemos/res/layout/video_player.xml
+++ b/samples/ApiDemos/res/layout/video_player.xml
@@ -14,11 +14,12 @@
      limitations under the License.
 -->
 
+<!-- BEGIN_INCLUDE(complete) -->
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent" android:layout_height="match_parent"
     >
     <view class="com.example.android.apis.view.VideoPlayerActivity$Content"
-        android:id="@+id/image"
+        android:id="@+id/content"
         android:src="@drawable/frantic"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
@@ -28,28 +29,35 @@
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fitsSystemWindows="true"
+        android:animateLayoutChanges="true"
         >
         <TextView
+            android:id="@+id/title"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="top|center_horizontal"
-            android:textColor="#ff000000"
-            android:background="#ffa0a0a0"
-            android:textSize="16dp"
-            android:textStyle="bold"
-            android:gravity="center"
-            android:text="You Could Put Controls Here"
+            android:textColor="#ffffffff"
+            android:background="#a0000000"
+            android:textAppearance="?android:attr/textAppearanceLarge"
+            android:gravity="left"
+            android:padding="16dp"
+            android:text="A title goes here"
             />
-        <TextView
+        <Button
+            android:id="@+id/play"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center"
+            android:gravity="center"
+            android:textSize="28dp"
+            />
+        <SeekBar
+            android:id="@+id/seekbar"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="bottom|center_horizontal"
-            android:textColor="#ff000000"
-            android:background="#ffa0a0a0"
-            android:textSize="16dp"
-            android:textStyle="bold"
-            android:gravity="center"
-            android:text="Or Put Them Here, Too"
+            android:layout_marginBottom="16dp"
             />
     </FrameLayout>
 </FrameLayout>
+<!-- END_INCLUDE(complete) -->
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index e716e24..c993b01 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -1265,6 +1265,9 @@
     <string name="chronometer_clear_format">Clear format string</string>
     <string name="chronometer_initial_format">Initial format: <xliff:g id="initial-format">%s</xliff:g></string>
 
+    <string name="play">Play</string>
+    <string name="pause">Pause</string>
+
     <!-- ============================ -->
     <!--  vibrator examples strings  -->
     <!-- ============================ -->
diff --git a/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java b/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
index 60c356b..96be9a2 100644
--- a/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
+++ b/samples/ApiDemos/src/com/example/android/apis/view/ContentBrowserActivity.java
@@ -26,14 +26,17 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.util.AttributeSet;
+import android.util.TypedValue;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.Window;
+import android.widget.Button;
 import android.widget.ScrollView;
 import android.widget.SearchView;
+import android.widget.SeekBar;
 import android.widget.ShareActionProvider;
 import android.widget.TextView;
 import android.widget.Toast;
@@ -57,6 +60,8 @@
     public static class Content extends ScrollView
             implements View.OnSystemUiVisibilityChangeListener, View.OnClickListener {
         TextView mText;
+        TextView mTitleView;
+        SeekBar mSeekView;
         boolean mNavVisible;
         int mBaseSystemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                 | SYSTEM_UI_FLAG_LAYOUT_STABLE;
@@ -72,6 +77,7 @@
             super(context, attrs);
     
             mText = new TextView(context);
+            mText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
             mText.setText(context.getString(R.string.alert_dialog_two_buttons2ultra_msg));
             mText.setClickable(false);
             mText.setOnClickListener(this);
@@ -80,6 +86,13 @@
                     ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
 
             setOnSystemUiVisibilityChangeListener(this);
+        }
+
+        public void init(TextView title, SeekBar seek) {
+            // This called by the containing activity to supply the surrounding
+            // state of the content browser that it will interact with.
+            mTitleView = title;
+            mSeekView = seek;
             setNavVisibility(true);
         }
 
@@ -139,6 +152,8 @@
 
             // Set the new desired visibility.
             setSystemUiVisibility(newVis);
+            mTitleView.setVisibility(visible ? VISIBLE : INVISIBLE);
+            mSeekView.setVisibility(visible ? VISIBLE : INVISIBLE);
         }
     }
 //END_INCLUDE(content)
@@ -156,6 +171,8 @@
 
         setContentView(R.layout.content_browser);
         mContent = (Content)findViewById(R.id.content);
+        mContent.init((TextView)findViewById(R.id.title),
+                (SeekBar)findViewById(R.id.seekbar));
 
         ActionBar bar = getActionBar();
         bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
diff --git a/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java b/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
index 0263ca6..c86a0ff 100644
--- a/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
+++ b/samples/ApiDemos/src/com/example/android/apis/view/VideoPlayerActivity.java
@@ -26,15 +26,16 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
-import android.view.ViewGroup;
 import android.view.Window;
+import android.widget.Button;
 import android.widget.ImageView;
-import android.widget.ScrollView;
 import android.widget.SearchView;
+import android.widget.SeekBar;
 import android.widget.ShareActionProvider;
 import android.widget.TextView;
 import android.widget.Toast;
@@ -58,11 +59,16 @@
      */
 //BEGIN_INCLUDE(content)
     public static class Content extends ImageView implements
-            View.OnSystemUiVisibilityChangeListener, View.OnClickListener {
-        TextView mText;
+            View.OnSystemUiVisibilityChangeListener, View.OnClickListener,
+            ActionBar.OnMenuVisibilityListener {
+        Activity mActivity;
+        TextView mTitleView;
+        Button mPlayButton;
+        SeekBar mSeekView;
+        boolean mAddedMenuListener;
+        boolean mMenusOpen;
+        boolean mPaused;
         boolean mNavVisible;
-        int mBaseSystemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
-                | SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | SYSTEM_UI_FLAG_LAYOUT_STABLE;
         int mLastSystemUiVis;
 
         Runnable mNavHider = new Runnable() {
@@ -75,17 +81,43 @@
             super(context, attrs);
             setOnSystemUiVisibilityChangeListener(this);
             setOnClickListener(this);
-            setNavVisibility(true);
+        }
+
+        public void init(Activity activity, TextView title, Button playButton,
+                SeekBar seek) {
+            // This called by the containing activity to supply the surrounding
+            // state of the video player that it will interact with.
+            mActivity = activity;
+            mTitleView = title;
+            mPlayButton = playButton;
+            mSeekView = seek;
+            mPlayButton.setOnClickListener(this);
+            setPlayPaused(true);
+        }
+
+        @Override protected void onAttachedToWindow() {
+            super.onAttachedToWindow();
+            if (mActivity != null) {
+                mAddedMenuListener = true;
+                mActivity.getActionBar().addOnMenuVisibilityListener(this);
+            }
+        }
+
+        @Override protected void onDetachedFromWindow() {
+            super.onDetachedFromWindow();
+            if (mAddedMenuListener) {
+                mActivity.getActionBar().removeOnMenuVisibilityListener(this);
+            }
         }
 
         @Override public void onSystemUiVisibilityChange(int visibility) {
-            // Detect when we go out of low-profile mode, to also go out
-            // of full screen.  We only do this when the low profile mode
-            // is changing from its last state, and turning off.
+            // Detect when we go out of nav-hidden mode, to clear our state
+            // back to having the full UI chrome up.  Only do this when
+            // the state is changing and nav is no longer hidden.
             int diff = mLastSystemUiVis ^ visibility;
             mLastSystemUiVis = visibility;
-            if ((diff&SYSTEM_UI_FLAG_LOW_PROFILE) != 0
-                    && (visibility&SYSTEM_UI_FLAG_LOW_PROFILE) == 0) {
+            if ((diff&SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0
+                    && (visibility&SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
                 setNavVisibility(true);
             }
         }
@@ -93,23 +125,35 @@
         @Override protected void onWindowVisibilityChanged(int visibility) {
             super.onWindowVisibilityChanged(visibility);
 
-            // When we become visible, we show our navigation elements briefly
-            // before hiding them.
-            setNavVisibility(true);
+            // When we become visible or invisible, play is paused.
+            setPlayPaused(true);
         }
 
         @Override public void onClick(View v) {
-            // When the user clicks, we make the navigation visible.  In a real
-            // implementation, this would probably toggle between pause/play.
+            if (v == mPlayButton) {
+                // Clicking on the play/pause button toggles its state.
+                setPlayPaused(!mPaused);
+            } else {
+                // Clicking elsewhere makes the navigation visible.
+                setNavVisibility(true);
+            }
+        }
+
+        @Override public void onMenuVisibilityChanged(boolean isVisible) {
+            mMenusOpen = isVisible;
             setNavVisibility(true);
         }
 
-        void setBaseSystemUiVisibility(int visibility) {
-            mBaseSystemUiVisibility = visibility;
+        void setPlayPaused(boolean paused) {
+            mPaused = paused;
+            mPlayButton.setText(paused ? R.string.play : R.string.pause);
+            setNavVisibility(true);
         }
 
         void setNavVisibility(boolean visible) {
-            int newVis = mBaseSystemUiVisibility;
+            int newVis = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
+                    | SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
+                    | SYSTEM_UI_FLAG_LAYOUT_STABLE;
             if (!visible) {
                 newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN
                         | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
@@ -120,12 +164,18 @@
                 Handler h = getHandler();
                 if (h != null) {
                     h.removeCallbacks(mNavHider);
-                    h.postDelayed(mNavHider, 3000);
+                    if (!mMenusOpen && !mPaused) {
+                        // If the menus are open or play is paused, we will not auto-hide.
+                        h.postDelayed(mNavHider, 3000);
+                    }
                 }
             }
 
             // Set the new desired visibility.
             setSystemUiVisibility(newVis);
+            mTitleView.setVisibility(visible ? VISIBLE : INVISIBLE);
+            mPlayButton.setVisibility(visible ? VISIBLE : INVISIBLE);
+            mSeekView.setVisibility(visible ? VISIBLE : INVISIBLE);
         }
     }
 //END_INCLUDE(content)
@@ -143,6 +193,9 @@
 
         setContentView(R.layout.video_player);
         mContent = (Content)findViewById(R.id.content);
+        mContent.init(this, (TextView)findViewById(R.id.title),
+                (Button)findViewById(R.id.play),
+                (SeekBar)findViewById(R.id.seekbar));
 
         ActionBar bar = getActionBar();
         bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
@@ -198,15 +251,6 @@
                 getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
                 item.setChecked(true);
                 return true;
-            case R.id.stable_layout:
-                item.setChecked(!item.isChecked());
-                mContent.setBaseSystemUiVisibility(item.isChecked()
-                        ? View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
-                                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
-                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
-                        : View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
-                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
-                return true;
         }
         return false;
     }