This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[path_provider] Android: Support multiple external storage options #2049
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
f9bca2b
Add support for Android devices with multiple external storage options.
ened 6414953
Merge branch 'master' into path-provider-android
e5fe50b
Update path_provider AGB & iOS project file
8c5b266
extend unit tests for new methods
8803465
[path_provider] Activate getApplicationSupportDirectory integration test
ecc8aca
Merge branch 'path_provider/integration_test' into path-provider-android
280f229
add integration tests
2a3e4d4
[path_provider] Activate getApplicationSupportDirectory integration test
d6f66f2
Merge branch 'path_provider/integration_test' into path-provider-android
72a943b
Add missing unit tests
b4459b3
Merge branch 'path_provider/integration_test' into path-provider-android
7323e37
Formatting
1cfb256
Merge branch 'path_provider/integration_test' into path-provider-android
2736545
Merge branch 'master' of https://github.com/flutter/plugins into path…
e05e869
Merge branch 'master' into path-provider-android
e577e50
Merge branch 'master' of https://github.com/flutter/plugins into path…
f2941d9
Merge branch 'master' of https://github.com/flutter/plugins into path…
cf475e1
[path_provider] Updates tests and copy Android environment names.
354c9fa
Bump version
b60142f
Revert unrelated PR changes.
1b5f73a
Resolve test issues introduced in #1953.
d08d778
Formatting.
11620f0
Remove unused import
6ce45b1
Merge branch 'master' into path-provider-android
ened e8c948b
{} nits and ' strings
ened a175402
Introduce a enum to specify the storage directory
ened 30465c3
Refactor test driver and remove now unused uuid dependency.
ened b3c78db
Missing doc nit
ened 499fbdb
dartfmt
ened 321c331
add missing storage directory types
ened d368c80
dartfmt once more
ened 9806d05
Merge branch 'master' into path-provider-android
ened 8937f5d
Merge branch 'master' of https://github.com/flutter/plugins into path…
ened abf34d6
Activate JUnit
ened 3d96cc9
Pass a dart enum (by index) directly to Android, update tests.
ened b7d4b58
Add unit tests for the storage directory enum.
ened 30f49ab
nit
ened 7c1be07
Formatting.
ened c8d7572
Formatting.
ened 408a3bb
Merge branch 'master' into path-provider-android
ened 4e9a465
Merge branch 'master' into path-provider-android
ened 76029b6
add clear exception when documents directory is unsupported
ened bc55bb2
Bump to V1.4.0 due to public API changes
ened File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
51 changes: 51 additions & 0 deletions
51
...rovider/android/src/main/java/io/flutter/plugins/pathprovider/StorageDirectoryMapper.java
This file contains hidden or 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,51 @@ | ||
| package io.flutter.plugins.pathprovider; | ||
|
|
||
| import android.os.Build.VERSION; | ||
| import android.os.Build.VERSION_CODES; | ||
| import android.os.Environment; | ||
|
|
||
| /** Helps to map the Dart `StorageDirectory` enum to a Android system constant. */ | ||
| class StorageDirectoryMapper { | ||
|
|
||
| /** | ||
| * Return a Android Environment constant for a Dart Index. | ||
| * | ||
| * @return The correct Android Environment constant or null, if the index is null. | ||
| * @throws IllegalArgumentException If `dartIndex` is not null but also not matches any known | ||
| * index. | ||
| */ | ||
| static String androidType(Integer dartIndex) throws IllegalArgumentException { | ||
| if (dartIndex == null) { | ||
| return null; | ||
| } | ||
|
|
||
| switch (dartIndex) { | ||
| case 0: | ||
| return Environment.DIRECTORY_MUSIC; | ||
| case 1: | ||
| return Environment.DIRECTORY_PODCASTS; | ||
| case 2: | ||
| return Environment.DIRECTORY_RINGTONES; | ||
| case 3: | ||
| return Environment.DIRECTORY_ALARMS; | ||
| case 4: | ||
| return Environment.DIRECTORY_NOTIFICATIONS; | ||
| case 5: | ||
| return Environment.DIRECTORY_PICTURES; | ||
| case 6: | ||
| return Environment.DIRECTORY_MOVIES; | ||
| case 7: | ||
| return Environment.DIRECTORY_DOWNLOADS; | ||
| case 8: | ||
| return Environment.DIRECTORY_DCIM; | ||
| case 9: | ||
| if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) { | ||
| return Environment.DIRECTORY_DOCUMENTS; | ||
| } else { | ||
| throw new IllegalArgumentException("Documents directory is unsupported."); | ||
| } | ||
ened marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| default: | ||
| throw new IllegalArgumentException("Unknown index: " + dartIndex); | ||
| } | ||
| } | ||
| } | ||
40 changes: 40 additions & 0 deletions
40
...der/android/src/test/java/io/flutter/plugins/pathprovider/StorageDirectoryMapperTest.java
This file contains hidden or 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,40 @@ | ||
| package io.flutter.plugins.pathprovider; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import android.os.Environment; | ||
| import org.junit.Test; | ||
|
|
||
| public class StorageDirectoryMapperTest { | ||
|
|
||
| @org.junit.Test | ||
| public void testAndroidType_null() { | ||
| assertNull(StorageDirectoryMapper.androidType(null)); | ||
| } | ||
|
|
||
| @org.junit.Test | ||
| public void testAndroidType_valid() { | ||
| assertEquals(Environment.DIRECTORY_MUSIC, StorageDirectoryMapper.androidType(0)); | ||
| assertEquals(Environment.DIRECTORY_PODCASTS, StorageDirectoryMapper.androidType(1)); | ||
| assertEquals(Environment.DIRECTORY_MUSIC, StorageDirectoryMapper.androidType(2)); | ||
| assertEquals(Environment.DIRECTORY_RINGTONES, StorageDirectoryMapper.androidType(3)); | ||
| assertEquals(Environment.DIRECTORY_ALARMS, StorageDirectoryMapper.androidType(4)); | ||
| assertEquals(Environment.DIRECTORY_NOTIFICATIONS, StorageDirectoryMapper.androidType(5)); | ||
| assertEquals(Environment.DIRECTORY_PICTURES, StorageDirectoryMapper.androidType(6)); | ||
| assertEquals(Environment.DIRECTORY_MOVIES, StorageDirectoryMapper.androidType(7)); | ||
| assertEquals(Environment.DIRECTORY_DOWNLOADS, StorageDirectoryMapper.androidType(8)); | ||
| assertEquals(Environment.DIRECTORY_DCIM, StorageDirectoryMapper.androidType(9)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAndroidType_invalid() { | ||
| try { | ||
| assertEquals(Environment.DIRECTORY_DCIM, StorageDirectoryMapper.androidType(10)); | ||
| fail(); | ||
| } catch (IllegalArgumentException e) { | ||
| assertEquals("Unknown index: " + 10, e.getMessage()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| org.gradle.jvmargs=-Xmx1536M | ||
| android.enableR8=true |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -39,6 +39,8 @@ class _MyHomePageState extends State<MyHomePage> { | |
| Future<Directory> _appLibraryDirectory; | ||
| Future<Directory> _appDocumentsDirectory; | ||
| Future<Directory> _externalDocumentsDirectory; | ||
| Future<List<Directory>> _externalStorageDirectories; | ||
| Future<List<Directory>> _externalCacheDirectories; | ||
|
|
||
| void _requestTempDirectory() { | ||
| setState(() { | ||
|
|
@@ -61,6 +63,23 @@ class _MyHomePageState extends State<MyHomePage> { | |
| return Padding(padding: const EdgeInsets.all(16.0), child: text); | ||
| } | ||
|
|
||
| Widget _buildDirectories( | ||
| BuildContext context, AsyncSnapshot<List<Directory>> snapshot) { | ||
| Text text = const Text(''); | ||
| if (snapshot.connectionState == ConnectionState.done) { | ||
| if (snapshot.hasError) { | ||
| text = Text('Error: ${snapshot.error}'); | ||
| } else if (snapshot.hasData) { | ||
| final String combined = | ||
| snapshot.data.map((Directory d) => d.path).join(', '); | ||
| text = Text('paths: $combined'); | ||
| } else { | ||
| text = const Text('path unavailable'); | ||
| } | ||
| } | ||
| return Padding(padding: const EdgeInsets.all(16.0), child: text); | ||
| } | ||
|
|
||
| void _requestAppDocumentsDirectory() { | ||
| setState(() { | ||
| _appDocumentsDirectory = getApplicationDocumentsDirectory(); | ||
|
|
@@ -85,6 +104,18 @@ class _MyHomePageState extends State<MyHomePage> { | |
| }); | ||
| } | ||
|
|
||
| void _requestExternalStorageDirectories(StorageDirectory type) { | ||
| setState(() { | ||
| _externalStorageDirectories = getExternalStorageDirectories(type: type); | ||
| }); | ||
| } | ||
|
|
||
| void _requestExternalCacheDirectories() { | ||
| setState(() { | ||
| _externalCacheDirectories = getExternalCacheDirectories(); | ||
| }); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Scaffold( | ||
|
|
@@ -140,7 +171,39 @@ class _MyHomePageState extends State<MyHomePage> { | |
| ), | ||
| ), | ||
| FutureBuilder<Directory>( | ||
| future: _externalDocumentsDirectory, builder: _buildDirectory) | ||
| future: _externalDocumentsDirectory, builder: _buildDirectory), | ||
| Column(children: <Widget>[ | ||
| Padding( | ||
| padding: const EdgeInsets.all(16.0), | ||
| child: RaisedButton( | ||
| child: Text( | ||
| '${Platform.isIOS ? "External directories are unavailable " "on iOS" : "Get External Storage Directories"}'), | ||
| onPressed: Platform.isIOS | ||
| ? null | ||
| : () { | ||
| _requestExternalStorageDirectories( | ||
| StorageDirectory.music, | ||
| ); | ||
| }, | ||
| ), | ||
| ), | ||
| ]), | ||
| FutureBuilder<List<Directory>>( | ||
| future: _externalStorageDirectories, | ||
| builder: _buildDirectories), | ||
| Column(children: <Widget>[ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cyanglaz I have followed the style in the current example code, but think the indentation could be shown better with a few additional "," here & there. Perhaps better to be added in a future PR? |
||
| Padding( | ||
| padding: const EdgeInsets.all(16.0), | ||
| child: RaisedButton( | ||
| child: Text( | ||
| '${Platform.isIOS ? "External directories are unavailable " "on iOS" : "Get External Cache Directories"}'), | ||
| onPressed: | ||
| Platform.isIOS ? null : _requestExternalCacheDirectories, | ||
| ), | ||
| ), | ||
| ]), | ||
| FutureBuilder<List<Directory>>( | ||
| future: _externalCacheDirectories, builder: _buildDirectories), | ||
| ], | ||
| ), | ||
| ), | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -6,7 +6,6 @@ dependencies: | |
| sdk: flutter | ||
| path_provider: | ||
| path: ../ | ||
| uuid: "^1.0.0" | ||
|
|
||
| dev_dependencies: | ||
| flutter_driver: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.