-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add root ID and path editing for external storage shortcut
Fixes: #1206
- Loading branch information
Showing
41 changed files
with
265 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
app/src/main/java/me/zhanghai/android/files/file/ExternalStorageUri.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2024 Hai Zhang <[email protected]> | ||
* All Rights Reserved. | ||
*/ | ||
|
||
package me.zhanghai.android.files.file | ||
|
||
import android.net.Uri | ||
import android.os.Parcelable | ||
import android.provider.DocumentsContract | ||
import kotlinx.parcelize.Parcelize | ||
import kotlinx.parcelize.WriteWith | ||
import me.zhanghai.android.files.compat.DocumentsContractCompat | ||
import me.zhanghai.android.files.util.StableUriParceler | ||
|
||
@Parcelize | ||
@JvmInline | ||
value class ExternalStorageUri(val value: @WriteWith<StableUriParceler> Uri) : Parcelable { | ||
constructor(rootId: String, path: String) : this( | ||
DocumentsContract.buildDocumentUriUsingTree( | ||
DocumentsContract.buildTreeDocumentUri( | ||
DocumentsContractCompat.EXTERNAL_STORAGE_PROVIDER_AUTHORITY, rootId | ||
), "$rootId:$path" | ||
) | ||
) | ||
|
||
val rootId: String | ||
get() = DocumentsContract.getTreeDocumentId(value) | ||
|
||
val path: String | ||
get() = DocumentsContract.getDocumentId(value).removePrefix("$rootId:") | ||
} | ||
|
||
fun Uri.asExternalStorageUriOrNull(): ExternalStorageUri? = | ||
if (isExternalStorageUri) ExternalStorageUri(this) else null | ||
|
||
fun Uri.asExternalStorageUri(): ExternalStorageUri { | ||
require(isExternalStorageUri) | ||
return ExternalStorageUri(this) | ||
} | ||
|
||
/** @see DocumentsContractCompat.isDocumentUri */ | ||
private val Uri.isExternalStorageUri: Boolean | ||
get() = | ||
DocumentsContractCompat.isDocumentUri(this) && | ||
authority == DocumentsContractCompat.EXTERNAL_STORAGE_PROVIDER_AUTHORITY && | ||
pathSegments.size == 4 | ||
|
||
val ExternalStorageUri.displayName: String | ||
get() = | ||
if (rootId == DocumentsContractCompat.EXTERNAL_STORAGE_PRIMARY_EMULATED_ROOT_ID) { | ||
path | ||
} else { | ||
DocumentsContract.getDocumentId(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.