From 4a72704c4d1eea3e3d3d2cfd65de1a3fef7c71da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=97=B1=20PixelyIon?= Date: Wed, 11 Dec 2019 05:44:16 +0530 Subject: [PATCH] Associate with NRO files This commit adds the ability to open NRO files directly from a file browser rather than the emulator itself. --- app/src/main/AndroidManifest.xml | 35 +++++++++++++++++-- app/src/main/java/emu/skyline/GameActivity.kt | 5 +-- app/src/main/java/emu/skyline/MainActivity.kt | 22 ++++-------- .../java/emu/skyline/loader/BaseLoader.kt | 12 +++++++ .../emu/skyline/utility/ThemePreference.kt | 2 +- 5 files changed, 55 insertions(+), 21 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 431f1483..8c90edf5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -50,12 +50,41 @@ android:name="emu.skyline.GameActivity" android:configChanges="orientation|screenSize" android:screenOrientation="landscape"> - + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/emu/skyline/GameActivity.kt b/app/src/main/java/emu/skyline/GameActivity.kt index 2ae5602e..82bf7036 100644 --- a/app/src/main/java/emu/skyline/GameActivity.kt +++ b/app/src/main/java/emu/skyline/GameActivity.kt @@ -9,6 +9,7 @@ import android.view.Surface import android.view.SurfaceHolder import android.view.WindowManager import androidx.appcompat.app.AppCompatActivity +import emu.skyline.loader.getTitleFormat import kotlinx.android.synthetic.main.game_activity.* import java.io.File import java.lang.reflect.Method @@ -34,8 +35,8 @@ class GameActivity : AppCompatActivity(), SurfaceHolder.Callback, InputQueue.Cal override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.game_activity) - rom = intent.getParcelableExtra("romUri")!! - val romType = intent.getIntExtra("romType", 0) + rom = intent.data!! + val romType = getTitleFormat(rom, contentResolver).ordinal romFd = contentResolver.openFileDescriptor(rom, "r")!! val preference = File("${applicationInfo.dataDir}/shared_prefs/${applicationInfo.packageName}_preferences.xml") preferenceFd = ParcelFileDescriptor.open(preference, ParcelFileDescriptor.MODE_READ_WRITE) diff --git a/app/src/main/java/emu/skyline/MainActivity.kt b/app/src/main/java/emu/skyline/MainActivity.kt index 7e0a20a5..b035f9f4 100644 --- a/app/src/main/java/emu/skyline/MainActivity.kt +++ b/app/src/main/java/emu/skyline/MainActivity.kt @@ -4,7 +4,6 @@ import android.content.Intent import android.content.SharedPreferences import android.net.Uri import android.os.Bundle -import android.provider.OpenableColumns import android.util.Log import android.view.Menu import android.view.MenuItem @@ -22,7 +21,6 @@ import emu.skyline.adapter.GameItem import emu.skyline.loader.BaseLoader import emu.skyline.loader.NroLoader import emu.skyline.loader.TitleEntry -import emu.skyline.loader.TitleFormat import emu.skyline.utility.RandomAccessDocument import kotlinx.android.synthetic.main.main_activity.* import java.io.File @@ -114,8 +112,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener { val item = parent.getItemAtPosition(position) if (item is GameItem) { val intent = Intent(this, GameActivity::class.java) - intent.putExtra("romUri", item.uri) - intent.putExtra("romType", item.meta.romType.ordinal) + intent.data = item.uri startActivity(intent) } } @@ -191,18 +188,13 @@ class MainActivity : AppCompatActivity(), View.OnClickListener { } 2 -> { try { + val uri = (data!!.data!!) val intent = Intent(this, GameActivity::class.java) - val uri = data!!.data!! - intent.putExtra("romUri", uri) - var uriStr = "" - contentResolver.query(uri, null, null, null, null)?.use { cursor -> - val nameIndex: Int = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) - cursor.moveToFirst() - uriStr = cursor.getString(nameIndex) - } - val type = TitleFormat.valueOf(uriStr.substring(uriStr.lastIndexOf(".") + 1).toUpperCase(Locale.ROOT)) - intent.putExtra("romType", type) - startActivity(intent) + intent.data = uri + if (resultCode != 0) + startActivityForResult(intent, resultCode) + else + startActivity(intent) } catch (e: Exception) { notifyUser(e.message!!) } diff --git a/app/src/main/java/emu/skyline/loader/BaseLoader.kt b/app/src/main/java/emu/skyline/loader/BaseLoader.kt index 5d7ec8d7..0f342d44 100644 --- a/app/src/main/java/emu/skyline/loader/BaseLoader.kt +++ b/app/src/main/java/emu/skyline/loader/BaseLoader.kt @@ -1,5 +1,6 @@ package emu.skyline.loader +import android.content.ContentResolver import android.content.Context import android.graphics.Bitmap import android.graphics.BitmapFactory @@ -12,11 +13,22 @@ import java.io.IOException import java.io.ObjectInputStream import java.io.ObjectOutputStream import java.io.Serializable +import java.util.* enum class TitleFormat { NRO, XCI, NSP } +fun getTitleFormat(uri: Uri, contentResolver: ContentResolver): TitleFormat { + var uriStr = "" + contentResolver.query(uri, null, null, null, null)?.use { cursor -> + val nameIndex: Int = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME) + cursor.moveToFirst() + uriStr = cursor.getString(nameIndex) + } + return TitleFormat.valueOf(uriStr.substring(uriStr.lastIndexOf(".") + 1).toUpperCase(Locale.ROOT)) +} + internal class TitleEntry(var name: String, var author: String, var romType: TitleFormat, var valid: Boolean, @Transient var uri: Uri, @Transient var icon: Bitmap) : Serializable { constructor(context: Context, author: String, romType: TitleFormat, valid: Boolean, uri: Uri) : this("", author, romType, valid, uri, context.resources.getDrawable(R.drawable.ic_missing, context.theme).toBitmap(256, 256)) { context.contentResolver.query(uri, null, null, null, null)?.use { cursor -> diff --git a/app/src/main/java/emu/skyline/utility/ThemePreference.kt b/app/src/main/java/emu/skyline/utility/ThemePreference.kt index 11553de8..7b973b4d 100644 --- a/app/src/main/java/emu/skyline/utility/ThemePreference.kt +++ b/app/src/main/java/emu/skyline/utility/ThemePreference.kt @@ -13,7 +13,7 @@ class ThemePreference : ListPreference { constructor(context: Context?) : super(context) override fun callChangeListener(newValue: Any?): Boolean { - AppCompatDelegate.setDefaultNightMode(when((newValue as String).toInt()) { + AppCompatDelegate.setDefaultNightMode(when ((newValue as String).toInt()) { 0 -> AppCompatDelegate.MODE_NIGHT_NO 1 -> AppCompatDelegate.MODE_NIGHT_YES 2 -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM