Compare commits
3 Commits
main
...
feat/andro
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3094b72bea | ||
|
|
a63af8d8ff | ||
|
|
40ffbd32cb |
@ -5,6 +5,7 @@
|
|||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
### Fixes
|
### Fixes
|
||||||
|
- Android: tapping the foreground service notification brings the app to the front. (#179) — thanks @Syhids
|
||||||
- Cron tool passes `id` to the gateway for update/remove/run/runs (keeps `jobId` input). (#180) — thanks @adamgall
|
- Cron tool passes `id` to the gateway for update/remove/run/runs (keeps `jobId` input). (#180) — thanks @adamgall
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -48,6 +48,10 @@ android {
|
|||||||
lint {
|
lint {
|
||||||
disable += setOf("IconLauncherShape")
|
disable += setOf("IconLauncherShape")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testOptions {
|
||||||
|
unitTests.isIncludeAndroidResources = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
@ -96,6 +100,7 @@ dependencies {
|
|||||||
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2")
|
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.10.2")
|
||||||
testImplementation("io.kotest:kotest-runner-junit5-jvm:6.0.7")
|
testImplementation("io.kotest:kotest-runner-junit5-jvm:6.0.7")
|
||||||
testImplementation("io.kotest:kotest-assertions-core-jvm:6.0.7")
|
testImplementation("io.kotest:kotest-assertions-core-jvm:6.0.7")
|
||||||
|
testImplementation("org.robolectric:robolectric:4.16")
|
||||||
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.13.3")
|
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.13.3")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -98,14 +98,31 @@ class NodeForegroundService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun buildNotification(title: String, text: String): Notification {
|
private fun buildNotification(title: String, text: String): Notification {
|
||||||
|
val launchIntent = Intent(this, MainActivity::class.java).apply {
|
||||||
|
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||||
|
}
|
||||||
|
val launchPending =
|
||||||
|
PendingIntent.getActivity(
|
||||||
|
this,
|
||||||
|
1,
|
||||||
|
launchIntent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
||||||
|
)
|
||||||
|
|
||||||
val stopIntent = Intent(this, NodeForegroundService::class.java).setAction(ACTION_STOP)
|
val stopIntent = Intent(this, NodeForegroundService::class.java).setAction(ACTION_STOP)
|
||||||
val flags = PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
val stopPending =
|
||||||
val stopPending = PendingIntent.getService(this, 2, stopIntent, flags)
|
PendingIntent.getService(
|
||||||
|
this,
|
||||||
|
2,
|
||||||
|
stopIntent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
|
||||||
|
)
|
||||||
|
|
||||||
return NotificationCompat.Builder(this, CHANNEL_ID)
|
return NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
.setSmallIcon(R.mipmap.ic_launcher)
|
.setSmallIcon(R.mipmap.ic_launcher)
|
||||||
.setContentTitle(title)
|
.setContentTitle(title)
|
||||||
.setContentText(text)
|
.setContentText(text)
|
||||||
|
.setContentIntent(launchPending)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setOnlyAlertOnce(true)
|
.setOnlyAlertOnce(true)
|
||||||
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
|
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
|
||||||
|
|||||||
@ -0,0 +1,43 @@
|
|||||||
|
package com.clawdbot.android
|
||||||
|
|
||||||
|
import android.app.Notification
|
||||||
|
import android.content.Intent
|
||||||
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Assert.assertNotNull
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.robolectric.Robolectric
|
||||||
|
import org.robolectric.RobolectricTestRunner
|
||||||
|
import org.robolectric.Shadows
|
||||||
|
import org.robolectric.annotation.Config
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner::class)
|
||||||
|
@Config(sdk = [34])
|
||||||
|
class NodeForegroundServiceTest {
|
||||||
|
@Test
|
||||||
|
fun buildNotificationSetsLaunchIntent() {
|
||||||
|
val service = Robolectric.buildService(NodeForegroundService::class.java).get()
|
||||||
|
val notification = buildNotification(service)
|
||||||
|
|
||||||
|
val pendingIntent = notification.contentIntent
|
||||||
|
assertNotNull(pendingIntent)
|
||||||
|
|
||||||
|
val savedIntent = Shadows.shadowOf(pendingIntent).savedIntent
|
||||||
|
assertNotNull(savedIntent)
|
||||||
|
assertEquals(MainActivity::class.java.name, savedIntent.component?.className)
|
||||||
|
|
||||||
|
val expectedFlags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||||
|
assertEquals(expectedFlags, savedIntent.flags and expectedFlags)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun buildNotification(service: NodeForegroundService): Notification {
|
||||||
|
val method =
|
||||||
|
NodeForegroundService::class.java.getDeclaredMethod(
|
||||||
|
"buildNotification",
|
||||||
|
String::class.java,
|
||||||
|
String::class.java,
|
||||||
|
)
|
||||||
|
method.isAccessible = true
|
||||||
|
return method.invoke(service, "Title", "Text") as Notification
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user