Skip to content

Commit

Permalink
Provide lookup key to access Flutter assets in the APK (flutter#4785)
Browse files Browse the repository at this point in the history
  • Loading branch information
szakarias authored Mar 16, 2018
1 parent 9671f63 commit 2c5a1bf
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
11 changes: 11 additions & 0 deletions shell/platform/android/io/flutter/app/FlutterPluginRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.content.Intent;
import android.util.Log;
import io.flutter.plugin.common.*;
import io.flutter.view.FlutterMain;
import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;
Expand Down Expand Up @@ -111,6 +112,16 @@ public FlutterView view() {
return mFlutterView;
}

@Override
public String lookupKeyForAsset(String asset) {
return FlutterMain.getLookupKeyForAsset(asset);
}

@Override
public String lookupKeyForAsset(String asset, String packageName) {
return FlutterMain.getLookupKeyForAsset(asset, packageName);
}

@Override
public Registrar publish(Object value) {
mPluginMap.put(pluginKey, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,29 @@ interface Registrar {
*/
FlutterView view();


/**
* Returns the file name for the given asset.
* The returned file name can be used to access the asset in the APK
* through the {@link AssetManager} API.
*
* @param asset the name of the asset. The name can be hierarchical
* @return the filename to be used with {@link AssetManager}
*/
String lookupKeyForAsset(String asset);

/**
* Returns the file name for the given asset which originates from the
* specified packageName. The returned file name can be used to access
* the asset in the APK through the {@link AssetManager} API.
*
* @param asset the name of the asset. The name can be hierarchical
* @param packageName the name of the package from which the asset originates
* @return the file name to be used with {@link AssetManager}
*/
String lookupKeyForAsset(String asset, String packageName);


/**
* Publishes a value associated with the plugin being registered.
*
Expand Down
28 changes: 27 additions & 1 deletion shell/platform/android/io/flutter/view/FlutterMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class FlutterMain {
private static final String MANIFEST = "flutter.yaml";

private static String fromFlutterAssets(String filePath) {
return sFlutterAssetsDir + "/" + filePath;
return sFlutterAssetsDir + File.separator + filePath;
}

private static final Set<String> SKY_RESOURCES = ImmutableSetBuilder.<String>newInstance()
Expand Down Expand Up @@ -308,4 +308,30 @@ public static String findAppBundlePath(Context applicationContext) {
File appBundle = new File(dataDirectory, sFlutterAssetsDir);
return appBundle.exists() ? appBundle.getPath() : null;
}

/**
* Returns the file name for the given asset.
* The returned file name can be used to access the asset in the APK
* through the {@link AssetManager} API.
*
* @param asset the name of the asset. The name can be hierarchical
* @return the filename to be used with {@link AssetManager}
*/
public static String getLookupKeyForAsset(String asset) {
return fromFlutterAssets(asset);
}

/**
* Returns the file name for the given asset which originates from the
* specified packageName. The returned file name can be used to access
* the asset in the APK through the {@link AssetManager} API.
*
* @param asset the name of the asset. The name can be hierarchical
* @param packageName the name of the package from which the asset originates
* @return the file name to be used with {@link AssetManager}
*/
public static String getLookupKeyForAsset(String asset, String packageName) {
return getLookupKeyForAsset(
"packages" + File.separator + packageName + File.separator + asset);
}
}
8 changes: 8 additions & 0 deletions shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ public FlutterPluginRegistry getPluginRegistry() {
return mNativeView.getPluginRegistry();
}

public String getLookupKeyForAsset(String asset) {
return FlutterMain.getLookupKeyForAsset(asset);
}

public String getLookupKeyForAsset(String asset, String packageName) {
return FlutterMain.getLookupKeyForAsset(asset, packageName);
}

public void addActivityLifecycleListener(ActivityLifecycleListener listener) {
mActivityLifecycleListeners.add(listener);
}
Expand Down

0 comments on commit 2c5a1bf

Please sign in to comment.