From b7b04576855c7325eb16384aa166f75e7f16f73f Mon Sep 17 00:00:00 2001 From: AzenKain Date: Wed, 6 Aug 2025 19:09:41 +0700 Subject: [PATCH] FIX: app runing state bug --- .../fireflypsandorid/GolangServerService.kt | 27 ++++++++++++++----- .../example/fireflypsandorid/MainActivity.kt | 16 +++++------ 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/example/fireflypsandorid/GolangServerService.kt b/app/src/main/java/com/example/fireflypsandorid/GolangServerService.kt index 9a369b7..02c69f8 100644 --- a/app/src/main/java/com/example/fireflypsandorid/GolangServerService.kt +++ b/app/src/main/java/com/example/fireflypsandorid/GolangServerService.kt @@ -10,6 +10,9 @@ import android.os.Build import android.os.IBinder import android.os.PowerManager import android.util.Log +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue import androidx.core.app.NotificationCompat import libandroid.Libandroid @@ -19,6 +22,7 @@ class GolangServerService : Service() { const val CHANNEL_ID = "GolangServerChannel" const val NOTIFICATION_ID = 1 private const val TAG = "GolangServerService" + var isRunning by mutableStateOf(false) } private var wakeLock: PowerManager.WakeLock? = null @@ -29,9 +33,13 @@ class GolangServerService : Service() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + if (isRunning) { + Log.d(TAG, "❗ Server is already running") + return START_STICKY + } + isRunning = true Log.d(TAG, "onStartCommand called") - // 1. Tạo intent để mở lại MainActivity khi người dùng click vào thông báo val notificationIntent = Intent(this, MainActivity::class.java) val pendingIntent = PendingIntent.getActivity( this, @@ -40,18 +48,15 @@ class GolangServerService : Service() { PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE ) - // 2. Tạo notification val notification = NotificationCompat.Builder(this, CHANNEL_ID) - .setContentTitle("Golang Server") - .setContentText("Server đang chạy") + .setContentTitle("FireflyGO Server") + .setContentText("FireflyGO is running...") .setSmallIcon(R.drawable.ic_launcher_foreground) .setContentIntent(pendingIntent) .build() - // 3. Chạy foreground startForeground(NOTIFICATION_ID, notification) - // 4. Giữ CPU không sleep (tùy chọn, nhưng hữu ích) try { val powerManager = getSystemService(POWER_SERVICE) as PowerManager wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "GolangServer::WakeLock") @@ -61,7 +66,6 @@ class GolangServerService : Service() { Log.e(TAG, "❌ WakeLock failed", e) } - // 5. Chạy server trong thread riêng Thread { try { val appDataPath = intent?.getStringExtra("appDataPath") @@ -69,16 +73,23 @@ class GolangServerService : Service() { Libandroid.setPathDataLocal(appDataPath) Log.d(TAG, "✅ Set path data: $appDataPath") } else { + isRunning = false Log.e(TAG, "❌ appDataPath not received in intent") + stopSelf() + return@Thread } Libandroid.setServerRunning(true) + isRunning = true Log.d(TAG, "✅ Server started") } catch (e: Exception) { + isRunning = false Log.e(TAG, "❌ Error starting server", e) + stopSelf() } }.start() + return START_STICKY } @@ -89,6 +100,7 @@ class GolangServerService : Service() { // 1. Tắt server try { val result = Libandroid.setServerRunning(false) + isRunning = false Log.d(TAG, "Server shutdown result: $result") } catch (e: Exception) { Log.e(TAG, "Error shutting down server", e) @@ -105,6 +117,7 @@ class GolangServerService : Service() { } catch (e: Exception) { Log.e(TAG, "❌ Failed to release WakeLock", e) } + isRunning = false } override fun onBind(intent: Intent?): IBinder? = null diff --git a/app/src/main/java/com/example/fireflypsandorid/MainActivity.kt b/app/src/main/java/com/example/fireflypsandorid/MainActivity.kt index ac484f9..dbfbdc1 100644 --- a/app/src/main/java/com/example/fireflypsandorid/MainActivity.kt +++ b/app/src/main/java/com/example/fireflypsandorid/MainActivity.kt @@ -72,9 +72,8 @@ class MainActivity : ComponentActivity() { @Composable fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) { val context = LocalContext.current - var isServerRunning by remember { mutableStateOf(false) } - - val serverImage = if (isServerRunning) + val isRunning = GolangServerService.isRunning + val serverImage = if (isRunning) painterResource(id = R.drawable.server_running) else painterResource(id = R.drawable.server_stopped) @@ -115,8 +114,7 @@ fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) { Button( onClick = { try { - isServerRunning = !isServerRunning - if (isServerRunning) { + if (!isRunning) { val intent = Intent(context, GolangServerService::class.java) intent.putExtra("appDataPath", appDataPath) context.startService(intent) @@ -128,7 +126,7 @@ fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) { } }, colors = ButtonDefaults.buttonColors( - containerColor = if (isServerRunning) Color(0xFFB71C1C) else Color(0xFF2196F3), + containerColor = if (isRunning) Color(0xFFB71C1C) else Color(0xFF2196F3), contentColor = Color.White ), shape = RoundedCornerShape(12.dp), @@ -137,7 +135,7 @@ fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) { .height(50.dp) ) { Text( - text = if (isServerRunning) "Stop Server" else "Start Server", + text = if (isRunning) "Stop Server" else "Start Server", fontSize = 20.sp ) } @@ -146,9 +144,9 @@ fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) { // Server status text Text( - text = if (isServerRunning) "Server is running" else "Server is stopped", + text = if (isRunning) "Server is running" else "Server is stopped", fontSize = 24.sp, - color = if (isServerRunning) Color(0xFF4CAF50) else Color.Gray + color = if (isRunning) Color(0xFF4CAF50) else Color.Gray ) Spacer(modifier = Modifier.height(24.dp))