Browse Source

android: Remove deprecated use of onActivityResult

nce_cpp
Charles Lombardo 3 years ago
committed by bunnei
parent
commit
ef1b2561ef
  1. 131
      src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt
  2. 29
      src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileBrowserHelper.kt

131
src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt

@ -5,12 +5,12 @@ package org.yuzu.yuzu_emu.ui.main
import android.content.DialogInterface import android.content.DialogInterface
import android.content.Intent import android.content.Intent
import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
@ -110,16 +110,8 @@ class MainActivity : AppCompatActivity(), MainView {
override fun launchFileListActivity(request: Int) { override fun launchFileListActivity(request: Int) {
when (request) { when (request) {
MainPresenter.REQUEST_ADD_DIRECTORY -> FileBrowserHelper.openDirectoryPicker(
this,
MainPresenter.REQUEST_ADD_DIRECTORY,
R.string.select_game_folder
)
MainPresenter.REQUEST_INSTALL_KEYS -> FileBrowserHelper.openFilePicker(
this,
MainPresenter.REQUEST_INSTALL_KEYS,
R.string.install_keys
)
MainPresenter.REQUEST_ADD_DIRECTORY -> getGamesDirectory.launch(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).data)
MainPresenter.REQUEST_INSTALL_KEYS -> getProdKey.launch(arrayOf("*/*"))
MainPresenter.REQUEST_SELECT_GPU_DRIVER -> { MainPresenter.REQUEST_SELECT_GPU_DRIVER -> {
// Get the driver name for the dialog message. // Get the driver name for the dialog message.
var driverName = GpuDriverHelper.customDriverName var driverName = GpuDriverHelper.customDriverName
@ -140,11 +132,7 @@ class MainActivity : AppCompatActivity(), MainView {
).show() ).show()
} }
.setNeutralButton(R.string.select_gpu_driver_install) { _: DialogInterface?, _: Int -> .setNeutralButton(R.string.select_gpu_driver_install) { _: DialogInterface?, _: Int ->
FileBrowserHelper.openFilePicker(
this,
MainPresenter.REQUEST_SELECT_GPU_DRIVER,
R.string.select_gpu_driver
)
getDriver.launch(arrayOf("application/zip"))
} }
.show() .show()
} }
@ -152,36 +140,70 @@ class MainActivity : AppCompatActivity(), MainView {
} }
/** /**
* @param requestCode An int describing whether the Activity that is returning did so successfully.
* @param resultCode An int describing what Activity is giving us this callback.
* @param result The information the returning Activity is providing us.
* Called by the framework whenever any actionbar/toolbar icon is clicked.
*
* @param item The icon that was clicked on.
* @return True if the event was handled, false to bubble it up to the OS.
*/ */
override fun onActivityResult(requestCode: Int, resultCode: Int, result: Intent?) {
super.onActivityResult(requestCode, resultCode, result)
when (requestCode) {
MainPresenter.REQUEST_ADD_DIRECTORY -> if (resultCode == RESULT_OK) {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return presenter.handleOptionSelection(item.itemId)
}
private fun refreshFragment() {
if (platformGamesFragment != null) {
NativeLibrary.ResetRomMetadata()
platformGamesFragment!!.refresh()
}
}
override fun onDestroy() {
EmulationActivity.tryDismissRunningNotification(this)
super.onDestroy()
}
private fun setInsets() {
ViewCompat.setOnApplyWindowInsetsListener(binding.gamesPlatformFrame) { view: View, windowInsets: WindowInsetsCompat ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updatePadding(left = insets.left, right = insets.right)
InsetsHelper.insetAppBar(insets, binding.appbarMain)
windowInsets
}
}
private val getGamesDirectory =
registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { result ->
if (result == null)
return@registerForActivityResult
val takeFlags = val takeFlags =
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
contentResolver.takePersistableUriPermission( contentResolver.takePersistableUriPermission(
Uri.parse(result!!.dataString),
result,
takeFlags takeFlags
) )
// When a new directory is picked, we currently will reset the existing games // When a new directory is picked, we currently will reset the existing games
// database. This effectively means that only one game directory is supported. // database. This effectively means that only one game directory is supported.
// TODO(bunnei): Consider fixing this in the future, or removing code for this. // TODO(bunnei): Consider fixing this in the future, or removing code for this.
contentResolver.insert(GameProvider.URI_RESET, null) contentResolver.insert(GameProvider.URI_RESET, null)
// Add the new directory // Add the new directory
presenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result))
presenter.onDirectorySelected(result.toString())
} }
MainPresenter.REQUEST_INSTALL_KEYS -> if (resultCode == RESULT_OK) {
private val getProdKey =
registerForActivityResult(ActivityResultContracts.OpenDocument()) { result ->
if (result == null)
return@registerForActivityResult
val takeFlags = val takeFlags =
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
contentResolver.takePersistableUriPermission( contentResolver.takePersistableUriPermission(
Uri.parse(result!!.dataString),
result,
takeFlags takeFlags
) )
val dstPath = DirectoryInitialization.userDirectory + "/keys/" val dstPath = DirectoryInitialization.userDirectory + "/keys/"
if (FileUtil.copyUriToInternalStorage(this, result.data, dstPath, "prod.keys")) {
if (FileUtil.copyUriToInternalStorage(this, result, dstPath, "prod.keys")) {
if (NativeLibrary.ReloadKeys()) { if (NativeLibrary.ReloadKeys()) {
Toast.makeText( Toast.makeText(
this, this,
@ -195,15 +217,19 @@ class MainActivity : AppCompatActivity(), MainView {
R.string.install_keys_failure, R.string.install_keys_failure,
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show() ).show()
launchFileListActivity(MainPresenter.REQUEST_INSTALL_KEYS)
} }
} }
} }
MainPresenter.REQUEST_SELECT_GPU_DRIVER -> if (resultCode == RESULT_OK) {
private val getDriver =
registerForActivityResult(ActivityResultContracts.OpenDocument()) { result ->
if (result == null)
return@registerForActivityResult
val takeFlags = val takeFlags =
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION
contentResolver.takePersistableUriPermission( contentResolver.takePersistableUriPermission(
Uri.parse(result!!.dataString),
result,
takeFlags takeFlags
) )
@ -218,8 +244,9 @@ class MainActivity : AppCompatActivity(), MainView {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
// Ignore file exceptions when a user selects an invalid zip // Ignore file exceptions when a user selects an invalid zip
try { try {
GpuDriverHelper.installCustomDriver(applicationContext, result.data)
} catch (_: IOException) {}
GpuDriverHelper.installCustomDriver(applicationContext, result)
} catch (_: IOException) {
}
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
installationDialog.dismiss() installationDialog.dismiss()
@ -228,7 +255,10 @@ class MainActivity : AppCompatActivity(), MainView {
if (driverName != null) { if (driverName != null) {
Toast.makeText( Toast.makeText(
applicationContext, applicationContext,
getString(R.string.select_gpu_driver_install_success, driverName),
getString(
R.string.select_gpu_driver_install_success,
driverName
),
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
} else { } else {
@ -243,36 +273,3 @@ class MainActivity : AppCompatActivity(), MainView {
} }
} }
} }
}
/**
* Called by the framework whenever any actionbar/toolbar icon is clicked.
*
* @param item The icon that was clicked on.
* @return True if the event was handled, false to bubble it up to the OS.
*/
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return presenter.handleOptionSelection(item.itemId)
}
private fun refreshFragment() {
if (platformGamesFragment != null) {
NativeLibrary.ResetRomMetadata()
platformGamesFragment!!.refresh()
}
}
override fun onDestroy() {
EmulationActivity.tryDismissRunningNotification(this)
super.onDestroy()
}
private fun setInsets() {
ViewCompat.setOnApplyWindowInsetsListener(binding.gamesPlatformFrame) { view: View, windowInsets: WindowInsetsCompat ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
view.updatePadding(left = insets.left, right = insets.right)
InsetsHelper.insetAppBar(insets, binding.appbarMain)
windowInsets
}
}
}

29
src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileBrowserHelper.kt

@ -1,29 +0,0 @@
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
package org.yuzu.yuzu_emu.utils
import android.content.Intent
import androidx.fragment.app.FragmentActivity
object FileBrowserHelper {
fun openDirectoryPicker(activity: FragmentActivity, requestCode: Int, title: Int) {
val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
i.putExtra(Intent.EXTRA_TITLE, title)
activity.startActivityForResult(i, requestCode)
}
fun openFilePicker(activity: FragmentActivity, requestCode: Int, title: Int) {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.flags =
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
intent.putExtra(Intent.EXTRA_TITLE, title)
intent.type = "*/*"
activity.startActivityForResult(intent, requestCode)
}
fun getSelectedDirectory(result: Intent): String? {
return result.dataString
}
}
Loading…
Cancel
Save