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

Commit 310a7f7

Browse files
Expose asset lookup from plugin binding. (#42019)
1 parent ddceed5 commit 310a7f7

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;
@@ -91,19 +92,22 @@ class FlutterPluginBinding {
9192
private final BinaryMessenger binaryMessenger;
9293
private final TextureRegistry textureRegistry;
9394
private final PlatformViewRegistry platformViewRegistry;
95+
private final FlutterAssets flutterAssets;
9496

9597
public FlutterPluginBinding(
9698
@NonNull Context applicationContext,
9799
@NonNull FlutterEngine flutterEngine,
98100
@NonNull BinaryMessenger binaryMessenger,
99101
@NonNull TextureRegistry textureRegistry,
100-
@NonNull PlatformViewRegistry platformViewRegistry
102+
@NonNull PlatformViewRegistry platformViewRegistry,
103+
@NonNull FlutterAssets flutterAssets
101104
) {
102105
this.applicationContext = applicationContext;
103106
this.flutterEngine = flutterEngine;
104107
this.binaryMessenger = binaryMessenger;
105108
this.textureRegistry = textureRegistry;
106109
this.platformViewRegistry = platformViewRegistry;
110+
this.flutterAssets = flutterAssets;
107111
}
108112

109113
@NonNull
@@ -136,5 +140,47 @@ public TextureRegistry getTextureRegistry() {
136140
public PlatformViewRegistry getPlatformViewRegistry() {
137141
return platformViewRegistry;
138142
}
143+
144+
@NonNull
145+
public FlutterAssets getFlutterAssets() {
146+
return flutterAssets;
147+
}
139148
}
149+
150+
/**
151+
* Provides Flutter plugins with access to Flutter asset information.
152+
*/
153+
interface FlutterAssets {
154+
/**
155+
* Returns the relative file path to the Flutter asset with the given name, including the file's
156+
* extension, e.g., {@code "myImage.jpg"}.
157+
*
158+
* <p>The returned file path is relative to the Android app's standard assets directory.
159+
* Therefore, the returned path is appropriate to pass to Android's {@code AssetManager},
160+
* but the path is not appropriate to load as an absolute path.
161+
*/
162+
String getAssetFilePathByName(@NonNull String assetFileName);
163+
164+
/**
165+
* Same as {@link #getAssetFilePathByName(String)} but with added support for an explicit
166+
* Android {@code packageName}.
167+
*/
168+
String getAssetFilePathByName(@NonNull String assetFileName, @NonNull String packageName);
169+
170+
/**
171+
* Returns the relative file path to the Flutter asset with the given subpath, including the file's
172+
* extension, e.g., {@code "/dir1/dir2/myImage.jpg"}.
173+
*
174+
* <p>The returned file path is relative to the Android app's standard assets directory.
175+
* Therefore, the returned path is appropriate to pass to Android's {@code AssetManager},
176+
* but the path is not appropriate to load as an absolute path.
177+
*/
178+
String getAssetFilePathBySubpath(@NonNull String assetSubpath);
179+
180+
/**
181+
* Same as {@link #getAssetFilePathBySubpath(String)} but with added support for an explicit
182+
* Android {@code packageName}.
183+
*/
184+
String getAssetFilePathBySubpath(@NonNull String assetSubpath, @NonNull String packageName);
185+
}
140186
}

0 commit comments

Comments
 (0)