|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugins.webviewflutter; |
| 6 | + |
| 7 | +import android.webkit.DownloadListener; |
| 8 | +import io.flutter.plugins.webviewflutter.GeneratedAndroidWebView.DownloadListenerFlutterApi; |
| 9 | + |
| 10 | +class DownloadListenerHostApiImpl implements GeneratedAndroidWebView.DownloadListenerHostApi { |
| 11 | + private final InstanceManager instanceManager; |
| 12 | + 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 -> {}); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + DownloadListenerHostApiImpl( |
| 25 | + InstanceManager instanceManager, |
| 26 | + DownloadListenerCreator downloadListenerCreator, |
| 27 | + DownloadListenerFlutterApi downloadListenerFlutterApi) { |
| 28 | + this.instanceManager = instanceManager; |
| 29 | + this.downloadListenerCreator = downloadListenerCreator; |
| 30 | + this.downloadListenerFlutterApi = downloadListenerFlutterApi; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void create(Long instanceId) { |
| 35 | + final DownloadListener downloadListener = |
| 36 | + downloadListenerCreator.createDownloadListener(instanceId, downloadListenerFlutterApi); |
| 37 | + instanceManager.addInstance(downloadListener, instanceId); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void dispose(Long instanceId) { |
| 42 | + instanceManager.removeInstance(instanceId); |
| 43 | + } |
| 44 | +} |
0 commit comments