[Android] [PATCH 3/3] Video Player: Use StatusBar after Android 4.2

Thomas Guillem thomas.guillem at gmail.com
Tue Sep 30 18:42:06 CEST 2014


Android 4.2 (API17) adds fullscreen support, and gives the possibility to
show/hide the StatusBar without any resize.

Replace player_overlay_header with android StatusBar and ActionBar.

 - The StatusBar will display the date, the battery, the connectivity and the
   notifications.

 - The ActionBar will display the Video title, using a custom layout that looks
   like the previous overlay header. It's important to use The ActionBar and
   not a view in the player layout since the show/hide animations of the
   ActionBar is synchronized with the StatusBar.
---
 vlc-android/res/layout-v17/action_bar_layout.xml   | 17 +++++
 .../res/layout-v17/player_overlay_header.xml       |  5 ++
 .../player_remote_control_overlay_header.xml       |  5 ++
 vlc-android/res/values-v17/styles.xml              | 10 +++
 .../vlc/gui/video/VideoPlayerActivity.java         | 78 +++++++++++++++++-----
 5 files changed, 100 insertions(+), 15 deletions(-)
 create mode 100644 vlc-android/res/layout-v17/action_bar_layout.xml
 create mode 100644 vlc-android/res/layout-v17/player_overlay_header.xml
 create mode 100644 vlc-android/res/layout-v17/player_remote_control_overlay_header.xml
 create mode 100644 vlc-android/res/values-v17/styles.xml

diff --git a/vlc-android/res/layout-v17/action_bar_layout.xml b/vlc-android/res/layout-v17/action_bar_layout.xml
new file mode 100644
index 0000000..7fdf996
--- /dev/null
+++ b/vlc-android/res/layout-v17/action_bar_layout.xml
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="fill_parent" >
+
+    <TextView
+        android:id="@+id/player_overlay_title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="top"
+        android:background="?attr/video_player_overlay"
+        android:text="@string/title"
+        android:textColor="#ffffff"
+        android:paddingLeft="5dp"
+        android:paddingRight="5dp"
+        android:textSize="15sp" />
+</RelativeLayout>
diff --git a/vlc-android/res/layout-v17/player_overlay_header.xml b/vlc-android/res/layout-v17/player_overlay_header.xml
new file mode 100644
index 0000000..f91d4b3
--- /dev/null
+++ b/vlc-android/res/layout-v17/player_overlay_header.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- no Layout here: custom overlay_header is deactivated after api v17, as
+         StatusBar and ActionBar are used -->
+</merge>
diff --git a/vlc-android/res/layout-v17/player_remote_control_overlay_header.xml b/vlc-android/res/layout-v17/player_remote_control_overlay_header.xml
new file mode 100644
index 0000000..f91d4b3
--- /dev/null
+++ b/vlc-android/res/layout-v17/player_remote_control_overlay_header.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<merge xmlns:android="http://schemas.android.com/apk/res/android">
+    <!-- no Layout here: custom overlay_header is deactivated after api v17, as
+         StatusBar and ActionBar are used -->
+</merge>
diff --git a/vlc-android/res/values-v17/styles.xml b/vlc-android/res/values-v17/styles.xml
new file mode 100644
index 0000000..fca6653
--- /dev/null
+++ b/vlc-android/res/values-v17/styles.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<resources xmlns:android="http://schemas.android.com/apk/res/android">
+    <style name="Theme.VLC.Player" parent="android:Theme.Holo">
+        <item name="android:windowBackground">@color/black</item>
+        <item name="android:windowActionBarOverlay">true</item>
+
+        <item name="video_player_overlay">@color/transparent_gray</item>
+        <item name="advanced_options_style">@style/Theme.VLC.AdvancedOptionsBlack</item>
+    </style>
+</resources>
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index ecd28ce..78d806d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -58,6 +58,7 @@ import org.videolan.vlc.util.VLCInstance;
 import org.videolan.vlc.util.WeakHandler;
 
 import android.annotation.TargetApi;
+import android.app.ActionBar;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.KeyguardManager;
@@ -98,6 +99,7 @@ import android.view.SurfaceView;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.View.OnSystemUiVisibilityChangeListener;
+import android.view.ViewGroup;
 import android.view.ViewGroup.LayoutParams;
 import android.view.WindowManager;
 import android.view.animation.Animation;
@@ -142,6 +144,8 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
     private SharedPreferences mSettings;
 
     /** Overlay */
+    private ActionBar mActionBar;
+    private boolean mOverlayUseStatusBar;
     private View mOverlayHeader;
     private View mOverlayOption;
     private View mOverlayProgress;
@@ -258,6 +262,9 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
                 }
             };
             Log.d(TAG, "MediaRouter information : " + mMediaRouter  .toString());
+            mOverlayUseStatusBar = true;
+        } else {
+            mOverlayUseStatusBar = false;
         }
 
         mSettings = PreferenceManager.getDefaultSharedPreferences(this);
@@ -287,16 +294,35 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
             );
 
         /** initialize Views an their Events */
-        mOverlayHeader = findViewById(R.id.player_overlay_header);
+        if (mOverlayUseStatusBar) {
+            mActionBar = getActionBar();
+            mActionBar.setDisplayShowHomeEnabled(false);
+            mActionBar.setDisplayShowTitleEnabled(false);
+            mActionBar.setBackgroundDrawable(null);
+            mActionBar.setDisplayShowCustomEnabled(true);
+            mActionBar.setCustomView(R.layout.action_bar_layout);
+
+            ViewGroup view = (ViewGroup) mActionBar.getCustomView();
+            /* Dispatch ActionBar touch events to the Activity */
+            view.setOnTouchListener(new View.OnTouchListener() {
+                @Override
+                public boolean onTouch(View v, MotionEvent event) {
+                    onTouchEvent(event);
+                    return true;
+                }
+            });
+            mTitle = (TextView) view.findViewById(R.id.player_overlay_title);
+        } else {
+            mOverlayHeader = findViewById(R.id.player_overlay_header);
+            /* header */
+            mTitle = (TextView) findViewById(R.id.player_overlay_title);
+            mSysTime = (TextView) findViewById(R.id.player_overlay_systime);
+            mBattery = (TextView) findViewById(R.id.player_overlay_battery);
+        }
         mOverlayOption = findViewById(R.id.option_overlay);
         mOverlayProgress = findViewById(R.id.progress_overlay);
         mOverlayBackground = findViewById(R.id.player_overlay_background);
 
-        /* header */
-        mTitle = (TextView) findViewById(R.id.player_overlay_title);
-        mSysTime = (TextView) findViewById(R.id.player_overlay_systime);
-        mBattery = (TextView) findViewById(R.id.player_overlay_battery);
-
         // Position and remaining time
         mTime = (TextView) findViewById(R.id.player_overlay_time);
         mTime.setOnClickListener(mRemainingTimeListener);
@@ -383,7 +409,8 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
         editor.commit();
 
         IntentFilter filter = new IntentFilter();
-        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
+        if (!mOverlayUseStatusBar)
+            filter.addAction(Intent.ACTION_BATTERY_CHANGED);
         filter.addAction(VLCApplication.SLEEP_INTENT);
         registerReceiver(mReceiver, filter);
 
@@ -641,6 +668,8 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
         {
             String action = intent.getAction();
             if (action.equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)) {
+                if (mOverlayUseStatusBar)
+                    return;
                 int batteryLevel = intent.getIntExtra("level", 0);
                 if (batteryLevel >= 50)
                     mBattery.setTextColor(Color.GREEN);
@@ -1635,7 +1664,10 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
         if (!mShowing) {
             mShowing = true;
             if (!mIsLocked) {
-                mOverlayHeader.setVisibility(View.VISIBLE);
+                if (mOverlayUseStatusBar)
+                    mActionBar.show();
+                else
+                    mOverlayHeader.setVisibility(View.VISIBLE);
                 mOverlayOption.setVisibility(View.VISIBLE);
                 mPlayPause.setVisibility(View.VISIBLE);
                 mMenu.setVisibility(View.VISIBLE);
@@ -1662,7 +1694,8 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
             Log.i(TAG, "remove View!");
             if (mOverlayTips != null) mOverlayTips.setVisibility(View.INVISIBLE);
             if (!fromUser && !mIsLocked) {
-                mOverlayHeader.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
+                if (!mOverlayUseStatusBar)
+                    mOverlayHeader.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
                 mOverlayOption.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
                 mOverlayProgress.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
                 mPlayPause.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
@@ -1672,7 +1705,10 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
                 mOverlayBackground.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
                 mOverlayBackground.setVisibility(View.INVISIBLE);
             }
-            mOverlayHeader.setVisibility(View.INVISIBLE);
+            if (mOverlayUseStatusBar)
+                mActionBar.hide();
+            else
+                mOverlayHeader.setVisibility(View.INVISIBLE);
             mOverlayOption.setVisibility(View.INVISIBLE);
             mOverlayProgress.setVisibility(View.INVISIBLE);
             mPlayPause.setVisibility(View.INVISIBLE);
@@ -1693,10 +1729,21 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
         int layout = 0;
         if (!AndroidDevices.hasCombBar() && LibVlcUtil.isJellyBeanOrLater())
             layout = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
-        int visibility =  (dim ? (AndroidDevices.hasCombBar()
-                ? View.SYSTEM_UI_FLAG_LOW_PROFILE
-                        : View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
-                        : View.SYSTEM_UI_FLAG_VISIBLE) | layout;
+        if (mOverlayUseStatusBar)
+            layout |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
+
+        int visibility = layout;
+        if (dim) {
+            if (AndroidDevices.hasCombBar()) {
+                visibility |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
+            } else {
+                visibility |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
+                if (mOverlayUseStatusBar)
+                    visibility |= View.SYSTEM_UI_FLAG_FULLSCREEN;
+            }
+        } else {
+            visibility |= View.SYSTEM_UI_FLAG_VISIBLE;
+        }
         mSurface.setSystemUiVisibility(visibility);
         mSubtitlesSurface.setSystemUiVisibility(visibility);
     }
@@ -1734,7 +1781,8 @@ public class VideoPlayerActivity extends Activity implements IVideoPlayer {
         mForward.setVisibility(isSeekable ? View.VISIBLE : View.GONE);
         mSeekbar.setMax(length);
         mSeekbar.setProgress(time);
-        mSysTime.setText(DateFormat.getTimeFormat(this).format(new Date(System.currentTimeMillis())));
+        if (!mOverlayUseStatusBar)
+            mSysTime.setText(DateFormat.getTimeFormat(this).format(new Date(System.currentTimeMillis())));
         if (time >= 0) mTime.setText(Strings.millisToString(time));
         if (length >= 0) mLength.setText(mDisplayRemainingTime && length > 0
                 ? "- " + Strings.millisToString(length - time)
-- 
2.1.0



More information about the Android mailing list