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 notification on disconnect #444

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
@@ -2,7 +2,9 @@

package info.mqtt.android.service

import android.app.ActivityManager
import android.content.*
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import info.mqtt.android.service.extension.parcelable
@@ -199,7 +201,13 @@ class MqttAndroidClient(
* The actual connection depends on the service, which we start and bind to here, but which we can't actually use until the serviceConnection
* onServiceConnected() method has run (asynchronously), so the connection itself takes place in the onServiceConnected() method
*/
if (mqttService == null) { // First time - must bind to the service
val isRunning = isMqServiceRunning()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Timber.d("isRunning=$isRunning ${mqttService?.connections?.size} foregroundServiceType=${mqttService?.foregroundServiceType}")
} else
Timber.d("isRunning=$isRunning ${mqttService?.connections?.size}")

if (mqttService == null || mqttService?.connections?.size == 0) { // First time - must bind to the service
val serviceStartIntent = Intent()
serviceStartIntent.setClassName(context, SERVICE_NAME)
var service: Any? = null
@@ -230,6 +238,12 @@ class MqttAndroidClient(
return token
}

private fun isMqServiceRunning(): Boolean {
val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
return manager.getRunningServices(Integer.MAX_VALUE)
.any { it.service.className == SERVICE_NAME }
}

private fun collect() {
if (mqttService == null) {
return
@@ -273,9 +287,26 @@ class MqttAndroidClient(
* @see .disconnect
*/
override fun disconnect(): IMqttToken {
val isRunning = isMqServiceRunning()
Timber.d("isRunning=$isRunning ${mqttService?.connections?.size}")

val token: IMqttToken = MqttTokenAndroid(this, null, null)
storeToken(token)
mqttService!!.disconnect(clientHandle!!, null, token)
clientHandle?.let {
mqttService?.disconnect(it, null, token)
}

// if there are no more connections, we can shutdown the service
if (mqttService?.connections?.isEmpty() == true) {
Timber.d("Shutdown service")
// For < Android O this should work (untested)
val myService = Intent(context, MqttService::class.java)
context.stopService(myService)
// For Android O it's probably enough
mqttService!!.stopForeground(true)

//unregisterResources()
}
return token
}