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() {
super.onCreate()
// Initialize dependencies manually (without Hilt for NodeService)
nodeClient = NodeClient()
nodeClient.setContext(this) // Set context for file-based device identity
settingsRepository = SettingsRepository(this)
}
}
......@@ -79,12 +79,17 @@ class NodeClient() {
}
init {
// Generate device identity on initialization
// Note: setContext should be called after creation to enable persistence
generateDeviceIdentity()
// Don't generate identity here - wait for setContext to be called
// This ensures we have a valid context for file-based storage
}
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
// SharedPreferences are not shared between processes by default
val identityFile = context?.let { File(it.filesDir, "device_identity.json") }
......
......@@ -29,8 +29,10 @@ private const val TAG = "NodeService"
class NodeService : Service() {
// Initialize directly - not nullable
private val nodeClient = NodeClient()
// Get NodeClient from Application class to share device identity across processes
private val nodeClient: NodeClient by lazy {
(application as com.nexlab.openclaw.node.OpenClawApp).nodeClient
}
private val CHANNEL_ID = "openclaw_node_channel"
private val NOTIFICATION_ID = 1
......@@ -56,8 +58,7 @@ class NodeService : Service() {
createNotificationChannel()
startForeground(NOTIFICATION_ID, createNotification())
// Initialize NodeClient with context for device identity persistence
nodeClient.setContext(this)
// NodeClient already has context from OpenClawApp - no need to set again
// Debug: Write to file
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