Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove forground service #583

Merged
merged 1 commit into from
Apr 5, 2024
Merged
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
@@ -203,8 +203,6 @@ class Connection private constructor(
}

companion object {
private const val FOREGROUND = false

fun createConnection(
clientHandle: String,
clientId: String,
@@ -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)
}
}
1 change: 0 additions & 1 deletion serviceLibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -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>

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
@@ -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
/**
@@ -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
@@ -1150,21 +1139,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
*
@@ -1295,7 +1269,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
@@ -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
@@ -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
}

@@ -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)
}