Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.blitzortung.android.alert.handler
import android.content.Context
import android.content.SharedPreferences
import android.location.Location
import android.os.Build
import android.util.Log
import org.blitzortung.android.alert.AlertParameters
import org.blitzortung.android.alert.AlertResult
Expand All @@ -41,6 +42,7 @@ import org.blitzortung.android.location.LocationHandler
import org.blitzortung.android.protocol.ConsumerContainer
import org.blitzortung.android.protocol.Event
import org.blitzortung.android.util.MeasurementSystem
import org.blitzortung.android.util.isAtLeast
import javax.inject.Inject
import javax.inject.Singleton

Expand Down Expand Up @@ -213,7 +215,12 @@ class AlertHandler @Inject constructor(
val signalingLatestTimestamp = alertDataHandler.getLatestTimstampWithin(signalingDistanceLimit, alertResult)
if (signalingLatestTimestamp > signalingLastTimestamp + signalingThresholdTime) {
Log.d(Main.LOG_TAG, "AlertHandler.alertSignal() signal ${signalingLatestTimestamp / 1000}")
alertSignal.emitSignal()
if (isAtLeast(Build.VERSION_CODES.O)) {
sendNotification(alertResult, true)
} else {
alertSignal.emitSignal()
}

signalingLastTimestamp = signalingLatestTimestamp
} else {
Log.d(
Expand All @@ -228,13 +235,7 @@ class AlertHandler @Inject constructor(
alertDataHandler.getLatestTimstampWithin(notificationDistanceLimit, alertResult)
if (notificationLatestTimestamp > notificationLastTimestamp) {
Log.d(Main.LOG_TAG, "AlertHandler.alertNotification() notification ${notificationLatestTimestamp / 1000}")
notificationHandler.sendNotification(
context.resources.getString(R.string.activity) + ": " + alertDataHandler.getTextMessage(
alertResult,
notificationDistanceLimit,
context.resources
)
)
sendNotification(alertResult, false)
notificationLastTimestamp = notificationLatestTimestamp
} else {
Log.d(
Expand All @@ -244,6 +245,17 @@ class AlertHandler @Inject constructor(
}
}

private fun sendNotification(alertResult: AlertResult, isCloseAlarm: Boolean) {
notificationHandler.sendNotification(
context.resources.getString(R.string.activity) + ": " + alertDataHandler.getTextMessage(
alertResult,
notificationDistanceLimit,
context.resources
),
isCloseAlarm
)
}

fun requestUpdates(alertEventConsumer: (AlertEvent) -> Unit) {
alertConsumerContainer.addConsumer(alertEventConsumer)
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/blitzortung/android/app/AppService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import androidx.annotation.RequiresApi
import androidx.core.app.ServiceCompat
import dagger.android.AndroidInjection
import org.blitzortung.android.alert.handler.AlertHandler
import org.blitzortung.android.app.controller.NotificationHandler.Companion.CHANNEL_ID
import org.blitzortung.android.app.controller.NotificationHandler.Companion.BACKGROUND_CHANNEL_ID
import org.blitzortung.android.app.view.OnSharedPreferenceChangeListener
import org.blitzortung.android.app.view.PreferenceKey
import org.blitzortung.android.app.view.get
Expand Down Expand Up @@ -150,7 +150,7 @@ class AppService : Service(), OnSharedPreferenceChangeListener {

@RequiresApi(Build.VERSION_CODES.Q)
private fun startForeground(contentIntent: PendingIntent?) {
val notification = Notification.Builder(this, CHANNEL_ID)
val notification = Notification.Builder(this, BACKGROUND_CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle(this.resources.getText(R.string.app_name))
.setContentText("bla")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ package org.blitzortung.android.app.controller
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.NotificationManager.IMPORTANCE_HIGH
import android.app.NotificationManager.IMPORTANCE_LOW
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.media.AudioAttributes
import android.os.Build
import androidx.annotation.RequiresApi

Expand All @@ -41,11 +43,11 @@ open class NotificationHandler @Inject constructor(

init {
if (isAtLeast(Build.VERSION_CODES.O)) {
createNotificationChannel()
createNotificationChannels()
}
}

fun sendNotification(notificationText: String) {
fun sendNotification(notificationText: String, isCloseAlarm: Boolean) { // Added isCloseAlarm parameter
val intent = Intent(context, Main::class.java).apply {
action = Intent.ACTION_MAIN
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
Expand All @@ -59,7 +61,7 @@ open class NotificationHandler @Inject constructor(

val notification = when {
isAtLeast(Build.VERSION_CODES.O) -> {
createNotification(contentIntent, notificationText)
createNotification(contentIntent, notificationText, isCloseAlarm) // Pass isCloseAlarm
}

else -> {
Expand All @@ -71,8 +73,9 @@ open class NotificationHandler @Inject constructor(
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotification(contentIntent: PendingIntent?, notificationText: String): Notification {
return Notification.Builder(context, CHANNEL_ID)
private fun createNotification(contentIntent: PendingIntent?, notificationText: String, isCloseAlarm: Boolean): Notification {
val channelId = if (isCloseAlarm) CLOSE_CHANNEL_ID else DISTANT_CHANNEL_ID
return Notification.Builder(context, channelId) // Use dynamic channelId
.setSmallIcon(R.drawable.icon)
.setContentTitle(context.resources.getText(R.string.app_name))
.setContentText(notificationText)
Expand All @@ -98,22 +101,49 @@ open class NotificationHandler @Inject constructor(
return builder.build()
}

private fun createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = context.getString(R.string.notification_channel_name)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
setImportance(IMPORTANCE_LOW)
}
// Register the channel with the system
notificationManager.createNotificationChannel(channel)
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannels() { // Renamed method
val backgroundName = context.getString(R.string.notification_channel_background_name)
val backgroundChannel = NotificationChannel(BACKGROUND_CHANNEL_ID, backgroundName, IMPORTANCE_LOW).apply {
description = context.getString(R.string.notification_channel_background_description)
enableLights(false)
enableVibration(false)
setSound(null, null) // No sound
// setBypassDnd(false) // Default is false, explicitly state for clarity if desired
}
notificationManager.createNotificationChannel(backgroundChannel)

val distantName = context.getString(R.string.notification_channel_distant_name)
val distantChannel = NotificationChannel(DISTANT_CHANNEL_ID, distantName, IMPORTANCE_LOW).apply {
description = context.getString(R.string.notification_channel_distant_description)
enableLights(false)
enableVibration(false)
setSound(null, null) // No sound
// setBypassDnd(false) // Default is false, explicitly state for clarity if desired
}
notificationManager.createNotificationChannel(distantChannel)

val closeName = context.getString(R.string.notification_channel_close_name)
val closeChannel = NotificationChannel(CLOSE_CHANNEL_ID, closeName, IMPORTANCE_HIGH).apply {
description = context.getString(R.string.notification_channel_close_description)
enableLights(true)
enableVibration(true)
// Default sound and vibration will be used for IMPORTANCE_HIGH,
// or you can set a custom sound if needed.
val audioAttributes = AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build()
setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes)
// setBypassDnd(false) // Default is false, explicitly state for clarity if desired
}
notificationManager.createNotificationChannel(closeChannel)
}

companion object {
const val CHANNEL_ID = "channel"
const val BACKGROUND_CHANNEL_ID = "blitzortung_background_channel"
const val DISTANT_CHANNEL_ID = "blitzortung_alert_distant_channel"
const val CLOSE_CHANNEL_ID = "blitzortung_alert_close_channel"
}

}
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,4 +386,10 @@ Russian by Ivan Karev (karev.ivan@gmail.com)\n
<string name="background_alerts" translatable="false">Background alert symbol</string>
<string name="default_alarm_signal">Default alarm sound</string>
<string name="post_notifications_required_for_alerts">Notification permission is required for alerts, disabling alerts and background queries.</string>
<string name="notification_channel_distant_name">Distant lightning</string>
<string name="notification_channel_close_name">Close lightning</string>
<string name="notification_channel_distant_description">Notification channel for alerts for distant lightning activity</string>
<string name="notification_channel_close_description">Notification channel for alerts for close lightning activity</string>
<string name="notification_channel_background_name">Background operation</string>
<string name="notification_channel_background_description">Channel for background operation notifications</string>
</resources>
Loading