-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Sleep Timer (new attempt) #12557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zgtm
wants to merge
2
commits into
TeamNewPipe:refactor
Choose a base branch
from
zgtm:sleeptimer
base: refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sleep Timer (new attempt) #12557
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -209,4 +209,11 @@ public void onPlayQueueEdited() { | |
| */ | ||
| public void onVideoSizeChanged(@NonNull final VideoSize videoSize) { | ||
| } | ||
|
|
||
| /** | ||
| * @param remainingTime the remaining sleep timer time, set to 0 to pause the player and | ||
| * disable the sleep timer | ||
| */ | ||
| public void onSleepTimerUpdate(final long remainingTime) { | ||
| } | ||
|
Comment on lines
+213
to
+218
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ensure that the time unit is mentioned. |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,12 +128,14 @@ private enum PlayButtonAction { | |
| private static final int POPUP_MENU_ID_AUDIO_TRACK = 70; | ||
| private static final int POPUP_MENU_ID_PLAYBACK_SPEED = 79; | ||
| private static final int POPUP_MENU_ID_CAPTION = 89; | ||
| private static final int POPUP_MENU_ID_SLEEP_TIMER = 90; // TODO is 90 still available? | ||
|
|
||
| protected boolean isSomePopupMenuVisible = false; | ||
| private PopupMenu qualityPopupMenu; | ||
| private PopupMenu audioTrackPopupMenu; | ||
| protected PopupMenu playbackSpeedPopupMenu; | ||
| private PopupMenu captionPopupMenu; | ||
| private PopupMenu sleepTimerPopupMenu; | ||
|
|
||
|
|
||
| /*////////////////////////////////////////////////////////////////////////// | ||
|
|
@@ -186,6 +188,8 @@ private void initViews() { | |
| audioTrackPopupMenu = new PopupMenu(themeWrapper, binding.audioTrackTextView); | ||
| playbackSpeedPopupMenu = new PopupMenu(context, binding.playbackSpeed); | ||
| captionPopupMenu = new PopupMenu(themeWrapper, binding.captionTextView); | ||
| sleepTimerPopupMenu = new PopupMenu(themeWrapper, binding.sleepTimer); | ||
| buildSleepTimerMenu(); | ||
|
|
||
| binding.progressBarLoadingPanel.getIndeterminateDrawable() | ||
| .setColorFilter(new PorterDuffColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY)); | ||
|
|
@@ -204,6 +208,9 @@ protected void initListeners() { | |
| binding.audioTrackTextView.setOnClickListener( | ||
| makeOnClickListener(this::onAudioTracksClicked)); | ||
| binding.playbackSpeed.setOnClickListener(makeOnClickListener(this::onPlaybackSpeedClicked)); | ||
| binding.sleepTimer.setOnClickListener(makeOnClickListener(this::onSleepTimerClicked)); | ||
| binding.sleepTimerCancel.setOnClickListener( | ||
| makeOnClickListener(this::onSleepTimerCancelClicked)); | ||
|
|
||
| binding.playbackSeekBar.setOnSeekBarChangeListener(this); | ||
| binding.captionTextView.setOnClickListener(makeOnClickListener(this::onCaptionClicked)); | ||
|
|
@@ -1239,6 +1246,49 @@ private void buildCaptionMenu(@NonNull final List<String> availableLanguages) { | |
| } | ||
| } | ||
|
|
||
| private void buildSleepTimerMenu() { | ||
| if (sleepTimerPopupMenu == null) { | ||
| return; | ||
| } | ||
| qualityPopupMenu.getMenu().removeGroup(POPUP_MENU_ID_SLEEP_TIMER); | ||
|
|
||
| final Resources res = context.getResources(); | ||
| sleepTimerPopupMenu.getMenu().add(POPUP_MENU_ID_SLEEP_TIMER, 0, 0, | ||
| res.getString(R.string.sleep_timer_popup_title)); | ||
|
|
||
| final String[] descriptions = context.getResources().getStringArray( | ||
| R.array.sleep_timer_description); | ||
| final int[] values = context.getResources().getIntArray( | ||
| R.array.sleep_timer_value); | ||
| for (int i = 0; i < descriptions.length && i < values.length; i++) { | ||
| String description = ""; | ||
| try { | ||
| final int hours = values[i] / 60; | ||
| final int minutes = values[i] % 60; | ||
| if (hours != 0) { | ||
| description += String.format(res.getQuantityString(R.plurals.hours, hours), | ||
| hours); | ||
| } | ||
|
|
||
| if (minutes != 0) { | ||
| if (hours != 0) { | ||
| description += " "; | ||
| } | ||
| description += String.format(res.getQuantityString(R.plurals.minutes, minutes), | ||
| minutes); | ||
| } | ||
| } catch (final Resources.NotFoundException ignored) { | ||
| // if this happens, the translation is missing, | ||
| // and the english string will be displayed instead | ||
| description = descriptions[i]; | ||
| } | ||
| sleepTimerPopupMenu.getMenu().add(POPUP_MENU_ID_SLEEP_TIMER, i + 1, i + 1, description); | ||
| } | ||
|
|
||
| sleepTimerPopupMenu.setOnMenuItemClickListener(this); | ||
| sleepTimerPopupMenu.setOnDismissListener(this); | ||
| } | ||
|
|
||
| protected abstract void onPlaybackSpeedClicked(); | ||
|
|
||
| private void onQualityClicked() { | ||
|
|
@@ -1255,6 +1305,19 @@ private void onAudioTracksClicked() { | |
| isSomePopupMenuVisible = true; | ||
| } | ||
|
|
||
| private void onSleepTimerClicked() { | ||
| sleepTimerPopupMenu.show(); | ||
| isSomePopupMenuVisible = true; | ||
| } | ||
|
|
||
| private void onSleepTimerCancelClicked() { | ||
| player.cancelSleepTimer(); | ||
|
|
||
| binding.sleepTimerCancel.setVisibility(View.INVISIBLE); | ||
| binding.sleepTimerTextView.setVisibility(View.INVISIBLE); | ||
| binding.sleepTimerTextView.setText("0:00"); | ||
| } | ||
|
|
||
| /** | ||
| * Called when an item of the quality selector or the playback speed selector is selected. | ||
| */ | ||
|
|
@@ -1278,8 +1341,12 @@ public boolean onMenuItemClick(@NonNull final MenuItem menuItem) { | |
|
|
||
| player.setPlaybackSpeed(speed); | ||
| binding.playbackSpeed.setText(formatSpeed(speed)); | ||
| } else if (menuItem.getGroupId() == POPUP_MENU_ID_SLEEP_TIMER) { | ||
| onSleepTimerItemClick(menuItem); | ||
| return true; | ||
| } | ||
|
|
||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
@@ -1324,6 +1391,24 @@ private void onAudioTrackItemClick(@NonNull final MenuItem menuItem) { | |
| binding.audioTrackTextView.setText(menuItem.getTitle()); | ||
| } | ||
|
|
||
| private void onSleepTimerItemClick(@NonNull final MenuItem menuItem) { | ||
| final int menuItemIndex = menuItem.getItemId(); | ||
| if (menuItemIndex == 0) { | ||
| return; | ||
| } | ||
|
|
||
| final int index = menuItemIndex - 1; | ||
| final int sleepTime = context.getResources().getIntArray(R.array.sleep_timer_value)[index]; | ||
| final long remainingTimeHours = sleepTime / 60; | ||
| final long remainingTimeMinutes = sleepTime % 60; | ||
| final String text = String.format("%d:%02d", remainingTimeHours, remainingTimeMinutes); | ||
|
|
||
| player.setSleepTimer(sleepTime); | ||
| binding.sleepTimerCancel.setVisibility(View.VISIBLE); | ||
| binding.sleepTimerTextView.setVisibility(View.VISIBLE); | ||
| binding.sleepTimerTextView.setText(text); | ||
| } | ||
|
|
||
| /** | ||
| * Called when some popup menu is dismissed. | ||
| */ | ||
|
|
@@ -1561,6 +1646,32 @@ public void onVideoSizeChanged(@NonNull final VideoSize videoSize) { | |
| } | ||
| //endregion | ||
|
|
||
| @Override | ||
| public void onSleepTimerUpdate(final long remainingTime) { | ||
| if (remainingTime == 0) { | ||
| binding.sleepTimerTextView.post(new Runnable() { | ||
| public void run() { | ||
| player.pause(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this method is just used for UI updates. The pause() call should happen inside the player. |
||
| binding.sleepTimerCancel.setVisibility(View.INVISIBLE); | ||
| binding.sleepTimerTextView.setVisibility(View.INVISIBLE); | ||
| binding.sleepTimerTextView.setText("0:00"); | ||
| } | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| final long remainingTimeHours = remainingTime / 60; | ||
| final long remainingTimeMinutes = remainingTime % 60; | ||
| final String text = String.format("%d:%02d", remainingTimeHours, remainingTimeMinutes); | ||
|
|
||
| // Since this callback can/will be called from a different thread, we need to run set | ||
| // the code in the UI thread | ||
| binding.sleepTimerTextView.post(new Runnable() { | ||
| public void run() { | ||
| binding.sleepTimerTextView.setText(text); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /*////////////////////////////////////////////////////////////////////////// | ||
| // SurfaceHolderCallback helpers | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:tint="@color/defaultIconTint" | ||
| android:viewportWidth="24" | ||
| android:viewportHeight="24"> | ||
| <path | ||
| android:fillColor="#FF000000" | ||
| android:pathData="M12.5 8H11v6l4.75 2.85 0.75-1.23-4-2.37zm4.837-6.19 4.607 3.845-1.28 1.535-4.61-3.843zm-10.674 0 1.282 1.536L3.337 7.19l-1.28-1.536zM12 4a9 9 0 1 0 0.001 18.001A9 9 0 0 0 12 4zm0 16c-3.86 0-7-3.14-7-7s3.14-7 7-7 7 3.14 7 7-3.14 7-7 7z" /> | ||
| </vector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:tint="@color/defaultIconTint" | ||
| android:viewportWidth="24" | ||
| android:viewportHeight="24"> | ||
| <path | ||
| android:fillColor="#FF000000" | ||
| android:pathData="M10.04 6.29C10.66 6.11 11.32 6 12 6c3.86 0 7 3.14 7 7 0 0.68-0.11 1.34-0.29 1.96l1.56 1.56c0.47-1.08 0.73-2.27 0.73-3.52A9 9 0 0 0 8.47 4.72l1.57 1.57zm7.297-4.48 4.607 3.845-1.28 1.535-4.61-3.843zM3.02 2.1 1.61 3.51l1.37 1.37-0.92 0.77 1.28 1.54 1.06-0.88 0.8 0.8A8.964 8.964 0 0 0 3 13a9 9 0 0 0 9 9c2.25 0 4.31-0.83 5.89-2.2l2.1 2.1 1.41-1.41L3.02 2.1zM12 20c-3.86 0-7-3.14-7-7 0-1.7 0.61-3.26 1.62-4.47l9.85 9.85A6.956 6.956 0 0 1 12 20zM7.48 3.73l0.46-0.38-1.28-1.54-0.6 0.5z" /> | ||
| </vector> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
remainingMinutes + 1used becauseuntilfloors the value? If so, add an explaining comment.