2020-03-27 20:36:02 +01:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
|
|
|
|
2019-12-02 14:39:08 +01:00
|
|
|
package emu.skyline
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.net.Uri
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.util.Log
|
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.AdapterView
|
|
|
|
import android.widget.AdapterView.OnItemClickListener
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2019-12-10 21:51:02 +01:00
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
2019-12-02 14:39:08 +01:00
|
|
|
import androidx.appcompat.widget.SearchView
|
|
|
|
import androidx.documentfile.provider.DocumentFile
|
|
|
|
import androidx.preference.PreferenceManager
|
|
|
|
import com.google.android.material.snackbar.Snackbar
|
2020-04-03 13:47:32 +02:00
|
|
|
import emu.skyline.adapter.AppAdapter
|
|
|
|
import emu.skyline.adapter.AppItem
|
2019-12-02 14:39:08 +01:00
|
|
|
import emu.skyline.loader.BaseLoader
|
|
|
|
import emu.skyline.loader.NroLoader
|
2019-12-11 15:16:35 +01:00
|
|
|
import emu.skyline.utility.GameDialog
|
2019-12-02 14:39:08 +01:00
|
|
|
import emu.skyline.utility.RandomAccessDocument
|
2019-12-10 11:13:19 +01:00
|
|
|
import kotlinx.android.synthetic.main.main_activity.*
|
2019-12-02 14:39:08 +01:00
|
|
|
import java.io.File
|
|
|
|
import java.io.IOException
|
2020-01-09 02:37:54 +01:00
|
|
|
import kotlin.concurrent.thread
|
2019-12-02 14:39:08 +01:00
|
|
|
|
|
|
|
class MainActivity : AppCompatActivity(), View.OnClickListener {
|
|
|
|
private lateinit var sharedPreferences: SharedPreferences
|
2020-04-03 13:47:32 +02:00
|
|
|
private var adapter = AppAdapter(this)
|
2020-01-09 02:37:54 +01:00
|
|
|
|
2020-03-08 13:28:18 +01:00
|
|
|
private fun notifyUser(text: String) {
|
2019-12-02 14:39:08 +01:00
|
|
|
Snackbar.make(findViewById(android.R.id.content), text, Snackbar.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
|
2020-03-29 21:07:11 +02:00
|
|
|
private fun findFile(ext: String, loader: BaseLoader, directory: DocumentFile, found: Boolean = false): Boolean {
|
|
|
|
var foundCurrent = found
|
2020-03-08 13:28:18 +01:00
|
|
|
|
|
|
|
directory.listFiles()
|
2020-03-29 21:07:11 +02:00
|
|
|
.forEach { file ->
|
|
|
|
if (file.isDirectory) {
|
|
|
|
foundCurrent = findFile(ext, loader, file, foundCurrent)
|
|
|
|
} else {
|
|
|
|
if (ext.equals(file.name?.substringAfterLast("."), ignoreCase = true)) {
|
|
|
|
val document = RandomAccessDocument(this, file)
|
|
|
|
if (loader.verifyFile(document)) {
|
2020-04-03 13:47:32 +02:00
|
|
|
val entry = loader.getAppEntry(document, file.uri)
|
2020-03-29 21:07:11 +02:00
|
|
|
runOnUiThread {
|
|
|
|
if (!foundCurrent) {
|
2020-04-03 13:47:32 +02:00
|
|
|
adapter.addHeader(loader.format.name)
|
2020-03-29 21:07:11 +02:00
|
|
|
foundCurrent = true
|
|
|
|
}
|
2020-04-03 13:47:32 +02:00
|
|
|
adapter.addItem(AppItem(entry))
|
2020-03-29 21:07:11 +02:00
|
|
|
}
|
2020-01-09 02:37:54 +01:00
|
|
|
}
|
2020-03-29 21:07:11 +02:00
|
|
|
document.close()
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 13:28:18 +01:00
|
|
|
|
2020-03-29 21:07:11 +02:00
|
|
|
return foundCurrent
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun refreshFiles(tryLoad: Boolean) {
|
|
|
|
if (tryLoad) {
|
|
|
|
try {
|
2020-03-08 13:28:18 +01:00
|
|
|
adapter.load(File("${applicationInfo.dataDir}/roms.bin"))
|
2019-12-02 14:39:08 +01:00
|
|
|
return
|
|
|
|
} catch (e: Exception) {
|
2020-03-08 13:28:18 +01:00
|
|
|
Log.w("refreshFiles", "Ran into exception while loading: ${e.message}")
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-03 13:47:32 +02:00
|
|
|
|
2020-01-09 02:37:54 +01:00
|
|
|
thread(start = true) {
|
2020-02-01 16:51:32 +01:00
|
|
|
val snackbar = Snackbar.make(findViewById(android.R.id.content), getString(R.string.searching_roms), Snackbar.LENGTH_INDEFINITE)
|
2020-03-29 21:07:11 +02:00
|
|
|
runOnUiThread { snackbar.show() }
|
2020-04-03 13:47:32 +02:00
|
|
|
|
2019-12-10 21:51:02 +01:00
|
|
|
try {
|
2020-03-29 21:07:11 +02:00
|
|
|
runOnUiThread { adapter.clear() }
|
|
|
|
val foundNros = findFile("nro", NroLoader(this), DocumentFile.fromTreeUri(this, Uri.parse(sharedPreferences.getString("search_location", "")))!!)
|
2020-04-03 13:47:32 +02:00
|
|
|
|
2020-01-09 02:37:54 +01:00
|
|
|
runOnUiThread {
|
2020-03-29 21:07:11 +02:00
|
|
|
if (!foundNros)
|
2020-01-09 02:37:54 +01:00
|
|
|
adapter.addHeader(getString(R.string.no_rom))
|
2020-04-03 13:47:32 +02:00
|
|
|
|
2020-01-09 02:37:54 +01:00
|
|
|
try {
|
2020-03-08 13:28:18 +01:00
|
|
|
adapter.save(File("${applicationInfo.dataDir}/roms.bin"))
|
2020-01-09 02:37:54 +01:00
|
|
|
} catch (e: IOException) {
|
2020-03-08 13:28:18 +01:00
|
|
|
Log.w("refreshFiles", "Ran into exception while saving: ${e.message}")
|
2020-01-09 02:37:54 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-03 13:47:32 +02:00
|
|
|
|
2020-01-09 02:37:54 +01:00
|
|
|
sharedPreferences.edit().putBoolean("refresh_required", false).apply()
|
|
|
|
} catch (e: IllegalArgumentException) {
|
|
|
|
runOnUiThread {
|
|
|
|
sharedPreferences.edit().remove("search_location").apply()
|
2020-04-03 13:47:32 +02:00
|
|
|
|
2020-01-09 02:37:54 +01:00
|
|
|
val intent = intent
|
|
|
|
finish()
|
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
|
|
|
runOnUiThread {
|
|
|
|
notifyUser(e.message!!)
|
|
|
|
}
|
2019-12-10 21:51:02 +01:00
|
|
|
}
|
2020-04-03 13:47:32 +02:00
|
|
|
|
2020-03-29 21:07:11 +02:00
|
|
|
runOnUiThread { snackbar.dismiss() }
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.main_activity)
|
|
|
|
PreferenceManager.setDefaultValues(this, R.xml.preferences, false)
|
|
|
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
|
2019-12-10 21:51:02 +01:00
|
|
|
AppCompatDelegate.setDefaultNightMode(when ((sharedPreferences.getString("app_theme", "2")?.toInt())) {
|
|
|
|
0 -> AppCompatDelegate.MODE_NIGHT_NO
|
|
|
|
1 -> AppCompatDelegate.MODE_NIGHT_YES
|
|
|
|
2 -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
|
|
|
else -> AppCompatDelegate.MODE_NIGHT_UNSPECIFIED
|
|
|
|
})
|
2019-12-10 11:13:19 +01:00
|
|
|
setSupportActionBar(toolbar)
|
|
|
|
open_fab.setOnClickListener(this)
|
|
|
|
log_fab.setOnClickListener(this)
|
|
|
|
game_list.adapter = adapter
|
|
|
|
game_list.onItemClickListener = OnItemClickListener { parent: AdapterView<*>, _: View?, position: Int, _: Long ->
|
2019-12-02 14:39:08 +01:00
|
|
|
val item = parent.getItemAtPosition(position)
|
2020-04-03 13:47:32 +02:00
|
|
|
if (item is AppItem) {
|
|
|
|
val intent = Intent(this, EmulationActivity::class.java)
|
2019-12-11 01:14:16 +01:00
|
|
|
intent.data = item.uri
|
2019-12-02 14:39:08 +01:00
|
|
|
startActivity(intent)
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 15:16:35 +01:00
|
|
|
game_list.onItemLongClickListener = AdapterView.OnItemLongClickListener { parent, _, position, _ ->
|
|
|
|
val item = parent.getItemAtPosition(position)
|
2020-04-03 13:47:32 +02:00
|
|
|
if (item is AppItem) {
|
2019-12-11 15:16:35 +01:00
|
|
|
val dialog = GameDialog(item)
|
|
|
|
dialog.show(supportFragmentManager, "game")
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2019-12-02 14:39:08 +01:00
|
|
|
if (sharedPreferences.getString("search_location", "") == "") {
|
|
|
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
2019-12-10 11:13:19 +01:00
|
|
|
intent.flags = Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION or Intent.FLAG_GRANT_PREFIX_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
|
|
|
|
startActivityForResult(intent, 1)
|
2019-12-02 14:39:08 +01:00
|
|
|
} else
|
|
|
|
refreshFiles(!sharedPreferences.getBoolean("refresh_required", false))
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
|
menuInflater.inflate(R.menu.toolbar_main, menu)
|
|
|
|
val mSearch = menu.findItem(R.id.action_search_main)
|
|
|
|
val searchView = mSearch.actionView as SearchView
|
|
|
|
searchView.isSubmitButtonEnabled = false
|
|
|
|
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
|
|
|
|
override fun onQueryTextSubmit(query: String): Boolean {
|
|
|
|
searchView.clearFocus()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onQueryTextChange(newText: String): Boolean {
|
|
|
|
adapter.filter.filter(newText)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return super.onCreateOptionsMenu(menu)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onClick(view: View) {
|
2019-12-10 11:13:19 +01:00
|
|
|
when (view.id) {
|
|
|
|
R.id.log_fab -> startActivity(Intent(this, LogActivity::class.java))
|
|
|
|
R.id.open_fab -> {
|
|
|
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
|
|
|
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
|
|
|
intent.type = "*/*"
|
|
|
|
startActivityForResult(intent, 2)
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
return when (item.itemId) {
|
|
|
|
R.id.action_settings -> {
|
2020-02-15 10:38:17 +01:00
|
|
|
startActivityForResult(Intent(this, SettingsActivity::class.java), 3)
|
2019-12-02 14:39:08 +01:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.action_refresh -> {
|
|
|
|
refreshFiles(false)
|
|
|
|
notifyUser(getString(R.string.refreshed))
|
|
|
|
true
|
|
|
|
}
|
|
|
|
else -> super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 10:38:17 +01:00
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, intent)
|
2019-12-02 14:39:08 +01:00
|
|
|
if (resultCode == RESULT_OK) {
|
2019-12-10 11:13:19 +01:00
|
|
|
when (requestCode) {
|
|
|
|
1 -> {
|
2020-02-15 10:38:17 +01:00
|
|
|
val uri = intent!!.data!!
|
2019-12-10 11:13:19 +01:00
|
|
|
contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
|
|
|
sharedPreferences.edit().putString("search_location", uri.toString()).apply()
|
|
|
|
refreshFiles(!sharedPreferences.getBoolean("refresh_required", false))
|
|
|
|
}
|
|
|
|
2 -> {
|
|
|
|
try {
|
2020-02-15 10:38:17 +01:00
|
|
|
val uri = (intent!!.data!!)
|
2020-04-03 13:47:32 +02:00
|
|
|
val intentGame = Intent(this, EmulationActivity::class.java)
|
2020-02-15 10:38:17 +01:00
|
|
|
intentGame.data = uri
|
2019-12-11 01:14:16 +01:00
|
|
|
if (resultCode != 0)
|
2020-02-15 10:38:17 +01:00
|
|
|
startActivityForResult(intentGame, resultCode)
|
2019-12-11 01:14:16 +01:00
|
|
|
else
|
2020-02-15 10:38:17 +01:00
|
|
|
startActivity(intentGame)
|
2019-12-10 11:13:19 +01:00
|
|
|
} catch (e: Exception) {
|
|
|
|
notifyUser(e.message!!)
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 10:38:17 +01:00
|
|
|
3 -> {
|
|
|
|
if (sharedPreferences.getBoolean("refresh_required", false))
|
|
|
|
refreshFiles(false)
|
|
|
|
}
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|