Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8057920

Browse files
Expose asset lookup from plugin binding. (#42019)
1 parent e150bf3 commit 8057920

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

shell/platform/android/io/flutter/embedding/engine/FlutterEnginePluginRegistry.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222

2323
import io.flutter.Log;
24+
import io.flutter.embedding.engine.loader.FlutterLoader;
2425
import io.flutter.embedding.engine.plugins.FlutterPlugin;
2526
import io.flutter.embedding.engine.plugins.PluginRegistry;
2627
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
@@ -98,7 +99,8 @@ class FlutterEnginePluginRegistry implements PluginRegistry,
9899
flutterEngine,
99100
flutterEngine.getDartExecutor(),
100101
flutterEngine.getRenderer(),
101-
flutterEngine.getPlatformViewsController().getRegistry()
102+
flutterEngine.getPlatformViewsController().getRegistry(),
103+
new DefaultFlutterAssets(FlutterLoader.getInstance())
102104
);
103105
}
104106

@@ -532,6 +534,30 @@ public void detachFromContentProvider() {
532534
}
533535
//----- End ContentProviderControlSurface -----
534536

537+
private static class DefaultFlutterAssets implements FlutterPlugin.FlutterAssets {
538+
final FlutterLoader flutterLoader;
539+
540+
private DefaultFlutterAssets(@NonNull FlutterLoader flutterLoader) {
541+
this.flutterLoader = flutterLoader;
542+
}
543+
544+
public String getAssetFilePathByName(@NonNull String assetFileName) {
545+
return flutterLoader.getLookupKeyForAsset(assetFileName);
546+
}
547+
548+
public String getAssetFilePathByName(@NonNull String assetFileName, @NonNull String packageName) {
549+
return flutterLoader.getLookupKeyForAsset(assetFileName, packageName);
550+
}
551+
552+
public String getAssetFilePathBySubpath(@NonNull String assetSubpath) {
553+
return flutterLoader.getLookupKeyForAsset(assetSubpath);
554+
}
555+
556+
public String getAssetFilePathBySubpath(@NonNull String assetSubpath, @NonNull String packageName) {
557+
return flutterLoader.getLookupKeyForAsset(assetSubpath, packageName);
558+
}
559+
}
560+
535561
private static class FlutterEngineActivityPluginBinding implements ActivityPluginBinding {
536562
@NonNull
537563
private final Activity activity;

shell/platform/android/io/flutter/embedding/engine/plugins/FlutterPlugin.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.support.annotation.NonNull;
1010

1111
import io.flutter.embedding.engine.FlutterEngine;
12+
import io.flutter.embedding.engine.loader.FlutterLoader;
1213
import io.flutter.plugin.common.BinaryMessenger;
1314
import io.flutter.plugin.platform.PlatformViewRegistry;
1415
import io.flutter.view.TextureRegistry;
@@ -101,19 +102,22 @@ class FlutterPluginBinding {
101102
private final BinaryMessenger binaryMessenger;
102103
private final TextureRegistry textureRegistry;
103104
private final PlatformViewRegistry platformViewRegistry;
105+
private final FlutterAssets flutterAssets;
104106

105107
public FlutterPluginBinding(
106108
@NonNull Context applicationContext,
107109
@NonNull FlutterEngine flutterEngine,
108110
@NonNull BinaryMessenger binaryMessenger,
109111
@NonNull TextureRegistry textureRegistry,
110-
@NonNull PlatformViewRegistry platformViewRegistry
112+
@NonNull PlatformViewRegistry platformViewRegistry,
113+
@NonNull FlutterAssets flutterAssets
111114
) {
112115
this.applicationContext = applicationContext;
113116
this.flutterEngine = flutterEngine;
114117
this.binaryMessenger = binaryMessenger;
115118
this.textureRegistry = textureRegistry;
116119
this.platformViewRegistry = platformViewRegistry;
120+
this.flutterAssets = flutterAssets;
117121
}
118122

119123
@NonNull
@@ -146,5 +150,47 @@ public TextureRegistry getTextureRegistry() {
146150
public PlatformViewRegistry getPlatformViewRegistry() {
147151
return platformViewRegistry;
148152
}
153+
154+
@NonNull
155+
public FlutterAssets getFlutterAssets() {
156+
return flutterAssets;
157+
}
149158
}
159+
160+
/**
161+
* Provides Flutter plugins with access to Flutter asset information.
162+
*/
163+
interface FlutterAssets {
164+
/**
165+
* Returns the relative file path to the Flutter asset with the given name, including the file's
166+
* extension, e.g., {@code "myImage.jpg"}.
167+
*
168+
* <p>The returned file path is relative to the Android app's standard assets directory.
169+
* Therefore, the returned path is appropriate to pass to Android's {@code AssetManager},
170+
* but the path is not appropriate to load as an absolute path.
171+
*/
172+
String getAssetFilePathByName(@NonNull String assetFileName);
173+
174+
/**
175+
* Same as {@link #getAssetFilePathByName(String)} but with added support for an explicit
176+
* Android {@code packageName}.
177+
*/
178+
String getAssetFilePathByName(@NonNull String assetFileName, @NonNull String packageName);
179+
180+
/**
181+
* Returns the relative file path to the Flutter asset with the given subpath, including the file's
182+
* extension, e.g., {@code "/dir1/dir2/myImage.jpg"}.
183+
*
184+
* <p>The returned file path is relative to the Android app's standard assets directory.
185+
* Therefore, the returned path is appropriate to pass to Android's {@code AssetManager},
186+
* but the path is not appropriate to load as an absolute path.
187+
*/
188+
String getAssetFilePathBySubpath(@NonNull String assetSubpath);
189+
190+
/**
191+
* Same as {@link #getAssetFilePathBySubpath(String)} but with added support for an explicit
192+
* Android {@code packageName}.
193+
*/
194+
String getAssetFilePathBySubpath(@NonNull String assetSubpath, @NonNull String packageName);
195+
}
150196
}

0 commit comments

Comments
 (0)