Files
Unciv/android/src/com/unciv/app/AndroidLauncher.kt

76 lines
3.1 KiB
Kotlin
Raw Normal View History

2019-12-16 02:28:34 +08:00
package com.unciv.app
import android.os.Build
2019-12-16 02:28:34 +08:00
import android.os.Bundle
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
import java.io.File
2019-12-16 02:28:34 +08:00
class AndroidLauncher : AndroidApplication() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
createNotificationChannels()
// Only allow mods on KK+, to avoid READ_EXTERNAL_STORAGE permission earlier versions need
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
copyMods()
}
val config = AndroidApplicationConfiguration().apply { useImmersiveMode = true }
val game = UncivGame(BuildConfig.VERSION_NAME,
CrashReportSenderAndroid(this))
{this.finish()}
initialize(game, config)
2019-12-16 02:28:34 +08:00
}
/**
* Necessary for Multiplayer Turner Checker, starting with Android Oreo
*/
private fun createNotificationChannels() {
MultiplayerTurnCheckWorker.createNotificationChannelInfo(context)
MultiplayerTurnCheckWorker.createNotificationChannelService(context)
}
/**
* 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)
if (!externalModsDir.exists()) externalModsDir.mkdirs()
externalModsDir.copyRecursively(internalModsDir)
}
override fun onPause() {
if (UncivGame.Companion.isCurrentInitialized()
&& UncivGame.Current.settings.multiplayerTurnCheckerEnabled
&& UncivGame.Current.isGameInfoInitialized()
&& 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
}