Skip to content

Commit

Permalink
Merge pull request #91 from openwebf/fix/android_so_not_found
Browse files Browse the repository at this point in the history
fix: fix android dynamic libraray not found on some devices
  • Loading branch information
andycall committed Nov 7, 2022
2 parents aa48858 + 7d32116 commit 0b1346d
Showing 1 changed file with 72 additions and 59 deletions.
131 changes: 72 additions & 59 deletions webf/android/src/main/java/com/openwebf/webf/WebFPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,72 +14,85 @@
* WebFPlugin
*/
public class WebFPlugin implements FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
public MethodChannel channel;
private FlutterEngine flutterEngine;
private Context mContext;
private WebF mWebF;
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
public MethodChannel channel;
private FlutterEngine flutterEngine;
private Context mContext;
private WebF mWebF;

// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "webf");
WebFPlugin plugin = new WebFPlugin();
plugin.mContext = registrar.context();
channel.setMethodCallHandler(plugin);
}
// This static function is optional and equivalent to onAttachedToEngine. It supports the old
// pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
// plugin registration via this function while apps migrate to use the new Android APIs
// post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
//
// It is encouraged to share logic between onAttachedToEngine and registerWith to keep
// them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
// depending on the user's project. onAttachedToEngine or registerWith must both be defined
// in the same class.
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "webf");
WebFPlugin plugin = new WebFPlugin();
plugin.mContext = registrar.context();
channel.setMethodCallHandler(plugin);
}

@Override
public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
mContext = flutterPluginBinding.getApplicationContext();
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "webf");
flutterEngine = flutterPluginBinding.getFlutterEngine();
channel.setMethodCallHandler(this);
}
@Override
public void onAttachedToEngine(FlutterPluginBinding flutterPluginBinding) {
loadLibrary();
mContext = flutterPluginBinding.getApplicationContext();
channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "webf");
flutterEngine = flutterPluginBinding.getFlutterEngine();
channel.setMethodCallHandler(this);
}

WebF getWebF() {
if (mWebF == null) {
mWebF = WebF.get(flutterEngine);
}
return mWebF;
WebF getWebF() {
if (mWebF == null) {
mWebF = WebF.get(flutterEngine);
}
return mWebF;
}

@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "getDynamicLibraryPath": {
WebF webf = getWebF();
result.success(webf == null ? "" : webf.getDynamicLibraryPath());
break;
}
case "getTemporaryDirectory":
result.success(getTemporaryDirectory());
break;
default:
result.notImplemented();
}
private static boolean isLibraryLoaded = false;
private static void loadLibrary() {
if (isLibraryLoaded) {
return;
}
// Loads `libwebf.so`.
System.loadLibrary("webf");
// Loads `libquickjs.so`.
System.loadLibrary("quickjs");
isLibraryLoaded = true;
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
WebF webf = WebF.get(flutterEngine);
if (webf == null) return;
webf.destroy();
flutterEngine = null;
@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {
case "getDynamicLibraryPath": {
WebF webf = getWebF();
result.success(webf == null ? "" : webf.getDynamicLibraryPath());
break;
}
case "getTemporaryDirectory":
result.success(getTemporaryDirectory());
break;
default:
result.notImplemented();
}
}

private String getTemporaryDirectory() {
return mContext.getCacheDir().getPath() + "/WebF";
}
@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
WebF webf = WebF.get(flutterEngine);
if (webf == null) return;
webf.destroy();
flutterEngine = null;
}

private String getTemporaryDirectory() {
return mContext.getCacheDir().getPath() + "/WebF";
}
}

0 comments on commit 0b1346d

Please sign in to comment.