Android: Open DocumentProvider directly when tapping the File Manager button
This commit is contained in:
parent
002a96adb0
commit
43d346aba4
|
@ -8,6 +8,7 @@ import android.content.Intent
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.provider.DocumentsContract
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.view.ViewCompat
|
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.NotificationDialog
|
||||||
import org.dolphinemu.dolphinemu.dialogs.TaskDialog
|
import org.dolphinemu.dolphinemu.dialogs.TaskDialog
|
||||||
import org.dolphinemu.dolphinemu.dialogs.UserDataImportWarningDialog
|
import org.dolphinemu.dolphinemu.dialogs.UserDataImportWarningDialog
|
||||||
|
import org.dolphinemu.dolphinemu.features.DocumentProvider
|
||||||
import org.dolphinemu.dolphinemu.model.TaskViewModel
|
import org.dolphinemu.dolphinemu.model.TaskViewModel
|
||||||
import org.dolphinemu.dolphinemu.utils.*
|
import org.dolphinemu.dolphinemu.utils.*
|
||||||
import org.dolphinemu.dolphinemu.utils.ThemeHelper.enableScrollTint
|
import org.dolphinemu.dolphinemu.utils.ThemeHelper.enableScrollTint
|
||||||
|
@ -112,27 +114,43 @@ class UserDataActivity : AppCompatActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openFileManager() {
|
private fun openFileManager() {
|
||||||
|
// First, try to open the user data folder directly
|
||||||
try {
|
try {
|
||||||
// First, try the package name used on "normal" phones
|
startActivity(getFileManagerIntentOnDocumentProvider(Intent.ACTION_VIEW))
|
||||||
startActivity(getFileManagerIntent("com.google.android.documentsui"))
|
return
|
||||||
} catch (e: ActivityNotFoundException) {
|
} catch (_: 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
|
|
||||||
)
|
|
||||||
|
|
||||||
val dialog = NotificationDialog()
|
try {
|
||||||
dialog.arguments = arguments
|
startActivity(getFileManagerIntentOnDocumentProvider("android.provider.action.BROWSE"))
|
||||||
dialog.show(supportFragmentManager, NotificationDialog.TAG)
|
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 {
|
private fun getFileManagerIntent(packageName: String): Intent {
|
||||||
|
@ -143,6 +161,15 @@ class UserDataActivity : AppCompatActivity() {
|
||||||
return intent
|
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() {
|
private fun importUserData() {
|
||||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
|
||||||
intent.type = "application/zip"
|
intent.type = "application/zip"
|
||||||
|
|
|
@ -27,7 +27,7 @@ class DocumentProvider : DocumentsProvider() {
|
||||||
private var rootDirectory: File? = null
|
private var rootDirectory: File? = null
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val ROOT_ID = "root"
|
public const val ROOT_ID = "root"
|
||||||
|
|
||||||
private val DEFAULT_ROOT_PROJECTION = arrayOf(
|
private val DEFAULT_ROOT_PROJECTION = arrayOf(
|
||||||
DocumentsContract.Root.COLUMN_ROOT_ID,
|
DocumentsContract.Root.COLUMN_ROOT_ID,
|
||||||
|
|
Loading…
Reference in New Issue