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/9] 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 daa427dc152a1fc343ba4de14eb477f9ecc5c179 Mon Sep 17 00:00:00 2001 From: nmurali94 Date: Sat, 15 Aug 2020 11:23:25 -0400 Subject: [PATCH 2/9] Restore license pop up after orientation change --- .../org/schabi/newpipe/about/License.java | 4 +- .../schabi/newpipe/about/LicenseFragment.java | 40 +++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/about/License.java b/app/src/main/java/org/schabi/newpipe/about/License.java index 370009860..877b51fd8 100644 --- a/app/src/main/java/org/schabi/newpipe/about/License.java +++ b/app/src/main/java/org/schabi/newpipe/about/License.java @@ -4,10 +4,12 @@ import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; +import java.io.Serializable; + /** * Class for storing information about a software license. */ -public class License implements Parcelable { +public class License implements Parcelable, Serializable { public static final Creator CREATOR = new Creator() { @Override public License createFromParcel(final Parcel source) { diff --git a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java index 404f12602..e869dbb14 100644 --- a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java +++ b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java @@ -1,7 +1,6 @@ package org.schabi.newpipe.about; import android.app.Activity; -import android.content.Context; import android.os.Bundle; import android.view.ContextMenu; import android.view.LayoutInflater; @@ -11,12 +10,14 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import org.schabi.newpipe.R; import org.schabi.newpipe.util.ShareUtils; +import java.io.Serializable; import java.util.Arrays; /** @@ -26,6 +27,8 @@ public class LicenseFragment extends Fragment { private static final String ARG_COMPONENTS = "components"; private SoftwareComponent[] softwareComponents; private SoftwareComponent componentForContextMenu; + private License activeLicense; + private static final String LICENSE_KEY = "ACTIVE_LICENSE"; public static LicenseFragment newInstance(final SoftwareComponent[] softwareComponents) { if (softwareComponents == null) { @@ -44,8 +47,8 @@ public class LicenseFragment extends Fragment { * @param context the context to use * @param license the license to show */ - private static void showLicense(final Context context, final License license) { - new LicenseFragmentHelper((Activity) context).execute(license); + private static void showLicense(final Activity context, final License license) { + new LicenseFragmentHelper(context).execute(license); } @Override @@ -54,6 +57,12 @@ public class LicenseFragment extends Fragment { softwareComponents = (SoftwareComponent[]) getArguments() .getParcelableArray(ARG_COMPONENTS); + if (savedInstanceState != null) { + final Serializable license = savedInstanceState.getSerializable(LICENSE_KEY); + if (license != null) { + activeLicense = (License) license; + } + } // Sort components by name Arrays.sort(softwareComponents, (o1, o2) -> o1.getName().compareTo(o2.getName())); } @@ -66,8 +75,10 @@ public class LicenseFragment extends Fragment { final ViewGroup softwareComponentsView = rootView.findViewById(R.id.software_components); final View licenseLink = rootView.findViewById(R.id.app_read_license); - licenseLink.setOnClickListener(v -> - showLicense(getActivity(), StandardLicenses.GPL3)); + licenseLink.setOnClickListener(v -> { + activeLicense = StandardLicenses.GPL3; + showLicense(getActivity(), StandardLicenses.GPL3); + }); for (final SoftwareComponent component : softwareComponents) { final View componentView = inflater @@ -81,11 +92,16 @@ public class LicenseFragment extends Fragment { component.getLicense().getAbbreviation())); componentView.setTag(component); - componentView.setOnClickListener(v -> - showLicense(getActivity(), component.getLicense())); + componentView.setOnClickListener(v -> { + activeLicense = component.getLicense(); + showLicense(getActivity(), component.getLicense()); + }); softwareComponentsView.addView(componentView); registerForContextMenu(componentView); } + if (activeLicense != null) { + showLicense(getActivity(), activeLicense); + } return rootView; } @@ -101,7 +117,7 @@ public class LicenseFragment extends Fragment { } @Override - public boolean onContextItemSelected(final MenuItem item) { + public boolean onContextItemSelected(@NonNull final MenuItem item) { // item.getMenuInfo() is null so we use the tag of the view final SoftwareComponent component = componentForContextMenu; if (component == null) { @@ -116,4 +132,12 @@ public class LicenseFragment extends Fragment { } return false; } + + @Override + public void onSaveInstanceState(@NonNull final Bundle savedInstanceState) { + super.onSaveInstanceState(savedInstanceState); + if (activeLicense != null) { + savedInstanceState.putSerializable(LICENSE_KEY, activeLicense); + } + } } 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 3/9] 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); From 24c24d6c724cdf392a8a90b0ffef88cfd5c9f641 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Sun, 16 Aug 2020 22:20:37 +0300 Subject: [PATCH 4/9] Skipping interception of some gestures --- .../google/android/material/appbar/FlingBehavior.java | 8 ++++++++ .../player/event/CustomBottomSheetBehavior.java | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/google/android/material/appbar/FlingBehavior.java b/app/src/main/java/com/google/android/material/appbar/FlingBehavior.java index 174631fde..b5c7fc564 100644 --- a/app/src/main/java/com/google/android/material/appbar/FlingBehavior.java +++ b/app/src/main/java/com/google/android/material/appbar/FlingBehavior.java @@ -68,6 +68,14 @@ public final class FlingBehavior extends AppBarLayout.Behavior { return false; } } + final View seekBar = child.findViewById(R.id.playbackSeekBar); + if (seekBar != null) { + final boolean visible = seekBar.getGlobalVisibleRect(globalRect); + if (visible && globalRect.contains((int) ev.getRawX(), (int) ev.getRawY())) { + allowScroll = false; + return false; + } + } allowScroll = true; switch (ev.getActionMasked()) { case MotionEvent.ACTION_DOWN: diff --git a/app/src/main/java/org/schabi/newpipe/player/event/CustomBottomSheetBehavior.java b/app/src/main/java/org/schabi/newpipe/player/event/CustomBottomSheetBehavior.java index 1d0b3ae26..19c621221 100644 --- a/app/src/main/java/org/schabi/newpipe/player/event/CustomBottomSheetBehavior.java +++ b/app/src/main/java/org/schabi/newpipe/player/event/CustomBottomSheetBehavior.java @@ -4,6 +4,7 @@ import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; +import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import androidx.annotation.NonNull; @@ -25,7 +26,7 @@ public class CustomBottomSheetBehavior extends BottomSheetBehavior private boolean skippingInterception = false; private final List skipInterceptionOfElements = Arrays.asList( R.id.detail_content_root_layout, R.id.relatedStreamsLayout, - R.id.playQueuePanel, R.id.viewpager); + R.id.playQueuePanel, R.id.viewpager, R.id.bottomControls); @Override public boolean onInterceptTouchEvent(@NonNull final CoordinatorLayout parent, @@ -51,6 +52,13 @@ public class CustomBottomSheetBehavior extends BottomSheetBehavior visible = viewGroup.getGlobalVisibleRect(globalRect); if (visible && globalRect.contains((int) event.getRawX(), (int) event.getRawY())) { + // Makes bottom part of the player draggable in portrait when + // playbackControlRoot is hidden + if (element == R.id.bottomControls + && child.findViewById(R.id.playbackControlRoot) + .getVisibility() != View.VISIBLE) { + return super.onInterceptTouchEvent(parent, child, event); + } skippingInterception = true; return false; } From 552d585fca1e7267c2ca62f11bb85efeb69b37bc Mon Sep 17 00:00:00 2001 From: TobiGr Date: Tue, 18 Aug 2020 13:28:13 +0200 Subject: [PATCH 5/9] Extract common part from if --- .../java/org/schabi/newpipe/player/VideoPlayerImpl.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) 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 0c69a22d5..0e06c1aaf 100644 --- a/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java +++ b/app/src/main/java/org/schabi/newpipe/player/VideoPlayerImpl.java @@ -322,17 +322,15 @@ public class VideoPlayerImpl extends VideoPlayer if (popupPlayerSelected()) { final float captionRatio = (captionScale - 1.0f) / 5.0f + 1.0f; view.setFractionalTextSize(SubtitleView.DEFAULT_TEXT_SIZE_FRACTION * captionRatio); - view.setApplyEmbeddedStyles(captionStyle.equals(CaptionStyleCompat.DEFAULT)); - view.setStyle(captionStyle); } else { final DisplayMetrics metrics = context.getResources().getDisplayMetrics(); final int minimumLength = Math.min(metrics.heightPixels, metrics.widthPixels); final float captionRatioInverse = 20f + 4f * (1.0f - captionScale); view.setFixedTextSize(TypedValue.COMPLEX_UNIT_PX, (float) minimumLength / captionRatioInverse); - view.setApplyEmbeddedStyles(captionStyle.equals(CaptionStyleCompat.DEFAULT)); - view.setStyle(captionStyle); } + view.setApplyEmbeddedStyles(captionStyle.equals(CaptionStyleCompat.DEFAULT)); + view.setStyle(captionStyle); } /** From e5ffddfc6b51dbe1bb0cab79677817e01abe13e2 Mon Sep 17 00:00:00 2001 From: mhmdanas <32234660+mhmdanas@users.noreply.github.com> Date: Wed, 12 Aug 2020 22:21:09 +0300 Subject: [PATCH 6/9] Use SubtitlesStream#getUrl instead of getURL --- app/build.gradle | 2 +- .../schabi/newpipe/player/resolver/VideoPlaybackResolver.java | 2 +- .../main/java/us/shandian/giga/get/DownloadMissionRecover.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 02edafd74..345f45f66 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -164,7 +164,7 @@ dependencies { exclude module: 'support-annotations' } - implementation 'com.github.TeamNewPipe:NewPipeExtractor:5ac80624a40f4c600ae493e66881b5bf008f0ddb' + implementation 'com.github.TeamNewPipe:NewPipeExtractor:6633f26ec5a73a8e932de575b7a0643b6ad6c890' implementation "com.github.TeamNewPipe:nanojson:1d9e1aea9049fc9f85e68b43ba39fe7be1c1f751" implementation "org.jsoup:jsoup:1.13.1" diff --git a/app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java b/app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java index 2eb766769..7965f421a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java +++ b/app/src/main/java/org/schabi/newpipe/player/resolver/VideoPlaybackResolver.java @@ -106,7 +106,7 @@ public class VideoPlaybackResolver implements PlaybackResolver { SELECTION_FLAG_AUTOSELECT, PlayerHelper.captionLanguageOf(context, subtitle)); final MediaSource textSource = dataSource.getSampleMediaSourceFactory() - .createMediaSource(Uri.parse(subtitle.getURL()), textFormat, TIME_UNSET); + .createMediaSource(Uri.parse(subtitle.getUrl()), textFormat, TIME_UNSET); mediaSources.add(textSource); } } diff --git a/app/src/main/java/us/shandian/giga/get/DownloadMissionRecover.java b/app/src/main/java/us/shandian/giga/get/DownloadMissionRecover.java index 14ac392a0..ab158ec51 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadMissionRecover.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadMissionRecover.java @@ -155,7 +155,7 @@ public class DownloadMissionRecover extends Thread { for (SubtitlesStream subtitles : mExtractor.getSubtitles(mRecovery.format)) { String tag = subtitles.getLanguageTag(); if (tag.equals(mRecovery.desired) && subtitles.isAutoGenerated() == mRecovery.desired2) { - url = subtitles.getURL(); + url = subtitles.getUrl(); break; } } From c66c81294e7a26ae4e27a56426a2023c6ea6fbf1 Mon Sep 17 00:00:00 2001 From: TobiGr Date: Tue, 1 Sep 2020 17:39:06 +0200 Subject: [PATCH 7/9] Remove unused and redundant code. --- .../main/java/org/schabi/newpipe/database/Migrations.java | 4 ++-- app/src/main/java/org/schabi/newpipe/player/BasePlayer.java | 5 ++--- .../java/org/schabi/newpipe/player/playqueue/PlayQueue.java | 4 ++-- .../org/schabi/newpipe/settings/MainSettingsFragment.java | 4 ++-- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/database/Migrations.java b/app/src/main/java/org/schabi/newpipe/database/Migrations.java index 088b9ed19..f5195ba8d 100644 --- a/app/src/main/java/org/schabi/newpipe/database/Migrations.java +++ b/app/src/main/java/org/schabi/newpipe/database/Migrations.java @@ -6,7 +6,7 @@ import androidx.annotation.NonNull; import androidx.room.migration.Migration; import androidx.sqlite.db.SupportSQLiteDatabase; -import org.schabi.newpipe.BuildConfig; +import org.schabi.newpipe.MainActivity; public final class Migrations { public static final int DB_VER_1 = 1; @@ -14,7 +14,7 @@ public final class Migrations { public static final int DB_VER_3 = 3; private static final String TAG = Migrations.class.getName(); - public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release"); + public static final boolean DEBUG = MainActivity.DEBUG; public static final Migration MIGRATION_1_2 = new Migration(DB_VER_1, DB_VER_2) { @Override diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index e04dc8681..33e0db9bc 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -54,8 +54,8 @@ import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.FailReason; import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; -import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.DownloaderImpl; +import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.local.history.HistoryRecordManager; @@ -98,7 +98,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; @SuppressWarnings({"WeakerAccess"}) public abstract class BasePlayer implements Player.EventListener, PlaybackListener, ImageLoadingListener { - public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release"); + public static final boolean DEBUG = MainActivity.DEBUG; @NonNull public static final String TAG = "BasePlayer"; @@ -880,7 +880,6 @@ public abstract class BasePlayer implements } setRecovery(); - final Throwable cause = error.getCause(); if (error instanceof BehindLiveWindowException) { reload(); } else { diff --git a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java index dc5803462..8bef0b2e0 100644 --- a/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java +++ b/app/src/main/java/org/schabi/newpipe/player/playqueue/PlayQueue.java @@ -7,7 +7,7 @@ import androidx.annotation.Nullable; import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; -import org.schabi.newpipe.BuildConfig; +import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.player.playqueue.events.AppendEvent; import org.schabi.newpipe.player.playqueue.events.ErrorEvent; import org.schabi.newpipe.player.playqueue.events.InitEvent; @@ -44,7 +44,7 @@ import io.reactivex.subjects.BehaviorSubject; */ public abstract class PlayQueue implements Serializable { private final String TAG = "PlayQueue@" + Integer.toHexString(hashCode()); - public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release"); + public static final boolean DEBUG = MainActivity.DEBUG; private ArrayList backup; private ArrayList streams; diff --git a/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java index 159625c92..5502a06eb 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/MainSettingsFragment.java @@ -4,12 +4,12 @@ import android.os.Bundle; import androidx.preference.Preference; -import org.schabi.newpipe.BuildConfig; import org.schabi.newpipe.CheckForNewAppVersionTask; +import org.schabi.newpipe.MainActivity; import org.schabi.newpipe.R; public class MainSettingsFragment extends BasePreferenceFragment { - public static final boolean DEBUG = !BuildConfig.BUILD_TYPE.equals("release"); + public static final boolean DEBUG = MainActivity.DEBUG; @Override public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) { From 99442b6e0489de74cc22f6bb404dfc33ea08ec7c Mon Sep 17 00:00:00 2001 From: vmazoyer Date: Fri, 31 Jul 2020 22:19:49 +0200 Subject: [PATCH 8/9] Remember last selected media type for downloads. --- .../newpipe/download/DownloadDialog.java | 27 ++++++++++++++++++- app/src/main/res/values/settings_keys.xml | 5 ++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java index 15ddaa865..ab44e0fc1 100644 --- a/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java +++ b/app/src/main/java/org/schabi/newpipe/download/DownloadDialog.java @@ -516,7 +516,23 @@ public class DownloadDialog extends DialogFragment videoButton.setVisibility(isVideoStreamsAvailable ? View.VISIBLE : View.GONE); subtitleButton.setVisibility(isSubtitleStreamsAvailable ? View.VISIBLE : View.GONE); - if (isVideoStreamsAvailable) { + prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); + final String defaultMedia = prefs.getString(getString(R.string.last_used_download_type), + getString(R.string.last_download_type_video_key)); + + if (isVideoStreamsAvailable + && (defaultMedia.equals(getString(R.string.last_download_type_video_key)))) { + videoButton.setChecked(true); + setupVideoSpinner(); + } else if (isAudioStreamsAvailable + && (defaultMedia.equals(getString(R.string.last_download_type_audio_key)))) { + audioButton.setChecked(true); + setupAudioSpinner(); + } else if (isSubtitleStreamsAvailable + && (defaultMedia.equals(getString(R.string.last_download_type_subtitle_key)))) { + subtitleButton.setChecked(true); + setupSubtitleSpinner(); + } else if (isVideoStreamsAvailable) { videoButton.setChecked(true); setupVideoSpinner(); } else if (isAudioStreamsAvailable) { @@ -595,6 +611,7 @@ public class DownloadDialog extends DialogFragment final StoredDirectoryHelper mainStorage; final MediaFormat format; final String mime; + final String selectedMediaType; // first, build the filename and get the output folder (if possible) // later, run a very very very large file checking logic @@ -603,6 +620,7 @@ public class DownloadDialog extends DialogFragment switch (radioStreamsGroup.getCheckedRadioButtonId()) { case R.id.audio_button: + selectedMediaType = getString(R.string.last_download_type_audio_key); mainStorage = mainStorageAudio; format = audioStreamsAdapter.getItem(selectedAudioIndex).getFormat(); switch (format) { @@ -617,12 +635,14 @@ public class DownloadDialog extends DialogFragment } break; case R.id.video_button: + selectedMediaType = getString(R.string.last_download_type_video_key); mainStorage = mainStorageVideo; format = videoStreamsAdapter.getItem(selectedVideoIndex).getFormat(); mime = format.mimeType; filename += format.suffix; break; case R.id.subtitle_button: + selectedMediaType = getString(R.string.last_download_type_subtitle_key); mainStorage = mainStorageVideo; // subtitle & video files go together format = subtitleStreamsAdapter.getItem(selectedSubtitleIndex).getFormat(); mime = format.mimeType; @@ -664,6 +684,11 @@ public class DownloadDialog extends DialogFragment // check for existing file with the same name checkSelectedDownload(mainStorage, mainStorage.findFile(filename), filename, mime); + + // remember the last media type downloaded by the user + prefs.edit() + .putString(getString(R.string.last_used_download_type), selectedMediaType) + .apply(); } private void checkSelectedDownload(final StoredDirectoryHelper mainStorage, diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 737830f60..7fb7d4f4d 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -236,6 +236,11 @@ clear_playback_states clear_search_history + @string/last_download_type_video_key + last_dl_type_video + last_dl_type_audio + last_dl_type_subtitle + downloads_storage_ask storage_use_saf From 0a22f21410ada89a8ff313ae83031cb95a7c83f7 Mon Sep 17 00:00:00 2001 From: Avently <7953703+avently@users.noreply.github.com> Date: Fri, 4 Sep 2020 05:39:55 +0300 Subject: [PATCH 9/9] Small fixes of issues with brightness, background playback, gestures --- .../fragments/detail/VideoDetailFragment.java | 43 +++++++++++++------ .../org/schabi/newpipe/player/BasePlayer.java | 5 +++ .../org/schabi/newpipe/player/MainPlayer.java | 3 ++ .../player/event/PlayerGestureListener.java | 3 +- 4 files changed, 39 insertions(+), 15 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 50ad1ae57..b92becbd1 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 @@ -337,6 +337,7 @@ public class VideoDetailFragment stopPlayerListener(); playerService = null; player = null; + saveCurrentAndRestoreDefaultBrightness(); } } @@ -425,7 +426,7 @@ public class VideoDetailFragment if (currentWorker != null) { currentWorker.dispose(); } - setupBrightness(true); + saveCurrentAndRestoreDefaultBrightness(); PreferenceManager.getDefaultSharedPreferences(getContext()) .edit() .putString(getString(R.string.stream_info_selected_tab_key), @@ -439,7 +440,7 @@ public class VideoDetailFragment activity.sendBroadcast(new Intent(ACTION_VIDEO_FRAGMENT_RESUMED)); - setupBrightness(false); + setupBrightness(); if (updateFlags != 0) { if (!isLoading.get() && currentInfo != null) { @@ -1908,6 +1909,7 @@ public class VideoDetailFragment @Override public void onFullscreenStateChanged(final boolean fullscreen) { + setupBrightness(); if (playerService.getView() == null || player.getParentActivity() == null) { return; } @@ -2022,29 +2024,41 @@ public class VideoDetailFragment && player.getPlayer().getPlaybackState() != Player.STATE_IDLE; } - private void setupBrightness(final boolean save) { + private void saveCurrentAndRestoreDefaultBrightness() { + 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 + lp.screenBrightness = -1; + activity.getWindow().setAttributes(lp); + } + + private void setupBrightness() { if (activity == null) { return; } final WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); - if (save) { - // Save current brightness level - PlayerHelper.setScreenBrightness(activity, lp.screenBrightness); - - // Restore the old brightness when fragment.onPause() called. - // It means when user leaves this fragment brightness will be set to system brightness - lp.screenBrightness = -1; + if (player == null + || !player.videoPlayerSelected() + || !player.isFullscreen() + || bottomSheetState != BottomSheetBehavior.STATE_EXPANDED) { + // Apply system brightness when the player is not in fullscreen + saveCurrentAndRestoreDefaultBrightness(); } else { // Restore already saved brightness level final float brightnessLevel = PlayerHelper.getScreenBrightness(activity); - if (brightnessLevel <= 0.0f && brightnessLevel > 1.0f) { + if (brightnessLevel == lp.screenBrightness) { return; } - lp.screenBrightness = brightnessLevel; + activity.getWindow().setAttributes(lp); } - activity.getWindow().setAttributes(lp); } private void checkLandscape() { @@ -2167,6 +2181,7 @@ public class VideoDetailFragment * @param toMain if true than the main fragment will be focused or the player otherwise */ private void moveFocusToMainFragment(final boolean toMain) { + setupBrightness(); final ViewGroup mainFragment = requireActivity().findViewById(R.id.fragment_holder); // Hamburger button steels a focus even under bottomSheet final Toolbar toolbar = requireActivity().findViewById(R.id.toolbar); @@ -2190,7 +2205,7 @@ public class VideoDetailFragment * Bottom padding should be equal to the mini player's height in this case * * @param showMore whether main fragment should be expanded or not - * */ + */ private void manageSpaceAtTheBottom(final boolean showMore) { final int peekHeight = getResources().getDimensionPixelSize(R.dimen.mini_player_height); final ViewGroup holder = requireActivity().findViewById(R.id.fragment_holder); diff --git a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java index 33e0db9bc..5e0f9ab94 100644 --- a/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/BasePlayer.java @@ -1339,6 +1339,11 @@ public abstract class BasePlayer implements return; } final StreamInfo currentInfo = currentMetadata.getMetadata(); + if (playQueue != null) { + // Save current position. It will help to restore this position once a user + // wants to play prev or next stream from the queue + playQueue.setRecovery(playQueue.getIndex(), simpleExoPlayer.getContentPosition()); + } savePlaybackState(currentInfo, simpleExoPlayer.getCurrentPosition()); } 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 f62cf27b4..8709174dc 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainPlayer.java @@ -184,6 +184,9 @@ public final class MainPlayer extends Service { @Override public void onTaskRemoved(final Intent rootIntent) { super.onTaskRemoved(rootIntent); + if (!playerImpl.videoPlayerSelected()) { + return; + } onDestroy(); // Unload from memory completely Runtime.getRuntime().halt(0); 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 5ee8485cf..4aa6070eb 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 @@ -202,7 +202,8 @@ public class PlayerGestureListener private boolean onScrollInMain(final MotionEvent initialEvent, final MotionEvent movingEvent, final float distanceX, final float distanceY) { - if (!isVolumeGestureEnabled && !isBrightnessGestureEnabled) { + if ((!isVolumeGestureEnabled && !isBrightnessGestureEnabled) + || !playerImpl.isFullscreen()) { return false; }