Merge pull request #11689 from K0bin/android-open-user-folder

Android: Make button in the settings open user folder
This commit is contained in:
Charles Lombardo 2023-03-29 10:53:48 -07:00 committed by GitHub
commit 15be899580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 21 deletions

View File

@ -8,6 +8,7 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.DocumentsContract
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
@ -20,6 +21,7 @@ import org.dolphinemu.dolphinemu.databinding.ActivityUserDataBinding
import org.dolphinemu.dolphinemu.dialogs.NotificationDialog
import org.dolphinemu.dolphinemu.dialogs.TaskDialog
import org.dolphinemu.dolphinemu.dialogs.UserDataImportWarningDialog
import org.dolphinemu.dolphinemu.features.DocumentProvider
import org.dolphinemu.dolphinemu.model.TaskViewModel
import org.dolphinemu.dolphinemu.utils.*
import org.dolphinemu.dolphinemu.utils.ThemeHelper.enableScrollTint
@ -49,6 +51,7 @@ class UserDataActivity : AppCompatActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)
val android7 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
val android10 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
val android11 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
val legacy = DirectoryInitialization.isUsingLegacyUserDirectory()
@ -57,6 +60,10 @@ class UserDataActivity : AppCompatActivity() {
if (android10) R.string.user_data_new_location_android_10 else R.string.user_data_new_location
mBinding.textType.setText(if (legacy) R.string.user_data_old_location else userDataNewLocation)
val openFileManagerStringId =
if (android7) R.string.user_data_open_user_folder else R.string.user_data_open_system_file_manager
mBinding.buttonOpenSystemFileManager.setText(openFileManagerStringId)
mBinding.textPath.text = DirectoryInitialization.getUserDirectory()
mBinding.textAndroid11.visibility = if (android11 && !legacy) View.VISIBLE else View.GONE
@ -112,27 +119,43 @@ class UserDataActivity : AppCompatActivity() {
}
private fun openFileManager() {
// First, try to open the user data folder directly
try {
// First, try the package name used on "normal" phones
startActivity(getFileManagerIntent("com.google.android.documentsui"))
} catch (e: ActivityNotFoundException) {
try {
// Next, try the AOSP package name
startActivity(getFileManagerIntent("com.android.documentsui"))
} catch (e2: ActivityNotFoundException) {
// Activity not found. Perhaps it was removed by the OEM, or by some new Android version
// that didn't exist at the time of writing. Not much we can do other than tell the user.
val arguments = Bundle()
arguments.putInt(
NotificationDialog.KEY_MESSAGE,
R.string.user_data_open_system_file_manager_failed
)
startActivity(getFileManagerIntentOnDocumentProvider(Intent.ACTION_VIEW))
return
} catch (_: ActivityNotFoundException) {}
val dialog = NotificationDialog()
dialog.arguments = arguments
dialog.show(supportFragmentManager, NotificationDialog.TAG)
}
}
try {
startActivity(getFileManagerIntentOnDocumentProvider("android.provider.action.BROWSE"))
return
} catch (_: ActivityNotFoundException) {}
try {
// Just try to open the file manager, try the package name used on "normal" phones
startActivity(getFileManagerIntent("com.google.android.documentsui"))
return
} catch (_: ActivityNotFoundException) {}
try {
// Next, try the AOSP package name
startActivity(getFileManagerIntent("com.android.documentsui"))
return
} catch (_: ActivityNotFoundException) {}
try {
// Activity not found. Perhaps it was removed by the OEM, or by some new Android version
// that didn't exist at the time of writing. Not much we can do other than tell the user.
val arguments = Bundle()
arguments.putInt(
NotificationDialog.KEY_MESSAGE,
R.string.user_data_open_system_file_manager_failed
)
val dialog = NotificationDialog()
dialog.arguments = arguments
dialog.show(supportFragmentManager, NotificationDialog.TAG)
return
} catch (_: ActivityNotFoundException) {}
}
private fun getFileManagerIntent(packageName: String): Intent {
@ -143,6 +166,15 @@ class UserDataActivity : AppCompatActivity() {
return intent
}
private fun getFileManagerIntentOnDocumentProvider(action: String): Intent {
val authority = "$packageName.user"
val intent = Intent(action)
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.data = DocumentsContract.buildRootUri(authority, DocumentProvider.ROOT_ID)
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION or Intent.FLAG_GRANT_PREFIX_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
return intent
}
private fun importUserData() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.type = "application/zip"

View File

@ -27,7 +27,7 @@ class DocumentProvider : DocumentsProvider() {
private var rootDirectory: File? = null
companion object {
private const val ROOT_ID = "root"
public const val ROOT_ID = "root"
private val DEFAULT_ROOT_PROJECTION = arrayOf(
DocumentsContract.Root.COLUMN_ROOT_ID,

View File

@ -401,8 +401,10 @@
<string name="user_data_new_location">Your user data (settings, saves, etc.) is stored in a location which <b>will be deleted</b> when you uninstall the app:</string>
<!-- Android 10 and up support android:hasFragileUserData -->
<string name="user_data_new_location_android_10">Your user data (settings, saves, etc.) is stored in a location which by default <b>will be deleted</b> when you uninstall the app:</string>
<string name="user_data_new_location_android_11">Because you\'re using Android 11 or newer, file manager apps can\'t access this folder in the same way as regular folders. You might be able to access the folder using the system file manager (if present on your device), or by connecting your device to a PC.</string>
<string name="user_data_new_location_android_11">Because you\'re using Android 11 or newer, file manager apps can\'t access this folder in the same way as regular folders. Instead, Dolphin acts as a file provider.</string>
<string name="user_data_open_system_file_manager">Open System File Manager</string>
<string name="user_data_open_user_folder">Open User Data Folder</string>
<string name="user_data_import">Import User Data</string>
<string name="user_data_export">Export User Data</string>
<string name="user_data_open_system_file_manager_failed">Sorry, Dolphin couldn\'t find the system file manager on your device.</string>