2019-12-16 02:28:34 +08:00
|
|
|
package com.unciv.app
|
|
|
|
|
2020-09-20 13:22:07 -07:00
|
|
|
import android.content.Intent
|
2019-12-16 02:28:34 +08:00
|
|
|
import android.os.Bundle
|
2020-02-17 17:34:46 +01:00
|
|
|
import androidx.core.app.NotificationManagerCompat
|
|
|
|
import androidx.work.WorkManager
|
2019-12-16 02:28:34 +08:00
|
|
|
import com.badlogic.gdx.backends.android.AndroidApplication
|
|
|
|
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
|
2023-01-18 19:28:16 +02:00
|
|
|
import com.unciv.logic.files.UncivFiles
|
2023-10-08 21:03:53 +02:00
|
|
|
import com.unciv.ui.components.fonts.Fonts
|
2023-03-04 18:22:09 +01:00
|
|
|
import com.unciv.utils.Display
|
2022-05-27 12:45:13 +02:00
|
|
|
import com.unciv.utils.Log
|
2020-02-09 02:28:31 -06:00
|
|
|
import java.io.File
|
2019-12-16 02:28:34 +08:00
|
|
|
|
2020-12-01 23:26:01 +02:00
|
|
|
open class AndroidLauncher : AndroidApplication() {
|
2023-02-28 17:56:57 +01:00
|
|
|
|
2023-03-04 18:22:09 +01:00
|
|
|
private var game: AndroidGame? = null
|
2023-02-28 17:56:57 +01:00
|
|
|
|
2019-12-16 02:28:34 +08:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2023-02-28 17:56:57 +01:00
|
|
|
|
|
|
|
// Setup Android logging
|
2023-11-13 21:26:38 +01:00
|
|
|
Log.backend = AndroidLogBackend(this)
|
2020-02-09 02:28:31 -06:00
|
|
|
|
2023-03-04 18:22:09 +01:00
|
|
|
// Setup Android display
|
2023-11-18 21:38:36 +01:00
|
|
|
val displayImpl = AndroidDisplay(this)
|
|
|
|
Display.platform = displayImpl
|
2023-03-04 18:22:09 +01:00
|
|
|
|
2023-02-28 17:56:57 +01:00
|
|
|
// Setup Android fonts
|
|
|
|
Fonts.fontImplementation = AndroidFont()
|
2021-05-19 22:27:23 +02:00
|
|
|
|
2023-02-28 17:56:57 +01:00
|
|
|
// Setup Android custom saver-loader
|
|
|
|
UncivFiles.saverLoader = AndroidSaverLoader(this)
|
|
|
|
UncivFiles.preferExternalStorage = true
|
2022-03-21 14:12:16 -05:00
|
|
|
|
2023-11-18 21:38:36 +01:00
|
|
|
val settings = UncivFiles.getSettingsForPlatformLaunchers(filesDir.path)
|
2023-12-25 20:04:34 +02:00
|
|
|
val config = AndroidApplicationConfiguration().apply { useImmersiveMode = settings.androidHideSystemUi }
|
2023-11-18 21:38:36 +01:00
|
|
|
|
|
|
|
// Setup orientation, immersive mode and display cutout
|
|
|
|
displayImpl.setOrientation(settings.displayOrientation)
|
|
|
|
displayImpl.setCutoutFromUiThread(settings.androidCutout)
|
|
|
|
|
2023-02-28 17:56:57 +01:00
|
|
|
// Create notification channels for Multiplayer notificator
|
|
|
|
MultiplayerTurnCheckWorker.createNotificationChannels(applicationContext)
|
2022-03-31 22:03:57 +02:00
|
|
|
|
2023-02-28 17:56:57 +01:00
|
|
|
copyMods()
|
2022-03-21 14:12:16 -05:00
|
|
|
|
2023-03-16 20:48:24 +02:00
|
|
|
game = AndroidGame(this)
|
2019-12-23 23:12:35 +03:00
|
|
|
initialize(game, config)
|
2022-03-05 19:00:56 +01:00
|
|
|
|
2023-03-06 09:35:14 +01:00
|
|
|
game!!.setDeepLinkedGame(intent)
|
|
|
|
game!!.addScreenObscuredListener()
|
2019-12-16 02:28:34 +08:00
|
|
|
}
|
2020-02-09 02:28:31 -06:00
|
|
|
|
2021-04-21 22:12:46 +03:00
|
|
|
/**
|
|
|
|
* Copies mods from external data directory (where users can access) to the private one (where
|
|
|
|
* libGDX reads from). Note: deletes all files currently in the private mod directory and
|
|
|
|
* replaces them with the ones in the external folder!)
|
|
|
|
*/
|
|
|
|
private fun copyMods() {
|
2020-10-07 14:02:38 +03:00
|
|
|
// Mod directory in the internal app data (where Gdx.files.local looks)
|
|
|
|
val internalModsDir = File("${filesDir.path}/mods")
|
2020-02-09 02:28:31 -06:00
|
|
|
|
2020-10-07 14:02:38 +03:00
|
|
|
// Mod directory in the shared app data (where the user can see and modify)
|
2023-06-25 08:38:18 +02:00
|
|
|
val externalPath = getExternalFilesDir(null)?.path ?: return
|
|
|
|
val externalModsDir = File("$externalPath/mods")
|
2020-02-09 02:28:31 -06:00
|
|
|
|
2020-10-07 14:02:38 +03:00
|
|
|
// Copy external mod directory (with data user put in it) to internal (where it can be read)
|
|
|
|
if (!externalModsDir.exists()) externalModsDir.mkdirs() // this can fail sometimes, which is why we check if it exists again in the next line
|
|
|
|
if (externalModsDir.exists()) externalModsDir.copyRecursively(internalModsDir, true)
|
|
|
|
}
|
2020-02-17 17:34:46 +01:00
|
|
|
|
|
|
|
override fun onPause() {
|
2023-03-06 09:35:14 +01:00
|
|
|
val game = this.game!!
|
2023-06-25 08:03:44 +02:00
|
|
|
if (game.isInitializedProxy()
|
2023-03-06 09:35:14 +01:00
|
|
|
&& game.gameInfo != null
|
|
|
|
&& game.settings.multiplayer.turnCheckerEnabled
|
|
|
|
&& game.files.getMultiplayerSaves().any()
|
2022-05-27 15:53:18 +02:00
|
|
|
) {
|
|
|
|
MultiplayerTurnCheckWorker.startTurnChecker(
|
2023-03-06 09:35:14 +01:00
|
|
|
applicationContext, game.files, game.gameInfo!!, game.settings.multiplayer)
|
2020-02-17 17:34:46 +01:00
|
|
|
}
|
|
|
|
super.onPause()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
2023-03-06 09:35:14 +01:00
|
|
|
try {
|
2020-06-27 23:57:22 +03:00
|
|
|
WorkManager.getInstance(applicationContext).cancelAllWorkByTag(MultiplayerTurnCheckWorker.WORK_TAG)
|
|
|
|
with(NotificationManagerCompat.from(this)) {
|
|
|
|
cancel(MultiplayerTurnCheckWorker.NOTIFICATION_ID_INFO)
|
|
|
|
cancel(MultiplayerTurnCheckWorker.NOTIFICATION_ID_SERVICE)
|
|
|
|
}
|
2023-03-06 09:35:14 +01:00
|
|
|
} catch (ignore: Exception) {
|
|
|
|
/* Sometimes this fails for no apparent reason - the multiplayer checker failing to
|
|
|
|
cancel should not be enough of a reason for the game to crash! */
|
2020-02-17 17:34:46 +01:00
|
|
|
}
|
|
|
|
super.onResume()
|
|
|
|
}
|
2020-09-20 13:22:07 -07:00
|
|
|
|
2022-03-05 19:00:56 +01:00
|
|
|
override fun onNewIntent(intent: Intent?) {
|
|
|
|
super.onNewIntent(intent)
|
|
|
|
if (intent == null)
|
|
|
|
return
|
2023-03-06 09:35:14 +01:00
|
|
|
game?.setDeepLinkedGame(intent)
|
2022-03-05 19:00:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
2023-02-28 17:56:57 +01:00
|
|
|
val saverLoader = UncivFiles.saverLoader as AndroidSaverLoader
|
|
|
|
saverLoader.onActivityResult(requestCode, data)
|
2020-09-20 13:22:07 -07:00
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
}
|
2020-12-01 23:26:01 +02:00
|
|
|
}
|