FIX: app runing state bug

This commit is contained in:
2025-08-06 19:09:41 +07:00
parent bdd458a64f
commit b7b0457685
2 changed files with 27 additions and 16 deletions

View File

@@ -10,6 +10,9 @@ import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.os.PowerManager import android.os.PowerManager
import android.util.Log 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 androidx.core.app.NotificationCompat
import libandroid.Libandroid import libandroid.Libandroid
@@ -19,6 +22,7 @@ class GolangServerService : Service() {
const val CHANNEL_ID = "GolangServerChannel" const val CHANNEL_ID = "GolangServerChannel"
const val NOTIFICATION_ID = 1 const val NOTIFICATION_ID = 1
private const val TAG = "GolangServerService" private const val TAG = "GolangServerService"
var isRunning by mutableStateOf(false)
} }
private var wakeLock: PowerManager.WakeLock? = null private var wakeLock: PowerManager.WakeLock? = null
@@ -29,9 +33,13 @@ class GolangServerService : Service() {
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { 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") 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 notificationIntent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity( val pendingIntent = PendingIntent.getActivity(
this, this,
@@ -40,18 +48,15 @@ class GolangServerService : Service() {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
) )
// 2. Tạo notification
val notification = NotificationCompat.Builder(this, CHANNEL_ID) val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Golang Server") .setContentTitle("FireflyGO Server")
.setContentText("Server đang chạy") .setContentText("FireflyGO is running...")
.setSmallIcon(R.drawable.ic_launcher_foreground) .setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentIntent(pendingIntent) .setContentIntent(pendingIntent)
.build() .build()
// 3. Chạy foreground
startForeground(NOTIFICATION_ID, notification) startForeground(NOTIFICATION_ID, notification)
// 4. Giữ CPU không sleep (tùy chọn, nhưng hữu ích)
try { try {
val powerManager = getSystemService(POWER_SERVICE) as PowerManager val powerManager = getSystemService(POWER_SERVICE) as PowerManager
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "GolangServer::WakeLock") wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "GolangServer::WakeLock")
@@ -61,7 +66,6 @@ class GolangServerService : Service() {
Log.e(TAG, "❌ WakeLock failed", e) Log.e(TAG, "❌ WakeLock failed", e)
} }
// 5. Chạy server trong thread riêng
Thread { Thread {
try { try {
val appDataPath = intent?.getStringExtra("appDataPath") val appDataPath = intent?.getStringExtra("appDataPath")
@@ -69,16 +73,23 @@ class GolangServerService : Service() {
Libandroid.setPathDataLocal(appDataPath) Libandroid.setPathDataLocal(appDataPath)
Log.d(TAG, "✅ Set path data: $appDataPath") Log.d(TAG, "✅ Set path data: $appDataPath")
} else { } else {
isRunning = false
Log.e(TAG, "❌ appDataPath not received in intent") Log.e(TAG, "❌ appDataPath not received in intent")
stopSelf()
return@Thread
} }
Libandroid.setServerRunning(true) Libandroid.setServerRunning(true)
isRunning = true
Log.d(TAG, "✅ Server started") Log.d(TAG, "✅ Server started")
} catch (e: Exception) { } catch (e: Exception) {
isRunning = false
Log.e(TAG, "❌ Error starting server", e) Log.e(TAG, "❌ Error starting server", e)
stopSelf()
} }
}.start() }.start()
return START_STICKY return START_STICKY
} }
@@ -89,6 +100,7 @@ class GolangServerService : Service() {
// 1. Tắt server // 1. Tắt server
try { try {
val result = Libandroid.setServerRunning(false) val result = Libandroid.setServerRunning(false)
isRunning = false
Log.d(TAG, "Server shutdown result: $result") Log.d(TAG, "Server shutdown result: $result")
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Error shutting down server", e) Log.e(TAG, "Error shutting down server", e)
@@ -105,6 +117,7 @@ class GolangServerService : Service() {
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "❌ Failed to release WakeLock", e) Log.e(TAG, "❌ Failed to release WakeLock", e)
} }
isRunning = false
} }
override fun onBind(intent: Intent?): IBinder? = null override fun onBind(intent: Intent?): IBinder? = null

View File

@@ -72,9 +72,8 @@ class MainActivity : ComponentActivity() {
@Composable @Composable
fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) { fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) {
val context = LocalContext.current val context = LocalContext.current
var isServerRunning by remember { mutableStateOf(false) } val isRunning = GolangServerService.isRunning
val serverImage = if (isRunning)
val serverImage = if (isServerRunning)
painterResource(id = R.drawable.server_running) painterResource(id = R.drawable.server_running)
else else
painterResource(id = R.drawable.server_stopped) painterResource(id = R.drawable.server_stopped)
@@ -115,8 +114,7 @@ fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) {
Button( Button(
onClick = { onClick = {
try { try {
isServerRunning = !isServerRunning if (!isRunning) {
if (isServerRunning) {
val intent = Intent(context, GolangServerService::class.java) val intent = Intent(context, GolangServerService::class.java)
intent.putExtra("appDataPath", appDataPath) intent.putExtra("appDataPath", appDataPath)
context.startService(intent) context.startService(intent)
@@ -128,7 +126,7 @@ fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) {
} }
}, },
colors = ButtonDefaults.buttonColors( colors = ButtonDefaults.buttonColors(
containerColor = if (isServerRunning) Color(0xFFB71C1C) else Color(0xFF2196F3), containerColor = if (isRunning) Color(0xFFB71C1C) else Color(0xFF2196F3),
contentColor = Color.White contentColor = Color.White
), ),
shape = RoundedCornerShape(12.dp), shape = RoundedCornerShape(12.dp),
@@ -137,7 +135,7 @@ fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) {
.height(50.dp) .height(50.dp)
) { ) {
Text( Text(
text = if (isServerRunning) "Stop Server" else "Start Server", text = if (isRunning) "Stop Server" else "Start Server",
fontSize = 20.sp fontSize = 20.sp
) )
} }
@@ -146,9 +144,9 @@ fun ServerControlScreen(appDataPath: String, modifier: Modifier = Modifier) {
// Server status text // Server status text
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, 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)) Spacer(modifier = Modifier.height(24.dp))