2019-12-16 02:28:34 +08:00
|
|
|
package com.unciv.app
|
|
|
|
|
2020-02-09 02:28:31 -06:00
|
|
|
import android.os.Build
|
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
|
|
|
|
import com.unciv.UncivGame
|
2020-02-09 02:28:31 -06:00
|
|
|
import java.io.File
|
2019-12-16 02:28:34 +08:00
|
|
|
|
|
|
|
class AndroidLauncher : AndroidApplication() {
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2020-02-23 19:45:25 +01:00
|
|
|
MultiplayerTurnCheckWorker.createNotificationChannels(applicationContext)
|
2020-02-09 02:28:31 -06:00
|
|
|
|
|
|
|
// Only allow mods on KK+, to avoid READ_EXTERNAL_STORAGE permission earlier versions need
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
|
copyMods()
|
|
|
|
}
|
|
|
|
|
2019-12-23 23:12:35 +03:00
|
|
|
val config = AndroidApplicationConfiguration().apply { useImmersiveMode = true }
|
2020-04-08 14:55:00 +02:00
|
|
|
val game = UncivGame (
|
|
|
|
version = BuildConfig.VERSION_NAME,
|
|
|
|
crashReportSender = CrashReportSenderAndroid(this),
|
2020-05-18 23:09:38 +03:00
|
|
|
exitEvent = this::finish,
|
|
|
|
fontImplementation = NativeFontAndroid(45)
|
2020-04-08 14:55:00 +02:00
|
|
|
)
|
2019-12-23 23:12:35 +03:00
|
|
|
initialize(game, config)
|
2019-12-16 02:28:34 +08:00
|
|
|
}
|
2020-02-09 02:28:31 -06: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() {
|
|
|
|
// Mod directory in the internal app data (where Gdx.files.local looks)
|
|
|
|
val internalModsDir = File("${filesDir.path}/mods")
|
|
|
|
|
|
|
|
// Mod directory in the shared app data (where the user can see and modify)
|
|
|
|
val externalModsDir = File("${getExternalFilesDir(null)?.path}/mods")
|
|
|
|
|
|
|
|
// Empty out the mods directory so it can be replaced by the external one
|
|
|
|
// Done to ensure it only contains mods in the external dir (so users can delete some)
|
|
|
|
if (internalModsDir.exists()) internalModsDir.deleteRecursively()
|
|
|
|
|
|
|
|
// Copy external mod directory (with data user put in it) to internal (where it can be read)
|
2020-04-27 19:23:59 +03:00
|
|
|
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)
|
2020-02-09 02:28:31 -06:00
|
|
|
}
|
2020-02-17 17:34:46 +01:00
|
|
|
|
|
|
|
override fun onPause() {
|
2020-02-19 12:18:40 +01:00
|
|
|
if (UncivGame.Companion.isCurrentInitialized()
|
2020-02-17 17:34:46 +01:00
|
|
|
&& UncivGame.Current.isGameInfoInitialized()
|
2020-02-22 21:49:08 +02:00
|
|
|
&& UncivGame.Current.settings.multiplayerTurnCheckerEnabled
|
2020-02-17 17:34:46 +01:00
|
|
|
&& UncivGame.Current.gameInfo.gameParameters.isOnlineMultiplayer) {
|
|
|
|
MultiplayerTurnCheckWorker.startTurnChecker(applicationContext, UncivGame.Current.gameInfo, UncivGame.Current.settings)
|
|
|
|
}
|
|
|
|
super.onPause()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
WorkManager.getInstance(applicationContext).cancelAllWorkByTag(MultiplayerTurnCheckWorker.WORK_TAG)
|
|
|
|
with(NotificationManagerCompat.from(this)) {
|
|
|
|
cancel(MultiplayerTurnCheckWorker.NOTIFICATION_ID_INFO)
|
|
|
|
cancel(MultiplayerTurnCheckWorker.NOTIFICATION_ID_SERVICE)
|
|
|
|
}
|
|
|
|
super.onResume()
|
|
|
|
}
|
2019-12-16 02:28:34 +08:00
|
|
|
}
|