2019-03-03 09:20:15 +00:00
|
|
|
/*
|
2020-10-06 08:46:04 +00:00
|
|
|
* Copyright (c) 2020 Hemanth Savarla.
|
2019-03-03 09:20:15 +00:00
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
2020-10-06 08:46:04 +00:00
|
|
|
* This is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
2019-03-03 09:20:15 +00:00
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
2020-10-06 08:46:04 +00:00
|
|
|
*
|
2019-03-03 09:20:15 +00:00
|
|
|
*/
|
2018-12-06 08:52:57 +00:00
|
|
|
package code.name.monkey.retromusic.dialogs
|
|
|
|
|
2020-05-23 21:47:23 +00:00
|
|
|
import android.annotation.SuppressLint
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.app.AlarmManager
|
2019-05-15 18:45:42 +00:00
|
|
|
import android.app.Dialog
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.app.PendingIntent
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.CountDownTimer
|
|
|
|
import android.os.SystemClock
|
2020-05-23 21:47:23 +00:00
|
|
|
import android.view.LayoutInflater
|
2019-05-15 18:45:42 +00:00
|
|
|
import android.widget.CheckBox
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.widget.SeekBar
|
2019-05-15 18:45:42 +00:00
|
|
|
import android.widget.TextView
|
2018-12-06 08:52:57 +00:00
|
|
|
import android.widget.Toast
|
2019-05-15 18:45:42 +00:00
|
|
|
import androidx.fragment.app.DialogFragment
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.R
|
2020-05-23 21:47:23 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.addAccentColor
|
2020-07-19 11:21:15 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.colorButtons
|
2020-07-19 14:39:04 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.materialDialog
|
2019-05-15 18:45:42 +00:00
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
2018-12-06 08:52:57 +00:00
|
|
|
import code.name.monkey.retromusic.service.MusicService
|
2019-08-02 08:59:40 +00:00
|
|
|
import code.name.monkey.retromusic.service.MusicService.ACTION_PENDING_QUIT
|
|
|
|
import code.name.monkey.retromusic.service.MusicService.ACTION_QUIT
|
2020-06-06 18:57:28 +00:00
|
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
2020-05-24 18:04:50 +00:00
|
|
|
import com.afollestad.materialdialogs.DialogAction
|
2019-05-15 18:45:42 +00:00
|
|
|
import com.afollestad.materialdialogs.MaterialDialog
|
2018-12-06 08:52:57 +00:00
|
|
|
|
2019-05-15 18:45:42 +00:00
|
|
|
class SleepTimerDialog : DialogFragment() {
|
2018-12-06 08:52:57 +00:00
|
|
|
|
|
|
|
private var seekArcProgress: Int = 0
|
|
|
|
private lateinit var timerUpdater: TimerUpdater
|
2020-07-19 14:39:04 +00:00
|
|
|
private lateinit var dialog: MaterialDialog
|
2019-05-15 18:45:42 +00:00
|
|
|
private lateinit var shouldFinishLastSong: CheckBox
|
|
|
|
private lateinit var seekBar: SeekBar
|
|
|
|
private lateinit var timerDisplay: TextView
|
|
|
|
|
2020-05-23 21:47:23 +00:00
|
|
|
@SuppressLint("InflateParams")
|
2019-05-15 18:45:42 +00:00
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
timerUpdater = TimerUpdater()
|
2020-09-07 09:31:27 +00:00
|
|
|
val layout =
|
|
|
|
LayoutInflater.from(requireContext()).inflate(R.layout.dialog_sleep_timer, null)
|
2020-05-23 21:47:23 +00:00
|
|
|
shouldFinishLastSong = layout.findViewById(R.id.shouldFinishLastSong)
|
|
|
|
seekBar = layout.findViewById(R.id.seekBar)
|
|
|
|
timerDisplay = layout.findViewById(R.id.timerDisplay)
|
|
|
|
|
2020-06-06 18:57:28 +00:00
|
|
|
val finishMusic = PreferenceUtil.isSleepTimerFinishMusic
|
2020-05-23 21:47:23 +00:00
|
|
|
shouldFinishLastSong.apply {
|
|
|
|
addAccentColor()
|
|
|
|
isChecked = finishMusic
|
|
|
|
}
|
|
|
|
seekBar.apply {
|
|
|
|
addAccentColor()
|
2020-06-06 18:57:28 +00:00
|
|
|
seekArcProgress = PreferenceUtil.lastSleepTimerValue
|
2020-05-23 21:47:23 +00:00
|
|
|
updateTimeDisplayTime()
|
|
|
|
seekBar.progress = seekArcProgress
|
|
|
|
}
|
|
|
|
|
|
|
|
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
|
|
|
|
override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
|
|
|
|
if (i < 1) {
|
|
|
|
seekBar.progress = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
seekArcProgress = i
|
|
|
|
updateTimeDisplayTime()
|
|
|
|
}
|
2019-05-15 18:45:42 +00:00
|
|
|
|
2020-05-23 21:47:23 +00:00
|
|
|
override fun onStartTrackingTouch(seekBar: SeekBar) {
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onStopTrackingTouch(seekBar: SeekBar) {
|
2020-06-06 18:57:28 +00:00
|
|
|
PreferenceUtil.lastSleepTimerValue = seekArcProgress
|
2020-05-23 21:47:23 +00:00
|
|
|
}
|
|
|
|
})
|
2020-07-19 14:39:04 +00:00
|
|
|
return materialDialog(R.string.action_sleep_timer)
|
2020-05-23 21:47:23 +00:00
|
|
|
.setView(layout)
|
|
|
|
.setPositiveButton(R.string.action_set) { _, _ ->
|
2020-06-06 18:57:28 +00:00
|
|
|
PreferenceUtil.isSleepTimerFinishMusic = shouldFinishLastSong.isChecked
|
2020-02-01 17:53:26 +00:00
|
|
|
val minutes = seekArcProgress
|
|
|
|
val pi = makeTimerPendingIntent(PendingIntent.FLAG_CANCEL_CURRENT)
|
|
|
|
val nextSleepTimerElapsedTime = SystemClock.elapsedRealtime() + minutes * 60 * 1000
|
2020-06-06 18:57:28 +00:00
|
|
|
PreferenceUtil.nextSleepTimerElapsedRealTime = nextSleepTimerElapsedTime.toInt()
|
2020-03-01 12:05:43 +00:00
|
|
|
val am = requireContext().getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
2020-02-01 17:53:26 +00:00
|
|
|
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextSleepTimerElapsedTime, pi)
|
|
|
|
|
|
|
|
Toast.makeText(
|
2020-03-01 12:05:43 +00:00
|
|
|
requireContext(),
|
|
|
|
requireContext().resources.getString(R.string.sleep_timer_set, minutes),
|
2020-02-01 17:53:26 +00:00
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
|
|
|
}
|
2020-05-23 21:47:23 +00:00
|
|
|
.setNegativeButton(android.R.string.cancel) { _, _ ->
|
2020-02-01 17:53:26 +00:00
|
|
|
val previous = makeTimerPendingIntent(PendingIntent.FLAG_NO_CREATE)
|
|
|
|
if (previous != null) {
|
2020-03-01 12:05:43 +00:00
|
|
|
val am =
|
|
|
|
requireContext().getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
2020-02-01 17:53:26 +00:00
|
|
|
am.cancel(previous)
|
|
|
|
previous.cancel()
|
|
|
|
Toast.makeText(
|
2020-03-01 12:05:43 +00:00
|
|
|
requireContext(),
|
|
|
|
requireContext().resources.getString(R.string.sleep_timer_canceled),
|
2020-02-01 17:53:26 +00:00
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
2020-05-23 21:47:23 +00:00
|
|
|
val musicService = MusicPlayerRemote.musicService
|
|
|
|
if (musicService != null && musicService.pendingQuit) {
|
|
|
|
musicService.pendingQuit = false
|
|
|
|
Toast.makeText(
|
|
|
|
requireContext(),
|
|
|
|
requireContext().resources.getString(R.string.sleep_timer_canceled),
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
2019-05-15 18:45:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-01 17:53:26 +00:00
|
|
|
}
|
2020-05-23 21:47:23 +00:00
|
|
|
.create()
|
2020-07-19 11:21:15 +00:00
|
|
|
.colorButtons()
|
2019-05-15 18:45:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateTimeDisplayTime() {
|
|
|
|
timerDisplay.text = "$seekArcProgress min"
|
|
|
|
}
|
|
|
|
|
2019-06-16 18:10:28 +00:00
|
|
|
private fun makeTimerPendingIntent(flag: Int): PendingIntent? {
|
2020-03-01 12:05:43 +00:00
|
|
|
return PendingIntent.getService(requireActivity(), 0, makeTimerIntent(), flag)
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-15 18:45:42 +00:00
|
|
|
private fun makeTimerIntent(): Intent {
|
2020-03-01 12:05:43 +00:00
|
|
|
val intent = Intent(requireActivity(), MusicService::class.java)
|
2019-05-15 18:45:42 +00:00
|
|
|
return if (shouldFinishLastSong.isChecked) {
|
2019-08-02 08:59:40 +00:00
|
|
|
intent.setAction(ACTION_PENDING_QUIT)
|
|
|
|
} else intent.setAction(ACTION_QUIT)
|
2019-05-15 18:45:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateCancelButton() {
|
|
|
|
val musicService = MusicPlayerRemote.musicService
|
|
|
|
if (musicService != null && musicService.pendingQuit) {
|
2020-07-19 14:39:04 +00:00
|
|
|
dialog.getActionButton(DialogAction.NEUTRAL).text =
|
|
|
|
dialog.context.getString(R.string.cancel_current_timer)
|
2019-05-15 18:45:42 +00:00
|
|
|
} else {
|
2020-07-19 14:39:04 +00:00
|
|
|
dialog.getActionButton(DialogAction.NEUTRAL).text = null
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:19:15 +00:00
|
|
|
private inner class TimerUpdater() :
|
2020-05-23 09:27:55 +00:00
|
|
|
CountDownTimer(
|
2020-06-06 18:57:28 +00:00
|
|
|
PreferenceUtil.nextSleepTimerElapsedRealTime - SystemClock.elapsedRealtime(),
|
2020-05-23 09:27:55 +00:00
|
|
|
1000
|
|
|
|
) {
|
2019-05-15 18:45:42 +00:00
|
|
|
|
|
|
|
override fun onTick(millisUntilFinished: Long) {
|
2020-05-23 22:26:45 +00:00
|
|
|
println("onTick: $millisUntilFinished")
|
|
|
|
seekBar.progress = millisUntilFinished.toInt()
|
2019-05-15 18:45:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFinish() {
|
|
|
|
updateCancelButton()
|
|
|
|
}
|
2018-12-06 08:52:57 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
}
|