Skip to content

Commit b55dc8f

Browse files
committed
update
1 parent ab8cc48 commit b55dc8f

File tree

3 files changed

+63
-49
lines changed

3 files changed

+63
-49
lines changed

android-app/app/src/main/java/com/nervosnetwork/ckblightclient/LightClientService.kt

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import java.io.*
1212

1313
class 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

light-client-lib/src/jni_bridge/callbacks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ struct LogFilterConfig {
4040
impl LogFilterConfig {
4141
const fn new() -> Self {
4242
Self {
43-
default_level: LevelFilter::Info,
43+
// Default to debug for normal diagnostics; can be overridden via nativeSetLogFilter()
44+
default_level: LevelFilter::Debug,
4445
module_filters: Vec::new(),
4546
}
4647
}
@@ -119,9 +120,8 @@ impl Log for JniLogger {
119120
let message = format!("{}", record.args());
120121
buffer.entries.push((record.level(), message));
121122

122-
if buffer.should_flush() {
123-
flush_log_buffer(buffer);
124-
}
123+
// Flush immediately to avoid losing short bursts (mobile app often emits only a few lines)
124+
flush_log_buffer(buffer);
125125
}
126126

127127
fn flush(&self) {

light-client-lib/src/jni_bridge/lifecycle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ pub extern "C" fn Java_com_nervosnetwork_ckblightclient_LightClientNative_native
9292
eprintln!("Failed to set logger: {}", e);
9393
return JNI_FALSE;
9494
}
95-
// Default to Info level - can be changed via nativeSetLogFilter()
96-
log::set_max_level(log::LevelFilter::Info);
95+
// Default to debug; filters can still be tightened via nativeSetLogFilter()
96+
log::set_max_level(log::LevelFilter::Trace);
9797
} else {
9898
info!("Reinitialization - JavaVM and callbacks already set, skipping...")
9999
}

0 commit comments

Comments
 (0)