Skip to content

Commit 8cee56c

Browse files
author
Arena Agent
committed
humanoperator: open app on notification click and start background listener on device boot
1 parent 5bbb069 commit 8cee56c

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

humanoperator/src/main/AndroidManifest.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
77
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
88
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
9+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
910

1011
<application
1112
android:allowBackup="true"
@@ -21,6 +22,16 @@
2122
android:exported="false"
2223
android:foregroundServiceType="dataSync" />
2324

25+
<receiver
26+
android:name=".BootReceiver"
27+
android:enabled="true"
28+
android:exported="true">
29+
<intent-filter>
30+
<action android:name="android.intent.action.BOOT_COMPLETED" />
31+
<category android:name="android.intent.category.DEFAULT" />
32+
</intent-filter>
33+
</receiver>
34+
2435
<activity
2536
android:name=".MainActivity"
2637
android:exported="true"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.screenoperator.humanoperator
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.os.Build
7+
import android.util.Log
8+
9+
class BootReceiver : BroadcastReceiver() {
10+
companion object {
11+
private const val TAG = "BootReceiver"
12+
}
13+
14+
override fun onReceive(context: Context, intent: Intent) {
15+
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
16+
Log.d(TAG, "onReceive: BOOT_COMPLETED received. Starting TaskListenerService...")
17+
val serviceIntent = Intent(context, TaskListenerService::class.java)
18+
try {
19+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
20+
context.startForegroundService(serviceIntent)
21+
} else {
22+
context.startService(serviceIntent)
23+
}
24+
Log.d(TAG, "Service started successfully on boot.")
25+
} catch (e: Exception) {
26+
Log.e(TAG, "Failed to start TaskListenerService on boot", e)
27+
}
28+
}
29+
}
30+
}

humanoperator/src/main/kotlin/com/screenoperator/humanoperator/TaskListenerService.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,20 @@ class TaskListenerService : Service(), SignalingClient.SignalingListener {
6060
}
6161

6262
private fun createForegroundNotification(): Notification {
63+
val intent = Intent(this, MainActivity::class.java).apply {
64+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP
65+
}
66+
val pendingIntent = android.app.PendingIntent.getActivity(
67+
this, 0, intent,
68+
android.app.PendingIntent.FLAG_UPDATE_CURRENT or android.app.PendingIntent.FLAG_IMMUTABLE
69+
)
70+
6371
return NotificationCompat.Builder(this, CHANNEL_ID)
6472
.setSmallIcon(android.R.drawable.ic_dialog_info)
6573
.setContentTitle("Human Operator Active")
6674
.setContentText("Listening for new tasks in the background")
6775
.setPriority(NotificationCompat.PRIORITY_LOW)
76+
.setContentIntent(pendingIntent)
6877
.build()
6978
}
7079

@@ -73,10 +82,11 @@ class TaskListenerService : Service(), SignalingClient.SignalingListener {
7382
try {
7483
// Intent to open MainActivity on click
7584
val intent = Intent(this, MainActivity::class.java).apply {
76-
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
85+
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP
7786
}
7887
val pendingIntent = android.app.PendingIntent.getActivity(
79-
this, 0, intent, android.app.PendingIntent.FLAG_IMMUTABLE
88+
this, taskId.hashCode(), intent,
89+
android.app.PendingIntent.FLAG_UPDATE_CURRENT or android.app.PendingIntent.FLAG_IMMUTABLE
8090
)
8191

8292
val notification = NotificationCompat.Builder(this, "human_operator_tasks")

0 commit comments

Comments
 (0)