Skip to content

Commit

Permalink
have internal links open storage provider
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko committed Feb 4, 2024
1 parent 9635409 commit ab1d0c9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
19 changes: 18 additions & 1 deletion app/src/main/java/com/geode/launcher/UserDirectoryProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.geode.launcher
import android.database.Cursor
import android.database.MatrixCursor
import android.net.Uri
import android.os.Build
import android.os.CancellationSignal
import android.os.ParcelFileDescriptor
import android.provider.DocumentsContract
import android.provider.DocumentsProvider
import android.webkit.MimeTypeMap
import androidx.annotation.RequiresApi
import com.geode.launcher.utils.LaunchUtils
import java.io.File

Expand Down Expand Up @@ -150,7 +152,7 @@ class UserDirectoryProvider : DocumentsProvider() {
notifyFileChange(file)
}

override fun renameDocument(documentId: String, displayName: String): String? {
override fun renameDocument(documentId: String, displayName: String): String {
val file = getFileForDocumentId(documentId)
val dest = findFileNameForNewFile(File(file.parentFile, displayName))
file.renameTo(dest)
Expand Down Expand Up @@ -203,4 +205,19 @@ class UserDirectoryProvider : DocumentsProvider() {
val uri = getDocumentUri(parentDocumentId)
context!!.contentResolver.notifyChange(uri, null)
}

@RequiresApi(Build.VERSION_CODES.O)
override fun findDocumentPath(
parentDocumentId: String?,
childDocumentId: String
): DocumentsContract.Path {
if (!parentDocumentId.isNullOrEmpty()) {
// not implementing this for now...
return super.findDocumentPath(parentDocumentId, childDocumentId)
}

val nonRootPath = listOf(ROOT) + childDocumentId.removePrefix(ROOT).split("/")

return DocumentsContract.Path(ROOT, nonRootPath)
}
}
49 changes: 37 additions & 12 deletions app/src/main/java/com/geode/launcher/utils/GeodeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.core.content.ContextCompat
import androidx.documentfile.provider.DocumentFile
import com.geode.launcher.BuildConfig
import com.geode.launcher.R
import com.geode.launcher.UserDirectoryProvider
import com.geode.launcher.activityresult.GeodeOpenFileActivityResult
import com.geode.launcher.activityresult.GeodeOpenFilesActivityResult
import com.geode.launcher.activityresult.GeodeSaveFileActivityResult
Expand Down Expand Up @@ -167,24 +168,48 @@ object GeodeUtils {
// TODO As of now this is unused
@JvmStatic
fun openFolder(path: String): Boolean {
activity.get()?.run {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addCategory(Intent.CATEGORY_OPENABLE)
val context = activity.get()!!

val pathFile = File(path)
val baseDirectory = LaunchUtils.getBaseDirectory(context)
val isInternalPath = pathFile.startsWith(baseDirectory)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val documentFile = DocumentFile.fromFile(File(path))
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, documentFile.uri)
val intent = if (isInternalPath) {
// TODO: figure out how to get this to point to the path it should be pointing at
// (the best i got was pointing at a file)
// val relativePath = pathFile.relativeTo(baseDirectory)

Intent(Intent.ACTION_VIEW).apply {
data = DocumentsContract.buildRootUri(
"${context.packageName}.user", UserDirectoryProvider.ROOT
)

addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION or
Intent.FLAG_GRANT_PREFIX_URI_PERMISSION or
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
)
}
} else {
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addCategory(Intent.CATEGORY_OPENABLE)

intent.setType("*/*")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val documentFile = DocumentFile.fromFile(File(path))
putExtra(DocumentsContract.EXTRA_INITIAL_URI, documentFile.uri)
}

if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
return true
type = "*/*"
}
}
return false

return try {
context.startActivity(intent)
true
} catch (_: ActivityNotFoundException) {
false
}
}

private external fun selectFileCallback(path: String)
Expand Down

0 comments on commit ab1d0c9

Please sign in to comment.