Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package info.fandroid.recorder.ui
- import android.annotation.SuppressLint
- import android.app.AlarmManager
- import android.app.Application
- import android.app.PendingIntent
- import android.content.Context
- import android.content.Intent
- import android.content.res.Resources
- import android.os.CountDownTimer
- import android.os.SystemClock
- import android.util.Log
- import androidx.core.os.ConfigurationCompat
- import androidx.lifecycle.AndroidViewModel
- import androidx.lifecycle.LiveData
- import androidx.lifecycle.MutableLiveData
- import androidx.lifecycle.viewModelScope
- import com.example.android.eggtimernotifications.receiver.AlarmReceiver
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.launch
- import kotlinx.coroutines.withContext
- import java.text.SimpleDateFormat
- import java.util.*
- import java.util.concurrent.TimeUnit
- class RecordViewModel(private val app: Application) : AndroidViewModel(app) {
- private val REQUEST_CODE = 0
- private val TRIGGER_TIME = "TRIGGER_AT"
- private val minute: Long = 60_000L
- private val second: Long = 1_000L
- private val notifyPendingIntent: PendingIntent
- private val alarmManager = app.getSystemService(Context.ALARM_SERVICE) as AlarmManager
- private var prefs =
- app.getSharedPreferences("com.example.android.eggtimernotifications", Context.MODE_PRIVATE)
- private val notifyIntent = Intent(app, AlarmReceiver::class.java)
- private val _elapsedTime = MutableLiveData<String>()
- val elapsedTime: LiveData<String>
- get() = _elapsedTime
- private var _alarmOn = MutableLiveData<Boolean>()
- val isAlarmOn: LiveData<Boolean>
- get() = _alarmOn
- private lateinit var timer: CountDownTimer
- init {
- _alarmOn.value = PendingIntent.getBroadcast(
- getApplication(),
- REQUEST_CODE,
- notifyIntent,
- PendingIntent.FLAG_NO_CREATE
- ) != null
- notifyPendingIntent = PendingIntent.getBroadcast(
- getApplication(),
- REQUEST_CODE,
- notifyIntent,
- PendingIntent.FLAG_UPDATE_CURRENT
- )
- //If alarm is not null, resume the timer back for this alarm
- if (_alarmOn.value!!) {
- createTimer()
- Log.d("RecordViewModel", "If alarm is not null, resume the timer back for this alarm")
- }
- }
- fun timeFormatter(time: Long): String {
- return String.format("%02d:%02d:%02d",
- TimeUnit.MILLISECONDS.toHours(time)%60,
- TimeUnit.MILLISECONDS.toMinutes(time)%60,
- TimeUnit.MILLISECONDS.toSeconds(time)%60)
- }
- fun stopTimer() {
- timer.cancel()
- resetTimer()
- }
- /**
- * Creates a new alarm, notification and timer
- */
- fun startTimer() {
- _alarmOn.value?.let {
- if (!it) {
- _alarmOn.value = true
- val triggerTime = SystemClock.elapsedRealtime()
- // // TODO: Step 1.15 call cancel notification
- // val notificationManager =
- // ContextCompat.getSystemService(
- // app,
- // NotificationManager::class.java
- // ) as NotificationManager
- // notificationManager.cancelNotifications()
- //
- // AlarmManagerCompat.setExactAndAllowWhileIdle(
- // alarmManager,
- // AlarmManager.ELAPSED_REALTIME_WAKEUP,
- // triggerTime,
- // notifyPendingIntent
- // )
- viewModelScope.launch {
- saveTime(triggerTime)
- }
- }
- }
- createTimer()
- Log.d("RecordViewModel", " createTimer()")
- }
- /**
- * Creates a new timer
- */
- private fun createTimer() {
- viewModelScope.launch {
- val triggerTime = loadTime()
- timer = object : CountDownTimer(triggerTime, second) {
- override fun onTick(millisUntilFinished: Long) {
- _elapsedTime.value = timeFormatter(SystemClock.elapsedRealtime() - triggerTime)
- }
- override fun onFinish() {
- resetTimer()
- }
- }
- timer.start()
- }
- }
- /**
- * Cancels the alarm, notification and resets the timer
- */
- private fun cancelNotification() {
- resetTimer()
- alarmManager.cancel(notifyPendingIntent)
- }
- /**
- * Resets the timer on screen and sets alarm value false
- */
- fun resetTimer() {
- _elapsedTime.value = timeFormatter(0)
- _alarmOn.value = false
- viewModelScope.launch { saveTime(0) }
- }
- private suspend fun saveTime(triggerTime: Long) =
- withContext(Dispatchers.IO) {
- prefs.edit().putLong(TRIGGER_TIME, triggerTime).apply()
- }
- private suspend fun loadTime(): Long =
- withContext(Dispatchers.IO) {
- prefs.getLong(TRIGGER_TIME, 0)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement