Work on issue #6475693 OnSystemUiVisibilityChangeListener reporting...

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

Improve API demos.

Change-Id: I3e831152a0dbb56270a80cf58e9441b55059c884
This commit is contained in:
Dianne Hackborn
2012-05-10 15:26:52 -07:00
parent f6b7dd7e7d
commit f2bc2a024c
5 changed files with 126 additions and 56 deletions

View File

@@ -26,28 +26,26 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
android:animateLayoutChanges="true"
> >
<TextView <TextView
android:id="@+id/title"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal" android:layout_gravity="top|center_horizontal"
android:textColor="#ff000000" android:textColor="#ff000000"
android:background="#ffa0a0a0" android:background="#c0f0f0f0"
android:textSize="16dp" android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" android:gravity="left"
android:gravity="center" android:padding="16dp"
android:text="You Could Put Controls Here" android:text="A title goes here"
/> />
<TextView <SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" android:layout_gravity="bottom|center_horizontal"
android:textColor="#ff000000" android:layout_marginBottom="16dp"
android:background="#ffa0a0a0"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:text="Or Put Them Here, Too"
/> />
</FrameLayout> </FrameLayout>
</FrameLayout> </FrameLayout>

View File

@@ -14,11 +14,12 @@
limitations under the License. limitations under the License.
--> -->
<!-- BEGIN_INCLUDE(complete) -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:layout_width="match_parent" android:layout_height="match_parent"
> >
<view class="com.example.android.apis.view.VideoPlayerActivity$Content" <view class="com.example.android.apis.view.VideoPlayerActivity$Content"
android:id="@+id/image" android:id="@+id/content"
android:src="@drawable/frantic" android:src="@drawable/frantic"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -28,28 +29,35 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:fitsSystemWindows="true"
android:animateLayoutChanges="true"
> >
<TextView <TextView
android:id="@+id/title"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal" android:layout_gravity="top|center_horizontal"
android:textColor="#ff000000" android:textColor="#ffffffff"
android:background="#ffa0a0a0" android:background="#a0000000"
android:textSize="16dp" android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" android:gravity="left"
android:gravity="center" android:padding="16dp"
android:text="You Could Put Controls Here" 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_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal" android:layout_gravity="bottom|center_horizontal"
android:textColor="#ff000000" android:layout_marginBottom="16dp"
android:background="#ffa0a0a0"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:text="Or Put Them Here, Too"
/> />
</FrameLayout> </FrameLayout>
</FrameLayout> </FrameLayout>
<!-- END_INCLUDE(complete) -->

View File

@@ -1265,6 +1265,9 @@
<string name="chronometer_clear_format">Clear format string</string> <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="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 --> <!-- vibrator examples strings -->
<!-- ============================ --> <!-- ============================ -->

View File

@@ -26,14 +26,17 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.widget.Button;
import android.widget.ScrollView; import android.widget.ScrollView;
import android.widget.SearchView; import android.widget.SearchView;
import android.widget.SeekBar;
import android.widget.ShareActionProvider; import android.widget.ShareActionProvider;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@@ -57,6 +60,8 @@ public class ContentBrowserActivity extends Activity
public static class Content extends ScrollView public static class Content extends ScrollView
implements View.OnSystemUiVisibilityChangeListener, View.OnClickListener { implements View.OnSystemUiVisibilityChangeListener, View.OnClickListener {
TextView mText; TextView mText;
TextView mTitleView;
SeekBar mSeekView;
boolean mNavVisible; boolean mNavVisible;
int mBaseSystemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN int mBaseSystemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| SYSTEM_UI_FLAG_LAYOUT_STABLE; | SYSTEM_UI_FLAG_LAYOUT_STABLE;
@@ -72,6 +77,7 @@ public class ContentBrowserActivity extends Activity
super(context, attrs); super(context, attrs);
mText = new TextView(context); mText = new TextView(context);
mText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
mText.setText(context.getString(R.string.alert_dialog_two_buttons2ultra_msg)); mText.setText(context.getString(R.string.alert_dialog_two_buttons2ultra_msg));
mText.setClickable(false); mText.setClickable(false);
mText.setOnClickListener(this); mText.setOnClickListener(this);
@@ -80,6 +86,13 @@ public class ContentBrowserActivity extends Activity
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
setOnSystemUiVisibilityChangeListener(this); 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); setNavVisibility(true);
} }
@@ -139,6 +152,8 @@ public class ContentBrowserActivity extends Activity
// Set the new desired visibility. // Set the new desired visibility.
setSystemUiVisibility(newVis); setSystemUiVisibility(newVis);
mTitleView.setVisibility(visible ? VISIBLE : INVISIBLE);
mSeekView.setVisibility(visible ? VISIBLE : INVISIBLE);
} }
} }
//END_INCLUDE(content) //END_INCLUDE(content)
@@ -156,6 +171,8 @@ public class ContentBrowserActivity extends Activity
setContentView(R.layout.content_browser); setContentView(R.layout.content_browser);
mContent = (Content)findViewById(R.id.content); mContent = (Content)findViewById(R.id.content);
mContent.init((TextView)findViewById(R.id.title),
(SeekBar)findViewById(R.id.seekbar));
ActionBar bar = getActionBar(); ActionBar bar = getActionBar();
bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this)); bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));

View File

@@ -26,15 +26,16 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.SearchView; import android.widget.SearchView;
import android.widget.SeekBar;
import android.widget.ShareActionProvider; import android.widget.ShareActionProvider;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@@ -58,11 +59,16 @@ public class VideoPlayerActivity extends Activity
*/ */
//BEGIN_INCLUDE(content) //BEGIN_INCLUDE(content)
public static class Content extends ImageView implements public static class Content extends ImageView implements
View.OnSystemUiVisibilityChangeListener, View.OnClickListener { View.OnSystemUiVisibilityChangeListener, View.OnClickListener,
TextView mText; ActionBar.OnMenuVisibilityListener {
Activity mActivity;
TextView mTitleView;
Button mPlayButton;
SeekBar mSeekView;
boolean mAddedMenuListener;
boolean mMenusOpen;
boolean mPaused;
boolean mNavVisible; boolean mNavVisible;
int mBaseSystemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | SYSTEM_UI_FLAG_LAYOUT_STABLE;
int mLastSystemUiVis; int mLastSystemUiVis;
Runnable mNavHider = new Runnable() { Runnable mNavHider = new Runnable() {
@@ -75,17 +81,43 @@ public class VideoPlayerActivity extends Activity
super(context, attrs); super(context, attrs);
setOnSystemUiVisibilityChangeListener(this); setOnSystemUiVisibilityChangeListener(this);
setOnClickListener(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) { @Override public void onSystemUiVisibilityChange(int visibility) {
// Detect when we go out of low-profile mode, to also go out // Detect when we go out of nav-hidden mode, to clear our state
// of full screen. We only do this when the low profile mode // back to having the full UI chrome up. Only do this when
// is changing from its last state, and turning off. // the state is changing and nav is no longer hidden.
int diff = mLastSystemUiVis ^ visibility; int diff = mLastSystemUiVis ^ visibility;
mLastSystemUiVis = visibility; mLastSystemUiVis = visibility;
if ((diff&SYSTEM_UI_FLAG_LOW_PROFILE) != 0 if ((diff&SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0
&& (visibility&SYSTEM_UI_FLAG_LOW_PROFILE) == 0) { && (visibility&SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
setNavVisibility(true); setNavVisibility(true);
} }
} }
@@ -93,23 +125,35 @@ public class VideoPlayerActivity extends Activity
@Override protected void onWindowVisibilityChanged(int visibility) { @Override protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility); super.onWindowVisibilityChanged(visibility);
// When we become visible, we show our navigation elements briefly // When we become visible or invisible, play is paused.
// before hiding them. setPlayPaused(true);
setNavVisibility(true);
} }
@Override public void onClick(View v) { @Override public void onClick(View v) {
// When the user clicks, we make the navigation visible. In a real if (v == mPlayButton) {
// implementation, this would probably toggle between pause/play. // 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); setNavVisibility(true);
} }
void setBaseSystemUiVisibility(int visibility) { void setPlayPaused(boolean paused) {
mBaseSystemUiVisibility = visibility; mPaused = paused;
mPlayButton.setText(paused ? R.string.play : R.string.pause);
setNavVisibility(true);
} }
void setNavVisibility(boolean visible) { 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) { if (!visible) {
newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN newVis |= SYSTEM_UI_FLAG_LOW_PROFILE | SYSTEM_UI_FLAG_FULLSCREEN
| SYSTEM_UI_FLAG_HIDE_NAVIGATION; | SYSTEM_UI_FLAG_HIDE_NAVIGATION;
@@ -120,12 +164,18 @@ public class VideoPlayerActivity extends Activity
Handler h = getHandler(); Handler h = getHandler();
if (h != null) { if (h != null) {
h.removeCallbacks(mNavHider); 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. // Set the new desired visibility.
setSystemUiVisibility(newVis); setSystemUiVisibility(newVis);
mTitleView.setVisibility(visible ? VISIBLE : INVISIBLE);
mPlayButton.setVisibility(visible ? VISIBLE : INVISIBLE);
mSeekView.setVisibility(visible ? VISIBLE : INVISIBLE);
} }
} }
//END_INCLUDE(content) //END_INCLUDE(content)
@@ -143,6 +193,9 @@ public class VideoPlayerActivity extends Activity
setContentView(R.layout.video_player); setContentView(R.layout.video_player);
mContent = (Content)findViewById(R.id.content); 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(); ActionBar bar = getActionBar();
bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this)); bar.addTab(bar.newTab().setText("Tab 1").setTabListener(this));
@@ -198,15 +251,6 @@ public class VideoPlayerActivity extends Activity
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
item.setChecked(true); item.setChecked(true);
return 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; return false;
} }