Skip to content

Commit b7a6cdb

Browse files
authored
Refactors (#656)
* apply Xcode suggestions, min iOS 9.0 -> 12.0 * remove .iml files * split into main.dart and home_page.dart * add leancode_lint, refactor example * example: more refactors * clean up android impl * example: clean up UI code a bit * FluttterDownloader.initialize(debug) - set default value to false * example: simplify DownloadListItem code * move DownloadListItem to separate item * DownloadListItem: add progress * update podspec * improve readme
1 parent 3cddb1e commit b7a6cdb

File tree

18 files changed

+685
-794
lines changed

18 files changed

+685
-794
lines changed

README.md

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ The following steps require to open your `ios` project in Xcode.
2727

2828
1. Enable background mode.
2929

30-
<img width="512" src="https://github.com/hnvn/flutter_downloader/blob/master/screenshot/enable_background_mode.png?raw=true"/>
30+
<img width="512"
31+
src="https://github.com/hnvn/flutter_downloader/blob/master/screenshot/enable_background_mode.png?raw=true"/>
3132

3233
2. Add `sqlite` library.
3334

@@ -110,7 +111,7 @@ private func registerPlugins(registry: FlutterPluginRegistry) {
110111
- **Support HTTP request:** if you want to download file with HTTP request, you
111112
need to disable Apple Transport Security (ATS) feature. There're two options:
112113

113-
1. Disable ATS for a specific domain only: (add following codes to your
114+
1. Disable ATS for a specific domain only: (add the following code to your
114115
`Info.plist` file)
115116

116117
```xml
@@ -134,7 +135,7 @@ private func registerPlugins(registry: FlutterPluginRegistry) {
134135
</dict>
135136
```
136137

137-
2. Completely disable ATS: (add following codes to your `Info.plist` file)
138+
2. Completely disable ATS. Add the following to your `Info.plist` file)
138139

139140
```xml
140141
<key>NSAppTransportSecurity</key>
@@ -146,7 +147,7 @@ private func registerPlugins(registry: FlutterPluginRegistry) {
146147
- **Configure maximum number of concurrent tasks:** the plugin allows 3 download
147148
tasks running at a moment by default (if you enqueue more than 3 tasks,
148149
there're only 3 tasks running, other tasks are put in pending state). You can
149-
change this number by adding following codes to your `Info.plist` file.
150+
change this number by adding the following code to your `Info.plist` file.
150151

151152
```xml
152153
<!-- changes this number to configure the maximum number of concurrent tasks -->
@@ -172,12 +173,14 @@ private func registerPlugins(registry: FlutterPluginRegistry) {
172173

173174
## Android integration
174175

175-
There's no additional configurations required on Android.
176+
You don't have to do anything extra to make the plugin work on Android.
176177

177-
In order to handle click action on notification to open the downloaded file on
178-
Android, you need to add some additional configurations.
178+
There are although a few optional settings you might want to configure.
179179

180-
Add the following to `AndroidManifest.xml`:
180+
### Open downloaded file from notification
181+
182+
To make tapping on notification open the downloaded file on Android, add the
183+
following code to `AndroidManifest.xml`:
181184

182185
```xml
183186
<provider
@@ -191,20 +194,19 @@ Add the following to `AndroidManifest.xml`:
191194
</provider>
192195
```
193196

194-
**Note:**
197+
**Notes**
195198

196199
- You have to save your downloaded files in external storage (where the other
197200
applications have permission to read your files)
198-
- The downloaded files are only able to be opened if your device has at least an
199-
application that can read these file types (mp3, pdf, etc)
201+
- The downloaded files are only able to be opened if your device has at least
202+
one application that can read these file types (mp3, pdf, etc.)
200203

201-
### Optional configuration:
204+
### Configure maximum number of concurrent download tasks
202205

203-
- **Configure maximum number of concurrent tasks:** the plugin depends on
204-
`WorkManager` library and `WorkManager` depends on the number of available
205-
processor to configure the maximum number of tasks running at a moment. You
206-
can setup a fixed number for this configuration by adding following codes to
207-
your `AndroidManifest.xml`:
206+
The plugin depends on `WorkManager` library and `WorkManager` depends on the
207+
number of available processor to configure the maximum number of tasks running
208+
at a moment. You can setup a fixed number for this configuration by adding the
209+
following code to your `AndroidManifest.xml`:
208210

209211
```xml
210212
<!-- Begin FlutterDownloader customization -->
@@ -233,9 +235,10 @@ Add the following to `AndroidManifest.xml`:
233235
<!-- End FlutterDownloader customization -->
234236
```
235237

236-
- **Localize notification messages:** you can localize notification messages of
237-
download progress by localizing following messages. (you can find the detail
238-
of string localization in Android in this [link][4])
238+
### Localize strings in notifications
239+
240+
You can localize texts in download progress notifications by localizing
241+
following messages.
239242

240243
```xml
241244
<string name="flutter_downloader_notification_started">Download started</string>
@@ -246,9 +249,13 @@ Add the following to `AndroidManifest.xml`:
246249
<string name="flutter_downloader_notification_paused">Download paused</string>
247250
```
248251

249-
- **PackageInstaller:** in order to open APK files, your application needs
250-
`REQUEST_INSTALL_PACKAGES` permission. Add the following code in your
251-
`AndroidManifest.xml`:
252+
You can learn more about localization on Android [here][4]).
253+
254+
### Install .apk files
255+
256+
To open and install `.apk` files, your application needs
257+
`REQUEST_INSTALL_PACKAGES` permission. Add the following in your
258+
`AndroidManifest.xml`:
252259

253260
```xml
254261
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
@@ -437,12 +444,16 @@ plugin is missing some feature.
437444

438445
Pull request are also very welcome!
439446

440-
[fluttercommunity_badge]: https://fluttercommunity.dev/_github/header/flutter_downloader
447+
[fluttercommunity_badge]:
448+
https://fluttercommunity.dev/_github/header/flutter_downloader
441449
[fluttercommunity_link]: https://github.com/fluttercommunity/community
442450
[pub_badge]: https://img.shields.io/pub/v/flutter_downloader.svg
443451
[pub_link]: https://pub.dartlang.org/packages/flutter_downloader
444-
[work_manager]: https://developer.android.com/topic/libraries/architecture/workmanager
445-
[url_session_download_task]: https://developer.apple.com/documentation/foundation/nsurlsessiondownloadtask?language=objc
446-
[android_9_cleartext_traffic]: https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6
452+
[work_manager]:
453+
https://developer.android.com/topic/libraries/architecture/workmanager
454+
[url_session_download_task]:
455+
https://developer.apple.com/documentation/foundation/nsurlsessiondownloadtask?language=objc
456+
[android_9_cleartext_traffic]:
457+
https://medium.com/@son.rommer/fix-cleartext-traffic-error-in-android-9-pie-2f4e9e2235e6
447458
[3]: https://medium.com/@guerrix/info-plist-localization-ad5daaea732a
448459
[4]: https://developer.android.com/training/basics/supporting-devices/languages

android/src/main/java/vn/hunghd/flutterdownloader/DownloadWorker.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class DownloadWorker extends Worker implements MethodChannel.MethodCallHa
9090
private static final int STEP_UPDATE = 5;
9191

9292
private static final AtomicBoolean isolateStarted = new AtomicBoolean(false);
93-
private static final ArrayDeque<List> isolateQueue = new ArrayDeque<>();
93+
private static final ArrayDeque<List<Object>> isolateQueue = new ArrayDeque<>();
9494
private static FlutterEngine backgroundFlutterEngine;
9595

9696
private final Pattern charsetPattern = Pattern.compile("(?i)\\bcharset=\\s*\"?([^\\s;\"]*)");
@@ -114,12 +114,7 @@ public DownloadWorker(@NonNull final Context context,
114114
@NonNull WorkerParameters params) {
115115
super(context, params);
116116

117-
new Handler(context.getMainLooper()).post(new Runnable() {
118-
@Override
119-
public void run() {
120-
startBackgroundIsolate(context);
121-
}
122-
});
117+
new Handler(context.getMainLooper()).post(() -> startBackgroundIsolate(context));
123118
}
124119

125120
private void startBackgroundIsolate(Context context) {
@@ -150,7 +145,7 @@ private void startBackgroundIsolate(Context context) {
150145
}
151146

152147
@Override
153-
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
148+
public void onMethodCall(MethodCall call, @NonNull MethodChannel.Result result) {
154149
if (call.method.equals("didInitializeDispatcher")) {
155150
synchronized (isolateStarted) {
156151
while (!isolateQueue.isEmpty()) {
@@ -819,12 +814,7 @@ public interface CallbackUri {
819814
void invoke(Uri uri);
820815
}
821816

822-
final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
823-
824-
public boolean verify(String hostname, SSLSession session) {
825-
return true;
826-
}
827-
};
817+
final static HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true;
828818

829819
/**
830820
* Trust every server - dont check for any certificate
@@ -838,11 +828,11 @@ public java.security.cert.X509Certificate[] getAcceptedIssuers() {
838828
return new java.security.cert.X509Certificate[] {};
839829
}
840830

841-
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
831+
public void checkClientTrusted(X509Certificate[] chain, String authType) {
842832
Log.i(TAG, "checkClientTrusted");
843833
}
844834

845-
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
835+
public void checkServerTrusted(X509Certificate[] chain, String authType) {
846836
Log.i(TAG, "checkServerTrusted");
847837
}
848838
} };

example/analysis_options.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include: package:leancode_lint/analysis_options.yaml
2+
3+
linter:
4+
rules:
5+
avoid_print: false

example/android/app/src/debug/AndroidManifest.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

example/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
xmlns:tools="http://schemas.android.com/tools">
3-
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
4-
calls FlutterMain.startInitialization(this); in its onCreate method.
5-
In most cases you can leave this as-is, but you if you want to provide
6-
additional functionality it is fine to subclass or reimplement
7-
FlutterApplication and put your custom class here. -->
2+
xmlns:tools="http://schemas.android.com/tools" package="vn.hunghd.flutter_downloader_example">
83

94
<uses-permission android:name="android.permission.INTERNET"/>
105
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>

example/android/app/src/profile/AndroidManifest.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

example/example.iml

Lines changed: 0 additions & 18 deletions
This file was deleted.

example/flutter_downloader_example.iml

Lines changed: 0 additions & 17 deletions
This file was deleted.

example/flutter_downloader_example_android.iml

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)