Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[path_provider] Fix93198: Added getDownloadsDirectory() for Android #4708

Merged
merged 31 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
46f685d
Fix4559: Added getDownloadsDirectory() for Android
talhakhan1297 Aug 15, 2023
0404fe8
Removed changes from path_provider example
talhakhan1297 Aug 15, 2023
1b1b64f
Updated Changelog
talhakhan1297 Aug 15, 2023
197128f
Updated Changelog and Versions
talhakhan1297 Aug 15, 2023
f010d48
Merge branch 'main' into fix4559
talhakhan1297 Aug 15, 2023
768121a
Merge branch 'main' of https://github.com/flutter/packages into fix4559
talhakhan1297 Aug 15, 2023
b2f8f99
Merge branch 'fix4559' of https://github.com/talhakhan1297/packages i…
talhakhan1297 Aug 15, 2023
a258d9f
Merge branch 'main' into fix4559
talhakhan1297 Aug 15, 2023
cd26675
Merge branch 'main' into fix4559
talhakhan1297 Aug 16, 2023
522b093
Merge branch 'main' into fix4559
talhakhan1297 Aug 16, 2023
08d24d8
Merge branch 'main' into fix4559
talhakhan1297 Aug 16, 2023
e5c853b
Merge branch 'main' of https://github.com/flutter/packages into fix4559
talhakhan1297 Aug 18, 2023
045a68f
Added test for null and empty cases
talhakhan1297 Aug 18, 2023
db66865
Merge branch 'main' into fix4559
talhakhan1297 Aug 19, 2023
d21e4f7
Merge branch 'main' into fix4559
talhakhan1297 Aug 21, 2023
aea4f5d
Update packages/path_provider/path_provider_android/CHANGELOG.md
talhakhan1297 Aug 21, 2023
7f7914b
Merge branch 'main' into fix4559
talhakhan1297 Aug 22, 2023
720b1f0
Merge branch 'main' into fix4559
talhakhan1297 Aug 22, 2023
c7a411c
Merge branch 'main' into fix4559
talhakhan1297 Aug 23, 2023
8dbeb71
Merge branch 'main' into fix4559
talhakhan1297 Aug 24, 2023
319fbdf
Merge branch 'main' into fix4559
talhakhan1297 Aug 24, 2023
83a773d
Merge branch 'main' into fix4559
talhakhan1297 Aug 25, 2023
5f61fb0
Merge branch 'main' into fix4559
talhakhan1297 Aug 28, 2023
da7ed51
Merge branch 'main' into fix4559
talhakhan1297 Aug 28, 2023
10a18dd
Updated Tests description
talhakhan1297 Aug 28, 2023
f50f6c8
Extracted _getExternalStoragePaths to fix nullable list issue
talhakhan1297 Aug 28, 2023
e295218
Merge branch 'main' into fix4559
talhakhan1297 Aug 28, 2023
38ea4c7
Replaced enum with bool and updated if condition.
talhakhan1297 Aug 29, 2023
0bd03cd
Merge branch 'main' into fix4559
talhakhan1297 Aug 29, 2023
b0b2864
Updated if condition with modern dart implementation
talhakhan1297 Aug 29, 2023
2d2e3b4
Merge branch 'main' of https://github.com/flutter/packages into fix4559
talhakhan1297 Aug 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/path_provider/path_provider_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 2.2.0

* Adds implementation of `getDownloadsDirectory()`.
* Updates minimum supported SDK version to Flutter 3.7/Dart 2.19.

## 2.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class _MyHomePageState extends State<MyHomePage> {
Future<String?>? _appDocumentsDirectory;
Future<String?>? _appCacheDirectory;
Future<String?>? _externalDocumentsDirectory;
Future<String?>? _externalDownloadsDirectory;
Future<List<String>?>? _externalStorageDirectories;
Future<List<String>?>? _externalCacheDirectories;

Expand Down Expand Up @@ -118,6 +119,12 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

void _requestDownloadsDirectory() {
setState(() {
_externalDownloadsDirectory = provider.getDownloadsPath();
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down Expand Up @@ -199,6 +206,15 @@ class _MyHomePageState extends State<MyHomePage> {
]),
FutureBuilder<List<String>?>(
future: _externalCacheDirectories, builder: _buildDirectories),
Padding(
padding: const EdgeInsets.all(16.0),
child: ElevatedButton(
onPressed: _requestDownloadsDirectory,
child: const Text('Get Downloads Directory'),
),
),
FutureBuilder<String?>(
future: _externalDownloadsDirectory, builder: _buildDirectory),
talhakhan1297 marked this conversation as resolved.
Show resolved Hide resolved
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,20 @@ class PathProviderAndroid extends PathProviderPlatform {
Future<List<String>?> getExternalStoragePaths({
StorageDirectory? type,
}) async {
return (await _api.getExternalStoragePaths(_convertStorageDirectory(type)))
.cast<String>();
return _getExternalStoragePaths(type: type);
}

@override
Future<String?> getDownloadsPath() {
throw UnsupportedError('getDownloadsPath is not supported on Android');
Future<String?> getDownloadsPath() async {
final List<String> paths =
await _getExternalStoragePaths(type: StorageDirectory.downloads);
return paths.isNotEmpty ? paths.first : null;
talhakhan1297 marked this conversation as resolved.
Show resolved Hide resolved
}

Future<List<String>> _getExternalStoragePaths({
StorageDirectory? type,
}) async {
return (await _api.getExternalStoragePaths(_convertStorageDirectory(type)))
.cast<String>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: path_provider_android
description: Android implementation of the path_provider plugin.
repository: https://github.com/flutter/packages/tree/main/packages/path_provider/path_provider_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
version: 2.1.0
version: 2.2.0

environment:
sdk: ">=2.19.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ const String kExternalCachePaths = 'externalCachePaths';
const String kExternalStoragePaths = 'externalStoragePaths';
const String kDownloadsPath = 'downloadsPath';

enum ExternalStoragePathsValueState { isEmpty, isNotEmpty }

class _Api implements TestPathProviderApi {
_Api({
this.externalStoragePathsValueState =
ExternalStoragePathsValueState.isNotEmpty,
});

final ExternalStoragePathsValueState externalStoragePathsValueState;
talhakhan1297 marked this conversation as resolved.
Show resolved Hide resolved

@override
String? getApplicationDocumentsPath() => kApplicationDocumentsPath;

Expand All @@ -34,8 +43,14 @@ class _Api implements TestPathProviderApi {
String? getExternalStoragePath() => kExternalStoragePaths;

@override
List<String?> getExternalStoragePaths(messages.StorageDirectory directory) =>
<String>[kExternalStoragePaths];
List<String?> getExternalStoragePaths(messages.StorageDirectory directory) {
switch (externalStoragePathsValueState) {
case ExternalStoragePathsValueState.isNotEmpty:
return <String?>[kExternalStoragePaths];
case ExternalStoragePathsValueState.isEmpty:
return <String?>[];
}
talhakhan1297 marked this conversation as resolved.
Show resolved Hide resolved
}

@override
String? getTemporaryPath() => kTemporaryPath;
Expand Down Expand Up @@ -98,13 +113,20 @@ void main() {
});
} // end of for-loop

test('getDownloadsPath fails', () async {
try {
await pathProvider.getDownloadsPath();
fail('should throw UnsupportedError');
} catch (e) {
expect(e, isUnsupportedError);
}
test('getDownloadsPath succeeds', () async {
talhakhan1297 marked this conversation as resolved.
Show resolved Hide resolved
final String? path = await pathProvider.getDownloadsPath();
expect(path, kExternalStoragePaths);
});

test(
'getDownloadsPath returns null, when getExternalStoragePaths returns '
'an empty list', () async {
final PathProviderAndroid pathProvider = PathProviderAndroid();
TestPathProviderApi.setup(_Api(
externalStoragePathsValueState:
ExternalStoragePathsValueState.isEmpty));
final String? path = await pathProvider.getDownloadsPath();
expect(path, null);
});
});
}