temporary solution to notification when app is in background
This commit is contained in:
parent
ea4e7a4790
commit
addfb7e126
2 changed files with 22 additions and 13 deletions
|
@ -1,5 +1,8 @@
|
||||||
package be.ugent.sel.studeez.activities
|
package be.ugent.sel.studeez.activities
|
||||||
|
|
||||||
|
import android.media.MediaPlayer
|
||||||
|
import android.media.RingtoneManager
|
||||||
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
|
@ -22,7 +25,13 @@ var onTimerInvisible: Job? = null
|
||||||
|
|
||||||
@AndroidEntryPoint
|
@AndroidEntryPoint
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
|
||||||
|
private var mediaPlayer: MediaPlayer? = null
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
val uri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
|
||||||
|
mediaPlayer = MediaPlayer.create(this, uri)
|
||||||
|
mediaPlayer?.isLooping = false
|
||||||
|
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContent {
|
setContent {
|
||||||
StudeezTheme {
|
StudeezTheme {
|
||||||
|
@ -37,19 +46,24 @@ class MainActivity : ComponentActivity() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
override fun onStop() {
|
override fun onStop() {
|
||||||
onTimerInvisible = lifecycleScope.launch {
|
onTimerInvisible = lifecycleScope.launch {
|
||||||
InvisibleSessionManager.updateTimer()
|
InvisibleSessionManager.updateTimer(mediaPlayer)
|
||||||
}
|
}
|
||||||
super.onStop()
|
super.onStop()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
|
mediaPlayer?.stop()
|
||||||
onTimerInvisible?.cancel()
|
onTimerInvisible?.cancel()
|
||||||
super.onStart()
|
super.onStart()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
mediaPlayer?.stop()
|
||||||
|
mediaPlayer?.release()
|
||||||
|
super.onDestroy()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|
|
@ -31,12 +31,8 @@ import androidx.hilt.navigation.compose.hiltViewModel
|
||||||
import be.ugent.sel.studeez.R
|
import be.ugent.sel.studeez.R
|
||||||
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalPomodoroTimer
|
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalPomodoroTimer
|
||||||
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer.StudyState
|
import be.ugent.sel.studeez.data.local.models.timer_functional.FunctionalTimer.StudyState
|
||||||
import be.ugent.sel.studeez.data.local.models.timer_functional.Time
|
|
||||||
import be.ugent.sel.studeez.resources
|
import be.ugent.sel.studeez.resources
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import java.time.LocalDateTime
|
|
||||||
import java.time.ZoneId
|
|
||||||
import java.util.*
|
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
@ -50,6 +46,7 @@ fun SessionScreen(
|
||||||
val mediaplayer = MediaPlayer.create(context, uri)
|
val mediaplayer = MediaPlayer.create(context, uri)
|
||||||
mediaplayer.isLooping = false
|
mediaplayer.isLooping = false
|
||||||
|
|
||||||
|
// evt mediaplayer meegeven vanaf hier als reserve oplossing
|
||||||
InvisibleSessionManager.setNewViewModel(viewModel = viewModel)
|
InvisibleSessionManager.setNewViewModel(viewModel = viewModel)
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
|
@ -88,11 +85,6 @@ fun SessionScreen(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private operator fun Time.minus(time: Time): Time {
|
|
||||||
return Time(this.time - time.time)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
private fun Timer(viewModel: SessionViewModel = hiltViewModel(), mediaplayer: MediaPlayer) {
|
private fun Timer(viewModel: SessionViewModel = hiltViewModel(), mediaplayer: MediaPlayer) {
|
||||||
var tikker by remember { mutableStateOf(false) }
|
var tikker by remember { mutableStateOf(false) }
|
||||||
|
@ -169,11 +161,14 @@ object InvisibleSessionManager {
|
||||||
this.viewModel = viewModel
|
this.viewModel = viewModel
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun updateTimer() {
|
suspend fun updateTimer(mediaPlayer: MediaPlayer?) {
|
||||||
if (isSession) {
|
if (isSession) {
|
||||||
while (true) {
|
while (true) {
|
||||||
delay(1.seconds)
|
delay(1.seconds)
|
||||||
viewModel.getTimer().tick()
|
viewModel.getTimer().tick()
|
||||||
|
if (viewModel.getTimer().hasCurrentCountdownEnded() && !viewModel.getTimer().hasEnded()) {
|
||||||
|
mediaPlayer?.start()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue