Commit 454dacec authored by Lisa (AI Assistant)'s avatar Lisa (AI Assistant)

Fix NodeClient sharing between Activity and Service

- OpenClawApp now calls setContext() to enable file-based identity
- NodeService uses lazy delegate to get NodeClient from OpenClawApp
- This ensures both Activity and Service use the same device identity
- generateDeviceIdentity() now skips if context is null (avoids crash)
parent 62cc4fa1
...@@ -18,6 +18,7 @@ class OpenClawApp : Application() { ...@@ -18,6 +18,7 @@ class OpenClawApp : Application() {
super.onCreate() super.onCreate()
// Initialize dependencies manually (without Hilt for NodeService) // Initialize dependencies manually (without Hilt for NodeService)
nodeClient = NodeClient() nodeClient = NodeClient()
nodeClient.setContext(this) // Set context for file-based device identity
settingsRepository = SettingsRepository(this) settingsRepository = SettingsRepository(this)
} }
} }
...@@ -79,12 +79,17 @@ class NodeClient() { ...@@ -79,12 +79,17 @@ class NodeClient() {
} }
init { init {
// Generate device identity on initialization // Don't generate identity here - wait for setContext to be called
// Note: setContext should be called after creation to enable persistence // This ensures we have a valid context for file-based storage
generateDeviceIdentity()
} }
private fun generateDeviceIdentity() { private fun generateDeviceIdentity() {
// Skip if no context available (Activity not initialized yet)
if (context == null) {
Log.w(TAG, "No context available for device identity generation")
return
}
// Use a file instead of SharedPreferences to ensure it works across processes // Use a file instead of SharedPreferences to ensure it works across processes
// SharedPreferences are not shared between processes by default // SharedPreferences are not shared between processes by default
val identityFile = context?.let { File(it.filesDir, "device_identity.json") } val identityFile = context?.let { File(it.filesDir, "device_identity.json") }
......
...@@ -29,8 +29,10 @@ private const val TAG = "NodeService" ...@@ -29,8 +29,10 @@ private const val TAG = "NodeService"
class NodeService : Service() { class NodeService : Service() {
// Initialize directly - not nullable // Get NodeClient from Application class to share device identity across processes
private val nodeClient = NodeClient() private val nodeClient: NodeClient by lazy {
(application as com.nexlab.openclaw.node.OpenClawApp).nodeClient
}
private val CHANNEL_ID = "openclaw_node_channel" private val CHANNEL_ID = "openclaw_node_channel"
private val NOTIFICATION_ID = 1 private val NOTIFICATION_ID = 1
...@@ -56,8 +58,7 @@ class NodeService : Service() { ...@@ -56,8 +58,7 @@ class NodeService : Service() {
createNotificationChannel() createNotificationChannel()
startForeground(NOTIFICATION_ID, createNotification()) startForeground(NOTIFICATION_ID, createNotification())
// Initialize NodeClient with context for device identity persistence // NodeClient already has context from OpenClawApp - no need to set again
nodeClient.setContext(this)
// Debug: Write to file // Debug: Write to file
try { try {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment