From 8d503143600a65881e8171ad9bb0bcfd9a80aa98 Mon Sep 17 00:00:00 2001 From: Slavi Petrov Date: Mon, 29 Jan 2018 12:35:56 +0200 Subject: [PATCH 1/5] Fix lint check issues related to performance, usability, assignment --- .../devlabs/fullscreenvideoview/Builder.java | 18 +- .../FullscreenVideoView.java | 239 ++++++++++-------- .../VideoControllerView.java | 12 +- .../fullscreenvideoview/VideoMediaPlayer.java | 9 +- ...onDelegate.java => OrientationHelper.java} | 17 +- .../main/res/layout/fullscreen_video_view.xml | 2 - .../src/main/res/layout/video_controller.xml | 1 - library/src/main/res/values/cd_strings.xml | 2 - library/src/main/res/values/strings.xml | 3 - 9 files changed, 158 insertions(+), 145 deletions(-) rename library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/{OrientationDelegate.java => OrientationHelper.java} (94%) delete mode 100644 library/src/main/res/values/strings.xml diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/Builder.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/Builder.java index 9671ed6..deb5d4e 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/Builder.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/Builder.java @@ -9,7 +9,7 @@ import java.io.File; import bg.devlabs.fullscreenvideoview.orientation.LandscapeOrientation; -import bg.devlabs.fullscreenvideoview.orientation.OrientationDelegate; +import bg.devlabs.fullscreenvideoview.orientation.OrientationHelper; import bg.devlabs.fullscreenvideoview.orientation.PortraitOrientation; /** @@ -17,26 +17,26 @@ * Dev Labs * slavi@devlabs.bg */ -@SuppressWarnings("unused") +@SuppressWarnings({"unused", "ClassWithTooManyMethods", "ClassNamingConvention"}) public class Builder { private final FullscreenVideoView fullscreenVideoView; private final VideoControllerView controller; - private final OrientationDelegate orientationDelegate; + private final OrientationHelper orientationHelper; private final VideoMediaPlayer videoMediaPlayer; Builder(FullscreenVideoView fullscreenVideoView, VideoControllerView controller, - OrientationDelegate orientationDelegate, VideoMediaPlayer videoMediaPlayer) { + OrientationHelper orientationHelper, VideoMediaPlayer videoMediaPlayer) { this.fullscreenVideoView = fullscreenVideoView; this.controller = controller; - this.orientationDelegate = orientationDelegate; + this.orientationHelper = orientationHelper; this.videoMediaPlayer = videoMediaPlayer; } - void videoFile(@NonNull final File videoFile) { + void videoFile(@NonNull File videoFile) { fullscreenVideoView.setupMediaPlayer(videoFile.getPath()); } - void videoUrl(@NonNull final String videoUrl) { + void videoUrl(@NonNull String videoUrl) { fullscreenVideoView.setupMediaPlayer(videoUrl); } @@ -121,12 +121,12 @@ public Builder rewindSeconds(int rewindSeconds) { } public Builder landscapeOrientation(LandscapeOrientation landscapeOrientation) { - orientationDelegate.setLandscapeOrientation(landscapeOrientation); + orientationHelper.setLandscapeOrientation(landscapeOrientation); return this; } public Builder portraitOrientation(PortraitOrientation portraitOrientation) { - orientationDelegate.setPortraitOrientation(portraitOrientation); + orientationHelper.setPortraitOrientation(portraitOrientation); return this; } diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/FullscreenVideoView.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/FullscreenVideoView.java index 3e8f330..f50554e 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/FullscreenVideoView.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/FullscreenVideoView.java @@ -20,7 +20,7 @@ import java.io.File; import java.io.IOException; -import bg.devlabs.fullscreenvideoview.orientation.OrientationDelegate; +import bg.devlabs.fullscreenvideoview.orientation.OrientationHelper; /** * Created by Slavi Petrov on 05.10.2017 @@ -29,68 +29,110 @@ */ @SuppressWarnings("unused") public class FullscreenVideoView extends FrameLayout { - // Views - private VideoSurfaceView surfaceView; - private SurfaceHolder surfaceHolder; + @Nullable + VideoSurfaceView surfaceView; + @Nullable + SurfaceHolder surfaceHolder; private SurfaceHolder.Callback surfaceHolderCallback; + @Nullable private ProgressBar progressBar; - private VideoControllerView controller; - // MediaPlayer - private VideoMediaPlayer videoMediaPlayer; - private boolean isMediaPlayerPrepared; + @Nullable + VideoControllerView controller; + @Nullable + VideoMediaPlayer videoMediaPlayer; + boolean isMediaPlayerPrepared; private Builder builder; - // Listeners + @Nullable private MediaPlayer.OnPreparedListener onPreparedListener; + @Nullable private View.OnTouchListener onTouchListener; - // Delegates - private OrientationDelegate orientationDelegate; + @Nullable + OrientationHelper orientationHelper; - public FullscreenVideoView(@NonNull final Context context) { + public FullscreenVideoView(@NonNull Context context) { super(context); } - public FullscreenVideoView(@NonNull final Context context, @Nullable final AttributeSet attrs) { + public FullscreenVideoView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(attrs); } - public FullscreenVideoView(@NonNull final Context context, @Nullable final AttributeSet attrs, - final int defStyleAttr) { + public FullscreenVideoView(@NonNull Context context, @Nullable AttributeSet attrs, + int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs); } - @SuppressWarnings("InstanceVariableUsedBeforeInitialized") - private void init(final AttributeSet attrs) { + private void init(AttributeSet attrs) { findChildViews(); // Skip this init rows - needed when changing FullscreenVideoView properties in XML if (!isInEditMode()) { videoMediaPlayer = new VideoMediaPlayer(this); initOrientationHandlers(); } + surfaceHolderCallback = new SurfaceHolder.Callback() { + @Override + public void surfaceCreated(SurfaceHolder holder) { + if (videoMediaPlayer != null) { + videoMediaPlayer.setDisplay(surfaceHolder); + } + } - surfaceHolderCallback = new SurfaceHolderCallback(); - surfaceHolder = surfaceView.getHolder(); - surfaceHolder.addCallback(surfaceHolderCallback); + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + + } - controller.init(this, videoMediaPlayer, attrs); + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + if (videoMediaPlayer != null && isMediaPlayerPrepared) { + videoMediaPlayer.pause(); + } + } + }; + if (surfaceView != null) { + surfaceHolder = surfaceView.getHolder(); + surfaceHolder.addCallback(surfaceHolderCallback); + } + if (controller != null) { + controller.init(this, videoMediaPlayer, attrs); + } setupProgressBarColor(); initOnBackPressedListener(); // Setup VideoView setupOnTouchListener(); - onPreparedListener = new VideoOnPreparedListener(); + onPreparedListener = new MediaPlayer.OnPreparedListener() { + @Override + public void onPrepared(MediaPlayer mediaPlayer) { + Log.d(FullscreenVideoView.class.getSimpleName(), "onPrepared: "); + if (!((Activity) getContext()).isDestroyed()) { + hideProgress(); + // Get the dimensions of the video + int videoWidth = videoMediaPlayer.getVideoWidth(); + int videoHeight = videoMediaPlayer.getVideoHeight(); + surfaceView.updateLayoutParams(videoWidth, videoHeight); + // Start media player if auto start is enabled + if (mediaPlayer != null && videoMediaPlayer.isAutoStartEnabled()) { + isMediaPlayerPrepared = true; + mediaPlayer.start(); + } + } + } + }; } private void initOrientationHandlers() { if (!isInEditMode()) { - orientationDelegate = new VideoOrientationDelegate(); - orientationDelegate.enable(); + if (orientationHelper != null) { + orientationHelper.enable(); + } } } private void findChildViews() { - final LayoutInflater layoutInflater = LayoutInflater.from(getContext()); + LayoutInflater layoutInflater = LayoutInflater.from(getContext()); layoutInflater.inflate(R.layout.fullscreen_video_view, this, true); surfaceView = findViewById(R.id.surface_view); progressBar = findViewById(R.id.progress_bar); @@ -100,18 +142,25 @@ private void findChildViews() { private void initOnBackPressedListener() { setFocusableInTouchMode(true); requestFocus(); - setOnKeyListener(new VideoOnKeyListener()); + setOnKeyListener(new OnKeyListener() { + @Override + public boolean onKey(View v, int keyCode, KeyEvent event) { + return (event.getAction() == KeyEvent.ACTION_UP) + && (keyCode == KeyEvent.KEYCODE_BACK) + && orientationHelper.shouldHandleOnBackPressed(); + } + }); } public Builder videoFile(File videoFile) { - builder = new Builder(this, controller, orientationDelegate, + builder = new Builder(this, controller, orientationHelper, videoMediaPlayer); builder.videoFile(videoFile); return builder; } public Builder videoUrl(String videoUrl) { - builder = new Builder(this, controller, orientationDelegate, + builder = new Builder(this, controller, orientationHelper, videoMediaPlayer); builder.videoUrl(videoUrl); return builder; @@ -121,9 +170,13 @@ public Builder videoUrl(String videoUrl) { protected void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { - orientationDelegate.activateFullscreen(); + if (orientationHelper != null) { + orientationHelper.activateFullscreen(); + } } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { - orientationDelegate.exitFullscreen(); + if (orientationHelper != null) { + orientationHelper.exitFullscreen(); + } } } @@ -135,11 +188,13 @@ protected void onDetachedFromWindow() { private void handleOnDetach() { Log.d(FullscreenVideoView.class.getSimpleName(), "onDetachedFromWindow: "); - controller.onDetach(); + if (controller != null) { + controller.onDetach(); + } // Disable and null the OrientationEventListener - if (orientationDelegate != null) { - orientationDelegate.disable(); + if (orientationHelper != null) { + orientationHelper.disable(); } if (videoMediaPlayer != null) { @@ -157,7 +212,7 @@ private void handleOnDetach() { } controller = null; - orientationDelegate = null; + orientationHelper = null; videoMediaPlayer = null; surfaceHolder = null; surfaceView = null; @@ -172,83 +227,73 @@ private void handleOnDetach() { void setupMediaPlayer(String videoPath) { try { showProgress(); - videoMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); - videoMediaPlayer.setDataSource(videoPath); - videoMediaPlayer.setOnPreparedListener(onPreparedListener); - videoMediaPlayer.prepareAsync(); + if (videoMediaPlayer != null) { + videoMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); + videoMediaPlayer.setDataSource(videoPath); + videoMediaPlayer.setOnPreparedListener(onPreparedListener); + videoMediaPlayer.prepareAsync(); + } } catch (IOException e) { e.printStackTrace(); } } private void setupOnTouchListener() { - onTouchListener = new VideoOnTouchListener(); + onTouchListener = new OnTouchListener() { + @Override + public boolean onTouch(View view, MotionEvent event) { + view.performClick(); + if (controller != null) { + controller.show(); + } + return false; + } + }; setOnTouchListener(onTouchListener); } private void setupProgressBarColor() { int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime); - progressBar.animate().setDuration(shortAnimTime); + if (progressBar != null) { + progressBar.animate().setDuration(shortAnimTime); + } } - private void hideProgress() { - progressBar.setVisibility(View.INVISIBLE); + void hideProgress() { + if (progressBar != null) { + progressBar.setVisibility(View.INVISIBLE); + } } private void showProgress() { - progressBar.setVisibility(View.VISIBLE); + if (progressBar != null) { + progressBar.setVisibility(View.VISIBLE); + } } boolean isLandscape() { - return orientationDelegate.isLandscape(); + return orientationHelper.isLandscape(); } void toggleFullscreen() { - orientationDelegate.toggleFullscreen(); - } - - void enableAutoStart() { - videoMediaPlayer.enableAutoStart(); - } - - private class VideoOnKeyListener implements View.OnKeyListener { - @Override - public boolean onKey(View v, int keyCode, KeyEvent event) { - return (event.getAction() == KeyEvent.ACTION_UP) && - (keyCode == KeyEvent.KEYCODE_BACK) && - orientationDelegate.shouldHandleOnBackPressed(); + if (orientationHelper != null) { + orientationHelper.toggleFullscreen(); } } - private class VideoOnPreparedListener implements MediaPlayer.OnPreparedListener { - @Override - public void onPrepared(MediaPlayer mediaPlayer) { - Log.d(FullscreenVideoView.class.getSimpleName(), "onPrepared: "); - if (!((Activity) getContext()).isDestroyed()) { - hideProgress(); - // Get the dimensions of the video - int videoWidth = videoMediaPlayer.getVideoWidth(); - int videoHeight = videoMediaPlayer.getVideoHeight(); - surfaceView.updateLayoutParams(videoWidth, videoHeight); - // Start media player if auto start is enabled - if (mediaPlayer != null && videoMediaPlayer.isAutoStartEnabled()) { - isMediaPlayerPrepared = true; - mediaPlayer.start(); - } - } + void enableAutoStart() { + if (videoMediaPlayer != null) { + videoMediaPlayer.enableAutoStart(); } } - private class VideoOrientationDelegate extends OrientationDelegate { - VideoOrientationDelegate() { - super(getContext(), FullscreenVideoView.this); - } - - @Override - public void onOrientationChanged() { - // Update the fullscreen button drawable + public void onOrientationChanged() { + // Update the fullscreen button drawable + if (controller != null) { controller.updateFullScreenDrawable(); - if (orientationDelegate.isLandscape()) { + } + if (surfaceView != null) { + if (orientationHelper.isLandscape()) { surfaceView.resetLayoutParams(); } else { surfaceView.updateLayoutParams(videoMediaPlayer.getVideoWidth(), @@ -256,34 +301,4 @@ public void onOrientationChanged() { } } } - - private class VideoOnTouchListener implements View.OnTouchListener { - @Override - public boolean onTouch(View view, MotionEvent motionEvent) { - view.performClick(); - controller.show(); - return false; - } - } - - private class SurfaceHolderCallback implements SurfaceHolder.Callback { - @Override - public void surfaceCreated(SurfaceHolder holder) { - if (videoMediaPlayer != null) { - videoMediaPlayer.setDisplay(surfaceHolder); - } - } - - @Override - public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { - - } - - @Override - public void surfaceDestroyed(SurfaceHolder holder) { - if (videoMediaPlayer != null && isMediaPlayerPrepared) { - videoMediaPlayer.pause(); - } - } - } } diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoControllerView.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoControllerView.java index 4cbc1ef..9f613d6 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoControllerView.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoControllerView.java @@ -24,6 +24,7 @@ import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; +import android.support.annotation.Nullable; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.util.Log; @@ -73,17 +74,21 @@ class VideoControllerView extends FrameLayout { private static final int FADE_OUT = 1; private static final int SHOW_PROGRESS = 2; + @Nullable VideoMediaPlayer videoMediaPlayer; private TextView endTime; private TextView currentTime; boolean isDragging; + @Nullable private Handler handler = new VideoControllerView.MessageHandler(this); private SeekBar progress; private ImageButton startPauseButton; private ImageButton ffwdButton; private ImageButton rewButton; private ImageButton fullscreenButton; + @Nullable private View.OnClickListener pauseListener = new PauseOnClickListener(); + @Nullable private View.OnClickListener fullscreenListener = new FullscreenOnClickListener(); // There are two scenarios that can trigger the seekbar listener to trigger: // @@ -96,8 +101,11 @@ class VideoControllerView extends FrameLayout { // The second scenario involves the user operating the scroll ball, in this // case there WON'T BE onStartTrackingTouch/onStopTrackingTouch notifications, // we will simply apply the updated position without suspending regular updates. + @Nullable private SeekBar.OnSeekBarChangeListener seekListener = new OnSeekChangeListener(); + @Nullable private View.OnClickListener rewListener = new RewindOnClickListener(); + @Nullable private View.OnClickListener ffwdListener = new FfwdOnClickListener(); // Drawables for the buttons of the controller private Drawable exitFullscreenDrawable = ContextCompat.getDrawable(getContext(), @@ -119,7 +127,7 @@ class VideoControllerView extends FrameLayout { // VideoView interface which is used to communicate with the VideoView private FullscreenVideoView fullscreenVideoView; - public VideoControllerView(Context context, AttributeSet attrs) { + VideoControllerView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater layoutInflater = LayoutInflater.from(getContext()); @@ -128,7 +136,7 @@ public VideoControllerView(Context context, AttributeSet attrs) { setupXmlAttributes(attrs); } - public VideoControllerView(Context context) { + VideoControllerView(Context context) { super(context); LayoutInflater layoutInflater = LayoutInflater.from(getContext()); layoutInflater.inflate(R.layout.video_controller, this, true); diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoMediaPlayer.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoMediaPlayer.java index 9b1d954..1de635b 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoMediaPlayer.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoMediaPlayer.java @@ -1,6 +1,7 @@ package bg.devlabs.fullscreenvideoview; import android.media.MediaPlayer; +import android.support.annotation.Nullable; /** * Created by Slavi Petrov on 20.10.2017 @@ -8,14 +9,14 @@ * slavi@devlabs.bg */ class VideoMediaPlayer extends MediaPlayer { + @Nullable private FullscreenVideoView fullscreenVideoView; private boolean isAutoStartEnabled; private boolean canPause = true; private boolean canSeekBackward = true; private boolean canSeekForward = true; - VideoMediaPlayer(FullscreenVideoView fullscreenVideoView) { - super(); + VideoMediaPlayer(@Nullable FullscreenVideoView fullscreenVideoView) { this.fullscreenVideoView = fullscreenVideoView; } @@ -36,7 +37,9 @@ boolean canSeekForward() { } void toggleFullScreen() { - fullscreenVideoView.toggleFullscreen(); + if (fullscreenVideoView != null) { + fullscreenVideoView.toggleFullscreen(); + } } void onPauseResume() { diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationDelegate.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java similarity index 94% rename from library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationDelegate.java rename to library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java index 73e45bd..6b07b1c 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationDelegate.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java @@ -27,7 +27,7 @@ *

* Handles orientation changes. Updates the VideoView layout params. Hides/shows the toolbar. */ -public abstract class OrientationDelegate extends OrientationEventListener { +public class OrientationHelper extends OrientationEventListener { private static final int LEFT_LANDSCAPE = 90; private static final int RIGHT_LANDSCAPE = 270; private static final int PORTRAIT = 0; @@ -43,7 +43,7 @@ public abstract class OrientationDelegate extends OrientationEventListener { private PortraitOrientation portraitOrientation = PortraitOrientation.DEFAULT; private boolean shouldEnterPortrait; - protected OrientationDelegate(Context context, FullscreenVideoView fullscreenVideoView) { + protected OrientationHelper(Context context, FullscreenVideoView fullscreenVideoView) { super(context); videoView = fullscreenVideoView; contentResolver = context.getContentResolver(); @@ -56,7 +56,7 @@ public void activateFullscreen() { } // Fullscreen active - onOrientationChanged(); + videoView.onOrientationChanged(); // Change the screen orientation to SENSOR_LANDSCAPE Activity activity = ((Activity) videoView.getContext()); @@ -76,9 +76,6 @@ public void activateFullscreen() { toggleSystemUiVisibility(activity.getWindow()); } - protected abstract void onOrientationChanged(); - - @SuppressWarnings("SuspiciousNameCombination") private void updateLayoutParams() { ViewGroup.LayoutParams params = videoView.getLayoutParams(); Context context = videoView.getContext(); @@ -102,7 +99,7 @@ public void exitFullscreen() { } // Update the fullscreen button drawable - onOrientationChanged(); + videoView.onOrientationChanged(); // Change the screen orientation to PORTRAIT Activity activity = (Activity) videoView.getContext(); @@ -175,7 +172,7 @@ public boolean shouldHandleOnBackPressed() { if (isLandscape) { // Locks the screen orientation to portrait setOrientation(portraitOrientation.getValue()); - onOrientationChanged(); + videoView.onOrientationChanged(); return true; } @@ -199,7 +196,6 @@ public void setPortraitOrientation(PortraitOrientation portraitOrientation) { this.portraitOrientation = portraitOrientation; } - @SuppressWarnings("StandardVariableNames") private static boolean shouldChangeOrientation(int a, int b) { return a > b - ROTATE_THRESHOLD && a < b + ROTATE_THRESHOLD; } @@ -232,8 +228,7 @@ public void onOrientationChanged(int orientation) { * @return true or false according to whether the rotation is enabled or disabled */ private static boolean isRotationEnabled(ContentResolver contentResolver) { - return Settings.System.getInt(contentResolver, Settings.System.ACCELEROMETER_ROTATION, - 0) == 1; + return Settings.System.getInt(contentResolver, Settings.System.ACCELEROMETER_ROTATION, 0) == 1; } public boolean isLandscape() { diff --git a/library/src/main/res/layout/fullscreen_video_view.xml b/library/src/main/res/layout/fullscreen_video_view.xml index b7cef07..ca4d69f 100644 --- a/library/src/main/res/layout/fullscreen_video_view.xml +++ b/library/src/main/res/layout/fullscreen_video_view.xml @@ -1,6 +1,5 @@ @@ -21,7 +20,6 @@ style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_marginTop="-6dp" android:background="@color/transparent" android:indeterminate="true" android:visibility="invisible" /> diff --git a/library/src/main/res/layout/video_controller.xml b/library/src/main/res/layout/video_controller.xml index 0c87ebf..c9c8d60 100644 --- a/library/src/main/res/layout/video_controller.xml +++ b/library/src/main/res/layout/video_controller.xml @@ -83,7 +83,6 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" - android:layout_marginTop="-7dp" android:background="@android:color/transparent" android:contentDescription="@string/cd_fullscreen_media_button" android:paddingBottom="4dp" diff --git a/library/src/main/res/values/cd_strings.xml b/library/src/main/res/values/cd_strings.xml index 7385ee7..2b6e18b 100644 --- a/library/src/main/res/values/cd_strings.xml +++ b/library/src/main/res/values/cd_strings.xml @@ -1,9 +1,7 @@ - Previous media button Rewind media button Pause media button Forward media button - Next media button Fullscreen media button \ No newline at end of file diff --git a/library/src/main/res/values/strings.xml b/library/src/main/res/values/strings.xml deleted file mode 100644 index e865710..0000000 --- a/library/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - FullscreenVideoView - From 4cdaed7c1754cfdd2b497d4048070b2a5ed116c6 Mon Sep 17 00:00:00 2001 From: Slavi Petrov Date: Mon, 29 Jan 2018 12:36:41 +0200 Subject: [PATCH 2/5] Update compileSdk version to 27 Update supportLib version to 27.0.2 --- dependencies.gradle | 4 ++-- sample/build.gradle | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index 408ccf0..db0ceae 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,6 +1,6 @@ ext.versions = [ minSdk : 19, - compileSdk : 26, + compileSdk : 27, buildTools : '26.0.2', publishVersion : '0.0.1', publishVersionCode: 1, @@ -8,7 +8,7 @@ ext.versions = [ bintrayPlugin : '1.7.3', mavenPlugin : '1.4.1', - supportLib : '27.0.0', + supportLib : '27.0.2', espresso : '3.0.1', kotlin : '1.2.0', leakCanary : '1.5.4' diff --git a/sample/build.gradle b/sample/build.gradle index ce2f9dc..ea567be 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -33,11 +33,9 @@ android { dependencies { implementation project(':library') - implementation "com.android.support:appcompat-v7:$versions.supportLib" implementation "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin" debugImplementation "com.squareup.leakcanary:leakcanary-android:$versions.leakCanary" - androidTestImplementation "com.android.support.test.espresso:espresso-core:$versions.espresso" } From 626486d3e93d60e233b20e4c2246e838c0625b09 Mon Sep 17 00:00:00 2001 From: Slavi Petrov Date: Mon, 29 Jan 2018 14:54:38 +0200 Subject: [PATCH 3/5] Add Support Design library to the sample project's build.gradle file --- sample/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/sample/build.gradle b/sample/build.gradle index ea567be..51e6020 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -34,6 +34,7 @@ android { dependencies { implementation project(':library') implementation "com.android.support:appcompat-v7:$versions.supportLib" + implementation "com.android.support:design:$versions.supportLib" implementation "org.jetbrains.kotlin:kotlin-stdlib:$versions.kotlin" debugImplementation "com.squareup.leakcanary:leakcanary-android:$versions.leakCanary" androidTestImplementation "com.android.support.test.espresso:espresso-core:$versions.espresso" From b9e3f83512e90b6b990843d19239d12b785ddf75 Mon Sep 17 00:00:00 2001 From: Slavi Petrov Date: Mon, 29 Jan 2018 15:55:25 +0200 Subject: [PATCH 4/5] Add check if the activity where the video view is used is paused Rename OrientationDelegate to OrientationHelper Remove unnecessary fields --- .../devlabs/fullscreenvideoview/Builder.java | 6 +- .../FullscreenVideoView.java | 158 ++++++++---------- .../VideoControllerView.java | 15 +- .../orientation/OrientationHelper.java | 2 +- 4 files changed, 82 insertions(+), 99 deletions(-) diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/Builder.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/Builder.java index deb5d4e..d5be3d8 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/Builder.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/Builder.java @@ -32,12 +32,14 @@ public class Builder { this.videoMediaPlayer = videoMediaPlayer; } - void videoFile(@NonNull File videoFile) { + Builder videoFile(@NonNull File videoFile) { fullscreenVideoView.setupMediaPlayer(videoFile.getPath()); + return this; } - void videoUrl(@NonNull String videoUrl) { + Builder videoUrl(@NonNull String videoUrl) { fullscreenVideoView.setupMediaPlayer(videoUrl); + return this; } public Builder enableAutoStart() { diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/FullscreenVideoView.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/FullscreenVideoView.java index f50554e..f6d58d9 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/FullscreenVideoView.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/FullscreenVideoView.java @@ -1,6 +1,5 @@ package bg.devlabs.fullscreenvideoview; -import android.app.Activity; import android.content.Context; import android.content.res.Configuration; import android.media.AudioManager; @@ -8,7 +7,6 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.AttributeSet; -import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -33,7 +31,6 @@ public class FullscreenVideoView extends FrameLayout { VideoSurfaceView surfaceView; @Nullable SurfaceHolder surfaceHolder; - private SurfaceHolder.Callback surfaceHolderCallback; @Nullable private ProgressBar progressBar; @Nullable @@ -41,13 +38,10 @@ public class FullscreenVideoView extends FrameLayout { @Nullable VideoMediaPlayer videoMediaPlayer; boolean isMediaPlayerPrepared; - private Builder builder; - @Nullable - private MediaPlayer.OnPreparedListener onPreparedListener; - @Nullable - private View.OnTouchListener onTouchListener; @Nullable OrientationHelper orientationHelper; + private SurfaceHolder.Callback surfaceHolderCallback; + boolean isPaused; public FullscreenVideoView(@NonNull Context context) { super(context); @@ -69,65 +63,54 @@ private void init(AttributeSet attrs) { // Skip this init rows - needed when changing FullscreenVideoView properties in XML if (!isInEditMode()) { videoMediaPlayer = new VideoMediaPlayer(this); - initOrientationHandlers(); + orientationHelper = new OrientationHelper(getContext(), this); + orientationHelper.enable(); } - surfaceHolderCallback = new SurfaceHolder.Callback() { + setupSurfaceHolder(); + if (controller != null) { + controller.init(orientationHelper, videoMediaPlayer, attrs); + } + setupProgressBarColor(); + setFocusableInTouchMode(true); + requestFocus(); + initOnBackPressedListener(); + // Setup onTouch listener + setOnTouchListener(new OnTouchListener() { @Override - public void surfaceCreated(SurfaceHolder holder) { - if (videoMediaPlayer != null) { - videoMediaPlayer.setDisplay(surfaceHolder); + public boolean onTouch(View view, MotionEvent event) { + view.performClick(); + if (controller != null) { + controller.show(); } + return false; } + }); + } - @Override - public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + private void setupSurfaceHolder() { + if (surfaceView != null) { + surfaceHolderCallback = new SurfaceHolder.Callback() { + @Override + public void surfaceCreated(SurfaceHolder holder) { + if (videoMediaPlayer != null) { + videoMediaPlayer.setDisplay(surfaceHolder); + } + } - } + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { - @Override - public void surfaceDestroyed(SurfaceHolder holder) { - if (videoMediaPlayer != null && isMediaPlayerPrepared) { - videoMediaPlayer.pause(); } - } - }; - if (surfaceView != null) { - surfaceHolder = surfaceView.getHolder(); - surfaceHolder.addCallback(surfaceHolderCallback); - } - if (controller != null) { - controller.init(this, videoMediaPlayer, attrs); - } - setupProgressBarColor(); - initOnBackPressedListener(); - // Setup VideoView - setupOnTouchListener(); - onPreparedListener = new MediaPlayer.OnPreparedListener() { - @Override - public void onPrepared(MediaPlayer mediaPlayer) { - Log.d(FullscreenVideoView.class.getSimpleName(), "onPrepared: "); - if (!((Activity) getContext()).isDestroyed()) { - hideProgress(); - // Get the dimensions of the video - int videoWidth = videoMediaPlayer.getVideoWidth(); - int videoHeight = videoMediaPlayer.getVideoHeight(); - surfaceView.updateLayoutParams(videoWidth, videoHeight); - // Start media player if auto start is enabled - if (mediaPlayer != null && videoMediaPlayer.isAutoStartEnabled()) { - isMediaPlayerPrepared = true; - mediaPlayer.start(); + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + if (videoMediaPlayer != null && isMediaPlayerPrepared) { + videoMediaPlayer.pause(); } } - } - }; - } - - private void initOrientationHandlers() { - if (!isInEditMode()) { - if (orientationHelper != null) { - orientationHelper.enable(); - } + }; + surfaceHolder = surfaceView.getHolder(); + surfaceHolder.addCallback(surfaceHolderCallback); } } @@ -140,8 +123,6 @@ private void findChildViews() { } private void initOnBackPressedListener() { - setFocusableInTouchMode(true); - requestFocus(); setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { @@ -153,17 +134,13 @@ public boolean onKey(View v, int keyCode, KeyEvent event) { } public Builder videoFile(File videoFile) { - builder = new Builder(this, controller, orientationHelper, - videoMediaPlayer); - builder.videoFile(videoFile); - return builder; + return new Builder(this, controller, orientationHelper, + videoMediaPlayer).videoFile(videoFile); } public Builder videoUrl(String videoUrl) { - builder = new Builder(this, controller, orientationHelper, - videoMediaPlayer); - builder.videoUrl(videoUrl); - return builder; + return new Builder(this, controller, orientationHelper, + videoMediaPlayer).videoUrl(videoUrl); } @Override @@ -186,8 +163,13 @@ protected void onDetachedFromWindow() { super.onDetachedFromWindow(); } + @Override + protected void onVisibilityChanged(@NonNull View changedView, int visibility) { + super.onVisibilityChanged(changedView, visibility); + isPaused = visibility != View.VISIBLE; + } + private void handleOnDetach() { - Log.d(FullscreenVideoView.class.getSimpleName(), "onDetachedFromWindow: "); if (controller != null) { controller.onDetach(); } @@ -216,8 +198,6 @@ private void handleOnDetach() { videoMediaPlayer = null; surfaceHolder = null; surfaceView = null; - onTouchListener = null; - onPreparedListener = null; progressBar = null; setOnKeyListener(null); @@ -225,12 +205,30 @@ private void handleOnDetach() { } void setupMediaPlayer(String videoPath) { + showProgress(); try { - showProgress(); if (videoMediaPlayer != null) { videoMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); videoMediaPlayer.setDataSource(videoPath); - videoMediaPlayer.setOnPreparedListener(onPreparedListener); + videoMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { + @Override + public void onPrepared(MediaPlayer mediaPlayer) { + hideProgress(); + // Get the dimensions of the video + int videoWidth = videoMediaPlayer.getVideoWidth(); + int videoHeight = videoMediaPlayer.getVideoHeight(); + if (surfaceView != null) { + surfaceView.updateLayoutParams(videoWidth, videoHeight); + } + if (!isPaused) { + isMediaPlayerPrepared = true; + // Start media player if auto start is enabled + if (mediaPlayer != null && videoMediaPlayer.isAutoStartEnabled()) { + mediaPlayer.start(); + } + } + } + }); videoMediaPlayer.prepareAsync(); } } catch (IOException e) { @@ -238,20 +236,6 @@ void setupMediaPlayer(String videoPath) { } } - private void setupOnTouchListener() { - onTouchListener = new OnTouchListener() { - @Override - public boolean onTouch(View view, MotionEvent event) { - view.performClick(); - if (controller != null) { - controller.show(); - } - return false; - } - }; - setOnTouchListener(onTouchListener); - } - private void setupProgressBarColor() { int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime); if (progressBar != null) { @@ -271,10 +255,6 @@ private void showProgress() { } } - boolean isLandscape() { - return orientationHelper.isLandscape(); - } - void toggleFullscreen() { if (orientationHelper != null) { orientationHelper.toggleFullscreen(); diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoControllerView.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoControllerView.java index 9f613d6..8597552 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoControllerView.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/VideoControllerView.java @@ -39,6 +39,8 @@ import java.lang.ref.WeakReference; import java.util.Locale; +import bg.devlabs.fullscreenvideoview.orientation.OrientationHelper; + /** * A view containing controls for a MediaPlayer. Typically contains the * buttons like "Play/Pause", "Rewind", "Fast Forward" and a progress @@ -124,10 +126,9 @@ class VideoControllerView extends FrameLayout { private int fastForwardDuration = Constants.FAST_FORWARD_DURATION; private int rewindDuration = Constants.REWIND_DURATION; - // VideoView interface which is used to communicate with the VideoView - private FullscreenVideoView fullscreenVideoView; + private OrientationHelper orientationHelper; - VideoControllerView(Context context, AttributeSet attrs) { + public VideoControllerView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater layoutInflater = LayoutInflater.from(getContext()); @@ -136,7 +137,7 @@ class VideoControllerView extends FrameLayout { setupXmlAttributes(attrs); } - VideoControllerView(Context context) { + public VideoControllerView(Context context) { super(context); LayoutInflater layoutInflater = LayoutInflater.from(getContext()); layoutInflater.inflate(R.layout.video_controller, this, true); @@ -412,7 +413,7 @@ public void updateFullScreenDrawable() { return; } - if (fullscreenVideoView.isLandscape()) { + if (orientationHelper.isLandscape()) { fullscreenButton.setImageDrawable(exitFullscreenDrawable); } else { fullscreenButton.setImageDrawable(enterFullscreenDrawable); @@ -508,11 +509,11 @@ public void setRewindDrawable(Drawable rewindDrawable) { this.rewindDrawable = rewindDrawable; } - public void init(FullscreenVideoView fullscreenVideoView, VideoMediaPlayer videoMediaPlayer, + public void init(OrientationHelper orientationHelper, VideoMediaPlayer videoMediaPlayer, AttributeSet attrs) { setupXmlAttributes(attrs); + this.orientationHelper = orientationHelper; this.videoMediaPlayer = videoMediaPlayer; - this.fullscreenVideoView = fullscreenVideoView; setMediaIcons(); } diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java index 6b07b1c..03061cc 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java @@ -43,7 +43,7 @@ public class OrientationHelper extends OrientationEventListener { private PortraitOrientation portraitOrientation = PortraitOrientation.DEFAULT; private boolean shouldEnterPortrait; - protected OrientationHelper(Context context, FullscreenVideoView fullscreenVideoView) { + public OrientationHelper(Context context, FullscreenVideoView fullscreenVideoView) { super(context); videoView = fullscreenVideoView; contentResolver = context.getContentResolver(); From 19b6794d64338ee69ff8afb77c3c22734850c638 Mon Sep 17 00:00:00 2001 From: Slavi Petrov Date: Mon, 29 Jan 2018 17:20:24 +0200 Subject: [PATCH 5/5] Change the way of hiding the action bar Change the library version --- library/release.gradle | 2 +- .../orientation/OrientationHelper.java | 34 +++++++------------ 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/library/release.gradle b/library/release.gradle index c797ecb..02dd709 100644 --- a/library/release.gradle +++ b/library/release.gradle @@ -5,7 +5,7 @@ apply from: 'keystore.gradle' ext { publishedGroupId = 'bg.devlabs.fullscreenvideoview' artifactId = 'library' - libraryVersion = '0.0.5' + libraryVersion = '0.0.6' } version = libraryVersion diff --git a/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java b/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java index 03061cc..1510a14 100644 --- a/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java +++ b/library/src/main/java/bg/devlabs/fullscreenvideoview/orientation/OrientationHelper.java @@ -70,7 +70,7 @@ public void activateFullscreen() { updateLayoutParams(); // Hiding the supportToolbar - hideActionBar(); + toggleActionBarVisibility(false); // Hide status bar toggleSystemUiVisibility(activity.getWindow()); @@ -105,7 +105,6 @@ public void exitFullscreen() { Activity activity = (Activity) videoView.getContext(); setOrientation(portraitOrientation.getValue()); - UiUtils.showOtherViews(getParent()); ViewGroup.LayoutParams params = videoView.getLayoutParams(); @@ -113,7 +112,7 @@ public void exitFullscreen() { params.height = originalHeight; videoView.setLayoutParams(params); - showActionBar(); + toggleActionBarVisibility(true); toggleSystemUiVisibility(activity.getWindow()); } @@ -131,33 +130,23 @@ private static void toggleSystemUiVisibility(Window activityWindow) { activityWindow.getDecorView().setSystemUiVisibility(newUiOptions); } - private void showActionBar() { - if (videoView.getContext() instanceof AppCompatActivity) { - ActionBar supportActionBar = ((AppCompatActivity) videoView.getContext()) - .getSupportActionBar(); + private void toggleActionBarVisibility(boolean visible) { + // AppCompatActivity support action bar + ActionBar supportActionBar = ((AppCompatActivity) videoView.getContext()) + .getSupportActionBar(); + // Activity action bar + android.app.ActionBar actionBar = ((Activity) videoView.getContext()).getActionBar(); + if (visible) { if (supportActionBar != null) { supportActionBar.show(); } - } - if (videoView.getContext() instanceof Activity) { - android.app.ActionBar actionBar = ((Activity) videoView.getContext()).getActionBar(); if (actionBar != null) { actionBar.show(); } - } - } - - private void hideActionBar() { - if (videoView.getContext() instanceof AppCompatActivity) { - ActionBar supportActionBar = ((AppCompatActivity) videoView.getContext()) - .getSupportActionBar(); + } else { if (supportActionBar != null) { supportActionBar.hide(); } - } - - if (videoView.getContext() instanceof Activity) { - android.app.ActionBar actionBar = ((Activity) videoView.getContext()).getActionBar(); if (actionBar != null) { actionBar.hide(); } @@ -228,7 +217,8 @@ public void onOrientationChanged(int orientation) { * @return true or false according to whether the rotation is enabled or disabled */ private static boolean isRotationEnabled(ContentResolver contentResolver) { - return Settings.System.getInt(contentResolver, Settings.System.ACCELEROMETER_ROTATION, 0) == 1; + return Settings.System.getInt(contentResolver, Settings.System.ACCELEROMETER_ROTATION, + 0) == 1; } public boolean isLandscape() {