Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ Future<List<Directory>?> getExternalStorageDirectories({
/// Path to the directory where downloaded files can be stored.
/// This is typically only relevant on desktop operating systems.
///
/// On Android and on iOS, this function throws an [UnsupportedError] as no equivalent
/// On iOS, this function throws an [UnsupportedError] as no equivalent
/// path exists.
Future<Directory?> getDownloadsDirectory() async {
final String? path = await _platform.getDownloadsPath();
Expand Down
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.1.0

* Added implementation of getDownloadsDirectory

## 2.0.9

* Updates Android compileSdkVersion to 31.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Looper;
import android.os.Environment;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.common.util.concurrent.FutureCallback;
Expand Down Expand Up @@ -55,6 +56,8 @@ private interface PathProviderImpl {

void getStorageDirectory(@NonNull Result result);

void getDownloadsDirectory(@NonNull Result result);

void getExternalCacheDirectories(@NonNull Result result);

void getExternalStorageDirectories(@NonNull String directoryName, @NonNull Result result);
Expand Down Expand Up @@ -84,6 +87,10 @@ public void getStorageDirectory(@NonNull Result result) {
executeInBackground(() -> getPathProviderStorageDirectory(), result);
}

public void getDownloadsDirectory(@NonNull Result result) {
executeInBackground(() -> getPathProviderDownloadsDirectory(), result);
}

public void getExternalCacheDirectories(@NonNull Result result) {
executeInBackground(() -> getPathProviderExternalCacheDirectories(), result);
}
Expand Down Expand Up @@ -136,6 +143,10 @@ public void getStorageDirectory(@NonNull Result result) {
result.success(getPathProviderStorageDirectory());
}

public void getDownloadsDirectory(@NonNull Result result) {
result.success(getPathProviderDownloadsDirectory());
}

public void getExternalCacheDirectories(@NonNull Result result) {
result.success(getPathProviderExternalCacheDirectories());
}
Expand Down Expand Up @@ -206,6 +217,9 @@ public void onMethodCall(MethodCall call, @NonNull Result result) {
case "getStorageDirectory":
impl.getStorageDirectory(result);
break;
case "getDownloadsDirectory":
impl.getDownloadsDirectory(result);
break;
case "getExternalCacheDirectories":
impl.getExternalCacheDirectories(result);
break;
Expand Down Expand Up @@ -242,6 +256,10 @@ private String getPathProviderStorageDirectory() {
return dir.getAbsolutePath();
}

private String getPathProviderDownloadsDirectory() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is deprecated; we deliberately do not use it for that reason.

}

private List<String> getPathProviderExternalCacheDirectories() {
final List<String> paths = new ArrayList<String>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class _MyHomePageState extends State<MyHomePage> {
final PathProviderPlatform provider = PathProviderPlatform.instance;
Future<String?>? _tempDirectory;
Future<String?>? _appSupportDirectory;
Future<String?>? _downloadsDirectory;
Future<String?>? _appDocumentsDirectory;
Future<String?>? _externalDocumentsDirectory;
Future<List<String>?>? _externalStorageDirectories;
Expand Down Expand Up @@ -90,6 +91,12 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

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

void _requestExternalStorageDirectory() {
setState(() {
_externalDocumentsDirectory = provider.getExternalStoragePath();
Expand Down Expand Up @@ -143,6 +150,15 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: _requestAppSupportDirectory,
),
),
FutureBuilder<String?>(
future: _downloadsDirectory, builder: _buildDirectory),
Padding(
padding: const EdgeInsets.all(16.0),
child: ElevatedButton(
child: const Text('Get Downloads Directory'),
onPressed: _requestDownloadsDirectory,
),
),
FutureBuilder<String?>(
future: _appSupportDirectory, builder: _buildDirectory),
Padding(
Expand Down
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/plugins/tree/master/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.0.9
version: 2.1.0

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.2

* Updated getDownloadsDirectory platform compatibility

## 2.0.1

* Update platform_plugin_interface version requirement.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class MethodChannelPathProvider extends PathProviderPlatform {

@override
Future<String?> getDownloadsPath() {
if (!_platform.isMacOS) {
throw UnsupportedError('Functionality only available on macOS');
if (_platform.isIOS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a breaking change. These packages aren't atomic, and the layering here is problematic (these checks should really never have been in the platform interface), so it would be possible to have a version of the Android package that doesn't support it, but the new version of the platform interface that removed the check, and as a result get a different error type.

throw UnsupportedError('Functionality not available on iOS');
}
return methodChannel.invokeMethod<String>('getDownloadsDirectory');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/plugins/tree/master/packages/path_provide
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 2.0.1
version: 2.0.2

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,20 @@ void main() {
expect(result, kDownloadsPath);
});

test('getDownloadsPath non-macos fails', () async {
test('getDownloadsPath Android succeeds', () async {
methodChannelPathProvider.setMockPathProviderPlatform(
FakePlatform(operatingSystem: 'android'));
final String? result = await methodChannelPathProvider.getDownloadsPath();
expect(
log,
<Matcher>[isMethodCall('getDownloadsDirectory', arguments: null)],
);
expect(result, kDownloadsPath);
});

test('getDownloadsPath iOS fails', () async {
methodChannelPathProvider
.setMockPathProviderPlatform(FakePlatform(operatingSystem: 'ios'));
try {
await methodChannelPathProvider.getDownloadsPath();
fail('should throw UnsupportedError');
Expand Down