Skip to content

Commit

Permalink
[notifications] introduce notifications module
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Feb 26, 2024
1 parent c7c8340 commit 131c26e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 30 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import me.capcom.smsgateway.modules.encryption.encryptionModule
import me.capcom.smsgateway.modules.gateway.GatewayModule
import me.capcom.smsgateway.modules.localserver.LocalServerModule
import me.capcom.smsgateway.modules.messages.messagesModule
import me.capcom.smsgateway.modules.notifications.notificationsModule
import me.capcom.smsgateway.modules.settings.PreferencesStorage
import me.capcom.smsgateway.modules.settings.settingsModule
import me.capcom.smsgateway.receivers.EventsReceiver
Expand All @@ -24,6 +25,7 @@ class App: Application() {
modules(
settingsModule,
dbModule,
notificationsModule,
messagesModule,
encryptionModule,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package me.capcom.smsgateway.modules.localserver

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.os.PowerManager
import androidx.core.app.NotificationCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.aventrix.jnanoid.jnanoid.NanoIdUtils
Expand Down Expand Up @@ -42,13 +39,15 @@ import me.capcom.smsgateway.modules.localserver.domain.PostMessageResponse
import me.capcom.smsgateway.modules.messages.MessagesService
import me.capcom.smsgateway.modules.messages.data.MessageSource
import me.capcom.smsgateway.modules.messages.data.SendRequest
import me.capcom.smsgateway.modules.notifications.NotificationsService
import org.koin.android.ext.android.inject
import kotlin.concurrent.thread

class WebService : Service() {

private val settingsHelper by lazy { SettingsHelper(this) }
private val settingsHelper: SettingsHelper by inject()
private val messagesService: MessagesService by inject()
private val notificationsService: NotificationsService by inject()

private val wakeLock: PowerManager.WakeLock by lazy {
(getSystemService(Context.POWER_SERVICE) as PowerManager).run {
Expand Down Expand Up @@ -207,38 +206,22 @@ class WebService : Service() {
override fun onCreate() {
super.onCreate()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = getString(R.string.sms_gateway)
val descriptionText = getString(R.string.local_sms_gateway_notifications)
val importance = NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}

server.start()
wakeLock.acquire()

status.postValue(true)
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notification = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getText(R.string.notification_title))
.setContentText(
getString(
R.string.sms_gateway_is_running_on_port,
settingsHelper.serverPort
)
val notification = notificationsService.makeNotification(
this,
getString(
R.string.sms_gateway_is_running_on_port,
settingsHelper.serverPort
)
.setSmallIcon(R.drawable.ic_sms)
.build()
)

startForeground(NOTIFICATION_ID, notification)
startForeground(NotificationsService.NOTIFICATION_ID_LOCAL_SERVICE, notification)

return super.onStartCommand(intent, flags, startId)
}
Expand All @@ -259,9 +242,6 @@ class WebService : Service() {
}

companion object {
private const val NOTIFICATION_CHANNEL_ID = "WEBSERVICE"
private const val NOTIFICATION_ID = 1

private val status = MutableLiveData<Boolean>(false)
val STATUS: LiveData<Boolean> = status

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.capcom.smsgateway.modules.notifications

import org.koin.dsl.module

val notificationsModule = module {
single { NotificationsService(get()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.capcom.smsgateway.modules.notifications

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.core.app.NotificationCompat
import me.capcom.smsgateway.R

class NotificationsService(
context: Context
) {
private val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as
NotificationManager

init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = context.getString(R.string.sms_gateway)
val descriptionText = context.getString(R.string.local_sms_gateway_notifications)
val importance = NotificationManager.IMPORTANCE_LOW
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
notificationManager.createNotificationChannel(mChannel)
}
}

fun makeNotification(context: Context, contentText: String): Notification {
return NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setContentTitle(context.getText(R.string.notification_title))
.setContentText(contentText)
.setSmallIcon(R.drawable.ic_sms)
.build()
}

companion object {
const val NOTIFICATION_CHANNEL_ID = "sms-gateway"

const val NOTIFICATION_ID_LOCAL_SERVICE = 1
const val NOTIFICATION_ID_SEND_WORKER = 2
}
}

0 comments on commit 131c26e

Please sign in to comment.