Skip to content

Commit f1469d6

Browse files
author
LaksCastro
committed
Merge branch 'release' of https://github.com/LaksCastro/shared-storage into release
2 parents 20d026c + 565c329 commit f1469d6

26 files changed

+914
-449
lines changed

.github/workflows/publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ jobs:
1212
- uses: dart-lang/setup-dart@v1
1313

1414
- name: Run Pub Publish
15-
run: dart pub publish --dry-run --force
15+
run: dart pub publish --dry-run --force

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
.history
1010
.svn/
1111

12+
*/**/pubspec.lock
13+
1214
# IntelliJ related
1315
*.iml
1416
*.ipr

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.2.0
2+
3+
Add basic support for `Storage Access Framework` and `targetSdk 31`
4+
5+
- The package now supports basic intents from `Storage Access Framework`
6+
- Your App needs update the `build.gradle` by targeting the current sdk to `31`
7+
18
## 0.1.1
29

310
Minor improvements on `pub.dev` documentation

README.md

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Plugin to fetch Android shared storage/folders info
99
- _**Android Only**_
1010
- _**Alpha version**_
1111
- _**Supports Android 4.1+ (API Level 16+)**_
12+
- _**The `targetSdk` should be set to `31`**_
1213

1314
### Features
1415

@@ -19,8 +20,8 @@ This plugin allow us to get path of top-level shared folder (Downloads, DCIM, Vi
1920
```dart
2021
/// Get Android [downloads] top-level shared folder
2122
/// You can also create a reference to a custom directory as: `EnvironmentDirectory.custom('Custom Folder')`
22-
final sharedDirectory =
23-
await getExternalStoragePublicDirectory(EnvironmentDirectory.downloads);
23+
final sharedDirectory =
24+
await getExternalStoragePublicDirectory(EnvironmentDirectory.downloads);
2425
2526
print(sharedDirectory.path); /// `/storage/emulated/0/Download`
2627
```
@@ -29,26 +30,70 @@ print(sharedDirectory.path); /// `/storage/emulated/0/Download`
2930

3031
```dart
3132
/// Get Android [downloads] shared folder for Android 9+
32-
final sharedDirectory =
33+
final sharedDirectory =
3334
await getMediaStoreContentDirectory(MediaStoreCollection.downloads);
3435
3536
print(sharedDirectory.path); /// `/external/downloads`
3637
```
3738

38-
- Get root Android path, note that is a read-only folder
39+
- Start `OPEN_DOCUMENT_TREE` activity to prompt user to select an folder to enable write and read access to be used by the `Storage Access Framework` API
3940

4041
```dart
41-
/// Get Android root folder
42-
final sharedDirectory = await getRootDirectory();
42+
/// Get permissions to manage an Android directory
43+
final selectedUriDir = await openDocumentTree();
4344
44-
print(sharedDirectory.path); /// `/system`
45+
print(selectedUriDir);
46+
```
47+
48+
- Create a new file using the `SAF` API
49+
50+
```dart
51+
/// Create a new file using the `SAF` API
52+
final newDocumentFile = await createDocumentFile(
53+
mimeType: ' text/plain',
54+
content: 'My Plain Text Comment Created by shared_storage plugin',
55+
displayName: 'CreatedBySharedStorageFlutterPlugin',
56+
directory: anySelectedUriByTheOpenDocumentTreeAPI,
57+
);
58+
59+
print(newDocumentFile);
60+
```
61+
62+
- Get all persisted [URI]s by the `openDocumentTree` API, from `SAF` API
63+
64+
```dart
65+
/// You have [write] and [read] access to all persisted [URI]s
66+
final listOfPersistedUris = await persistedUriPermissions();
67+
68+
print(listOfPersistedUris);
69+
```
70+
71+
- Revoke a current persisted [URI], from `SAF` API
72+
73+
```dart
74+
/// Can be any [URI] returned by the `persistedUriPermissions`
75+
final uri = ...;
76+
77+
/// After calling this, you no longer has access to the [uri]
78+
await releasePersistableUriPermission(uri);
79+
```
80+
81+
- Convenient method to know if a given [uri] is a persisted `uri` ("persisted uri" means that you have `write` and `read` access to the `uri` even if devices reboot)
82+
83+
```dart
84+
/// Can be any [URI], but the method will only return [true] if the [uri]
85+
/// is also present in the list returned by `persistedUriPermissions`
86+
final uri = ...;
87+
88+
/// Verify if you have [write] and [read] access to a given [uri]
89+
final isPersisted = await isPersistedUri(uri);
4590
```
4691

4792
### Android API's
4893

4994
Most Flutter plugins uses Android API's under the hood. So this plugin do the same, and to retrieve Android shared folder paths the following API's are being used:
5095

51-
[`🔗 android.os.Environment`](https://developer.android.com/reference/android/os/Environment#summary) [`🔗 android.provider.MediaStore`](https://developer.android.com/reference/android/provider/MediaStore#summary)
96+
[`🔗android.os.Environment`](https://developer.android.com/reference/android/os/Environment#summary) [`🔗android.provider.MediaStore`](https://developer.android.com/reference/android/provider/MediaStore#summary) [`🔗android.provider.DocumentsProvider`](https://developer.android.com/guide/topics/providers/document-provider)
5297

5398
<br>
5499

android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
group 'com.pinciat.external_path'
1+
group 'io.lakscastro.sharedstorage'
22
version '1.0-SNAPSHOT'
33

44
buildscript {
@@ -37,4 +37,5 @@ android {
3737

3838
dependencies {
3939
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
40+
implementation "androidx.documentfile:documentfile:1.0.1"
4041
}

android/settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'external_path'
1+
rootProject.name = 'sharedstorage'
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.pinciat.external_path">
2+
package="io.lakscastro.sharedstorage">
33
</manifest>

android/src/main/kotlin/io/lakscastro/sharedstorage/EnvironmentDirectoryOf.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package io.lakscastro.sharedstorage
22

33
import android.os.Environment
4+
import java.io.File
45

5-
fun environmentDirectoryOf(directory: String): String {
6+
fun environmentDirectoryOf(directory: String): File {
67
val mapper = mapOf(
78
"EnvironmentDirectory.Alarms" to Environment.DIRECTORY_ALARMS,
89
"EnvironmentDirectory.DCIM" to Environment.DIRECTORY_DCIM,
@@ -15,5 +16,5 @@ fun environmentDirectoryOf(directory: String): String {
1516
"EnvironmentDirectory.Ringtones" to Environment.DIRECTORY_RINGTONES
1617
)
1718

18-
return mapper[directory] ?: directory
19+
return Environment.getExternalStoragePublicDirectory(mapper[directory] ?: directory)
1920
}

0 commit comments

Comments
 (0)