-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Sleep timer #2892
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
Sleep timer #2892
Changes from all commits
fa20e99
a00be6e
4a7ee54
145762c
f25c871
7826505
d0e183a
c030231
47eb883
372106d
94dcdc4
532608b
94a3b3b
5b90f16
3775557
cab4414
d137b6d
2cb1f73
938e232
5d73c89
69db58c
f412755
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,8 @@ | |
| import android.graphics.Bitmap; | ||
| import android.graphics.BitmapFactory; | ||
| import android.media.AudioManager; | ||
| import android.os.Handler; | ||
| import android.os.Looper; | ||
| import android.preference.PreferenceManager; | ||
| import android.util.Log; | ||
| import android.view.View; | ||
|
|
@@ -78,6 +80,9 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.net.UnknownHostException; | ||
| import java.util.Date; | ||
| import java.util.Timer; | ||
| import java.util.TimerTask; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import io.reactivex.Observable; | ||
|
|
@@ -193,6 +198,17 @@ public abstract class BasePlayer implements | |
|
|
||
| //////////////////////////////////////////////////////////////////////////*/ | ||
|
|
||
|
|
||
| /*////////////////////////////////////////////////////////////////////////// | ||
| // Timer | ||
| //////////////////////////////////////////////////////////////////////////*/ | ||
|
|
||
| private Timer timer = new Timer(); | ||
| private int timerHour = 0; | ||
| private int timerMinute = 0; | ||
|
Comment on lines
+207
to
+208
Member
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. Remove these |
||
|
|
||
| //////////////////////////////////////////////////////////////////////////*/ | ||
|
|
||
| public BasePlayer(@NonNull final Context context) { | ||
| this.context = context; | ||
|
|
||
|
|
@@ -1285,4 +1301,44 @@ private boolean isPlaybackResumeEnabled() { | |
| return prefs.getBoolean(context.getString(R.string.enable_watch_history_key), true) | ||
| && prefs.getBoolean(context.getString(R.string.enable_playback_resume_key), true); | ||
| } | ||
|
|
||
| public void setTimer(int hourOfDay, int minute, Context ctx) { | ||
| long time = ((hourOfDay * 60) + minute) * 60 * 1000; | ||
|
Contributor
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. I think this is a logical error here. |
||
| if (time == 0) { | ||
| Toast.makeText(ctx, ctx.getString(R.string.timer_select_time_message), Toast.LENGTH_SHORT).show(); | ||
| return; | ||
| } | ||
| timer.cancel(); | ||
| timer.purge(); | ||
| timerHour = hourOfDay; | ||
| timerMinute = minute; | ||
| timer = new Timer(); | ||
| timer.schedule(new TimerTask() { | ||
| @Override | ||
| public void run() { | ||
| new Handler(Looper.getMainLooper()).post(() -> { | ||
| if (DEBUG) Log.d(TAG, "Timer finished: Stopping the player"); | ||
| onPause(); | ||
| }); | ||
|
|
||
| } | ||
| }, time); | ||
| Date d = new Date(); | ||
| d.setTime(System.currentTimeMillis() + time); | ||
| Toast.makeText(ctx.getApplicationContext(), String.format(ctx.getString(R.string.player_stop_message), d.toString()), Toast.LENGTH_LONG).show(); | ||
|
Comment on lines
+1306
to
+1328
Member
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. Format suggestion: long time = ((hourOfDay * 60) + minute) * 60 * 1000;
if (time == 0) {
Toast.makeText(ctx, ctx.getString(R.string.timer_select_time_message), Toast.LENGTH_SHORT).show();
return;
}
timer.cancel();
timer.purge();
timer = new Timer();
timer.schedule(() -> {
if (DEBUG) Log.d(TAG, "Timer finished: stopping the player");
onPause();
}, time);
Date d = new Date();
d.setTime(System.currentTimeMillis() + time);
Toast.makeText(ctx.getApplicationContext(), String.format(ctx.getString(R.string.player_stop_message), d.toString()), Toast.LENGTH_LONG).show();There is an issue: the user should be able to cancel the timer at any time, not just during the
Contributor
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. Correct, we need a custom TimePickerDialog which allows us to add
|
||
| } | ||
|
|
||
| public void cancelTimer() { | ||
| timer.cancel(); | ||
| timer.purge(); | ||
| } | ||
|
|
||
| public int getHourOfDay() { | ||
| return this.timerHour; | ||
| } | ||
|
|
||
| public int getMinutes() { | ||
| return this.timerMinute; | ||
| } | ||
javedh-dev marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+1335
to
+1342
Member
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. Delete these, there is no need to store these values in the Player class. |
||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,12 @@ | ||
| package org.schabi.newpipe.player; | ||
|
|
||
| import android.app.TimePickerDialog; | ||
| import android.content.ComponentName; | ||
| import android.content.Intent; | ||
| import android.content.ServiceConnection; | ||
| import android.os.Bundle; | ||
| import android.os.IBinder; | ||
| import android.provider.Settings; | ||
| import androidx.appcompat.app.AppCompatActivity; | ||
| import androidx.recyclerview.widget.LinearLayoutManager; | ||
| import androidx.recyclerview.widget.RecyclerView; | ||
| import androidx.appcompat.widget.Toolbar; | ||
| import androidx.recyclerview.widget.ItemTouchHelper; | ||
| import android.util.Log; | ||
| import android.view.Menu; | ||
| import android.view.MenuItem; | ||
|
|
@@ -23,6 +19,12 @@ | |
| import android.widget.SeekBar; | ||
| import android.widget.TextView; | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity; | ||
| import androidx.appcompat.widget.Toolbar; | ||
| import androidx.recyclerview.widget.ItemTouchHelper; | ||
| import androidx.recyclerview.widget.LinearLayoutManager; | ||
| import androidx.recyclerview.widget.RecyclerView; | ||
|
|
||
| import com.google.android.exoplayer2.PlaybackParameters; | ||
| import com.google.android.exoplayer2.Player; | ||
|
|
||
|
|
@@ -92,6 +94,7 @@ public abstract class ServicePlayerActivity extends AppCompatActivity | |
| private TextView playbackSpeedButton; | ||
| private TextView playbackPitchButton; | ||
|
|
||
|
|
||
| //////////////////////////////////////////////////////////////////////////// | ||
| // Abstracts | ||
| //////////////////////////////////////////////////////////////////////////// | ||
|
|
@@ -164,10 +167,19 @@ public boolean onOptionsItemSelected(MenuItem item) { | |
| return true; | ||
| case R.id.action_switch_main: | ||
| return switchTo(MainVideoPlayer.class); | ||
| case R.id.action_set_timer: | ||
| updateTimer(this); | ||
| return true; | ||
| } | ||
| return onPlayerOptionSelected(item) || super.onOptionsItemSelected(item); | ||
| } | ||
|
|
||
| private void updateTimer(ServicePlayerActivity servicePlayerActivity) { | ||
| TimePickerDialog dialog = new TimePickerDialog(servicePlayerActivity, (view, hourOfDay, minute) -> | ||
| player.setTimer(hourOfDay, minute, getApplicationContext()), player.getHourOfDay(), player.getMinutes(), true); | ||
|
Contributor
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. In case we keep the TimePicker, you should initialize the clock with the current time if no timer is running and check if the user has enabled 24 hour format: |
||
| dialog.show(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void onDestroy() { | ||
| super.onDestroy(); | ||
|
|
||
| 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="?attr/colorControlNormal" | ||
| android:viewportWidth="24" | ||
| android:viewportHeight="24"> | ||
| <path | ||
| android:fillColor="@android:color/white" | ||
| android:pathData="M15,1L9,1v2h6L15,1zM11,14h2L13,8h-2v6zM19.03,7.39l1.42,-1.42c-0.43,-0.51 -0.9,-0.99 -1.41,-1.41l-1.42,1.42C16.07,4.74 14.12,4 12,4c-4.97,0 -9,4.03 -9,9s4.02,9 9,9 9,-4.03 9,-9c0,-2.12 -0.74,-4.07 -1.97,-5.61zM12,20c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z" /> | ||
| </vector> |
| 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="?attr/colorControlNormal" | ||
| android:viewportWidth="24" | ||
| android:viewportHeight="24"> | ||
| <path | ||
| android:fillColor="@android:color/white" | ||
| android:pathData="M15,1L9,1v2h6L15,1zM11,14h2L13,8h-2v6zM19.03,7.39l1.42,-1.42c-0.43,-0.51 -0.9,-0.99 -1.41,-1.41l-1.42,1.42C16.07,4.74 14.12,4 12,4c-4.97,0 -9,4.03 -9,9s4.02,9 9,9 9,-4.03 9,-9c0,-2.12 -0.74,-4.07 -1.97,-5.61zM12,20c-3.87,0 -7,-3.13 -7,-7s3.13,-7 7,-7 7,3.13 7,7 -3.13,7 -7,7z" /> | ||
| </vector> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="24.0" | ||
| android:viewportHeight="24.0"> | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:viewportWidth="24.0" | ||
| android:viewportHeight="24.0"> | ||
| <path | ||
| android:fillColor="#FF000000" | ||
| android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/> | ||
| android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z" /> | ||
| </vector> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| <vector android:height="24dp" android:tint="#FFFFFF" | ||
| android:viewportHeight="24.0" android:viewportWidth="24.0" | ||
| android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
| <path android:fillColor="#FF000000" android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z"/> | ||
| <vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:width="24dp" | ||
| android:height="24dp" | ||
| android:tint="#FFFFFF" | ||
| android:viewportWidth="24.0" | ||
| android:viewportHeight="24.0"> | ||
| <path | ||
| android:fillColor="#FF000000" | ||
| android:pathData="M9,16.2L4.8,12l-1.4,1.4L9,19 21,7l-1.4,-1.4L9,16.2z" /> | ||
| </vector> |
Uh oh!
There was an error while loading. Please reload this page.