package com.wpc.library.videocomponents;
|
|
import android.annotation.SuppressLint;
|
import android.app.ActionBar;
|
import android.app.Activity;
|
import android.content.Context;
|
import android.media.MediaPlayer;
|
import android.util.AttributeSet;
|
import android.view.MotionEvent;
|
import android.view.View;
|
import android.widget.VideoView;
|
|
/**
|
* This is the actual video player. It is the top-level content of the
|
* activity's view hierarchy, going under the status bar and nav bar areas.
|
*/
|
public class SimpleVideoView extends VideoView implements
|
View.OnSystemUiVisibilityChangeListener, View.OnClickListener,
|
ActionBar.OnMenuVisibilityListener, MediaPlayer.OnPreparedListener,
|
MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener {
|
private Activity mActivity;
|
private MediaController mMediaController;
|
private GestureMediaController mGestureController;
|
private boolean mAddedMenuListener;
|
private int mLastSystemUiVis;
|
|
public SimpleVideoView(Context context, AttributeSet attrs) {
|
super(context, attrs);
|
mActivity = (Activity) context;
|
setOnSystemUiVisibilityChangeListener(this);
|
setOnClickListener(this);
|
setOnPreparedListener(this);
|
setOnCompletionListener(this);
|
setOnErrorListener(this);
|
}
|
|
public void setMediaController(MediaController mediaController) {
|
// This called by the containing activity to supply the surrounding
|
// state of the video player that it will interact with.
|
mMediaController = mediaController;
|
pause();
|
}
|
|
public void setGestureMediaController(GestureMediaController gestureController) {
|
mGestureController = gestureController;
|
}
|
|
@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 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_HIDE_NAVIGATION) != 0
|
&& (visibility & SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
|
setNavVisibility(true);
|
}
|
}
|
|
@Override
|
protected void onWindowVisibilityChanged(int visibility) {
|
super.onWindowVisibilityChanged(visibility);
|
// When we become visible or invisible, play is paused.
|
pause();
|
}
|
|
@Override
|
public void onClick(View v) {
|
// Clicking anywhere makes the navigation visible.
|
setNavVisibility(true);
|
}
|
|
@Override
|
@SuppressLint("ClickableViewAccessibility")
|
public boolean onTouchEvent(MotionEvent ev) {
|
boolean result = super.onTouchEvent(ev);
|
if (mGestureController != null) {
|
result = mGestureController.onTouchEvent(ev);
|
}
|
return result;
|
}
|
|
@Override
|
public void onMenuVisibilityChanged(boolean isVisible) {
|
setNavVisibility(true);
|
}
|
|
@Override
|
public void onPrepared(MediaPlayer mp) {
|
mMediaController.setEnabled(true);
|
}
|
|
@Override
|
public void onCompletion(MediaPlayer mp) {
|
pause();
|
}
|
|
@Override
|
public boolean onError(MediaPlayer mp, int what, int extra) {
|
pause();
|
return false;
|
}
|
|
@Override
|
public void start() {
|
super.start();
|
setKeepScreenOn(true);
|
setNavVisibility(true);
|
mMediaController.show();
|
mMediaController.refresh();
|
}
|
|
@Override
|
public void pause() {
|
super.pause();
|
setKeepScreenOn(false);
|
setNavVisibility(true);
|
mMediaController.show();
|
mMediaController.refresh();
|
}
|
|
void setNavVisibility(boolean visible) {
|
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;
|
}
|
|
// Set the new desired visibility.
|
setSystemUiVisibility(newVis);
|
}
|
}
|