Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1.  
  2. package info.fandroid.recorder.ui
  3.  
  4. import android.annotation.SuppressLint
  5. import android.app.AlarmManager
  6. import android.app.Application
  7. import android.app.PendingIntent
  8. import android.content.Context
  9. import android.content.Intent
  10. import android.content.res.Resources
  11. import android.os.CountDownTimer
  12. import android.os.SystemClock
  13. import android.util.Log
  14. import androidx.core.os.ConfigurationCompat
  15. import androidx.lifecycle.AndroidViewModel
  16. import androidx.lifecycle.LiveData
  17. import androidx.lifecycle.MutableLiveData
  18. import androidx.lifecycle.viewModelScope
  19. import com.example.android.eggtimernotifications.receiver.AlarmReceiver
  20. import kotlinx.coroutines.Dispatchers
  21. import kotlinx.coroutines.launch
  22. import kotlinx.coroutines.withContext
  23. import java.text.SimpleDateFormat
  24. import java.util.*
  25. import java.util.concurrent.TimeUnit
  26.  
  27.  
  28. class RecordViewModel(private val app: Application) : AndroidViewModel(app) {
  29.  
  30. private val REQUEST_CODE = 0
  31. private val TRIGGER_TIME = "TRIGGER_AT"
  32.  
  33. private val minute: Long = 60_000L
  34. private val second: Long = 1_000L
  35.  
  36. private val notifyPendingIntent: PendingIntent
  37.  
  38. private val alarmManager = app.getSystemService(Context.ALARM_SERVICE) as AlarmManager
  39. private var prefs =
  40. app.getSharedPreferences("com.example.android.eggtimernotifications", Context.MODE_PRIVATE)
  41. private val notifyIntent = Intent(app, AlarmReceiver::class.java)
  42.  
  43.  
  44. private val _elapsedTime = MutableLiveData<String>()
  45. val elapsedTime: LiveData<String>
  46. get() = _elapsedTime
  47.  
  48.  
  49. private var _alarmOn = MutableLiveData<Boolean>()
  50. val isAlarmOn: LiveData<Boolean>
  51. get() = _alarmOn
  52.  
  53.  
  54. private lateinit var timer: CountDownTimer
  55.  
  56.  
  57.  
  58. init {
  59. _alarmOn.value = PendingIntent.getBroadcast(
  60. getApplication(),
  61. REQUEST_CODE,
  62. notifyIntent,
  63. PendingIntent.FLAG_NO_CREATE
  64. ) != null
  65.  
  66. notifyPendingIntent = PendingIntent.getBroadcast(
  67. getApplication(),
  68. REQUEST_CODE,
  69. notifyIntent,
  70. PendingIntent.FLAG_UPDATE_CURRENT
  71. )
  72.  
  73. //If alarm is not null, resume the timer back for this alarm
  74. if (_alarmOn.value!!) {
  75. createTimer()
  76. Log.d("RecordViewModel", "If alarm is not null, resume the timer back for this alarm")
  77.  
  78. }
  79.  
  80. }
  81.  
  82.  
  83. fun timeFormatter(time: Long): String {
  84. return String.format("%02d:%02d:%02d",
  85. TimeUnit.MILLISECONDS.toHours(time)%60,
  86. TimeUnit.MILLISECONDS.toMinutes(time)%60,
  87. TimeUnit.MILLISECONDS.toSeconds(time)%60)
  88. }
  89.  
  90.  
  91.  
  92. fun stopTimer() {
  93. timer.cancel()
  94. resetTimer()
  95. }
  96.  
  97.  
  98. /**
  99. * Creates a new alarm, notification and timer
  100. */
  101. fun startTimer() {
  102.  
  103. _alarmOn.value?.let {
  104. if (!it) {
  105. _alarmOn.value = true
  106. val triggerTime = SystemClock.elapsedRealtime()
  107.  
  108. // // TODO: Step 1.15 call cancel notification
  109. // val notificationManager =
  110. // ContextCompat.getSystemService(
  111. // app,
  112. // NotificationManager::class.java
  113. // ) as NotificationManager
  114. // notificationManager.cancelNotifications()
  115. //
  116. // AlarmManagerCompat.setExactAndAllowWhileIdle(
  117. // alarmManager,
  118. // AlarmManager.ELAPSED_REALTIME_WAKEUP,
  119. // triggerTime,
  120. // notifyPendingIntent
  121. // )
  122.  
  123. viewModelScope.launch {
  124. saveTime(triggerTime)
  125. }
  126. }
  127. }
  128. createTimer()
  129.  
  130. Log.d("RecordViewModel", " createTimer()")
  131. }
  132.  
  133. /**
  134. * Creates a new timer
  135. */
  136. private fun createTimer() {
  137. viewModelScope.launch {
  138. val triggerTime = loadTime()
  139. timer = object : CountDownTimer(triggerTime, second) {
  140. override fun onTick(millisUntilFinished: Long) {
  141. _elapsedTime.value = timeFormatter(SystemClock.elapsedRealtime() - triggerTime)
  142.  
  143. }
  144.  
  145. override fun onFinish() {
  146. resetTimer()
  147. }
  148. }
  149. timer.start()
  150. }
  151. }
  152.  
  153. /**
  154. * Cancels the alarm, notification and resets the timer
  155. */
  156. private fun cancelNotification() {
  157. resetTimer()
  158. alarmManager.cancel(notifyPendingIntent)
  159. }
  160.  
  161. /**
  162. * Resets the timer on screen and sets alarm value false
  163. */
  164. fun resetTimer() {
  165. _elapsedTime.value = timeFormatter(0)
  166. _alarmOn.value = false
  167. viewModelScope.launch { saveTime(0) }
  168. }
  169.  
  170. private suspend fun saveTime(triggerTime: Long) =
  171. withContext(Dispatchers.IO) {
  172. prefs.edit().putLong(TRIGGER_TIME, triggerTime).apply()
  173. }
  174.  
  175. private suspend fun loadTime(): Long =
  176. withContext(Dispatchers.IO) {
  177. prefs.getLong(TRIGGER_TIME, 0)
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement