Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -751,12 +751,12 @@ abstract class BaseSimpleActivity : AppCompatActivity() {
}
}

fun handleAndroidSAFDialog(path: String, callback: (success: Boolean) -> Unit): Boolean {
fun handleAndroidSAFDialog(path: String, openInSystemAppAllowed: Boolean = false, callback: (success: Boolean) -> Unit): Boolean {
hideKeyboard()
return if (!packageName.startsWith("org.fossify")) {
callback(true)
false
} else if (isShowingAndroidSAFDialog(path)) {
} else if (isShowingAndroidSAFDialog(path, openInSystemAppAllowed)) {
funAfterSAFPermission = callback
true
} else {
Expand Down
98 changes: 76 additions & 22 deletions commons/src/main/kotlin/org/fossify/commons/extensions/Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -226,31 +226,29 @@ fun BaseSimpleActivity.isShowingSAFCreateDocumentDialogSdk30(path: String): Bool
}
}

fun BaseSimpleActivity.isShowingAndroidSAFDialog(path: String): Boolean {
fun BaseSimpleActivity.isShowingAndroidSAFDialog(path: String, openInSystemAppAllowed: Boolean = false): Boolean {
return if (isRestrictedSAFOnlyRoot(path) && (getAndroidTreeUri(path).isEmpty() || !hasProperStoredAndroidTreeUri(path))) {
runOnUiThread {
if (!isDestroyed && !isFinishing) {
ConfirmationAdvancedDialog(this, "", R.string.confirm_storage_access_android_text, R.string.ok, R.string.cancel) { success ->
if (success) {
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE).apply {
putExtra(EXTRA_SHOW_ADVANCED, true)
putExtra(DocumentsContract.EXTRA_INITIAL_URI, createAndroidDataOrObbUri(path))
try {
startActivityForResult(this, OPEN_DOCUMENT_TREE_FOR_ANDROID_DATA_OR_OBB)
checkedDocumentPath = path
return@apply
} catch (e: Exception) {
type = "*/*"
}

try {
startActivityForResult(this, OPEN_DOCUMENT_TREE_FOR_ANDROID_DATA_OR_OBB)
checkedDocumentPath = path
} catch (e: ActivityNotFoundException) {
toast(R.string.system_service_disabled, Toast.LENGTH_LONG)
} catch (e: Exception) {
toast(R.string.unknown_error_occurred)
}
if (!openInSystemAppAllowed) {
ConfirmationDialog(
this,
"",
R.string.confirm_storage_access_restricted_text,
positive = android.R.string.ok,
negative = 0
) {}
} else {
ConfirmationAdvancedDialog(
this,
"",
R.string.confirm_storage_access_restricted_text,
R.string.confirm_storage_access_restricted_text_open_system,
R.string.cancel
) { success ->
if (success) {
val uri = createAndroidDataOrObbUri(path)
launchSystemFileManager(uri)
}
}
}
Expand All @@ -262,6 +260,62 @@ fun BaseSimpleActivity.isShowingAndroidSAFDialog(path: String): Boolean {
}
}

/**
* Launch system file manager by testing different possible intents depending on the device
* Each intent is tested in a OR condition which allows to stop at the first successful one
*/
fun BaseSimpleActivity.launchSystemFileManager(uri: Uri) {
if (
startIntentForUriAction(
uri,
"android.intent.action.VIEW",
ComponentName("com.google.android.documentsui", "com.android.documentsui.files.FilesActivity")
) ||
startIntentForUriAction(
uri,
"android.intent.action.VIEW",
ComponentName("com.android.documentsui", "com.android.documentsui.files.FilesActivity")
) ||
startIntentForUriAction(
uri,
"android.intent.action.VIEW",
ComponentName("com.android.documentsui", "com.android.documentsui.FilesActivity")
) ||
startIntentForUriAction(uri, "android.intent.action.VIEW", null) ||
startIntentForUriAction(uri, "android.provider.action.BROWSE", null) ||
startIntentForUriAction(uri, "android.provider.action.BROWSE_DOCUMENT_ROOT", null)
) {
return
} else {
toast(R.string.confirm_storage_access_restricted_text_not_found)
}
}

/**
* Start the intent
* @param uri URI to pass to the intent
* @param action Action of the intent
* @param componentName Optional ComponentName
* @return true if the intent was successful
*/
fun BaseSimpleActivity.startIntentForUriAction(
uri: Uri,
action: String,
componentName: ComponentName?
): Boolean {
val intent = Intent(action, uri)
if (componentName != null) {
intent.setComponent(componentName)
}
return try {
startActivity(intent)
true
} catch (e: java.lang.Exception) {
e.printStackTrace()
false
}
}

fun BaseSimpleActivity.isShowingOTGDialog(path: String): Boolean {
return if (!isRPlus() && isPathOnOTG(path) && (baseConfig.OTGTreeUri.isEmpty() || !hasProperStoredTreeUri(true))) {
showOTGPermissionDialog(path)
Expand Down
3 changes: 3 additions & 0 deletions commons/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@
<string name="confirm_storage_access_text_sd">If you don\'t see the SD card, try this</string>
<string name="confirm_storage_access_android_text">Please allow the app accessing the selected storage on the next screen by pressing \'Use this folder\' at the bottom.</string>
<string name="confirm_storage_access_android_text_specific">Please allow access to \'&lt;b&gt;%s&lt;/b&gt;\' on the next screen by pressing \'&lt;b&gt;Use this folder&lt;/b&gt;\' at the bottom.</string>
<string name="confirm_storage_access_restricted_text">Due to system restrictions, this folder is not available.</string>
<string name="confirm_storage_access_restricted_text_open_system">Open in system app</string>
<string name="confirm_storage_access_restricted_text_not_found">A system file manager app could not be found.</string>
<string name="confirm_create_doc_for_new_folder_text">Please press \'&lt;b&gt;Save&lt;/b&gt;\' at the bottom of the next screen to create the new folder.</string>
<string name="confirm_selection">Confirm selection</string>
<string name="loading">Loading…</string>
Expand Down
Loading