From c2bd81fc757f76bab83dc38a597347ca87a7b4e1 Mon Sep 17 00:00:00 2001 From: AzenKain Date: Thu, 25 Jun 2026 23:55:16 +0700 Subject: [PATCH] feat: implement authentication management and deep link handling in MainActivity and GolangServerService --- app/build.gradle.kts | 3 - app/src/main/AndroidManifest.xml | 82 +------- .../firefly_go_android/GolangServerService.kt | 176 ------------------ .../firefly_go_android/MainActivity.kt | 41 +--- .../res/drawable/ic_launcher_background.xml | 77 -------- .../res/mipmap-anydpi-v26/ic_launcher.xml | 7 - .../mipmap-anydpi-v26/ic_launcher_round.xml | 7 - app/src/main/res/values/strings.xml | 3 - app/src/main/res/xml/file_paths.xml | 6 - gradle/wrapper/gradle-wrapper.properties | 9 - 10 files changed, 11 insertions(+), 400 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 884a392..1860322 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -83,15 +83,12 @@ dependencies { // Auto updater library implementation(libs.autoupdater) -<<<<<<< HEAD -======= // OkHttp Client implementation("com.squareup.okhttp3:okhttp:4.12.0") // Chrome Custom Tabs implementation("androidx.browser:browser:1.8.0") ->>>>>>> 2459650 (feat: v4) // Unit Test testImplementation(libs.junit) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8d44fbb..09d9bf0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,4 +1,3 @@ -<<<<<<< HEAD @@ -35,11 +34,19 @@ + + + + + + -======= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ->>>>>>> 2459650 (feat: v4) \ No newline at end of file diff --git a/app/src/main/java/com/example/firefly_go_android/GolangServerService.kt b/app/src/main/java/com/example/firefly_go_android/GolangServerService.kt index 33a42ff..a862aaf 100644 --- a/app/src/main/java/com/example/firefly_go_android/GolangServerService.kt +++ b/app/src/main/java/com/example/firefly_go_android/GolangServerService.kt @@ -1,4 +1,3 @@ -<<<<<<< HEAD package com.example.firefly_go_android import android.annotation.SuppressLint @@ -172,179 +171,4 @@ class GolangServerService : Service() { Log.d(TAG, "Notification channel created") } } -======= -package com.example.firefly_go_android - -import android.annotation.SuppressLint -import android.app.NotificationChannel -import android.app.NotificationManager -import android.app.PendingIntent -import android.app.Service -import android.content.Intent -import android.content.pm.ServiceInfo -import android.graphics.BitmapFactory -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 androidx.core.content.ContextCompat -import libandroid.Libandroid -import androidx.core.content.edit - -class GolangServerService : Service() { - - companion object { - 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 - - override fun onCreate() { - super.onCreate() - createNotificationChannel() - } - - @SuppressLint("WakelockTimeout") - 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") - - val notificationIntent = Intent(this, MainActivity::class.java) - val pendingIntent = PendingIntent.getActivity( - this, - 0, - notificationIntent, - PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE - ) - val largeIcon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher) - - val notification = NotificationCompat.Builder(this, CHANNEL_ID) - .setSmallIcon(R.mipmap.ic_launcher_round) - .setLargeIcon(largeIcon) - .setContentTitle("FireflyGO Server") - .setContentText("Server is running...") - .setColor(ContextCompat.getColor(this, R.color.teal_700)) - .setOngoing(true) - .setOnlyAlertOnce(true) - .setContentIntent(pendingIntent) - .setPriority(NotificationCompat.PRIORITY_DEFAULT) - .setShowWhen(false) - .setCategory(NotificationCompat.CATEGORY_SERVICE) - .build() - - if (Build.VERSION.SDK_INT >= 34) { - startForeground( - NOTIFICATION_ID, - notification, - ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE - ) - } else if (Build.VERSION.SDK_INT >= 29) { - startForeground( - NOTIFICATION_ID, - notification, - ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST - ) - } else { - startForeground(NOTIFICATION_ID, notification) - } - - // Khởi tạo WakeLock và WifiLock - try { - val powerManager = getSystemService(POWER_SERVICE) as PowerManager - wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "GolangServer::WakeLock") - wakeLock?.acquire() - Log.d(TAG, "WakeLock acquired") - } catch (e: Exception) { - Log.e(TAG, "Lock failed", e) - } - - Thread { - try { - val sharedPrefs = getSharedPreferences("AppPrefs", MODE_PRIVATE) - var appDataPath = intent?.getStringExtra("appDataPath") - - if (appDataPath != null) { - sharedPrefs.edit { putString("saved_app_data_path", appDataPath) } - } else { - appDataPath = sharedPrefs.getString("saved_app_data_path", null) - } - - if (appDataPath != null) { - Libandroid.setPathDataLocal(appDataPath) - Log.d(TAG, "Set path data: $appDataPath") - } else { - isRunning = false - Log.e(TAG, "appDataPath not received and not found in SharedPreferences") - 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 - } - - override fun onDestroy() { - super.onDestroy() - Log.d(TAG, "onDestroy called") - - 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) - } - - // Nhả các khóa tài nguyên - try { - wakeLock?.let { - if (it.isHeld) { - it.release() - Log.d(TAG, "WakeLock released") - } - } - } catch (e: Exception) { - Log.e(TAG, "Failed to release Locks", e) - } - isRunning = false - } - - override fun onBind(intent: Intent?): IBinder? = null - - private fun createNotificationChannel() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - val channel = NotificationChannel( - CHANNEL_ID, - "Golang Server Channel", - NotificationManager.IMPORTANCE_LOW - ).apply { - description = "Channel for running Golang backend in foreground" - } - - val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager - manager.createNotificationChannel(channel) - Log.d(TAG, "Notification channel created") - } - } ->>>>>>> 2459650 (feat: v4) } \ No newline at end of file diff --git a/app/src/main/java/com/example/firefly_go_android/MainActivity.kt b/app/src/main/java/com/example/firefly_go_android/MainActivity.kt index d5a196d..bff8d4b 100644 --- a/app/src/main/java/com/example/firefly_go_android/MainActivity.kt +++ b/app/src/main/java/com/example/firefly_go_android/MainActivity.kt @@ -33,16 +33,11 @@ import androidx.compose.animation.* import androidx.compose.animation.core.* import androidx.compose.foundation.background import androidx.compose.foundation.clickable -<<<<<<< HEAD -import androidx.compose.foundation.lazy.LazyColumn -import androidx.compose.foundation.lazy.rememberLazyListState -======= import androidx.compose.foundation.border import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.collectIsPressedAsState ->>>>>>> 2459650 (feat: v4) import androidx.compose.foundation.shape.CircleShape import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.BugReport @@ -82,14 +77,11 @@ import androidx.core.content.ContextCompat import androidx.core.net.toUri import android.provider.Settings -<<<<<<< HEAD -======= import com.example.firefly_go_android.network.AuthManager import com.example.firefly_go_android.ui.ScamWarningDialog import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.launch ->>>>>>> 2459650 (feat: v4) data class AppVersion( val latestVersion: String, val changelog: String, @@ -97,33 +89,23 @@ data class AppVersion( ) class MainActivity : ComponentActivity() { -<<<<<<< HEAD -======= private lateinit var authManager: AuthManager private var onAuthStatusChanged: (() -> Unit)? = null ->>>>>>> 2459650 (feat: v4) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestBatteryExemption(this) requestInstallPermission(this) requestStoragePermission(this) -<<<<<<< HEAD -======= authManager = AuthManager(this) ->>>>>>> 2459650 (feat: v4) val appDataPath = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "FireflyGo").absolutePath val dataDir = File("$appDataPath/data") if (!dataDir.exists()) dataDir.mkdirs() val sharedPrefs = getSharedPreferences("AppPrefs", MODE_PRIVATE) -<<<<<<< HEAD - // Lấy thông tin Package -======= ->>>>>>> 2459650 (feat: v4) val packageInfo = if (Build.VERSION.SDK_INT >= 33) { packageManager.getPackageInfo(packageName, android.content.pm.PackageManager.PackageInfoFlags.of(0)) } else { @@ -172,15 +154,6 @@ class MainActivity : ComponentActivity() { val appVersion = AppVersion(latestVersion, changelog, apkUrl) -<<<<<<< HEAD - enableEdgeToEdge() - setContent { - FireflyPsAndoridTheme { - Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> - Box(modifier = Modifier.fillMaxSize()) { - ServerControlScreen(appDataPath, dataDir, appVersion, Modifier.padding(innerPadding)) - AutoUpdateDialog(onDismiss = {}, appVersion, dataDir, true) -======= handleDeepLink(intent) enableEdgeToEdge() @@ -223,7 +196,6 @@ class MainActivity : ComponentActivity() { if (showScamWarning) { ScamWarningDialog(onDismiss = { showScamWarning = false }) } ->>>>>>> 2459650 (feat: v4) } } } @@ -231,8 +203,6 @@ class MainActivity : ComponentActivity() { } -<<<<<<< HEAD -======= override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) handleDeepLink(intent) @@ -257,7 +227,6 @@ class MainActivity : ComponentActivity() { } } ->>>>>>> 2459650 (feat: v4) } @SuppressLint("BatteryLife") @@ -304,10 +273,7 @@ fun requestStoragePermission(context: Context) { } } } -<<<<<<< HEAD -======= ->>>>>>> 2459650 (feat: v4) fun copyRawToFile(context: Context, targetDir: File, override: Boolean = false): Boolean { val files = listOf( "data-in-game.json" to "data-in-game.json", @@ -364,10 +330,9 @@ fun removeFile(targetDir: File, fileName: String): Boolean { } -<<<<<<< HEAD @SuppressLint("ImplicitSamInstance") @Composable -fun ServerControlScreen(appDataPath: String, dataDir: File, appVersion: AppVersion, modifier: Modifier = Modifier) { +fun ServerControlScreen(appDataPath: String, dataDir: File, appVersion: AppVersion, authManager: AuthManager, modifier: Modifier = Modifier) { val context = LocalContext.current val isRunning = GolangServerService.isRunning @@ -1110,7 +1075,8 @@ fun ActionButtons( } } } -======= +} + @Composable fun Modifier.bounceClick( interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, @@ -1132,5 +1098,4 @@ fun Modifier.bounceClick( indication = null, onClick = onClick ) ->>>>>>> 2459650 (feat: v4) } diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml index c7b0017..ca3826a 100644 --- a/app/src/main/res/drawable/ic_launcher_background.xml +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -1,4 +1,3 @@ -<<<<<<< HEAD -======= - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ->>>>>>> 2459650 (feat: v4) diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml index 16c5094..c4a603d 100644 --- a/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml @@ -1,12 +1,5 @@ -<<<<<<< HEAD -======= - - - - ->>>>>>> 2459650 (feat: v4) \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml index 16c5094..c4a603d 100644 --- a/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml +++ b/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml @@ -1,12 +1,5 @@ -<<<<<<< HEAD -======= - - - - ->>>>>>> 2459650 (feat: v4) \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8f619ca..7fd17a6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,7 +1,5 @@ Firefly Go -<<<<<<< HEAD -======= Android Edition Sign In Server is running @@ -56,5 +54,4 @@ Auto-Scroll Clear Close ->>>>>>> 2459650 (feat: v4) \ No newline at end of file diff --git a/app/src/main/res/xml/file_paths.xml b/app/src/main/res/xml/file_paths.xml index 6f20394..59daf6a 100644 --- a/app/src/main/res/xml/file_paths.xml +++ b/app/src/main/res/xml/file_paths.xml @@ -1,9 +1,3 @@ -<<<<<<< HEAD -======= - - - ->>>>>>> 2459650 (feat: v4) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 614ef18..0e07c39 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,15 +1,6 @@ -<<<<<<< HEAD #Wed Oct 08 15:20:16 ICT 2025 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -======= -#Wed Oct 08 15:20:16 ICT 2025 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists ->>>>>>> 2459650 (feat: v4)