|
| 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.content.res.AssetManager; |
| 8 | +import androidx.annotation.NonNull; |
| 9 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 10 | +import io.flutter.plugin.common.PluginRegistry; |
| 11 | +import java.io.IOException; |
| 12 | + |
| 13 | +/** |
| 14 | + * Provides access to the assets registered as part of the App bundle. |
| 15 | + */ |
| 16 | +abstract class FlutterAssetManager { |
| 17 | + final AssetManager assetManager; |
| 18 | + |
| 19 | + /** |
| 20 | + * Constructs a new instance of the {@link FlutterAssetManager}. |
| 21 | + * |
| 22 | + * @param assetManager Instance of Android's {@link AssetManager} used to access assets within the App bundle. |
| 23 | + */ |
| 24 | + public FlutterAssetManager(AssetManager assetManager) { |
| 25 | + this.assetManager = assetManager; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Gets the relative file path to the Flutter asset with the given name, including the file's extension, e.g., "myImage.jpg". |
| 30 | + * |
| 31 | + * The returned file path is relative to the Android app's standard asset's directory. Therefore, |
| 32 | + * the returned path is appropriate to pass to Android's AssetManager, but the path is not |
| 33 | + * appropriate to load as an absolute path. |
| 34 | + */ |
| 35 | + abstract String getAssetFilePathByName(String name); |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns a String array of all the assets at the given path. |
| 39 | + * |
| 40 | + * @param path A relative path within the assets, i.e., "docs/home.html". This value cannot be null. |
| 41 | + * @return String[] Array of strings, one for each asset. These file names are relative to 'path'. This value may be null. |
| 42 | + * @throws IOException Throws an IOException in case I/O operations were interrupted. |
| 43 | + */ |
| 44 | + public String[] list(@NonNull String path) throws IOException { |
| 45 | + return assetManager.list(path); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Provides access to assets using the {@link PluginRegistry.Registrar} for looking up file paths to Flutter assets. |
| 50 | + * |
| 51 | + * @deprecated The {@link RegistrarFlutterAssetManager} is for Flutter's v1 embedding. For |
| 52 | + * instructions on migrating a plugin from Flutter's v1 Android embedding to v2, visit |
| 53 | + * http://flutter.dev/go/android-plugin-migration |
| 54 | + */ |
| 55 | + @Deprecated |
| 56 | + static class RegistrarFlutterAssetManager extends FlutterAssetManager { |
| 57 | + final PluginRegistry.Registrar registrar; |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructs a new instance of the {@link RegistrarFlutterAssetManager}. |
| 61 | + * |
| 62 | + * @param assetManager Instance of Android's {@link AssetManager} used to access assets within the App bundle. |
| 63 | + * @param registrar Instance of {@link io.flutter.plugin.common.PluginRegistry.Registrar} used to look up file paths to assets registered by Flutter. |
| 64 | + */ |
| 65 | + RegistrarFlutterAssetManager(AssetManager assetManager, PluginRegistry.Registrar registrar) { |
| 66 | + super(assetManager); |
| 67 | + this.registrar = registrar; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String getAssetFilePathByName(String name) { |
| 72 | + return registrar.lookupKeyForAsset(name); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Provides access to assets using the {@link FlutterPlugin.FlutterAssets} for looking up file paths to Flutter assets. |
| 78 | + */ |
| 79 | + static class PluginBindingFlutterAssetManager extends FlutterAssetManager { |
| 80 | + final FlutterPlugin.FlutterAssets flutterAssets; |
| 81 | + |
| 82 | + /** |
| 83 | + * Constructs a new instance of the {@link PluginBindingFlutterAssetManager}. |
| 84 | + * |
| 85 | + * @param assetManager Instance of Android's {@link AssetManager} used to access assets within the App bundle. |
| 86 | + * @param flutterAssets Instance of {@link io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterAssets} used to look up file paths to assets registered by Flutter. |
| 87 | + */ |
| 88 | + PluginBindingFlutterAssetManager(AssetManager assetManager, FlutterPlugin.FlutterAssets flutterAssets) { |
| 89 | + super(assetManager); |
| 90 | + this.flutterAssets = flutterAssets; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public String getAssetFilePathByName(String name) { |
| 95 | + return flutterAssets.getAssetFilePathByName(name); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
0 commit comments