Android: Update game list on setTitle()

This fixes a bug where newly installed game does not appear in the game
list after quitting the first game run.
This commit is contained in:
kichikuou
2020-02-01 11:06:02 +09:00
parent e5f1036b6b
commit b7bbbaee61
3 changed files with 37 additions and 25 deletions
@@ -68,6 +68,7 @@ class GameActivity : SDLActivity() {
if (str.isNullOrEmpty())
return
File(gameRoot, Launcher.TITLE_FILE).writeText(str)
Launcher.updateGameList()
}
// These functions are called in the SDL thread by JNI.
@@ -30,6 +30,7 @@ import java.util.zip.ZipInputStream
private var gLauncher: Launcher? = null
interface LauncherObserver {
fun onGameListChange()
fun onInstallProgress(path: String)
fun onInstallSuccess(path: File)
fun onInstallFailure(msgId: Int)
@@ -50,6 +51,10 @@ class Launcher private constructor(private val rootDir: File) {
}
return gLauncher!!
}
fun updateGameList() {
gLauncher?.updateGameList()
}
}
data class Entry(val path: File, val title: String)
@@ -61,25 +66,7 @@ class Launcher private constructor(private val rootDir: File) {
private set
init {
var saveDirFound = false
for (path in rootDir.listFiles()) {
if (!path.isDirectory)
continue
if (path.name == "save") {
saveDirFound = true
continue
}
try {
val title = File(path, TITLE_FILE).readText()
games.add(Entry(path, title))
} catch (e: IOException) {
// Incomplete game installation. Delete it.
path.deleteRecursively()
}
}
if (!saveDirFound) {
File(rootDir, "save").mkdir()
}
updateGameList()
}
fun install(input: InputStream) {
@@ -112,6 +99,29 @@ class Launcher private constructor(private val rootDir: File) {
fun uninstall(id: Int) {
games[id].path.deleteRecursively()
games.removeAt(id)
observer?.onGameListChange()
}
private fun updateGameList() {
var saveDirFound = false
for (path in rootDir.listFiles()) {
if (!path.isDirectory)
continue
if (path.name == "save") {
saveDirFound = true
continue
}
try {
val title = File(path, TITLE_FILE).readText()
games.add(Entry(path, title))
} catch (e: IOException) {
// Incomplete game installation. Delete it.
path.deleteRecursively()
}
}
if (!saveDirFound) {
File(rootDir, "save").mkdir()
}
}
private fun createDirForGame(): File {
@@ -28,7 +28,6 @@ import java.io.*
class LauncherActivity : ListActivity(), AdapterView.OnItemLongClickListener, LauncherObserver {
private lateinit var launcher: Launcher
private lateinit var adapter: ArrayAdapter<String>
private var progressDialog: ProgressDialogFragment? = null
override fun onCreate(savedInstanceState: Bundle?) {
@@ -40,10 +39,7 @@ class LauncherActivity : ListActivity(), AdapterView.OnItemLongClickListener, La
showProgressDialog()
}
val items = launcher.titles.toMutableList()
items.add(getString(R.string.install_from_zip))
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, items)
listAdapter = adapter
onGameListChange()
listView.onItemLongClickListener = this
}
@@ -83,6 +79,12 @@ class LauncherActivity : ListActivity(), AdapterView.OnItemLongClickListener, La
launcher.install(input)
}
override fun onGameListChange() {
val items = launcher.titles.toMutableList()
items.add(getString(R.string.install_from_zip))
listAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, items)
}
override fun onInstallProgress(path: String) {
progressDialog?.setProgress(getString(R.string.install_progress, path))
}
@@ -106,7 +108,6 @@ class LauncherActivity : ListActivity(), AdapterView.OnItemLongClickListener, La
private fun uninstall(id: Int) {
launcher.uninstall(id)
adapter.remove(adapter.getItem(id))
}
private fun showProgressDialog() {