mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2026-01-21 07:34:33 +01:00
Redesign service approach
This commit is contained in:
@@ -45,7 +45,6 @@ class ConnectionObserverAdapter<T> : ConnectionObserver {
|
||||
}
|
||||
|
||||
fun setValue(value: T) {
|
||||
Log.d("AAATESTAAA", "setValue()")
|
||||
_status.value = SuccessResult(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package no.nordicsemi.android.service
|
||||
|
||||
import android.app.Notification
|
||||
import android.app.NotificationChannel
|
||||
import android.app.NotificationManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.lifecycle.LifecycleService
|
||||
|
||||
private const val CHANNEL_ID = "FOREGROUND_BLE_SERVICE"
|
||||
|
||||
abstract class NotificationService : LifecycleService() {
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
val result = super.onStartCommand(intent, flags, startId)
|
||||
startForegroundService()
|
||||
return result
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
// when user has disconnected from the sensor, we have to cancel the notification that we've created some milliseconds before using unbindService
|
||||
cancelNotification()
|
||||
stopForegroundService()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the service as a foreground service
|
||||
*/
|
||||
private fun startForegroundService() {
|
||||
// when the activity closes we need to show the notification that user is connected to the peripheral sensor
|
||||
// We start the service as a foreground service as Android 8.0 (Oreo) onwards kills any running background services
|
||||
val notification = createNotification(R.string.csc_notification_connected_message, 0)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
startForeground(NOTIFICATION_ID, notification)
|
||||
} else {
|
||||
val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
nm.notify(NOTIFICATION_ID, notification)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the service as a foreground service
|
||||
*/
|
||||
private fun stopForegroundService() {
|
||||
// when the activity rebinds to the service, remove the notification and stop the foreground service
|
||||
// on devices running Android 8.0 (Oreo) or above
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
stopForeground(true)
|
||||
} else {
|
||||
cancelNotification()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the notification
|
||||
*
|
||||
* @param messageResId the message resource id. The message must have one String parameter,<br></br>
|
||||
* f.e. `<string name="name">%s is connected</string>`
|
||||
* @param defaults
|
||||
*/
|
||||
private fun createNotification(messageResId: Int, defaults: Int): Notification {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
createNotificationChannel(CHANNEL_ID)
|
||||
}
|
||||
|
||||
val intent: Intent? = packageManager.getLaunchIntentForPackage(packageName)
|
||||
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)
|
||||
|
||||
return NotificationCompat.Builder(this, CHANNEL_ID)
|
||||
.setContentTitle(getString(R.string.app_name))
|
||||
.setContentText(getString(messageResId, "Device"))
|
||||
.setSmallIcon(R.drawable.ic_notification_icon)
|
||||
.setColor(ContextCompat.getColor(this, R.color.md_theme_primary))
|
||||
.setContentIntent(pendingIntent)
|
||||
.build()
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
private fun createNotificationChannel(channelName: String) {
|
||||
val channel = NotificationChannel(
|
||||
channelName,
|
||||
getString(R.string.channel_connected_devices_title),
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
)
|
||||
channel.description = getString(R.string.channel_connected_devices_description)
|
||||
channel.setShowBadge(false)
|
||||
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
|
||||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the existing notification. If there is no active notification this method does nothing
|
||||
*/
|
||||
private fun cancelNotification() {
|
||||
val nm = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
nm.cancel(NOTIFICATION_ID)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val NOTIFICATION_ID = 200
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.service
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
@@ -19,4 +20,21 @@ class ServiceManager @Inject constructor(
|
||||
}
|
||||
context.startService(intent)
|
||||
}
|
||||
|
||||
fun <T> startService(service: Class<T>, device: BluetoothDevice) {
|
||||
val intent = Intent(context, service).apply {
|
||||
putExtra(DEVICE_DATA, device)
|
||||
}
|
||||
context.startService(intent)
|
||||
}
|
||||
|
||||
fun <T> startService(service: Class<T>) {
|
||||
val intent = Intent(context, service)
|
||||
context.startService(intent)
|
||||
}
|
||||
|
||||
fun <T> stopService(service: Class<T>) {
|
||||
val intent = Intent(context, service)
|
||||
context.stopService(intent)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user