Skip to content

Commit

Permalink
Remove forground service
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Mar 31, 2024
1 parent a6d0202 commit a121b7b
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ class Connection private constructor(
}

companion object {
private const val FOREGROUND = false

fun createConnection(
clientHandle: String,
clientId: String,
Expand All @@ -219,14 +217,7 @@ class Connection private constructor(
} else {
"tcp://$host:$port"
}
val intent = Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
}
val foregroundNotification = Notify.foregroundNotification(context, clientId, intent, R.string.notifyForeground)
val client = MqttAndroidClient(context, uri, clientId).apply {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O && FOREGROUND)
setForegroundService(foregroundNotification)
}
val client = MqttAndroidClient(context, uri, clientId)
return Connection(clientHandle, clientId, host, port, context, client, tlsConnection, connectionOptions)
}
}
Expand Down
1 change: 0 additions & 1 deletion serviceLibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<application>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package info.mqtt.android.service

import android.app.Notification
import android.content.*
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.util.SparseArray
Expand Down Expand Up @@ -77,9 +75,6 @@ class MqttAndroidClient @JvmOverloads constructor(
@Volatile
private var serviceBound = false

// notification for Foreground Service
private var foregroundServiceNotification: Notification? = null

private var clientJob: Job? = null
private var clientScope: CoroutineScope? = null
/**
Expand Down Expand Up @@ -206,17 +201,11 @@ class MqttAndroidClient @JvmOverloads constructor(
val serviceStartIntent = Intent()
serviceStartIntent.setClassName(context, SERVICE_NAME)
var service: Any? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && foregroundServiceNotification != null) {
serviceStartIntent.putExtra(MqttService.MQTT_FOREGROUND_SERVICE_NOTIFICATION, foregroundServiceNotification)
serviceStartIntent.putExtra(MqttService.MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID, FOREGROUND_ID)
service = context.startForegroundService(serviceStartIntent)
} else {
try {
service = context.startService(serviceStartIntent)
} catch (ex: IllegalStateException) {
val listener = token.actionCallback
listener?.onFailure(token, ex)
}
try {
service = context.startService(serviceStartIntent)
} catch (ex: IllegalStateException) {
val listener = token.actionCallback
listener?.onFailure(token, ex)
}
if (service == null) {
val listener = token.actionCallback
Expand Down Expand Up @@ -1147,21 +1136,6 @@ class MqttAndroidClient @JvmOverloads constructor(
return tokenMap[activityToken!!.toInt()]
}

/**
* Sets foregroundServiceNotification object. If it is not null at the time of
* MqttService start then the service will run in foreground mode which is
* mandatory to keep MQTT service operation when app is
* in the background on Android version >=8.
*
*
* This method has no effect if Build.VERSION.SDK_INT < Build.VERSION_CODES.O
*
* @param notification notification to be used when MqttService runs in foreground mode
*/
fun setForegroundService(notification: Notification) {
foregroundServiceNotification = notification
}

/**
* Sets the DisconnectedBufferOptions for this client
*
Expand Down Expand Up @@ -1292,7 +1266,6 @@ class MqttAndroidClient @JvmOverloads constructor(

companion object {
private val SERVICE_NAME = MqttService::class.java.name
private const val FOREGROUND_ID = 77
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package info.mqtt.android.service

import android.annotation.SuppressLint
import android.app.Notification
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
Expand All @@ -13,7 +12,6 @@ import android.os.Build
import android.os.Bundle
import android.os.IBinder
import android.os.PowerManager
import info.mqtt.android.service.extension.parcelableExtra
import info.mqtt.android.service.room.MqMessageDatabase
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -237,16 +235,6 @@ class MqttService : Service(), MqttTraceHandler {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// run till explicitly stopped, restart when process restarted
registerBroadcastReceivers()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val foregroundServiceNotification = intent?.parcelableExtra<Notification>(MQTT_FOREGROUND_SERVICE_NOTIFICATION)
if (foregroundServiceNotification != null) {
isForegroundStarted = true
startForeground(
intent.getIntExtra(MQTT_FOREGROUND_SERVICE_NOTIFICATION_ID, 1),
foregroundServiceNotification
)
}
}
return START_STICKY
}

Expand Down Expand Up @@ -331,7 +319,7 @@ class MqttService : Service(), MqttTraceHandler {
if (isForegroundStarted) {
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
stopForeground(Service.STOP_FOREGROUND_REMOVE)
stopForeground(STOP_FOREGROUND_REMOVE)
else
stopForeground(true)
}
Expand Down

0 comments on commit a121b7b

Please sign in to comment.