From d7574973e97fa7a3a21aa59967311f6091941830 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Wed, 5 Aug 2020 12:46:25 +0300 Subject: [PATCH 1/2] Reduced CPU usage when playing a video by 7-10% --- .../fragments/detail/VideoDetailFragment.java | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index 66077f3ee..d014c5b38 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -202,6 +202,7 @@ public class VideoDetailFragment private ImageView thumbnailImageView; private ImageView thumbnailPlayButton; private AnimatedProgressBar positionView; + private ViewGroup playerPlaceholder; private View videoTitleRoot; private TextView videoTitleTextView; @@ -705,6 +706,7 @@ public class VideoDetailFragment thumbnailBackgroundButton = rootView.findViewById(R.id.detail_thumbnail_root_layout); thumbnailImageView = rootView.findViewById(R.id.detail_thumbnail_image_view); thumbnailPlayButton = rootView.findViewById(R.id.detail_thumbnail_play_button); + playerPlaceholder = rootView.findViewById(R.id.player_placeholder); contentRootLayoutHiding = rootView.findViewById(R.id.detail_content_root_hiding); @@ -1265,17 +1267,15 @@ public class VideoDetailFragment return; } - final FrameLayout viewHolder = getView().findViewById(R.id.player_placeholder); - // Check if viewHolder already contains a child - if (player.getRootView().getParent() != viewHolder) { + if (player.getRootView().getParent() != playerPlaceholder) { removeVideoPlayerView(); } setHeightThumbnail(); // Prevent from re-adding a view multiple times if (player.getRootView().getParent() == null) { - viewHolder.addView(player.getRootView()); + playerPlaceholder.addView(player.getRootView()); } } @@ -1290,9 +1290,8 @@ public class VideoDetailFragment return; } - final FrameLayout viewHolder = getView().findViewById(R.id.player_placeholder); - viewHolder.getLayoutParams().height = FrameLayout.LayoutParams.MATCH_PARENT; - viewHolder.requestLayout(); + playerPlaceholder.getLayoutParams().height = FrameLayout.LayoutParams.MATCH_PARENT; + playerPlaceholder.requestLayout(); } private void prepareDescription(final Description description) { @@ -1771,8 +1770,17 @@ public class VideoDetailFragment final int progressSeconds = (int) TimeUnit.MILLISECONDS.toSeconds(progress); final int durationSeconds = (int) TimeUnit.MILLISECONDS.toSeconds(duration); positionView.setMax(durationSeconds); - positionView.setProgressAnimated(progressSeconds); - detailPositionView.setText(Localization.getDurationString(progressSeconds)); + // If there is no player inside fragment use animation, otherwise don't because + // it affects CPU + if (playerPlaceholder.getChildCount() == 0) { + positionView.setProgressAnimated(progressSeconds); + } else { + positionView.setProgress(progressSeconds); + } + final String position = Localization.getDurationString(progressSeconds); + if (position != detailPositionView.getText()) { + detailPositionView.setText(position); + } if (positionView.getVisibility() != View.VISIBLE) { animateView(positionView, true, 100); animateView(detailPositionView, true, 100); @@ -1949,7 +1957,7 @@ public class VideoDetailFragment (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams(); final AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior(); final ValueAnimator valueAnimator = ValueAnimator - .ofInt(0, -getView().findViewById(R.id.player_placeholder).getHeight()); + .ofInt(0, -playerPlaceholder.getHeight()); valueAnimator.setInterpolator(new DecelerateInterpolator()); valueAnimator.addUpdateListener(animation -> { behavior.setTopAndBottomOffset((int) animation.getAnimatedValue()); From aed1687a45749b73386e0e79d0452396960893d9 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Sun, 16 Aug 2020 22:44:27 +0300 Subject: [PATCH 2/2] Improved an animation logic --- .../newpipe/fragments/detail/VideoDetailFragment.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index d014c5b38..29b943f7f 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -1769,10 +1769,11 @@ public class VideoDetailFragment private void showPlaybackProgress(final long progress, final long duration) { final int progressSeconds = (int) TimeUnit.MILLISECONDS.toSeconds(progress); final int durationSeconds = (int) TimeUnit.MILLISECONDS.toSeconds(duration); + // If the old and the new progress values have a big difference then use + // animation. Otherwise don't because it affects CPU + final boolean shouldAnimate = Math.abs(positionView.getProgress() - progressSeconds) > 2; positionView.setMax(durationSeconds); - // If there is no player inside fragment use animation, otherwise don't because - // it affects CPU - if (playerPlaceholder.getChildCount() == 0) { + if (shouldAnimate) { positionView.setProgressAnimated(progressSeconds); } else { positionView.setProgress(progressSeconds);