@@ -12,6 +12,7 @@ import java.io.*
1212
1313class LightClientService : Service () {
1414 private var isRunning = false
15+ private var isStarting = false
1516 private val TAG = " LightClientService"
1617
1718 companion object {
@@ -155,62 +156,75 @@ class LightClientService : Service() {
155156 }
156157
157158 private fun startLightClient () {
158- if (isRunning) {
159- appendLog(" Already running! " )
159+ if (isRunning || isStarting ) {
160+ appendLog(" Already running or starting, please wait... " )
160161 return
161162 }
162163
163- try {
164- val configPath = File (filesDir, MainActivity .CONFIG_NAME ).absolutePath
165-
166- appendLog(" ---" )
167- appendLog(" Starting CKB Light Client via JNI..." )
168- appendLog(" Config: $configPath " )
169- appendLog(" ---" )
170-
171- // Initialize native light client
172- val initSuccess = LightClientNative .nativeInit(
173- configPath,
174- nativeLogCallback,
175- nativeStatusCallback
176- )
177-
178- if (! initSuccess) {
179- appendLog(" ERROR: Native initialization failed" )
180- stopSelf()
181- return
182- }
164+ isStarting = true
165+ updateNotification(" CKB Light Client is starting..." )
183166
184- appendLog(" Native initialization succeeded" )
167+ Thread {
168+ try {
169+ val configPath = File (filesDir, MainActivity .CONFIG_NAME ).absolutePath
185170
186- // Apply saved RUST_LOG filter
187- val prefs = getSharedPreferences(" CKBLightClientPrefs" , Context .MODE_PRIVATE )
188- val savedFilter = prefs.getString(" rust_log_filter" , " debug" ) ? : " info"
189- LightClientNative .nativeSetLogFilter(savedFilter)
190- appendLog(" Applied log filter: $savedFilter " )
171+ appendLog(" ---" )
172+ appendLog(" Starting CKB Light Client via JNI (background thread)..." )
173+ appendLog(" Config: $configPath " )
174+ appendLog(" ---" )
191175
192- // Start the light client
193- val startSuccess = LightClientNative .nativeStart()
176+ // Initialize native light client
177+ val initSuccess = LightClientNative .nativeInit(
178+ configPath,
179+ nativeLogCallback,
180+ nativeStatusCallback
181+ )
182+
183+ if (! initSuccess) {
184+ appendLog(" ERROR: Native initialization failed" )
185+ stopSelf()
186+ return @Thread
187+ }
194188
195- if (startSuccess) {
196- isRunning = true
197- appendLog(" Light Client started successfully!" )
198- updateNotification(" CKB Light Client is running" )
199- } else {
200- appendLog(" ERROR: Native start failed" )
189+ appendLog(" Native initialization succeeded" )
190+
191+ // Apply saved RUST_LOG filter
192+ val prefs = getSharedPreferences(" CKBLightClientPrefs" , Context .MODE_PRIVATE )
193+ val savedFilter = prefs.getString(" rust_log_filter" , " debug" ) ? : " info"
194+ LightClientNative .nativeSetLogFilter(savedFilter)
195+ appendLog(" Applied log filter: $savedFilter " )
196+
197+ // Start the light client
198+ val startSuccess = LightClientNative .nativeStart()
199+
200+ if (startSuccess) {
201+ isRunning = true
202+ appendLog(" Light Client started successfully!" )
203+ updateNotification(" CKB Light Client is running" )
204+ } else {
205+ appendLog(" ERROR: Native start failed" )
206+ stopSelf()
207+ }
208+
209+ } catch (e: Exception ) {
210+ appendLog(" ERROR: ${e.message} " )
211+ Log .e(TAG , " Start error" , e)
212+ isRunning = false
201213 stopSelf()
214+ } finally {
215+ isStarting = false
202216 }
203-
204- } catch (e: Exception ) {
205- appendLog(" ERROR: ${e.message} " )
206- Log .e(TAG , " Start error" , e)
207- isRunning = false
208- stopSelf()
209- }
217+ }.start()
210218 }
211219
212220 private fun stopLightClient () {
221+ if (isStarting) {
222+ appendLog(" Still starting, please wait before stopping." )
223+ return
224+ }
225+
213226 if (! isRunning) {
227+ appendLog(" Not running!" )
214228 return
215229 }
216230
0 commit comments