diff --git a/CHANGELOG.md b/CHANGELOG.md index b8250355..00061f78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.6.1 - 29-05-2021 + +* fix bug on Android: `getContentLength` +* upgrade Android dependencies (WorkerManager 2.5.0) + ## 1.6.0 - 12-04-2021 * Stable version of nullsafety diff --git a/android/build.gradle b/android/build.gradle index eb9cd682..93ef487c 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -34,8 +34,8 @@ android { } dependencies { - implementation 'androidx.work:work-runtime:2.4.0' - implementation 'androidx.annotation:annotation:1.1.0' - implementation 'androidx.core:core:1.3.1' - implementation 'androidx.fragment:fragment:1.2.5' + implementation 'androidx.work:work-runtime:2.5.0' + implementation 'androidx.annotation:annotation:1.2.0' + implementation 'androidx.core:core:1.5.0' + implementation 'androidx.fragment:fragment:1.3.4' } diff --git a/android/src/main/java/vn/hunghd/flutterdownloader/DownloadWorker.java b/android/src/main/java/vn/hunghd/flutterdownloader/DownloadWorker.java index e93c9a1e..2514494d 100644 --- a/android/src/main/java/vn/hunghd/flutterdownloader/DownloadWorker.java +++ b/android/src/main/java/vn/hunghd/flutterdownloader/DownloadWorker.java @@ -308,7 +308,7 @@ private void downloadFile(Context context, String fileURL, String savedDir, Stri if ((responseCode == HttpURLConnection.HTTP_OK || (isResume && responseCode == HttpURLConnection.HTTP_PARTIAL)) && !isStopped()) { String contentType = httpConn.getContentType(); - long contentLength = httpConn.getContentLengthLong(); + long contentLength = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N ? httpConn.getContentLengthLong() : httpConn.getContentLength(); log("Content-Type = " + contentType); log("Content-Length = " + contentLength); diff --git a/example/lib/main.dart b/example/lib/main.dart index a0e7e1b8..d6b32106 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -151,12 +151,10 @@ class _MyHomePageState extends State { if (_tasks != null && _tasks!.isNotEmpty) { final task = _tasks!.firstWhere((task) => task.taskId == id); - if (task != null) { - setState(() { - task.status = status; - task.progress = progress; - }); - } + setState(() { + task.status = status; + task.progress = progress; + }); } }); } @@ -209,7 +207,7 @@ class _MyHomePageState extends State { } }); }, - onAtionClick: (task) { + onActionClick: (task) { if (task.status == DownloadTaskStatus.undefined) { _requestDownload(task); } else if (task.status == DownloadTaskStatus.running) { @@ -255,11 +253,7 @@ class _MyHomePageState extends State { ), FlatButton( onPressed: () { - _checkPermission().then((hasGranted) { - setState(() { - _permissionReady = hasGranted; - }); - }); + _retryRequestPermission(); }, child: Text( 'Retry', @@ -273,6 +267,18 @@ class _MyHomePageState extends State { ), ); + Future _retryRequestPermission() async { + final hasGranted = await _checkPermission(); + + if (hasGranted) { + await _prepareSaveDir(); + } + + setState(() { + _permissionReady = hasGranted; + }); + } + void _requestDownload(_TaskInfo task) async { task.taskId = await FlutterDownloader.enqueue( url: task.link!, @@ -366,7 +372,7 @@ class _MyHomePageState extends State { count++; } - tasks?.forEach((task) { + tasks!.forEach((task) { for (_TaskInfo info in _tasks!) { if (info.link == task.url) { info.taskId = task.taskId; @@ -378,33 +384,40 @@ class _MyHomePageState extends State { _permissionReady = await _checkPermission(); - _localPath = (await _findLocalPath()) + Platform.pathSeparator + 'Download'; + if (_permissionReady) { + await _prepareSaveDir(); + } + + setState(() { + _isLoading = false; + }); + } + + Future _prepareSaveDir() async { + _localPath = + (await _findLocalPath())! + Platform.pathSeparator + 'Download'; final savedDir = Directory(_localPath); bool hasExisted = await savedDir.exists(); if (!hasExisted) { savedDir.create(); } - - setState(() { - _isLoading = false; - }); } - Future _findLocalPath() async { + Future _findLocalPath() async { final directory = widget.platform == TargetPlatform.android - ? await (getExternalStorageDirectory() as FutureOr) + ? await getExternalStorageDirectory() : await getApplicationDocumentsDirectory(); - return directory.path; + return directory?.path; } } class DownloadItem extends StatelessWidget { final _ItemHolder? data; final Function(_TaskInfo?)? onItemClick; - final Function(_TaskInfo)? onAtionClick; + final Function(_TaskInfo)? onActionClick; - DownloadItem({this.data, this.onItemClick, this.onAtionClick}); + DownloadItem({this.data, this.onItemClick, this.onActionClick}); @override Widget build(BuildContext context) { @@ -450,7 +463,7 @@ class DownloadItem extends StatelessWidget { ), ) : Container() - ].where((child) => child != null).toList(), + ].toList(), ), ), ); @@ -460,7 +473,7 @@ class DownloadItem extends StatelessWidget { if (task.status == DownloadTaskStatus.undefined) { return RawMaterialButton( onPressed: () { - onAtionClick!(task); + onActionClick!(task); }, child: Icon(Icons.file_download), shape: CircleBorder(), @@ -469,7 +482,7 @@ class DownloadItem extends StatelessWidget { } else if (task.status == DownloadTaskStatus.running) { return RawMaterialButton( onPressed: () { - onAtionClick!(task); + onActionClick!(task); }, child: Icon( Icons.pause, @@ -481,7 +494,7 @@ class DownloadItem extends StatelessWidget { } else if (task.status == DownloadTaskStatus.paused) { return RawMaterialButton( onPressed: () { - onAtionClick!(task); + onActionClick!(task); }, child: Icon( Icons.play_arrow, @@ -501,7 +514,7 @@ class DownloadItem extends StatelessWidget { ), RawMaterialButton( onPressed: () { - onAtionClick!(task); + onActionClick!(task); }, child: Icon( Icons.delete_forever, @@ -522,7 +535,7 @@ class DownloadItem extends StatelessWidget { Text('Failed', style: TextStyle(color: Colors.red)), RawMaterialButton( onPressed: () { - onAtionClick!(task); + onActionClick!(task); }, child: Icon( Icons.refresh, diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 2954d8cb..44cbfddd 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -10,14 +10,14 @@ description: Demonstrates how to use the flutter_downloader plugin. version: 1.0.0+1 environment: - sdk: '>=2.12.0-259.16.beta <3.0.0' - flutter: ^1.12.13 + sdk: '>=2.12.0 <3.0.0' + flutter: '>=1.20.0' dependencies: flutter: sdk: flutter - path_provider: ^2.0.0 - permission_handler: ^5.0.1+1 + path_provider: ^2.0.2 + permission_handler: ^8.0.0+2 flutter_downloader: path: ../ diff --git a/lib/src/models.dart b/lib/src/models.dart index ec22a3a9..f651e324 100644 --- a/lib/src/models.dart +++ b/lib/src/models.dart @@ -49,7 +49,7 @@ class DownloadTask { final DownloadTaskStatus status; final int progress; final String url; - final String filename; + final String? filename; final String savedDir; final int timeCreated; diff --git a/pubspec.yaml b/pubspec.yaml index 5870d56f..cec23c7f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_downloader description: A plugin for creating and managing download tasks. Supports iOS and Android. -version: 1.6.0 +version: 1.6.1 homepage: https://github.com/fluttercommunity/flutter_downloader maintainer: Hung Duy Ha (@hnvn) @@ -14,8 +14,8 @@ flutter: pluginClass: FlutterDownloaderPlugin environment: - sdk: '>=2.12.0-259.16.beta <3.0.0' - flutter: ^1.12.13 + sdk: '>=2.12.0 <3.0.0' + flutter: '>=1.20.0' dependencies: flutter: