VERY experimental Nintendo Switch 2 emulator
C++
android: Use file picker intent for save exporter
t895e60440e42c5f
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/InstallableFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/InstallableFragment.ktindex ec116ab62b..6940fc7573 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/InstallableFragment.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/InstallableFragment.kt@@ -21,6 +21,8 @@ import org.yuzu.yuzu_emu.databinding.FragmentInstallablesBindingimport org.yuzu.yuzu_emu.model.HomeViewModelimport org.yuzu.yuzu_emu.model.Installableimport org.yuzu.yuzu_emu.ui.main.MainActivity+import java.time.LocalDateTime+import java.time.format.DateTimeFormatterclass InstallableFragment : Fragment() {private var _binding: FragmentInstallablesBinding? = null@@ -78,7 +80,15 @@ class InstallableFragment : Fragment() {R.string.manage_save_data,R.string.import_export_saves_description,install = { mainActivity.importSaves.launch(arrayOf("application/zip")) },- export = { mainActivity.exportSave() }+ export = {+ mainActivity.exportSaves.launch(+ "yuzu saves - ${+ LocalDateTime.now().format(+ DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")+ )+ }.zip"+ )+ })} else {Installable(diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.ktindex 211b7cf69d..ace5dddea6 100644--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt@@ -6,7 +6,6 @@ package org.yuzu.yuzu_emu.ui.mainimport android.content.Intentimport android.net.Uriimport android.os.Bundle-import android.provider.DocumentsContractimport android.view.Viewimport android.view.ViewGroup.MarginLayoutParamsimport android.view.WindowManager@@ -20,7 +19,6 @@ import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreenimport androidx.core.view.ViewCompatimport androidx.core.view.WindowCompatimport androidx.core.view.WindowInsetsCompat-import androidx.documentfile.provider.DocumentFileimport androidx.lifecycle.Lifecycleimport androidx.lifecycle.lifecycleScopeimport androidx.lifecycle.repeatOnLifecycle@@ -41,7 +39,6 @@ import org.yuzu.yuzu_emu.NativeLibraryimport org.yuzu.yuzu_emu.Rimport org.yuzu.yuzu_emu.activities.EmulationActivityimport org.yuzu.yuzu_emu.databinding.ActivityMainBinding-import org.yuzu.yuzu_emu.features.DocumentProviderimport org.yuzu.yuzu_emu.features.settings.model.Settingsimport org.yuzu.yuzu_emu.fragments.IndeterminateProgressDialogFragmentimport org.yuzu.yuzu_emu.fragments.MessageDialogFragment@@ -53,9 +50,6 @@ import org.yuzu.yuzu_emu.model.TaskViewModelimport org.yuzu.yuzu_emu.utils.*import java.io.BufferedInputStreamimport java.io.BufferedOutputStream-import java.io.FileOutputStream-import java.time.LocalDateTime-import java.time.format.DateTimeFormatterimport java.util.zip.ZipEntryimport java.util.zip.ZipInputStream@@ -73,7 +67,6 @@ class MainActivity : AppCompatActivity(), ThemeProvider {// Get first subfolder in saves folder (should be the user folder)val savesFolderRoot get() = File(savesFolder).listFiles()?.firstOrNull()?.canonicalPath ?: ""- private var lastZipCreated: File? = nulloverride fun onCreate(savedInstanceState: Bundle?) {val splashScreen = installSplashScreen()@@ -656,75 +649,31 @@ class MainActivity : AppCompatActivity(), ThemeProvider {}.show(supportFragmentManager, IndeterminateProgressDialogFragment.TAG)}- /**- * Zips the save files located in the given folder path and creates a new zip file with the current date and time.- * @return true if the zip file is successfully created, false otherwise.- */- private fun zipSave(): Boolean {- try {- val tempFolder = File(getPublicFilesDir().canonicalPath, "temp")- tempFolder.mkdirs()- val saveFolder = File(savesFolderRoot)- val outputZipFile = File(- tempFolder,- "yuzu saves - ${- LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"))- }.zip"- )- outputZipFile.createNewFile()- val result = FileUtil.zipFromInternalStorage(- saveFolder,- savesFolderRoot,- BufferedOutputStream(FileOutputStream(outputZipFile))- )- if (result == TaskState.Failed) {- return false- }- lastZipCreated = outputZipFile- } catch (e: Exception) {- return false- }- return true- }-/*** Exports the save file located in the given folder path by creating a zip file and sharing it via intent.*/- fun exportSave() {- CoroutineScope(Dispatchers.IO).launch {- val wasZipCreated = zipSave()- val lastZipFile = lastZipCreated- if (!wasZipCreated || lastZipFile == null) {- withContext(Dispatchers.Main) {- Toast.makeText(- this@MainActivity,- getString(R.string.export_save_failed),- Toast.LENGTH_LONG- ).show()- }- return@launch- }+ val exportSaves = registerForActivityResult(+ ActivityResultContracts.CreateDocument("application/zip")+ ) { result ->+ if (result == null) {+ return@registerForActivityResult+ }- withContext(Dispatchers.Main) {- val file = DocumentFile.fromSingleUri(- this@MainActivity,- DocumentsContract.buildDocumentUri(- DocumentProvider.AUTHORITY,- "${DocumentProvider.ROOT_ID}/temp/${lastZipFile.name}"- )- )!!- val intent = Intent(Intent.ACTION_SEND)- .setDataAndType(file.uri, "application/zip")- .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)- .putExtra(Intent.EXTRA_STREAM, file.uri)- startForResultExportSave.launch(- Intent.createChooser(- intent,- getString(R.string.share_save_file)- )- )+ IndeterminateProgressDialogFragment.newInstance(+ this,+ R.string.save_files_exporting,+ false+ ) {+ val zipResult = FileUtil.zipFromInternalStorage(+ File(savesFolderRoot),+ savesFolderRoot,+ BufferedOutputStream(contentResolver.openOutputStream(result))+ )+ return@newInstance when (zipResult) {+ TaskState.Completed -> getString(R.string.export_success)+ TaskState.Cancelled, TaskState.Failed -> getString(R.string.export_failed)}- }+ }.show(supportFragmentManager, IndeterminateProgressDialogFragment.TAG)}private val startForResultExportSave =diff --git a/src/android/app/src/main/res/values/strings.xml b/src/android/app/src/main/res/values/strings.xmlindex 98c3f20f87..471af87959 100644--- a/src/android/app/src/main/res/values/strings.xml+++ b/src/android/app/src/main/res/values/strings.xml@@ -91,6 +91,7 @@<string name="manage_save_data">Manage save data</string><string name="manage_save_data_description">Save data found. Please select an option below.</string><string name="import_export_saves_description">Import or export save files</string>+ <string name="save_files_exporting">Exporting save files…</string><string name="save_file_imported_success">Imported successfully</string><string name="save_file_invalid_zip_structure">Invalid save directory structure</string><string name="save_file_invalid_zip_structure_description">The first subfolder name must be the title ID of the game.</string>@@ -256,6 +257,7 @@<string name="cancelling">Cancelling</string><string name="install">Install</string><string name="delete">Delete</string>+ <string name="export_success">Exported successfully</string><!-- GPU driver installation --><string name="select_gpu_driver">Select GPU driver</string>