|
5 | 5 | package io.flutter.plugins.webviewflutter; |
6 | 6 |
|
7 | 7 | import android.webkit.DownloadListener; |
8 | | -import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.DownloadListenerFlutterApi; |
| 8 | +import androidx.annotation.NonNull; |
| 9 | +import androidx.annotation.Nullable; |
| 10 | +import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.DownloadListenerHostApi; |
9 | 11 |
|
10 | | -class DownloadListenerHostApiImpl implements GeneratedAndroidWebView.DownloadListenerHostApi { |
| 12 | +/** |
| 13 | + * Host api implementation for {@link DownloadListener}. |
| 14 | + * |
| 15 | + * <p>Handles creating {@link DownloadListener}s that intercommunicate with a paired Dart object. |
| 16 | + */ |
| 17 | +public class DownloadListenerHostApiImpl implements DownloadListenerHostApi { |
11 | 18 | private final InstanceManager instanceManager; |
12 | 19 | private final DownloadListenerCreator downloadListenerCreator; |
13 | | - private final GeneratedAndroidWebView.DownloadListenerFlutterApi downloadListenerFlutterApi; |
14 | | - |
15 | | - static class DownloadListenerCreator { |
16 | | - DownloadListener createDownloadListener( |
17 | | - Long instanceId, DownloadListenerFlutterApi downloadListenerFlutterApi) { |
18 | | - return (url, userAgent, contentDisposition, mimetype, contentLength) -> |
19 | | - downloadListenerFlutterApi.onDownloadStart( |
20 | | - instanceId, url, userAgent, contentDisposition, mimetype, contentLength, reply -> {}); |
| 20 | + private final DownloadListenerFlutterApiImpl flutterApi; |
| 21 | + |
| 22 | + /** |
| 23 | + * Implementation of {@link DownloadListener} that passes arguments of callback methods to Dart. |
| 24 | + * |
| 25 | + * <p>No messages are sent to Dart after {@link DownloadListenerImpl#release} is called. |
| 26 | + */ |
| 27 | + public static class DownloadListenerImpl implements DownloadListener, Releasable { |
| 28 | + @Nullable private DownloadListenerFlutterApiImpl flutterApi; |
| 29 | + |
| 30 | + /** |
| 31 | + * Creates a {@link DownloadListenerImpl} that passes arguments of callbacks methods to Dart. |
| 32 | + * |
| 33 | + * @param flutterApi handles sending messages to Dart |
| 34 | + */ |
| 35 | + public DownloadListenerImpl(@NonNull DownloadListenerFlutterApiImpl flutterApi) { |
| 36 | + this.flutterApi = flutterApi; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void onDownloadStart( |
| 41 | + String url, |
| 42 | + String userAgent, |
| 43 | + String contentDisposition, |
| 44 | + String mimetype, |
| 45 | + long contentLength) { |
| 46 | + if (flutterApi != null) { |
| 47 | + flutterApi.onDownloadStart( |
| 48 | + this, url, userAgent, contentDisposition, mimetype, contentLength, reply -> {}); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void release() { |
| 54 | + if (flutterApi != null) { |
| 55 | + flutterApi.dispose(this, reply -> {}); |
| 56 | + } |
| 57 | + flutterApi = null; |
21 | 58 | } |
22 | 59 | } |
23 | 60 |
|
24 | | - DownloadListenerHostApiImpl( |
| 61 | + /** Handles creating {@link DownloadListenerImpl}s for a {@link DownloadListenerHostApiImpl}. */ |
| 62 | + public static class DownloadListenerCreator { |
| 63 | + /** |
| 64 | + * Creates a {@link DownloadListenerImpl}. |
| 65 | + * |
| 66 | + * @param flutterApi handles sending messages to Dart |
| 67 | + * @return the created {@link DownloadListenerImpl} |
| 68 | + */ |
| 69 | + public DownloadListenerImpl createDownloadListener(DownloadListenerFlutterApiImpl flutterApi) { |
| 70 | + return new DownloadListenerImpl(flutterApi); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Creates a host API that handles creating {@link DownloadListener}s. |
| 76 | + * |
| 77 | + * @param instanceManager maintains instances stored to communicate with Dart objects |
| 78 | + * @param downloadListenerCreator handles creating {@link DownloadListenerImpl}s |
| 79 | + * @param flutterApi handles sending messages to Dart |
| 80 | + */ |
| 81 | + public DownloadListenerHostApiImpl( |
25 | 82 | InstanceManager instanceManager, |
26 | 83 | DownloadListenerCreator downloadListenerCreator, |
27 | | - DownloadListenerFlutterApi downloadListenerFlutterApi) { |
| 84 | + DownloadListenerFlutterApiImpl flutterApi) { |
28 | 85 | this.instanceManager = instanceManager; |
29 | 86 | this.downloadListenerCreator = downloadListenerCreator; |
30 | | - this.downloadListenerFlutterApi = downloadListenerFlutterApi; |
| 87 | + this.flutterApi = flutterApi; |
31 | 88 | } |
32 | 89 |
|
33 | 90 | @Override |
34 | 91 | public void create(Long instanceId) { |
35 | 92 | final DownloadListener downloadListener = |
36 | | - downloadListenerCreator.createDownloadListener(instanceId, downloadListenerFlutterApi); |
| 93 | + downloadListenerCreator.createDownloadListener(flutterApi); |
37 | 94 | instanceManager.addInstance(downloadListener, instanceId); |
38 | 95 | } |
39 | | - |
40 | | - @Override |
41 | | - public void dispose(Long instanceId) { |
42 | | - instanceManager.removeInstance(instanceId); |
43 | | - } |
44 | 96 | } |
0 commit comments