This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[path_provider] Switch Android to an internal method channel #4617
Merged
bparrishMines
merged 9 commits into
flutter:master
from
guille-ideotec:android-internal-method-channel
Dec 22, 2021
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2128e92
Begin switch to internal method channel by copying MacOS PR#4547
guille-ideotec ada9fa5
Actual implemented and unsupported methods for Android
guille-ideotec 701ad91
Copied from original method channed instead of MacOS port
guille-ideotec 18edec9
Fixed failing unit test
guille-ideotec 548a549
Fix other failing unit test
guille-ideotec 9724575
Removed incorrect test
guille-ideotec d83d022
Import platform interface instead of enum directly
guille-ideotec 3aab432
Removed unnecessary meta dependency
guille-ideotec e4e868a
Substituted meta dependency for flutter/foundation
guille-ideotec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/path_provider/path_provider_android/lib/path_provider_android.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:path_provider_platform_interface/path_provider_platform_interface.dart'; | ||
|
|
||
| /// The Android implementation of [PathProviderPlatform]. | ||
| class PathProviderAndroid extends PathProviderPlatform { | ||
| /// The method channel used to interact with the native platform. | ||
| @visibleForTesting | ||
| MethodChannel methodChannel = | ||
| const MethodChannel('plugins.flutter.io/path_provider_android'); | ||
|
|
||
| /// Registers this class as the default instance of [PathProviderPlatform]. | ||
| static void registerWith() { | ||
| PathProviderPlatform.instance = PathProviderAndroid(); | ||
| } | ||
|
|
||
| @override | ||
| Future<String?> getTemporaryPath() { | ||
| return methodChannel.invokeMethod<String>('getTemporaryDirectory'); | ||
| } | ||
|
|
||
| @override | ||
| Future<String?> getApplicationSupportPath() { | ||
| return methodChannel.invokeMethod<String>('getApplicationSupportDirectory'); | ||
| } | ||
|
|
||
| @override | ||
| Future<String?> getLibraryPath() { | ||
| throw UnsupportedError('getLibraryPath is not supported on Android'); | ||
| } | ||
|
|
||
| @override | ||
| Future<String?> getApplicationDocumentsPath() { | ||
| return methodChannel | ||
| .invokeMethod<String>('getApplicationDocumentsDirectory'); | ||
| } | ||
|
|
||
| @override | ||
| Future<String?> getExternalStoragePath() { | ||
| return methodChannel.invokeMethod<String>('getStorageDirectory'); | ||
| } | ||
|
|
||
| @override | ||
| Future<List<String>?> getExternalCachePaths() { | ||
| return methodChannel | ||
| .invokeListMethod<String>('getExternalCacheDirectories'); | ||
| } | ||
|
|
||
| @override | ||
| Future<List<String>?> getExternalStoragePaths({ | ||
| StorageDirectory? type, | ||
| }) async { | ||
| return methodChannel.invokeListMethod<String>( | ||
| 'getExternalStorageDirectories', | ||
| <String, dynamic>{'type': type?.index}, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<String?> getDownloadsPath() { | ||
| throw UnsupportedError('getDownloadsPath is not supported on Android'); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
packages/path_provider/path_provider_android/test/path_provider_android_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:path_provider_android/path_provider_android.dart'; | ||
| import 'package:path_provider_platform_interface/path_provider_platform_interface.dart'; | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
| const String kTemporaryPath = 'temporaryPath'; | ||
| const String kApplicationSupportPath = 'applicationSupportPath'; | ||
| const String kLibraryPath = 'libraryPath'; | ||
| const String kApplicationDocumentsPath = 'applicationDocumentsPath'; | ||
| const String kExternalCachePaths = 'externalCachePaths'; | ||
| const String kExternalStoragePaths = 'externalStoragePaths'; | ||
| const String kDownloadsPath = 'downloadsPath'; | ||
|
|
||
| group('PathProviderAndroid', () { | ||
| late PathProviderAndroid pathProvider; | ||
| final List<MethodCall> log = <MethodCall>[]; | ||
|
|
||
| setUp(() async { | ||
| pathProvider = PathProviderAndroid(); | ||
| TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger | ||
| .setMockMethodCallHandler(pathProvider.methodChannel, | ||
| (MethodCall methodCall) async { | ||
| log.add(methodCall); | ||
| switch (methodCall.method) { | ||
| case 'getTemporaryDirectory': | ||
| return kTemporaryPath; | ||
| case 'getApplicationSupportDirectory': | ||
| return kApplicationSupportPath; | ||
| case 'getLibraryDirectory': | ||
| return kLibraryPath; | ||
| case 'getApplicationDocumentsDirectory': | ||
| return kApplicationDocumentsPath; | ||
| case 'getExternalStorageDirectories': | ||
| return <String>[kExternalStoragePaths]; | ||
| case 'getExternalCacheDirectories': | ||
| return <String>[kExternalCachePaths]; | ||
| case 'getDownloadsDirectory': | ||
| return kDownloadsPath; | ||
| default: | ||
| return null; | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| tearDown(() { | ||
| log.clear(); | ||
| }); | ||
|
|
||
| test('getTemporaryPath', () async { | ||
| final String? path = await pathProvider.getTemporaryPath(); | ||
| expect( | ||
| log, | ||
| <Matcher>[isMethodCall('getTemporaryDirectory', arguments: null)], | ||
| ); | ||
| expect(path, kTemporaryPath); | ||
| }); | ||
|
|
||
| test('getApplicationSupportPath', () async { | ||
| final String? path = await pathProvider.getApplicationSupportPath(); | ||
| expect( | ||
| log, | ||
| <Matcher>[ | ||
| isMethodCall('getApplicationSupportDirectory', arguments: null) | ||
| ], | ||
| ); | ||
| expect(path, kApplicationSupportPath); | ||
| }); | ||
|
|
||
| test('getLibraryPath fails', () async { | ||
| try { | ||
| await pathProvider.getLibraryPath(); | ||
| fail('should throw UnsupportedError'); | ||
| } catch (e) { | ||
| expect(e, isUnsupportedError); | ||
| } | ||
| }); | ||
|
|
||
| test('getApplicationDocumentsPath', () async { | ||
| final String? path = await pathProvider.getApplicationDocumentsPath(); | ||
| expect( | ||
| log, | ||
| <Matcher>[ | ||
| isMethodCall('getApplicationDocumentsDirectory', arguments: null) | ||
| ], | ||
| ); | ||
| expect(path, kApplicationDocumentsPath); | ||
| }); | ||
|
|
||
| test('getExternalCachePaths succeeds', () async { | ||
| final List<String>? result = await pathProvider.getExternalCachePaths(); | ||
| expect( | ||
| log, | ||
| <Matcher>[isMethodCall('getExternalCacheDirectories', arguments: null)], | ||
| ); | ||
| expect(result!.length, 1); | ||
| expect(result.first, kExternalCachePaths); | ||
| }); | ||
|
|
||
| for (final StorageDirectory? type in <StorageDirectory?>[ | ||
| null, | ||
| ...StorageDirectory.values | ||
| ]) { | ||
| test('getExternalStoragePaths (type: $type) android succeeds', () async { | ||
| final List<String>? result = | ||
| await pathProvider.getExternalStoragePaths(type: type); | ||
| expect( | ||
| log, | ||
| <Matcher>[ | ||
| isMethodCall( | ||
| 'getExternalStorageDirectories', | ||
| arguments: <String, dynamic>{'type': type?.index}, | ||
| ) | ||
| ], | ||
| ); | ||
|
|
||
| expect(result!.length, 1); | ||
| expect(result.first, kExternalStoragePaths); | ||
| }); | ||
| } // end of for-loop | ||
|
|
||
| test('getDownloadsPath fails', () async { | ||
| try { | ||
| await pathProvider.getDownloadsPath(); | ||
| fail('should throw UnsupportedError'); | ||
| } catch (e) { | ||
| expect(e, isUnsupportedError); | ||
| } | ||
| }); | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized that I forgot we needed to bump the Flutter SDK requirement to 2.8 as part of this since it uses Dart plugin registration :(
I don't think it's really fixable unfortunately, unless we:
because if we just bump the SDK requirement this incorrect version will be the last one 2.5 users will resolve to. We can wait and see if there's non-trivial fallout from this first. (If it's only a small number of people, we can tell them to temporarily pin a path_provider_android dependency.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're getting a fair number of reports of this already (flutter/flutter#95706), so we'll need to fix this.
Instead of a full revert, I think we could temporarily change the channel name back since nothing about the channel has changed yet, then change it back with an SDK bump.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a new PR reverting the channel name. I would need to know if it requires a version bump (with corresponding CHANGELOG.md line) and I guess I also need an exception for new tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4636