diff options
author | euxane | 2024-09-18 23:41:13 +0200 |
---|---|---|
committer | euxane | 2024-09-18 23:41:13 +0200 |
commit | a63f59f2bc8f58140923abb459a98c595dbcb7f4 (patch) | |
tree | 8b2f6d293107f2fa4c4b9f3ebb724bb86fa41585 /app/src/main/java/org | |
parent | 8cb2bff8042f9647b159c9121721f0bdae587692 (diff) | |
download | tincapp-a63f59f2bc8f58140923abb459a98c595dbcb7f4.tar.gz |
storageprovider: move documents URI from intent util
Diffstat (limited to 'app/src/main/java/org')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt | 14 | ||||
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt | 18 |
2 files changed, 15 insertions, 17 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt b/app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt index 4705c0b..a4cc5c5 100644 --- a/app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt +++ b/app/src/main/java/org/pacien/tincapp/storageprovider/BrowseFilesIntents.kt | |||
@@ -21,23 +21,13 @@ package org.pacien.tincapp.storageprovider | |||
21 | import android.content.Context | 21 | import android.content.Context |
22 | import android.content.Intent | 22 | import android.content.Intent |
23 | import android.net.Uri | 23 | import android.net.Uri |
24 | import android.provider.DocumentsContract | ||
25 | import android.provider.DocumentsContract.Document | 24 | import android.provider.DocumentsContract.Document |
26 | import org.pacien.tincapp.BuildConfig | ||
27 | 25 | ||
28 | object BrowseFilesIntents { | 26 | object BrowseFilesIntents { |
29 | private const val URI_AUTHORITY = BuildConfig.APPLICATION_ID + ".files" | ||
30 | |||
31 | fun documentUri(documentId: String) = | ||
32 | DocumentsContract.buildDocumentUri(URI_AUTHORITY, documentId) | ||
33 | |||
34 | fun childDocumentsUri(parentDocumentId: String) = | ||
35 | DocumentsContract.buildChildDocumentsUri(URI_AUTHORITY, parentDocumentId) | ||
36 | |||
37 | fun openDocumentTree(context: Context, documentId: String) = | 27 | fun openDocumentTree(context: Context, documentId: String) = |
38 | openDocumentTree(context, documentUri(documentId)) | 28 | openDocumentTree(context, FilesDocumentsProvider.documentUri(documentId)) |
39 | 29 | ||
40 | fun openDocumentTree(context: Context, contentUri: Uri) { | 30 | private fun openDocumentTree(context: Context, contentUri: Uri) { |
41 | val intent = Intent(Intent.ACTION_VIEW).apply { | 31 | val intent = Intent(Intent.ACTION_VIEW).apply { |
42 | setDataAndType(contentUri, Document.MIME_TYPE_DIR) | 32 | setDataAndType(contentUri, Document.MIME_TYPE_DIR) |
43 | addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) | 33 | addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
diff --git a/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt b/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt index 48d0319..80b7331 100644 --- a/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt +++ b/app/src/main/java/org/pacien/tincapp/storageprovider/FilesDocumentsProvider.kt | |||
@@ -29,6 +29,7 @@ import android.provider.DocumentsContract.Document | |||
29 | import android.provider.DocumentsContract.Root | 29 | import android.provider.DocumentsContract.Root |
30 | import android.provider.DocumentsProvider | 30 | import android.provider.DocumentsProvider |
31 | import androidx.annotation.RequiresApi | 31 | import androidx.annotation.RequiresApi |
32 | import org.pacien.tincapp.BuildConfig | ||
32 | import org.pacien.tincapp.R | 33 | import org.pacien.tincapp.R |
33 | import org.pacien.tincapp.context.AppPaths | 34 | import org.pacien.tincapp.context.AppPaths |
34 | import org.pacien.tincapp.utils.isParentOf | 35 | import org.pacien.tincapp.utils.isParentOf |
@@ -41,8 +42,9 @@ import kotlin.io.path.relativeTo | |||
41 | 42 | ||
42 | class FilesDocumentsProvider : DocumentsProvider() { | 43 | class FilesDocumentsProvider : DocumentsProvider() { |
43 | companion object { | 44 | companion object { |
44 | const val ROOT_ID = "" | 45 | private const val URI_AUTHORITY = BuildConfig.APPLICATION_ID + ".files" |
45 | const val ROOT_DOCUMENT_ID = "/" | 46 | private const val ROOT_ID = "" |
47 | private const val ROOT_DOCUMENT_ID = "/" | ||
46 | const val VIRTUAL_ROOT_NETWORKS = "networks" | 48 | const val VIRTUAL_ROOT_NETWORKS = "networks" |
47 | const val VIRTUAL_ROOT_LOG = "log" | 49 | const val VIRTUAL_ROOT_LOG = "log" |
48 | 50 | ||
@@ -65,6 +67,12 @@ class FilesDocumentsProvider : DocumentsProvider() { | |||
65 | Document.COLUMN_FLAGS, | 67 | Document.COLUMN_FLAGS, |
66 | Document.COLUMN_SIZE, | 68 | Document.COLUMN_SIZE, |
67 | ) | 69 | ) |
70 | |||
71 | fun documentUri(documentId: String): Uri = | ||
72 | DocumentsContract.buildDocumentUri(URI_AUTHORITY, documentId) | ||
73 | |||
74 | fun childDocumentsUri(parentDocumentId: String): Uri = | ||
75 | DocumentsContract.buildChildDocumentsUri(URI_AUTHORITY, parentDocumentId) | ||
68 | } | 76 | } |
69 | 77 | ||
70 | override fun onCreate(): Boolean = true | 78 | override fun onCreate(): Boolean = true |
@@ -95,7 +103,7 @@ class FilesDocumentsProvider : DocumentsProvider() { | |||
95 | sortOrder: String? | 103 | sortOrder: String? |
96 | ): Cursor = | 104 | ): Cursor = |
97 | MatrixCursor(projection ?: DEFAULT_DOCUMENT_PROJECTION).apply { | 105 | MatrixCursor(projection ?: DEFAULT_DOCUMENT_PROJECTION).apply { |
98 | setNotificationUrl(BrowseFilesIntents.childDocumentsUri(parentDocumentId!!)) | 106 | setNotificationUrl(childDocumentsUri(parentDocumentId!!)) |
99 | when (parentDocumentId) { | 107 | when (parentDocumentId) { |
100 | ROOT_DOCUMENT_ID -> { | 108 | ROOT_DOCUMENT_ID -> { |
101 | addVirtualDirRow(VIRTUAL_ROOT_NETWORKS, Document.FLAG_DIR_SUPPORTS_CREATE) | 109 | addVirtualDirRow(VIRTUAL_ROOT_NETWORKS, Document.FLAG_DIR_SUPPORTS_CREATE) |
@@ -130,7 +138,7 @@ class FilesDocumentsProvider : DocumentsProvider() { | |||
130 | override fun deleteDocument(documentId: String?) { | 138 | override fun deleteDocument(documentId: String?) { |
131 | fileForDocumentId(documentId!!).apply { | 139 | fileForDocumentId(documentId!!).apply { |
132 | if (!deleteRecursively()) throw FileSystemException(this) | 140 | if (!deleteRecursively()) throw FileSystemException(this) |
133 | notifyChange(BrowseFilesIntents.childDocumentsUri(documentIdForFile(parentFile))) | 141 | notifyChange(childDocumentsUri(documentIdForFile(parentFile!!))) |
134 | } | 142 | } |
135 | } | 143 | } |
136 | 144 | ||
@@ -155,7 +163,7 @@ class FilesDocumentsProvider : DocumentsProvider() { | |||
155 | else -> createNewFile() | 163 | else -> createNewFile() |
156 | } | 164 | } |
157 | if (!success) throw FileSystemException(this) | 165 | if (!success) throw FileSystemException(this) |
158 | notifyChange(BrowseFilesIntents.childDocumentsUri(parentDocumentId)) | 166 | notifyChange(childDocumentsUri(parentDocumentId)) |
159 | }.let { | 167 | }.let { |
160 | documentIdForFile(it) | 168 | documentIdForFile(it) |
161 | } | 169 | } |