Applied changes from Flexify mr
Fixed Alarm Manager Request Permissions Fixed TimerHandler Delay
This commit is contained in:
parent
e3ca490ef2
commit
41b9232a1b
|
@ -4,12 +4,14 @@ import android.app.AlarmManager
|
|||
import android.app.AlarmManager.ELAPSED_REALTIME_WAKEUP
|
||||
import android.app.PendingIntent
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.SystemClock
|
||||
import android.provider.Settings.*
|
||||
import android.provider.Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM
|
||||
import android.widget.Toast
|
||||
import androidx.annotation.RequiresApi
|
||||
|
||||
|
@ -38,6 +40,8 @@ class Timer(private var msTimerDuration: Long) {
|
|||
|
||||
fun expire() {
|
||||
state = State.Expired
|
||||
msTimerDuration = 0
|
||||
totalTimerDuration = 0
|
||||
}
|
||||
|
||||
fun getRemainingSeconds(): Int {
|
||||
|
@ -52,10 +56,6 @@ class Timer(private var msTimerDuration: Long) {
|
|||
if (wasRunning) start(context)
|
||||
}
|
||||
|
||||
fun isRunning(): Boolean {
|
||||
return state == State.Running
|
||||
}
|
||||
|
||||
fun isExpired(): Boolean {
|
||||
return state == State.Expired
|
||||
}
|
||||
|
@ -65,19 +65,43 @@ class Timer(private var msTimerDuration: Long) {
|
|||
}
|
||||
|
||||
fun getRemainingMillis(): Long {
|
||||
return if (state==State.Running) endTime - SystemClock.elapsedRealtime()
|
||||
return if (state == State.Running) endTime - SystemClock.elapsedRealtime()
|
||||
else
|
||||
msTimerDuration
|
||||
}
|
||||
|
||||
private fun isRunning(): Boolean {
|
||||
return state == State.Running
|
||||
}
|
||||
|
||||
private fun requestPermission(context: Context): Boolean {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return true
|
||||
val intent = Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM)
|
||||
intent.data = Uri.parse("package:" + context.packageName)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
return try {
|
||||
val receiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context2: Context?, intent: Intent?) {
|
||||
context.unregisterReceiver(this)
|
||||
registerPendingIntent(context)
|
||||
}
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
context.registerReceiver(
|
||||
receiver,
|
||||
IntentFilter(AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED),
|
||||
Context.RECEIVER_NOT_EXPORTED
|
||||
)
|
||||
} else {
|
||||
context.registerReceiver(
|
||||
receiver,
|
||||
IntentFilter(AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED)
|
||||
)
|
||||
}
|
||||
|
||||
context.startActivity(intent)
|
||||
true
|
||||
false
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
|
|
|
@ -39,6 +39,8 @@ class TimerService : Service() {
|
|||
Log.d("TimerService", "Received stop broadcast intent")
|
||||
timer.stop(applicationContext)
|
||||
timer.expire()
|
||||
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
|
@ -95,12 +97,15 @@ class TimerService : Service() {
|
|||
|
||||
timerRunnable = object : Runnable {
|
||||
override fun run() {
|
||||
val startTime = SystemClock.elapsedRealtime()
|
||||
if (timer.isExpired()) return
|
||||
updateNotification(timer.getRemainingSeconds())
|
||||
timerHandler.postDelayed(this, timer.getRemainingMillis() % 1000)
|
||||
|
||||
val delay = timer.getRemainingMillis() % 1000
|
||||
timerHandler.postDelayed(this, if (SystemClock.elapsedRealtime() - startTime + delay > 980) 20 else delay)
|
||||
}
|
||||
}
|
||||
timerHandler.postDelayed(timerRunnable!!, 1000)
|
||||
timerHandler.postDelayed(timerRunnable!!, 20)
|
||||
}
|
||||
|
||||
private fun onTimerExpired() {
|
||||
|
@ -121,7 +126,7 @@ class TimerService : Service() {
|
|||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
timerHandler.removeCallbacks(timerRunnable!!)
|
||||
timerRunnable?.let { timerHandler.removeCallbacks(it) }
|
||||
applicationContext.unregisterReceiver(stopReceiver)
|
||||
applicationContext.unregisterReceiver(addReceiver)
|
||||
mediaPlayer?.stop()
|
||||
|
|
Loading…
Reference in New Issue