Skip to content

Commit

Permalink
[path_provider] Fix93198: Added getDownloadsDirectory() for Android (f…
Browse files Browse the repository at this point in the history
…lutter#4708)

This PR adds the Android implementation for the getDownloadsDirectory() function by making getDownloadsDirectory redirect at the Dart level to getExternalStorageDirectories with the downloads directory constant.

*List which issues are fixed by this PR. You must list at least one issue.*
Fixes [#93198](flutter/flutter#93198)
  • Loading branch information
talhakhan1297 authored Aug 30, 2023
1 parent 1f208aa commit e7d812c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/path_provider/path_provider_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.0

* Adds implementation of `getDownloadsDirectory()`.

## 2.1.1

* Adds pub topics to package metadata.
Expand Down
16 changes: 16 additions & 0 deletions packages/path_provider/path_provider_android/example/lib/main.dart
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),
],
),
),
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.isEmpty ? null : paths.first;
}

Future<List<String>> _getExternalStoragePaths({
StorageDirectory? type,
}) async {
return (await _api.getExternalStoragePaths(_convertStorageDirectory(type)))
.cast<String>();
}
}
2 changes: 1 addition & 1 deletion packages/path_provider/path_provider_android/pubspec.yaml
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.1
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 @@ -18,6 +18,10 @@ const String kExternalStoragePaths = 'externalStoragePaths';
const String kDownloadsPath = 'downloadsPath';

class _Api implements TestPathProviderApi {
_Api({this.returnsExternalStoragePaths = true});

final bool returnsExternalStoragePaths;

@override
String? getApplicationDocumentsPath() => kApplicationDocumentsPath;

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

@override
List<String?> getExternalStoragePaths(messages.StorageDirectory directory) =>
<String>[kExternalStoragePaths];
List<String?> getExternalStoragePaths(messages.StorageDirectory directory) {
return <String?>[if (returnsExternalStoragePaths) kExternalStoragePaths];
}

@override
String? getTemporaryPath() => kTemporaryPath;
Expand Down Expand Up @@ -98,13 +103,18 @@ 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 {
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(returnsExternalStoragePaths: false));
final String? path = await pathProvider.getDownloadsPath();
expect(path, null);
});
});
}

0 comments on commit e7d812c

Please sign in to comment.