diff options
| author | 2023-03-22 03:24:32 -0400 | |
|---|---|---|
| committer | 2023-06-03 00:05:46 -0700 | |
| commit | e2a7143a3de230cec21a802f598e8e4e954b3a5e (patch) | |
| tree | 6cb4b4b4c53c4cd4d9a7c71c90ef19c6f8f236ce /src/android | |
| parent | android: Fix RTL layouts (diff) | |
| download | yuzu-e2a7143a3de230cec21a802f598e8e4e954b3a5e.tar.gz yuzu-e2a7143a3de230cec21a802f598e8e4e954b3a5e.tar.xz yuzu-e2a7143a3de230cec21a802f598e8e4e954b3a5e.zip | |
android: Remove deprecated use of onActivityResult
Diffstat (limited to 'src/android')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/ui/main/MainActivity.kt | 217 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileBrowserHelper.kt | 29 |
2 files changed, 107 insertions, 139 deletions
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.kt index dd8750c4f..b87125d1c 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 | |||
| @@ -5,12 +5,12 @@ package org.yuzu.yuzu_emu.ui.main | |||
| 5 | 5 | ||
| 6 | import android.content.DialogInterface | 6 | import android.content.DialogInterface |
| 7 | import android.content.Intent | 7 | import android.content.Intent |
| 8 | import android.net.Uri | ||
| 9 | import android.os.Bundle | 8 | import android.os.Bundle |
| 10 | import android.view.Menu | 9 | import android.view.Menu |
| 11 | import android.view.MenuItem | 10 | import android.view.MenuItem |
| 12 | import android.view.View | 11 | import android.view.View |
| 13 | import android.widget.Toast | 12 | import android.widget.Toast |
| 13 | import androidx.activity.result.contract.ActivityResultContracts | ||
| 14 | import androidx.appcompat.app.AppCompatActivity | 14 | import androidx.appcompat.app.AppCompatActivity |
| 15 | import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen | 15 | import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen |
| 16 | import androidx.core.view.ViewCompat | 16 | import androidx.core.view.ViewCompat |
| @@ -110,16 +110,8 @@ class MainActivity : AppCompatActivity(), MainView { | |||
| 110 | 110 | ||
| 111 | override fun launchFileListActivity(request: Int) { | 111 | override fun launchFileListActivity(request: Int) { |
| 112 | when (request) { | 112 | when (request) { |
| 113 | MainPresenter.REQUEST_ADD_DIRECTORY -> FileBrowserHelper.openDirectoryPicker( | 113 | MainPresenter.REQUEST_ADD_DIRECTORY -> getGamesDirectory.launch(Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).data) |
| 114 | this, | 114 | MainPresenter.REQUEST_INSTALL_KEYS -> getProdKey.launch(arrayOf("*/*")) |
| 115 | MainPresenter.REQUEST_ADD_DIRECTORY, | ||
| 116 | R.string.select_game_folder | ||
| 117 | ) | ||
| 118 | MainPresenter.REQUEST_INSTALL_KEYS -> FileBrowserHelper.openFilePicker( | ||
| 119 | this, | ||
| 120 | MainPresenter.REQUEST_INSTALL_KEYS, | ||
| 121 | R.string.install_keys | ||
| 122 | ) | ||
| 123 | MainPresenter.REQUEST_SELECT_GPU_DRIVER -> { | 115 | MainPresenter.REQUEST_SELECT_GPU_DRIVER -> { |
| 124 | // Get the driver name for the dialog message. | 116 | // Get the driver name for the dialog message. |
| 125 | var driverName = GpuDriverHelper.customDriverName | 117 | var driverName = GpuDriverHelper.customDriverName |
| @@ -140,11 +132,7 @@ class MainActivity : AppCompatActivity(), MainView { | |||
| 140 | ).show() | 132 | ).show() |
| 141 | } | 133 | } |
| 142 | .setNeutralButton(R.string.select_gpu_driver_install) { _: DialogInterface?, _: Int -> | 134 | .setNeutralButton(R.string.select_gpu_driver_install) { _: DialogInterface?, _: Int -> |
| 143 | FileBrowserHelper.openFilePicker( | 135 | getDriver.launch(arrayOf("application/zip")) |
| 144 | this, | ||
| 145 | MainPresenter.REQUEST_SELECT_GPU_DRIVER, | ||
| 146 | R.string.select_gpu_driver | ||
| 147 | ) | ||
| 148 | } | 136 | } |
| 149 | .show() | 137 | .show() |
| 150 | } | 138 | } |
| @@ -152,100 +140,6 @@ class MainActivity : AppCompatActivity(), MainView { | |||
| 152 | } | 140 | } |
| 153 | 141 | ||
| 154 | /** | 142 | /** |
| 155 | * @param requestCode An int describing whether the Activity that is returning did so successfully. | ||
| 156 | * @param resultCode An int describing what Activity is giving us this callback. | ||
| 157 | * @param result The information the returning Activity is providing us. | ||
| 158 | */ | ||
| 159 | override fun onActivityResult(requestCode: Int, resultCode: Int, result: Intent?) { | ||
| 160 | super.onActivityResult(requestCode, resultCode, result) | ||
| 161 | when (requestCode) { | ||
| 162 | MainPresenter.REQUEST_ADD_DIRECTORY -> if (resultCode == RESULT_OK) { | ||
| 163 | val takeFlags = | ||
| 164 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION | ||
| 165 | contentResolver.takePersistableUriPermission( | ||
| 166 | Uri.parse(result!!.dataString), | ||
| 167 | takeFlags | ||
| 168 | ) | ||
| 169 | // When a new directory is picked, we currently will reset the existing games | ||
| 170 | // database. This effectively means that only one game directory is supported. | ||
| 171 | // TODO(bunnei): Consider fixing this in the future, or removing code for this. | ||
| 172 | contentResolver.insert(GameProvider.URI_RESET, null) | ||
| 173 | // Add the new directory | ||
| 174 | presenter.onDirectorySelected(FileBrowserHelper.getSelectedDirectory(result)) | ||
| 175 | } | ||
| 176 | MainPresenter.REQUEST_INSTALL_KEYS -> if (resultCode == RESULT_OK) { | ||
| 177 | val takeFlags = | ||
| 178 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION | ||
| 179 | contentResolver.takePersistableUriPermission( | ||
| 180 | Uri.parse(result!!.dataString), | ||
| 181 | takeFlags | ||
| 182 | ) | ||
| 183 | val dstPath = DirectoryInitialization.userDirectory + "/keys/" | ||
| 184 | if (FileUtil.copyUriToInternalStorage(this, result.data, dstPath, "prod.keys")) { | ||
| 185 | if (NativeLibrary.ReloadKeys()) { | ||
| 186 | Toast.makeText( | ||
| 187 | this, | ||
| 188 | R.string.install_keys_success, | ||
| 189 | Toast.LENGTH_SHORT | ||
| 190 | ).show() | ||
| 191 | refreshFragment() | ||
| 192 | } else { | ||
| 193 | Toast.makeText( | ||
| 194 | this, | ||
| 195 | R.string.install_keys_failure, | ||
| 196 | Toast.LENGTH_LONG | ||
| 197 | ).show() | ||
| 198 | launchFileListActivity(MainPresenter.REQUEST_INSTALL_KEYS) | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | MainPresenter.REQUEST_SELECT_GPU_DRIVER -> if (resultCode == RESULT_OK) { | ||
| 203 | val takeFlags = | ||
| 204 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION | ||
| 205 | contentResolver.takePersistableUriPermission( | ||
| 206 | Uri.parse(result!!.dataString), | ||
| 207 | takeFlags | ||
| 208 | ) | ||
| 209 | |||
| 210 | val progressBinding = DialogProgressBarBinding.inflate(layoutInflater) | ||
| 211 | progressBinding.progressBar.isIndeterminate = true | ||
| 212 | val installationDialog = MaterialAlertDialogBuilder(this) | ||
| 213 | .setTitle(R.string.installing_driver) | ||
| 214 | .setView(progressBinding.root) | ||
| 215 | .show() | ||
| 216 | |||
| 217 | lifecycleScope.launch { | ||
| 218 | withContext(Dispatchers.IO) { | ||
| 219 | // Ignore file exceptions when a user selects an invalid zip | ||
| 220 | try { | ||
| 221 | GpuDriverHelper.installCustomDriver(applicationContext, result.data) | ||
| 222 | } catch (_: IOException) {} | ||
| 223 | |||
| 224 | withContext(Dispatchers.Main) { | ||
| 225 | installationDialog.dismiss() | ||
| 226 | |||
| 227 | val driverName = GpuDriverHelper.customDriverName | ||
| 228 | if (driverName != null) { | ||
| 229 | Toast.makeText( | ||
| 230 | applicationContext, | ||
| 231 | getString(R.string.select_gpu_driver_install_success, driverName), | ||
| 232 | Toast.LENGTH_SHORT | ||
| 233 | ).show() | ||
| 234 | } else { | ||
| 235 | Toast.makeText( | ||
| 236 | applicationContext, | ||
| 237 | R.string.select_gpu_driver_error, | ||
| 238 | Toast.LENGTH_LONG | ||
| 239 | ).show() | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | /** | ||
| 249 | * Called by the framework whenever any actionbar/toolbar icon is clicked. | 143 | * Called by the framework whenever any actionbar/toolbar icon is clicked. |
| 250 | * | 144 | * |
| 251 | * @param item The icon that was clicked on. | 145 | * @param item The icon that was clicked on. |
| @@ -275,4 +169,107 @@ class MainActivity : AppCompatActivity(), MainView { | |||
| 275 | windowInsets | 169 | windowInsets |
| 276 | } | 170 | } |
| 277 | } | 171 | } |
| 172 | |||
| 173 | private val getGamesDirectory = | ||
| 174 | registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { result -> | ||
| 175 | if (result == null) | ||
| 176 | return@registerForActivityResult | ||
| 177 | |||
| 178 | val takeFlags = | ||
| 179 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION | ||
| 180 | contentResolver.takePersistableUriPermission( | ||
| 181 | result, | ||
| 182 | takeFlags | ||
| 183 | ) | ||
| 184 | |||
| 185 | // When a new directory is picked, we currently will reset the existing games | ||
| 186 | // database. This effectively means that only one game directory is supported. | ||
| 187 | // TODO(bunnei): Consider fixing this in the future, or removing code for this. | ||
| 188 | contentResolver.insert(GameProvider.URI_RESET, null) | ||
| 189 | // Add the new directory | ||
| 190 | presenter.onDirectorySelected(result.toString()) | ||
| 191 | } | ||
| 192 | |||
| 193 | private val getProdKey = | ||
| 194 | registerForActivityResult(ActivityResultContracts.OpenDocument()) { result -> | ||
| 195 | if (result == null) | ||
| 196 | return@registerForActivityResult | ||
| 197 | |||
| 198 | val takeFlags = | ||
| 199 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION | ||
| 200 | contentResolver.takePersistableUriPermission( | ||
| 201 | result, | ||
| 202 | takeFlags | ||
| 203 | ) | ||
| 204 | |||
| 205 | val dstPath = DirectoryInitialization.userDirectory + "/keys/" | ||
| 206 | if (FileUtil.copyUriToInternalStorage(this, result, dstPath, "prod.keys")) { | ||
| 207 | if (NativeLibrary.ReloadKeys()) { | ||
| 208 | Toast.makeText( | ||
| 209 | this, | ||
| 210 | R.string.install_keys_success, | ||
| 211 | Toast.LENGTH_SHORT | ||
| 212 | ).show() | ||
| 213 | refreshFragment() | ||
| 214 | } else { | ||
| 215 | Toast.makeText( | ||
| 216 | this, | ||
| 217 | R.string.install_keys_failure, | ||
| 218 | Toast.LENGTH_LONG | ||
| 219 | ).show() | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | private val getDriver = | ||
| 225 | registerForActivityResult(ActivityResultContracts.OpenDocument()) { result -> | ||
| 226 | if (result == null) | ||
| 227 | return@registerForActivityResult | ||
| 228 | |||
| 229 | val takeFlags = | ||
| 230 | Intent.FLAG_GRANT_WRITE_URI_PERMISSION or Intent.FLAG_GRANT_READ_URI_PERMISSION | ||
| 231 | contentResolver.takePersistableUriPermission( | ||
| 232 | result, | ||
| 233 | takeFlags | ||
| 234 | ) | ||
| 235 | |||
| 236 | val progressBinding = DialogProgressBarBinding.inflate(layoutInflater) | ||
| 237 | progressBinding.progressBar.isIndeterminate = true | ||
| 238 | val installationDialog = MaterialAlertDialogBuilder(this) | ||
| 239 | .setTitle(R.string.installing_driver) | ||
| 240 | .setView(progressBinding.root) | ||
| 241 | .show() | ||
| 242 | |||
| 243 | lifecycleScope.launch { | ||
| 244 | withContext(Dispatchers.IO) { | ||
| 245 | // Ignore file exceptions when a user selects an invalid zip | ||
| 246 | try { | ||
| 247 | GpuDriverHelper.installCustomDriver(applicationContext, result) | ||
| 248 | } catch (_: IOException) { | ||
| 249 | } | ||
| 250 | |||
| 251 | withContext(Dispatchers.Main) { | ||
| 252 | installationDialog.dismiss() | ||
| 253 | |||
| 254 | val driverName = GpuDriverHelper.customDriverName | ||
| 255 | if (driverName != null) { | ||
| 256 | Toast.makeText( | ||
| 257 | applicationContext, | ||
| 258 | getString( | ||
| 259 | R.string.select_gpu_driver_install_success, | ||
| 260 | driverName | ||
| 261 | ), | ||
| 262 | Toast.LENGTH_SHORT | ||
| 263 | ).show() | ||
| 264 | } else { | ||
| 265 | Toast.makeText( | ||
| 266 | applicationContext, | ||
| 267 | R.string.select_gpu_driver_error, | ||
| 268 | Toast.LENGTH_LONG | ||
| 269 | ).show() | ||
| 270 | } | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } | ||
| 274 | } | ||
| 278 | } | 275 | } |
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileBrowserHelper.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileBrowserHelper.kt deleted file mode 100644 index 91f670255..000000000 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/FileBrowserHelper.kt +++ /dev/null | |||
| @@ -1,29 +0,0 @@ | |||
| 1 | // SPDX-FileCopyrightText: 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | package org.yuzu.yuzu_emu.utils | ||
| 5 | |||
| 6 | import android.content.Intent | ||
| 7 | import androidx.fragment.app.FragmentActivity | ||
| 8 | |||
| 9 | object FileBrowserHelper { | ||
| 10 | fun openDirectoryPicker(activity: FragmentActivity, requestCode: Int, title: Int) { | ||
| 11 | val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) | ||
| 12 | i.putExtra(Intent.EXTRA_TITLE, title) | ||
| 13 | activity.startActivityForResult(i, requestCode) | ||
| 14 | } | ||
| 15 | |||
| 16 | fun openFilePicker(activity: FragmentActivity, requestCode: Int, title: Int) { | ||
| 17 | val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) | ||
| 18 | intent.addCategory(Intent.CATEGORY_OPENABLE) | ||
| 19 | intent.flags = | ||
| 20 | Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION | ||
| 21 | intent.putExtra(Intent.EXTRA_TITLE, title) | ||
| 22 | intent.type = "*/*" | ||
| 23 | activity.startActivityForResult(intent, requestCode) | ||
| 24 | } | ||
| 25 | |||
| 26 | fun getSelectedDirectory(result: Intent): String? { | ||
| 27 | return result.dataString | ||
| 28 | } | ||
| 29 | } | ||