Skip to content

Commit 9147468

Browse files
committed
Notification service remains active even if web service is disabled #244
1 parent 408046a commit 9147468

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

app/src/main/java/com/ismartcoding/plain/services/HttpServerService.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.ismartcoding.plain.helpers.NotificationHelper
2424
import com.ismartcoding.plain.helpers.UrlHelper
2525
import com.ismartcoding.plain.web.HttpServerManager
2626
import com.ismartcoding.plain.web.NsdHelper
27+
import com.ismartcoding.plain.features.Permission
2728
import io.ktor.client.request.get
2829
import io.ktor.http.HttpStatusCode
2930
import kotlinx.coroutines.delay
@@ -116,6 +117,7 @@ class HttpServerService : LifecycleService() {
116117
HttpServerManager.portsInUse.clear()
117118
NsdHelper.registerService(this, TempData.httpPort)
118119
sendEvent(HttpServerStateChangedEvent(HttpServerState.ON))
120+
PNotificationListenerService.toggle(this, Permission.NOTIFICATION_LISTENER.isEnabledAsync(this))
119121
} else {
120122
if (!checkResult.http) {
121123
if (PortHelper.isPortInUse(TempData.httpPort)) {
@@ -142,6 +144,7 @@ class HttpServerService : LifecycleService() {
142144
}
143145

144146
sendEvent(HttpServerStateChangedEvent(HttpServerState.ERROR))
147+
PNotificationListenerService.toggle(this, false)
145148
}
146149
}
147150

@@ -167,5 +170,6 @@ class HttpServerService : LifecycleService() {
167170
LogCat.e(ex.toString())
168171
ex.printStackTrace()
169172
}
173+
PNotificationListenerService.toggle(this, false)
170174
}
171175
}

app/src/main/java/com/ismartcoding/plain/ui/MainActivity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ class MainActivity : AppCompatActivity() {
279279

280280
private suspend fun doWhenReadyAsync() {
281281
// PackageHelper.cacheAppLabels()
282-
PNotificationListenerService.toggle(this@MainActivity, Permission.NOTIFICATION_LISTENER.isEnabledAsync(this@MainActivity))
282+
val webEnabled = WebPreference.getAsync(this@MainActivity)
283+
val permissionEnabled = Permission.NOTIFICATION_LISTENER.isEnabledAsync(this@MainActivity)
284+
PNotificationListenerService.toggle(this@MainActivity, webEnabled && permissionEnabled)
283285
}
284286

285287
override fun onDestroy() {

app/src/main/java/com/ismartcoding/plain/ui/page/web/WebSettingsPage.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ fun WebSettingsPage(
9999
permissionList = Permissions.getWebList(context)
100100
systemAlertWindow = Permission.SYSTEM_ALERT_WINDOW.can(context)
101101
if (event.map[Permission.NOTIFICATION_LISTENER.toSysPermission()] == true) {
102-
PNotificationListenerService.toggle(context, true)
102+
// Only enable listener if web is enabled
103+
PNotificationListenerService.toggle(context, WebPreference.getAsync(context))
103104
}
104105
}
105106

@@ -125,7 +126,9 @@ fun WebSettingsPage(
125126
scope.launch {
126127
withIO { ApiPermissionsPreference.putAsync(context, m.permission, enable) }
127128
if (m.permission == Permission.NOTIFICATION_LISTENER) {
128-
PNotificationListenerService.toggle(context, enable)
129+
// Enable only when web is enabled; otherwise disable
130+
val webEnabled = WebPreference.getAsync(context)
131+
PNotificationListenerService.toggle(context, enable && webEnabled)
129132
}
130133
if (enable) {
131134
val ps = m.permissions.filter { !it.can(context) }

app/src/main/java/com/ismartcoding/plain/web/HttpServerManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ object HttpServerManager {
9191
ex.printStackTrace()
9292
}
9393
context.stopService(Intent(context, HttpServerService::class.java))
94+
// Ensure notification listener is disabled when server is stopped explicitly
95+
com.ismartcoding.plain.services.PNotificationListenerService.toggle(context, false)
9496
httpServerError = ""
9597
portsInUse.clear()
9698
sendEvent(HttpServerStateChangedEvent(HttpServerState.OFF))

0 commit comments

Comments
 (0)