Skip to content

Commit 56512c9

Browse files
authored
fix(filesystem): don't request permissions on 11 or newer (#1690)
1 parent 3c57aa4 commit 56512c9

3 files changed

Lines changed: 48 additions & 26 deletions

File tree

filesystem/README.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ Read about [Configuring iOS](https://capacitorjs.com/docs/ios/configuration) for
2020

2121
## Android
2222

23-
If using <a href="#directory">`Directory.Documents`</a> or
24-
`Directory.ExternalStorage`, this API requires the following permissions be added to your `AndroidManifest.xml`:
23+
If using <a href="#directory">`Directory.Documents`</a> or <a href="#directory">`Directory.ExternalStorage`</a>, in Android 10 and older, this API requires the following permissions be added to your `AndroidManifest.xml`:
2524

2625
```xml
2726
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
@@ -30,8 +29,7 @@ If using <a href="#directory">`Directory.Documents`</a> or
3029

3130
Read about [Setting Permissions](https://capacitorjs.com/docs/android/configuration#setting-permissions) in the [Android Guide](https://capacitorjs.com/docs/android) for more information on setting Android permissions.
3231

33-
Note that <a href="#directory">`Directory.Documents`</a> and
34-
`Directory.ExternalStorage` are only available on Android 9 or older.
32+
Note that <a href="#directory">`Directory.ExternalStorage`</a> is only available on Android 9 or older and <a href="#directory">`Directory.Documents`</a> only allows to access the files/foders created by your app on Android on Android 11 and newer.
3533

3634
Working with large files may require you to add `android:largeHeap="true"` to the `<application>` tag in `AndroidManifest.xml`.
3735

@@ -469,7 +467,7 @@ Add a listener to file download progress events.
469467
| Prop | Type | Description | Since |
470468
| ----------- | ---------------------------------- | ------------------------------------------------------------------------------------ | ----- |
471469
| **`name`** | <code>string</code> | Name of the file or directory. | |
472-
| **`type`** | <code>'directory' \| 'file'</code> | Type of the file. | 4.0.0 |
470+
| **`type`** | <code>'file' \| 'directory'</code> | Type of the file. | 4.0.0 |
473471
| **`size`** | <code>number</code> | Size of the file in bytes. | 4.0.0 |
474472
| **`ctime`** | <code>number</code> | Time of creation in milliseconds. It's not available on Android 7 and older devices. | 4.0.0 |
475473
| **`mtime`** | <code>number</code> | Time of last modification in milliseconds. | 4.0.0 |
@@ -503,7 +501,7 @@ Add a listener to file download progress events.
503501

504502
| Prop | Type | Description | Since |
505503
| ----------- | ---------------------------------- | ------------------------------------------------------------------------------------ | ----- |
506-
| **`type`** | <code>'directory' \| 'file'</code> | Type of the file. | 1.0.0 |
504+
| **`type`** | <code>'file' \| 'directory'</code> | Type of the file. | 1.0.0 |
507505
| **`size`** | <code>number</code> | Size of the file in bytes. | 1.0.0 |
508506
| **`ctime`** | <code>number</code> | Time of creation in milliseconds. It's not available on Android 7 and older devices. | 1.0.0 |
509507
| **`mtime`** | <code>number</code> | Time of last modification in milliseconds. | 1.0.0 |
@@ -600,14 +598,14 @@ A listener function that receives progress events.
600598

601599
#### Directory
602600

603-
| Members | Value | Description | Since |
604-
| --------------------- | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
605-
| **`Documents`** | <code>'DOCUMENTS'</code> | The Documents directory On iOS it's the app's documents directory. Use this directory to store user-generated content. On Android it's the Public Documents folder, so it's accessible from other apps. It's not accesible on Android 10 unless the app enables legacy External Storage by adding `android:requestLegacyExternalStorage="true"` in the `application` tag in the `AndroidManifest.xml`. It's not accesible on Android 11 or newer. | 1.0.0 |
606-
| **`Data`** | <code>'DATA'</code> | The Data directory On iOS it will use the Documents directory. On Android it's the directory holding application files. Files will be deleted when the application is uninstalled. | 1.0.0 |
607-
| **`Library`** | <code>'LIBRARY'</code> | The Library directory On iOS it will use the Library directory. On Android it's the directory holding application files. Files will be deleted when the application is uninstalled. | 1.1.0 |
608-
| **`Cache`** | <code>'CACHE'</code> | The Cache directory Can be deleted in cases of low memory, so use this directory to write app-specific files that your app can re-create easily. | 1.0.0 |
609-
| **`External`** | <code>'EXTERNAL'</code> | The external directory On iOS it will use the Documents directory On Android it's the directory on the primary shared/external storage device where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media. Files will be deleted when the application is uninstalled. | 1.0.0 |
610-
| **`ExternalStorage`** | <code>'EXTERNAL_STORAGE'</code> | The external storage directory On iOS it will use the Documents directory On Android it's the primary shared/external storage directory. It's not accesible on Android 10 unless the app enables legacy External Storage by adding `android:requestLegacyExternalStorage="true"` in the `application` tag in the `AndroidManifest.xml`. It's not accesible on Android 11 or newer. | 1.0.0 |
601+
| Members | Value | Description | Since |
602+
| --------------------- | ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
603+
| **`Documents`** | <code>'DOCUMENTS'</code> | The Documents directory. On iOS it's the app's documents directory. Use this directory to store user-generated content. On Android it's the Public Documents folder, so it's accessible from other apps. It's not accesible on Android 10 unless the app enables legacy External Storage by adding `android:requestLegacyExternalStorage="true"` in the `application` tag in the `AndroidManifest.xml`. On Android 11 or newer the app can only access the files/folders the app created. | 1.0.0 |
604+
| **`Data`** | <code>'DATA'</code> | The Data directory. On iOS it will use the Documents directory. On Android it's the directory holding application files. Files will be deleted when the application is uninstalled. | 1.0.0 |
605+
| **`Library`** | <code>'LIBRARY'</code> | The Library directory. On iOS it will use the Library directory. On Android it's the directory holding application files. Files will be deleted when the application is uninstalled. | 1.1.0 |
606+
| **`Cache`** | <code>'CACHE'</code> | The Cache directory. Can be deleted in cases of low memory, so use this directory to write app-specific files. that your app can re-create easily. | 1.0.0 |
607+
| **`External`** | <code>'EXTERNAL'</code> | The external directory. On iOS it will use the Documents directory. On Android it's the directory on the primary shared/external storage device where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media. Files will be deleted when the application is uninstalled. | 1.0.0 |
608+
| **`ExternalStorage`** | <code>'EXTERNAL_STORAGE'</code> | The external storage directory. On iOS it will use the Documents directory. On Android it's the primary shared/external storage directory. It's not accesible on Android 10 unless the app enables legacy External Storage by adding `android:requestLegacyExternalStorage="true"` in the `application` tag in the `AndroidManifest.xml`. It's not accesible on Android 11 or newer. | 1.0.0 |
611609

612610

613611
#### Encoding

filesystem/android/src/main/java/com/capacitorjs/plugins/filesystem/FilesystemPlugin.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
)
3939
public class FilesystemPlugin extends Plugin {
4040

41+
static final String PUBLIC_STORAGE = "publicStorage";
4142
private Filesystem implementation;
4243

4344
@Override
@@ -437,6 +438,28 @@ private void _copy(PluginCall call, Boolean doRename) {
437438
}
438439
}
439440

441+
@PluginMethod
442+
public void checkPermissions(PluginCall call) {
443+
if (isStoragePermissionGranted()) {
444+
JSObject permissionsResultJSON = new JSObject();
445+
permissionsResultJSON.put(PUBLIC_STORAGE, "granted");
446+
call.resolve(permissionsResultJSON);
447+
} else {
448+
super.checkPermissions(call);
449+
}
450+
}
451+
452+
@PluginMethod
453+
public void requestPermissions(PluginCall call) {
454+
if (isStoragePermissionGranted()) {
455+
JSObject permissionsResultJSON = new JSObject();
456+
permissionsResultJSON.put(PUBLIC_STORAGE, "granted");
457+
call.resolve(permissionsResultJSON);
458+
} else {
459+
requestPermissionForAlias(PUBLIC_STORAGE, call, "permissionCallback");
460+
}
461+
}
462+
440463
@PermissionCallback
441464
private void permissionCallback(PluginCall call) {
442465
if (!isStoragePermissionGranted()) {
@@ -485,10 +508,11 @@ private void permissionCallback(PluginCall call) {
485508

486509
/**
487510
* Checks the the given permission is granted or not
488-
* @return Returns true if the permission is granted and false if it is denied.
511+
* @return Returns true if the app is running on Android 30 or newer or if the permission is already granted
512+
* or false if it is denied.
489513
*/
490514
private boolean isStoragePermissionGranted() {
491-
return getPermissionState("publicStorage") == PermissionState.GRANTED;
515+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || getPermissionState(PUBLIC_STORAGE) == PermissionState.GRANTED;
492516
}
493517

494518
/**

filesystem/src/definitions.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ export interface PermissionStatus {
1010

1111
export enum Directory {
1212
/**
13-
* The Documents directory
13+
* The Documents directory.
1414
* On iOS it's the app's documents directory.
1515
* Use this directory to store user-generated content.
1616
* On Android it's the Public Documents folder, so it's accessible from other apps.
1717
* It's not accesible on Android 10 unless the app enables legacy External Storage
1818
* by adding `android:requestLegacyExternalStorage="true"` in the `application` tag
1919
* in the `AndroidManifest.xml`.
20-
* It's not accesible on Android 11 or newer.
20+
* On Android 11 or newer the app can only access the files/folders the app created.
2121
*
2222
* @since 1.0.0
2323
*/
2424
Documents = 'DOCUMENTS',
2525

2626
/**
27-
* The Data directory
27+
* The Data directory.
2828
* On iOS it will use the Documents directory.
2929
* On Android it's the directory holding application files.
3030
* Files will be deleted when the application is uninstalled.
@@ -34,7 +34,7 @@ export enum Directory {
3434
Data = 'DATA',
3535

3636
/**
37-
* The Library directory
37+
* The Library directory.
3838
* On iOS it will use the Library directory.
3939
* On Android it's the directory holding application files.
4040
* Files will be deleted when the application is uninstalled.
@@ -44,17 +44,17 @@ export enum Directory {
4444
Library = 'LIBRARY',
4545

4646
/**
47-
* The Cache directory
48-
* Can be deleted in cases of low memory, so use this directory to write app-specific files
47+
* The Cache directory.
48+
* Can be deleted in cases of low memory, so use this directory to write app-specific files.
4949
* that your app can re-create easily.
5050
*
5151
* @since 1.0.0
5252
*/
5353
Cache = 'CACHE',
5454

5555
/**
56-
* The external directory
57-
* On iOS it will use the Documents directory
56+
* The external directory.
57+
* On iOS it will use the Documents directory.
5858
* On Android it's the directory on the primary shared/external
5959
* storage device where the application can place persistent files it owns.
6060
* These files are internal to the applications, and not typically visible
@@ -66,8 +66,8 @@ export enum Directory {
6666
External = 'EXTERNAL',
6767

6868
/**
69-
* The external storage directory
70-
* On iOS it will use the Documents directory
69+
* The external storage directory.
70+
* On iOS it will use the Documents directory.
7171
* On Android it's the primary shared/external storage directory.
7272
* It's not accesible on Android 10 unless the app enables legacy External Storage
7373
* by adding `android:requestLegacyExternalStorage="true"` in the `application` tag

0 commit comments

Comments
 (0)