From 883e4fcd7c2827ea5fc778447bed966842aa1a0e Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Fri, 11 Sep 2020 20:52:38 +0300 Subject: [PATCH] Small fixes of issues with old devices support, brightness, etc --- .../fragments/detail/VideoDetailFragment.java | 57 ++++++++++++------- .../org/schabi/newpipe/player/MainPlayer.java | 1 + .../newpipe/player/VideoPlayerImpl.java | 1 + .../player/event/PlayerGestureListener.java | 14 +++-- app/src/main/res/layout-large-land/player.xml | 6 +- app/src/main/res/layout/player.xml | 6 +- 6 files changed, 55 insertions(+), 30 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 b731d0270..319523ab7 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 @@ -102,13 +102,12 @@ import org.schabi.newpipe.util.ListHelper; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.PermissionHelper; +import org.schabi.newpipe.util.SerializedCache; import org.schabi.newpipe.util.ShareUtils; import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.views.AnimatedProgressBar; import org.schabi.newpipe.views.LargeTextMovementMethod; -import java.io.Serializable; -import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; @@ -337,7 +336,7 @@ public class VideoDetailFragment stopPlayerListener(); playerService = null; player = null; - saveCurrentAndRestoreDefaultBrightness(); + restoreDefaultBrightness(); } } @@ -426,7 +425,7 @@ public class VideoDetailFragment if (currentWorker != null) { currentWorker.dispose(); } - saveCurrentAndRestoreDefaultBrightness(); + restoreDefaultBrightness(); PreferenceManager.getDefaultSharedPreferences(requireContext()) .edit() .putString(getString(R.string.stream_info_selected_tab_key), @@ -538,31 +537,51 @@ public class VideoDetailFragment super.onSaveInstanceState(outState); if (!isLoading.get() && currentInfo != null && isVisible()) { - outState.putSerializable(INFO_KEY, currentInfo); + final String infoCacheKey = SerializedCache.getInstance() + .put(currentInfo, StreamInfo.class); + if (infoCacheKey != null) { + outState.putString(INFO_KEY, infoCacheKey); + } } if (playQueue != null) { - outState.putSerializable(VideoPlayer.PLAY_QUEUE_KEY, playQueue); + final String queueCacheKey = SerializedCache.getInstance() + .put(playQueue, PlayQueue.class); + if (queueCacheKey != null) { + outState.putString(VideoPlayer.PLAY_QUEUE_KEY, queueCacheKey); + } + } + final String stackCacheKey = SerializedCache.getInstance().put(stack, LinkedList.class); + if (stackCacheKey != null) { + outState.putString(STACK_KEY, stackCacheKey); } - outState.putSerializable(STACK_KEY, stack); } @Override protected void onRestoreInstanceState(@NonNull final Bundle savedState) { super.onRestoreInstanceState(savedState); - Serializable serializable = savedState.getSerializable(INFO_KEY); - if (serializable instanceof StreamInfo) { - currentInfo = (StreamInfo) serializable; - InfoCache.getInstance().putInfo(serviceId, url, currentInfo, InfoItem.InfoType.STREAM); + final String infoCacheKey = savedState.getString(INFO_KEY); + if (infoCacheKey != null) { + currentInfo = SerializedCache.getInstance().take(infoCacheKey, StreamInfo.class); + if (currentInfo != null) { + InfoCache.getInstance() + .putInfo(serviceId, url, currentInfo, InfoItem.InfoType.STREAM); + } } - serializable = savedState.getSerializable(STACK_KEY); - if (serializable instanceof Collection) { - //noinspection unchecked - stack.addAll((Collection) serializable); + final String stackCacheKey = savedState.getString(STACK_KEY); + if (stackCacheKey != null) { + final LinkedList cachedStack = + SerializedCache.getInstance().take(stackCacheKey, LinkedList.class); + if (cachedStack != null) { + stack.addAll(cachedStack); + } + } + final String queueCacheKey = savedState.getString(VideoPlayer.PLAY_QUEUE_KEY); + if (queueCacheKey != null) { + playQueue = SerializedCache.getInstance().take(queueCacheKey, PlayQueue.class); } - playQueue = (PlayQueue) savedState.getSerializable(VideoPlayer.PLAY_QUEUE_KEY); } /*////////////////////////////////////////////////////////////////////////// @@ -2027,13 +2046,11 @@ public class VideoDetailFragment && player.getPlayer().getPlaybackState() != Player.STATE_IDLE; } - private void saveCurrentAndRestoreDefaultBrightness() { + private void restoreDefaultBrightness() { final WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); if (lp.screenBrightness == -1) { return; } - // Save current brightness level - PlayerHelper.setScreenBrightness(activity, lp.screenBrightness); // Restore the old brightness when fragment.onPause() called or // when a player is in portrait @@ -2052,7 +2069,7 @@ public class VideoDetailFragment || !player.isFullscreen() || bottomSheetState != BottomSheetBehavior.STATE_EXPANDED) { // Apply system brightness when the player is not in fullscreen - saveCurrentAndRestoreDefaultBrightness(); + restoreDefaultBrightness(); } else { // Restore already saved brightness level final float brightnessLevel = PlayerHelper.getScreenBrightness(activity); diff --git a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java index 273f37cc8..c78ddf036 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java @@ -171,6 +171,7 @@ public final class MainPlayer extends Service { // Android TV will handle back button in case controls will be visible // (one more additional unneeded click while the player is hidden) playerImpl.hideControls(0, 0); + playerImpl.onQueueClosed(); // Notification shows information about old stream but if a user selects // a stream from backStack it's not actual anymore // So we should hide the notification at all. diff --git a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java index a5758301c..52df3d956 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java @@ -257,6 +257,7 @@ public class VideoPlayerImpl extends VideoPlayer } else { getRootView().setVisibility(View.VISIBLE); initVideoPlayer(); + onQueueClosed(); // Android TV: without it focus will frame the whole player playPauseButton.requestFocus(); } diff --git a/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java b/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java index 4aa6070eb..a2def2a64 100644 --- a/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java +++ b/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java @@ -9,6 +9,7 @@ import android.view.View; import android.view.ViewConfiguration; import android.view.Window; import android.view.WindowManager; +import android.widget.ProgressBar; import androidx.appcompat.content.res.AppCompatResources; import org.schabi.newpipe.R; import org.schabi.newpipe.player.BasePlayer; @@ -264,14 +265,19 @@ public class PlayerGestureListener } final Window window = parent.getWindow(); - - playerImpl.getBrightnessProgressBar().incrementProgressBy((int) distanceY); - final float currentProgressPercent = (float) playerImpl.getBrightnessProgressBar() - .getProgress() / playerImpl.getMaxGestureLength(); final WindowManager.LayoutParams layoutParams = window.getAttributes(); + final ProgressBar bar = playerImpl.getBrightnessProgressBar(); + final float oldBrightness = layoutParams.screenBrightness; + bar.setProgress((int) (bar.getMax() * Math.max(0, Math.min(1, oldBrightness)))); + bar.incrementProgressBy((int) distanceY); + + final float currentProgressPercent = (float) bar.getProgress() / bar.getMax(); layoutParams.screenBrightness = currentProgressPercent; window.setAttributes(layoutParams); + // Save current brightness level + PlayerHelper.setScreenBrightness(parent, currentProgressPercent); + if (DEBUG) { Log.d(TAG, "onScroll().brightnessControl, " + "currentBrightness = " + currentProgressPercent); diff --git a/app/src/main/res/layout-large-land/player.xml b/app/src/main/res/layout-large-land/player.xml index 46edda8b7..3c410801e 100644 --- a/app/src/main/res/layout-large-land/player.xml +++ b/app/src/main/res/layout-large-land/player.xml @@ -353,10 +353,10 @@ android:id="@+id/playbackSeekBar" style="@style/Widget.AppCompat.SeekBar" android:layout_width="0dp" - android:layout_height="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="center" android:layout_weight="1" - android:paddingBottom="4dp" - android:paddingTop="8dp" + android:layout_marginTop="2dp" tools:progress="25" android:nextFocusDown="@id/screenRotationButton" tools:secondaryProgress="50"/> diff --git a/app/src/main/res/layout/player.xml b/app/src/main/res/layout/player.xml index 88489d8d5..ec95bd8c3 100644 --- a/app/src/main/res/layout/player.xml +++ b/app/src/main/res/layout/player.xml @@ -352,10 +352,10 @@ android:id="@+id/playbackSeekBar" style="@style/Widget.AppCompat.SeekBar" android:layout_width="0dp" - android:layout_height="match_parent" + android:layout_height="wrap_content" + android:layout_gravity="center" android:layout_weight="1" - android:paddingBottom="4dp" - android:paddingTop="8dp" + android:layout_marginTop="2dp" tools:progress="25" tools:secondaryProgress="50"/>