Skip to content

Commit 9323e33

Browse files
authored
[path_provider] Add getApplicationCachePath() - implementations (flutter#4619)
Platform implementations split out from flutter#4483.
1 parent 051d20a commit 9323e33

35 files changed

+287
-35
lines changed

packages/path_provider/path_provider_android/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.1.0
22

3+
* Adds getApplicationCachePath() for storing app-specific cache files.
34
* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.
45

56
## 2.0.27

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
4+
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

77
package io.flutter.plugins.pathprovider;
@@ -84,6 +84,9 @@ public interface PathProviderApi {
8484
@Nullable
8585
String getApplicationDocumentsPath();
8686

87+
@Nullable
88+
String getApplicationCachePath();
89+
8790
@Nullable
8891
String getExternalStoragePath();
8992

@@ -176,6 +179,31 @@ static void setup(@NonNull BinaryMessenger binaryMessenger, @Nullable PathProvid
176179
channel.setMessageHandler(null);
177180
}
178181
}
182+
{
183+
BinaryMessenger.TaskQueue taskQueue = binaryMessenger.makeBackgroundTaskQueue();
184+
BasicMessageChannel<Object> channel =
185+
new BasicMessageChannel<>(
186+
binaryMessenger,
187+
"dev.flutter.pigeon.PathProviderApi.getApplicationCachePath",
188+
getCodec(),
189+
taskQueue);
190+
if (api != null) {
191+
channel.setMessageHandler(
192+
(message, reply) -> {
193+
ArrayList<Object> wrapped = new ArrayList<Object>();
194+
try {
195+
String output = api.getApplicationCachePath();
196+
wrapped.add(0, output);
197+
} catch (Throwable exception) {
198+
ArrayList<Object> wrappedError = wrapError(exception);
199+
wrapped = wrappedError;
200+
}
201+
reply.reply(wrapped);
202+
});
203+
} else {
204+
channel.setMessageHandler(null);
205+
}
206+
}
179207
{
180208
BinaryMessenger.TaskQueue taskQueue = binaryMessenger.makeBackgroundTaskQueue();
181209
BasicMessageChannel<Object> channel =

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
6666
return getPathProviderApplicationDocumentsDirectory();
6767
}
6868

69+
@Override
70+
public @Nullable String getApplicationCachePath() {
71+
return context.getCacheDir().getPath();
72+
}
73+
6974
@Override
7075
public @Nullable String getExternalStoragePath() {
7176
return getPathProviderStorageDirectory();

packages/path_provider/path_provider_android/example/integration_test/path_provider_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ void main() {
2828
_verifySampleFile(result, 'applicationSupport');
2929
});
3030

31+
testWidgets('getApplicationCacheDirectory', (WidgetTester tester) async {
32+
final PathProviderPlatform provider = PathProviderPlatform.instance;
33+
final String? result = await provider.getApplicationCachePath();
34+
_verifySampleFile(result, 'applicationCache');
35+
});
36+
3137
testWidgets('getLibraryDirectory', (WidgetTester tester) async {
3238
final PathProviderPlatform provider = PathProviderPlatform.instance;
3339
expect(() => provider.getLibraryPath(),

packages/path_provider/path_provider_android/example/lib/main.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class _MyHomePageState extends State<MyHomePage> {
3939
Future<String?>? _tempDirectory;
4040
Future<String?>? _appSupportDirectory;
4141
Future<String?>? _appDocumentsDirectory;
42+
Future<String?>? _appCacheDirectory;
4243
Future<String?>? _externalDocumentsDirectory;
4344
Future<List<String>?>? _externalStorageDirectories;
4445
Future<List<String>?>? _externalCacheDirectories;
@@ -92,6 +93,12 @@ class _MyHomePageState extends State<MyHomePage> {
9293
});
9394
}
9495

96+
void _requestAppCacheDirectory() {
97+
setState(() {
98+
_appCacheDirectory = provider.getApplicationCachePath();
99+
});
100+
}
101+
95102
void _requestExternalStorageDirectory() {
96103
setState(() {
97104
_externalDocumentsDirectory = provider.getExternalStoragePath();
@@ -147,6 +154,15 @@ class _MyHomePageState extends State<MyHomePage> {
147154
),
148155
FutureBuilder<String?>(
149156
future: _appSupportDirectory, builder: _buildDirectory),
157+
Padding(
158+
padding: const EdgeInsets.all(16.0),
159+
child: ElevatedButton(
160+
onPressed: _requestAppCacheDirectory,
161+
child: const Text('Get Application Cache Directory'),
162+
),
163+
),
164+
FutureBuilder<String?>(
165+
future: _appCacheDirectory, builder: _buildDirectory),
150166
Padding(
151167
padding: const EdgeInsets.all(16.0),
152168
child: ElevatedButton(

packages/path_provider/path_provider_android/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
# The example app is bundled with the plugin so we use a path dependency on
1717
# the parent directory to use the current plugin's version.
1818
path: ../
19-
path_provider_platform_interface: ^2.0.0
19+
path_provider_platform_interface: ^2.1.0
2020

2121
dev_dependencies:
2222
flutter_test:

packages/path_provider/path_provider_android/lib/messages.g.dart

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
4+
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
77

@@ -98,6 +98,27 @@ class PathProviderApi {
9898
}
9999
}
100100

101+
Future<String?> getApplicationCachePath() async {
102+
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
103+
'dev.flutter.pigeon.PathProviderApi.getApplicationCachePath', codec,
104+
binaryMessenger: _binaryMessenger);
105+
final List<Object?>? replyList = await channel.send(null) as List<Object?>?;
106+
if (replyList == null) {
107+
throw PlatformException(
108+
code: 'channel-error',
109+
message: 'Unable to establish connection on channel.',
110+
);
111+
} else if (replyList.length > 1) {
112+
throw PlatformException(
113+
code: replyList[0]! as String,
114+
message: replyList[1] as String?,
115+
details: replyList[2],
116+
);
117+
} else {
118+
return (replyList[0] as String?);
119+
}
120+
}
121+
101122
Future<String?> getExternalStoragePath() async {
102123
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
103124
'dev.flutter.pigeon.PathProviderApi.getExternalStoragePath', codec,

packages/path_provider/path_provider_android/lib/path_provider_android.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class PathProviderAndroid extends PathProviderPlatform {
6262
return _api.getApplicationDocumentsPath();
6363
}
6464

65+
@override
66+
Future<String?> getApplicationCachePath() {
67+
return _api.getApplicationCachePath();
68+
}
69+
6570
@override
6671
Future<String?> getExternalStoragePath() {
6772
return _api.getExternalStoragePath();

packages/path_provider/path_provider_android/pigeons/messages.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ abstract class PathProviderApi {
3636
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
3737
String? getApplicationDocumentsPath();
3838
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
39+
String? getApplicationCachePath();
40+
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
3941
String? getExternalStoragePath();
4042
@TaskQueue(type: TaskQueueType.serialBackgroundThread)
4143
List<String?> getExternalCachePaths();

packages/path_provider/path_provider_android/pubspec.yaml

Lines changed: 2 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/packages/tree/main/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.27
5+
version: 2.1.0
66

77
environment:
88
sdk: ">=2.18.0 <4.0.0"
@@ -20,7 +20,7 @@ flutter:
2020
dependencies:
2121
flutter:
2222
sdk: flutter
23-
path_provider_platform_interface: ^2.0.1
23+
path_provider_platform_interface: ^2.1.0
2424

2525
dev_dependencies:
2626
flutter_test:

packages/path_provider/path_provider_android/test/messages_test.g.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v9.2.4), do not edit directly.
4+
// Autogenerated from Pigeon (v9.2.5), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
77
// ignore_for_file: avoid_relative_lib_imports
@@ -24,6 +24,8 @@ abstract class TestPathProviderApi {
2424

2525
String? getApplicationDocumentsPath();
2626

27+
String? getApplicationCachePath();
28+
2729
String? getExternalStoragePath();
2830

2931
List<String?> getExternalCachePaths();
@@ -84,6 +86,23 @@ abstract class TestPathProviderApi {
8486
});
8587
}
8688
}
89+
{
90+
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
91+
'dev.flutter.pigeon.PathProviderApi.getApplicationCachePath', codec,
92+
binaryMessenger: binaryMessenger);
93+
if (api == null) {
94+
_testBinaryMessengerBinding!.defaultBinaryMessenger
95+
.setMockDecodedMessageHandler<Object?>(channel, null);
96+
} else {
97+
_testBinaryMessengerBinding!.defaultBinaryMessenger
98+
.setMockDecodedMessageHandler<Object?>(channel,
99+
(Object? message) async {
100+
// ignore message
101+
final String? output = api.getApplicationCachePath();
102+
return <Object?>[output];
103+
});
104+
}
105+
}
87106
{
88107
final BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>(
89108
'dev.flutter.pigeon.PathProviderApi.getExternalStoragePath', codec,

packages/path_provider/path_provider_android/test/path_provider_android_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const String kTemporaryPath = 'temporaryPath';
1212
const String kApplicationSupportPath = 'applicationSupportPath';
1313
const String kLibraryPath = 'libraryPath';
1414
const String kApplicationDocumentsPath = 'applicationDocumentsPath';
15+
const String kApplicationCachePath = 'applicationCachePath';
1516
const String kExternalCachePaths = 'externalCachePaths';
1617
const String kExternalStoragePaths = 'externalStoragePaths';
1718
const String kDownloadsPath = 'downloadsPath';
@@ -23,6 +24,9 @@ class _Api implements TestPathProviderApi {
2324
@override
2425
String? getApplicationSupportPath() => kApplicationSupportPath;
2526

27+
@override
28+
String? getApplicationCachePath() => kApplicationCachePath;
29+
2630
@override
2731
List<String?> getExternalCachePaths() => <String>[kExternalCachePaths];
2832

@@ -58,6 +62,11 @@ void main() {
5862
expect(path, kApplicationSupportPath);
5963
});
6064

65+
test('getApplicationCachePath', () async {
66+
final String? path = await pathProvider.getApplicationCachePath();
67+
expect(path, kApplicationCachePath);
68+
});
69+
6170
test('getLibraryPath fails', () async {
6271
try {
6372
await pathProvider.getLibraryPath();

packages/path_provider/path_provider_foundation/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.3.0
2+
3+
* Adds getApplicationCachePath() for storing app-specific cache files.
4+
15
## 2.2.4
26

37
* Updates to the latest version of `pigeon`.

packages/path_provider/path_provider_foundation/darwin/Classes/PathProviderPlugin.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public class PathProviderPlugin: NSObject, FlutterPlugin, PathProviderApi {
2525
func getDirectoryPath(type: DirectoryType) -> String? {
2626
var path = getDirectory(ofType: fileManagerDirectoryForType(type))
2727
#if os(macOS)
28-
// In a non-sandboxed app, this is a shared directory where applications are
28+
// In a non-sandboxed app, these are shared directories where applications are
2929
// expected to use its bundle ID as a subdirectory. (For non-sandboxed apps,
3030
// adding the extra path is harmless).
3131
// This is not done for iOS, for compatibility with older versions of the
3232
// plugin.
33-
if type == .applicationSupport {
33+
if type == .applicationSupport || type == .applicationCache {
3434
if let basePath = path {
3535
let basePathURL = URL.init(fileURLWithPath: basePath)
3636
path = basePathURL.appendingPathComponent(Bundle.main.bundleIdentifier!).path
@@ -49,6 +49,8 @@ public class PathProviderPlugin: NSObject, FlutterPlugin, PathProviderApi {
4949
/// Returns the FileManager constant corresponding to the given type.
5050
private func fileManagerDirectoryForType(_ type: DirectoryType) -> FileManager.SearchPathDirectory {
5151
switch type {
52+
case .applicationCache:
53+
return FileManager.SearchPathDirectory.cachesDirectory
5254
case .applicationDocuments:
5355
return FileManager.SearchPathDirectory.documentDirectory
5456
case .applicationSupport:

packages/path_provider/path_provider_foundation/darwin/Classes/messages.g.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ enum DirectoryType: Int {
4444
case downloads = 2
4545
case library = 3
4646
case temp = 4
47+
case applicationCache = 5
4748
}
4849
/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
4950
protocol PathProviderApi {

packages/path_provider/path_provider_foundation/example/integration_test/path_provider_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ void main() {
2929
_verifySampleFile(result, 'applicationSupport');
3030
});
3131

32+
testWidgets('getApplicationCacheDirectory', (WidgetTester tester) async {
33+
final PathProviderPlatform provider = PathProviderPlatform.instance;
34+
final String? result = await provider.getApplicationCachePath();
35+
_verifySampleFile(result, 'applicationCache');
36+
});
37+
3238
testWidgets('getLibraryDirectory', (WidgetTester tester) async {
3339
final PathProviderPlatform provider = PathProviderPlatform.instance;
3440
final String? result = await provider.getLibraryPath();

packages/path_provider/path_provider_foundation/example/lib/main.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class _MyAppState extends State<MyApp> {
2727
String? _appSupportDirectory = 'Unknown';
2828
String? _documentsDirectory = 'Unknown';
2929
String? _containerDirectory = 'Unknown';
30+
String? _cacheDirectory = 'Unknown';
3031

3132
@override
3233
void initState() {
@@ -42,6 +43,7 @@ class _MyAppState extends State<MyApp> {
4243
String? libraryDirectory;
4344
String? documentsDirectory;
4445
String? containerDirectory;
46+
String? cacheDirectory;
4547
final PathProviderPlatform provider = PathProviderPlatform.instance;
4648
final PathProviderFoundation providerFoundation = PathProviderFoundation();
4749

@@ -82,13 +84,20 @@ class _MyAppState extends State<MyApp> {
8284
'Failed to get app group container directory: $exception';
8385
}
8486

87+
try {
88+
cacheDirectory = await provider.getApplicationCachePath();
89+
} catch (exception) {
90+
cacheDirectory = 'Failed to get cache directory: $exception';
91+
}
92+
8593
setState(() {
8694
_tempDirectory = tempDirectory;
8795
_downloadsDirectory = downloadsDirectory;
8896
_libraryDirectory = libraryDirectory;
8997
_appSupportDirectory = appSupportDirectory;
9098
_documentsDirectory = documentsDirectory;
9199
_containerDirectory = containerDirectory;
100+
_cacheDirectory = cacheDirectory;
92101
});
93102
}
94103

@@ -108,6 +117,7 @@ class _MyAppState extends State<MyApp> {
108117
Text('Library Directory: $_libraryDirectory\n'),
109118
Text('Application Support Directory: $_appSupportDirectory\n'),
110119
Text('App Group Container Directory: $_containerDirectory\n'),
120+
Text('Cache Directory: $_cacheDirectory\n'),
111121
],
112122
),
113123
),

packages/path_provider/path_provider_foundation/example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies:
1616
# The example app is bundled with the plugin so we use a path dependency on
1717
# the parent directory to use the current plugin's version.
1818
path: ../
19-
path_provider_platform_interface: ^2.0.0
19+
path_provider_platform_interface: ^2.1.0
2020

2121
dev_dependencies:
2222
flutter_test:

packages/path_provider/path_provider_foundation/lib/messages.g.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum DirectoryType {
1717
downloads,
1818
library,
1919
temp,
20+
applicationCache,
2021
}
2122

2223
class PathProviderApi {

0 commit comments

Comments
 (0)