Skip to content

Commit 0619c3a

Browse files
[path_provider] Switch Android to an internal method channel (flutter#4617)
1 parent 09c61ea commit 0619c3a

File tree

5 files changed

+211
-3
lines changed

5 files changed

+211
-3
lines changed

packages/path_provider/path_provider_android/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.10
2+
3+
* Switches to a package-internal implementation of the platform interface.
4+
15
## 2.0.9
26

37
* Updates Android compileSdkVersion to 31.

packages/path_provider/path_provider_android/android/src/main/java/io/flutter/plugins/pathprovider/PathProviderPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void getApplicationSupportDirectory(@NonNull Result result) {
153153
public PathProviderPlugin() {}
154154

155155
private void setup(BinaryMessenger messenger, Context context) {
156-
String channelName = "plugins.flutter.io/path_provider";
156+
String channelName = "plugins.flutter.io/path_provider_android";
157157
// TODO(gaaclarke): Remove reflection guard when https://github.com/flutter/engine/pull/29147
158158
// becomes available on the stable branch.
159159
try {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/services.dart';
7+
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
8+
9+
/// The Android implementation of [PathProviderPlatform].
10+
class PathProviderAndroid extends PathProviderPlatform {
11+
/// The method channel used to interact with the native platform.
12+
@visibleForTesting
13+
MethodChannel methodChannel =
14+
const MethodChannel('plugins.flutter.io/path_provider_android');
15+
16+
/// Registers this class as the default instance of [PathProviderPlatform].
17+
static void registerWith() {
18+
PathProviderPlatform.instance = PathProviderAndroid();
19+
}
20+
21+
@override
22+
Future<String?> getTemporaryPath() {
23+
return methodChannel.invokeMethod<String>('getTemporaryDirectory');
24+
}
25+
26+
@override
27+
Future<String?> getApplicationSupportPath() {
28+
return methodChannel.invokeMethod<String>('getApplicationSupportDirectory');
29+
}
30+
31+
@override
32+
Future<String?> getLibraryPath() {
33+
throw UnsupportedError('getLibraryPath is not supported on Android');
34+
}
35+
36+
@override
37+
Future<String?> getApplicationDocumentsPath() {
38+
return methodChannel
39+
.invokeMethod<String>('getApplicationDocumentsDirectory');
40+
}
41+
42+
@override
43+
Future<String?> getExternalStoragePath() {
44+
return methodChannel.invokeMethod<String>('getStorageDirectory');
45+
}
46+
47+
@override
48+
Future<List<String>?> getExternalCachePaths() {
49+
return methodChannel
50+
.invokeListMethod<String>('getExternalCacheDirectories');
51+
}
52+
53+
@override
54+
Future<List<String>?> getExternalStoragePaths({
55+
StorageDirectory? type,
56+
}) async {
57+
return methodChannel.invokeListMethod<String>(
58+
'getExternalStorageDirectories',
59+
<String, dynamic>{'type': type?.index},
60+
);
61+
}
62+
63+
@override
64+
Future<String?> getDownloadsPath() {
65+
throw UnsupportedError('getDownloadsPath is not supported on Android');
66+
}
67+
}

packages/path_provider/path_provider_android/pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: path_provider_android
22
description: Android implementation of the path_provider plugin.
33
repository: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
5-
version: 2.0.9
5+
version: 2.0.10
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"
@@ -15,11 +15,12 @@ flutter:
1515
android:
1616
package: io.flutter.plugins.pathprovider
1717
pluginClass: PathProviderPlugin
18+
dartPluginClass: PathProviderAndroid
1819

1920
dependencies:
2021
flutter:
2122
sdk: flutter
22-
path_provider_platform_interface: ^2.0.0
23+
path_provider_platform_interface: ^2.0.1
2324

2425
dev_dependencies:
2526
flutter_driver:
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/services.dart';
6+
import 'package:flutter_test/flutter_test.dart';
7+
import 'package:path_provider_android/path_provider_android.dart';
8+
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
9+
10+
void main() {
11+
TestWidgetsFlutterBinding.ensureInitialized();
12+
const String kTemporaryPath = 'temporaryPath';
13+
const String kApplicationSupportPath = 'applicationSupportPath';
14+
const String kLibraryPath = 'libraryPath';
15+
const String kApplicationDocumentsPath = 'applicationDocumentsPath';
16+
const String kExternalCachePaths = 'externalCachePaths';
17+
const String kExternalStoragePaths = 'externalStoragePaths';
18+
const String kDownloadsPath = 'downloadsPath';
19+
20+
group('PathProviderAndroid', () {
21+
late PathProviderAndroid pathProvider;
22+
final List<MethodCall> log = <MethodCall>[];
23+
24+
setUp(() async {
25+
pathProvider = PathProviderAndroid();
26+
TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger
27+
.setMockMethodCallHandler(pathProvider.methodChannel,
28+
(MethodCall methodCall) async {
29+
log.add(methodCall);
30+
switch (methodCall.method) {
31+
case 'getTemporaryDirectory':
32+
return kTemporaryPath;
33+
case 'getApplicationSupportDirectory':
34+
return kApplicationSupportPath;
35+
case 'getLibraryDirectory':
36+
return kLibraryPath;
37+
case 'getApplicationDocumentsDirectory':
38+
return kApplicationDocumentsPath;
39+
case 'getExternalStorageDirectories':
40+
return <String>[kExternalStoragePaths];
41+
case 'getExternalCacheDirectories':
42+
return <String>[kExternalCachePaths];
43+
case 'getDownloadsDirectory':
44+
return kDownloadsPath;
45+
default:
46+
return null;
47+
}
48+
});
49+
});
50+
51+
tearDown(() {
52+
log.clear();
53+
});
54+
55+
test('getTemporaryPath', () async {
56+
final String? path = await pathProvider.getTemporaryPath();
57+
expect(
58+
log,
59+
<Matcher>[isMethodCall('getTemporaryDirectory', arguments: null)],
60+
);
61+
expect(path, kTemporaryPath);
62+
});
63+
64+
test('getApplicationSupportPath', () async {
65+
final String? path = await pathProvider.getApplicationSupportPath();
66+
expect(
67+
log,
68+
<Matcher>[
69+
isMethodCall('getApplicationSupportDirectory', arguments: null)
70+
],
71+
);
72+
expect(path, kApplicationSupportPath);
73+
});
74+
75+
test('getLibraryPath fails', () async {
76+
try {
77+
await pathProvider.getLibraryPath();
78+
fail('should throw UnsupportedError');
79+
} catch (e) {
80+
expect(e, isUnsupportedError);
81+
}
82+
});
83+
84+
test('getApplicationDocumentsPath', () async {
85+
final String? path = await pathProvider.getApplicationDocumentsPath();
86+
expect(
87+
log,
88+
<Matcher>[
89+
isMethodCall('getApplicationDocumentsDirectory', arguments: null)
90+
],
91+
);
92+
expect(path, kApplicationDocumentsPath);
93+
});
94+
95+
test('getExternalCachePaths succeeds', () async {
96+
final List<String>? result = await pathProvider.getExternalCachePaths();
97+
expect(
98+
log,
99+
<Matcher>[isMethodCall('getExternalCacheDirectories', arguments: null)],
100+
);
101+
expect(result!.length, 1);
102+
expect(result.first, kExternalCachePaths);
103+
});
104+
105+
for (final StorageDirectory? type in <StorageDirectory?>[
106+
null,
107+
...StorageDirectory.values
108+
]) {
109+
test('getExternalStoragePaths (type: $type) android succeeds', () async {
110+
final List<String>? result =
111+
await pathProvider.getExternalStoragePaths(type: type);
112+
expect(
113+
log,
114+
<Matcher>[
115+
isMethodCall(
116+
'getExternalStorageDirectories',
117+
arguments: <String, dynamic>{'type': type?.index},
118+
)
119+
],
120+
);
121+
122+
expect(result!.length, 1);
123+
expect(result.first, kExternalStoragePaths);
124+
});
125+
} // end of for-loop
126+
127+
test('getDownloadsPath fails', () async {
128+
try {
129+
await pathProvider.getDownloadsPath();
130+
fail('should throw UnsupportedError');
131+
} catch (e) {
132+
expect(e, isUnsupportedError);
133+
}
134+
});
135+
});
136+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy