Add broadcast receiver to AlarmModule

This commit is contained in:
Brandon Presley 2022-10-28 16:48:29 +13:00
parent 21d9149498
commit 46dcfb96bf

View File

@ -3,8 +3,10 @@ package com.massive
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.* import android.app.*
import android.content.ActivityNotFoundException import android.content.ActivityNotFoundException
import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.CountDownTimer import android.os.CountDownTimer
@ -14,14 +16,10 @@ import android.util.Log
import android.widget.Toast import android.widget.Toast
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import com.facebook.react.bridge.ActivityEventListener
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.BaseActivityEventListener
import com.facebook.react.bridge.Callback import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod import com.facebook.react.bridge.ReactMethod
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
import kotlin.math.floor import kotlin.math.floor
@ -34,20 +32,15 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
return "AlarmModule" return "AlarmModule"
} }
private val mActivityEventListener: ActivityEventListener = private val broadcastReceiver = object : BroadcastReceiver() {
object : BaseActivityEventListener() { override fun onReceive(context: Context?, intent: Intent?) {
override fun onActivityResult( Toast.makeText(reactApplicationContext, "called from test receiver", Toast.LENGTH_SHORT)
activity: Activity?, .show()
requestCode: Int,
resultCode: Int,
data: Intent?
) {
Log.d("AlarmModule", "onActivityResult")
} }
} }
init { init {
reactApplicationContext.addActivityEventListener(mActivityEventListener) reactApplicationContext.registerReceiver(broadcastReceiver, IntentFilter(STOP_BROADCAST))
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
@ -82,8 +75,7 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
manager.cancel(NOTIFICATION_ID_DONE) manager.cancel(NOTIFICATION_ID_DONE)
reactApplicationContext.stopService( reactApplicationContext.stopService(
Intent( Intent(
reactApplicationContext, reactApplicationContext, AlarmService::class.java
AlarmService::class.java
) )
) )
countdownTimer?.cancel() countdownTimer?.cancel()
@ -95,8 +87,7 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
@ReactMethod @ReactMethod
fun ignoringBattery(callback: Callback) { fun ignoringBattery(callback: Callback) {
val packageName = reactApplicationContext.packageName val packageName = reactApplicationContext.packageName
val pm = val pm = reactApplicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
reactApplicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
callback.invoke(pm.isIgnoringBatteryOptimizations(packageName)) callback.invoke(pm.isIgnoringBatteryOptimizations(packageName))
} else { } else {
@ -128,16 +119,14 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
return object : CountDownTimer(endMs.toLong(), 1000) { return object : CountDownTimer(endMs.toLong(), 1000) {
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
override fun onTick(current: Long) { override fun onTick(current: Long) {
val seconds = floor((current / 1000).toDouble() % 60) val seconds =
.toInt().toString().padStart(2, '0') floor((current / 1000).toDouble() % 60).toInt().toString().padStart(2, '0')
val minutes = floor((current / 1000).toDouble() / 60) val minutes =
.toInt().toString().padStart(2, '0') floor((current / 1000).toDouble() / 60).toInt().toString().padStart(2, '0')
builder.setContentText("$minutes:$seconds") builder.setContentText("$minutes:$seconds").setAutoCancel(false).setDefaults(0)
.setAutoCancel(false)
.setDefaults(0)
.setProgress(endMs, current.toInt(), false) .setProgress(endMs, current.toInt(), false)
.setCategory(NotificationCompat.CATEGORY_PROGRESS) .setCategory(NotificationCompat.CATEGORY_PROGRESS).priority =
.priority = NotificationCompat.PRIORITY_LOW NotificationCompat.PRIORITY_LOW
val manager = getManager() val manager = getManager()
manager.notify(NOTIFICATION_ID_PENDING, builder.build()) manager.notify(NOTIFICATION_ID_PENDING, builder.build())
} }
@ -146,30 +135,18 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
override fun onFinish() { override fun onFinish() {
val context = reactApplicationContext val context = reactApplicationContext
val finishIntent = Intent(context, StopAlarm::class.java) val finishIntent = Intent(context, StopAlarm::class.java)
val finishPending = val finishPending = PendingIntent.getActivity(
PendingIntent.getActivity( context, 0, finishIntent, PendingIntent.FLAG_IMMUTABLE
context,
0,
finishIntent,
PendingIntent.FLAG_IMMUTABLE
) )
val fullIntent = Intent(context, TimerDone::class.java) val fullIntent = Intent(context, TimerDone::class.java)
val fullPending = val fullPending = PendingIntent.getActivity(
PendingIntent.getActivity( context, 0, fullIntent, PendingIntent.FLAG_IMMUTABLE
context,
0,
fullIntent,
PendingIntent.FLAG_IMMUTABLE
) )
builder.setContentText("Timer finished.") builder.setContentText("Timer finished.").setProgress(0, 0, false)
.setProgress(0, 0, false) .setAutoCancel(true).setOngoing(true).setFullScreenIntent(fullPending, true)
.setAutoCancel(true) .setContentIntent(finishPending).setChannelId(CHANNEL_ID_DONE)
.setOngoing(true) .setCategory(NotificationCompat.CATEGORY_ALARM).priority =
.setFullScreenIntent(fullPending, true) NotificationCompat.PRIORITY_HIGH
.setContentIntent(finishPending)
.setChannelId(CHANNEL_ID_DONE)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.priority = NotificationCompat.PRIORITY_HIGH
val manager = getManager() val manager = getManager()
manager.notify(NOTIFICATION_ID_DONE, builder.build()) manager.notify(NOTIFICATION_ID_DONE, builder.build())
manager.cancel(NOTIFICATION_ID_PENDING) manager.cancel(NOTIFICATION_ID_PENDING)
@ -188,12 +165,11 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
val contentIntent = Intent(context, MainActivity::class.java) val contentIntent = Intent(context, MainActivity::class.java)
val pendingContent = val pendingContent =
PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE) PendingIntent.getActivity(context, 0, contentIntent, PendingIntent.FLAG_IMMUTABLE)
val stopIntent = Intent(context, StopTimer::class.java) val stopIntent = Intent(STOP_BROADCAST)
val pendingStop = val pendingStop =
PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE) PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE)
return NotificationCompat.Builder(context, CHANNEL_ID_PENDING) return NotificationCompat.Builder(context, CHANNEL_ID_PENDING)
.setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24) .setSmallIcon(R.drawable.ic_baseline_hourglass_bottom_24).setContentTitle("Resting")
.setContentTitle("Resting")
.setContentIntent(pendingContent) .setContentIntent(pendingContent)
.addAction(R.drawable.ic_baseline_stop_24, "Stop", pendingStop) .addAction(R.drawable.ic_baseline_stop_24, "Stop", pendingStop)
.setDeleteIntent(pendingStop) .setDeleteIntent(pendingStop)
@ -202,9 +178,7 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
@RequiresApi(Build.VERSION_CODES.O) @RequiresApi(Build.VERSION_CODES.O)
fun getManager(): NotificationManager { fun getManager(): NotificationManager {
val alarmsChannel = NotificationChannel( val alarmsChannel = NotificationChannel(
CHANNEL_ID_DONE, CHANNEL_ID_DONE, CHANNEL_ID_DONE, NotificationManager.IMPORTANCE_HIGH
CHANNEL_ID_DONE,
NotificationManager.IMPORTANCE_HIGH
) )
alarmsChannel.description = "Alarms for rest timers." alarmsChannel.description = "Alarms for rest timers."
alarmsChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC alarmsChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
@ -212,11 +186,8 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
NotificationManager::class.java NotificationManager::class.java
) )
notificationManager.createNotificationChannel(alarmsChannel) notificationManager.createNotificationChannel(alarmsChannel)
val timersChannel = val timersChannel = NotificationChannel(
NotificationChannel( CHANNEL_ID_PENDING, CHANNEL_ID_PENDING, NotificationManager.IMPORTANCE_LOW
CHANNEL_ID_PENDING,
CHANNEL_ID_PENDING,
NotificationManager.IMPORTANCE_LOW
) )
timersChannel.setSound(null, null) timersChannel.setSound(null, null)
timersChannel.description = "Progress on rest timers." timersChannel.description = "Progress on rest timers."
@ -225,6 +196,7 @@ class AlarmModule internal constructor(context: ReactApplicationContext?) :
} }
companion object { companion object {
const val STOP_BROADCAST = "stop-timer-event"
const val CHANNEL_ID_PENDING = "Timer" const val CHANNEL_ID_PENDING = "Timer"
const val CHANNEL_ID_DONE = "Alarm" const val CHANNEL_ID_DONE = "Alarm"
const val NOTIFICATION_ID_PENDING = 1 const val NOTIFICATION_ID_PENDING = 1