Cordova源码解析(3)_webview实现类的封装设计

本文深入分析Cordova的CordovaWebViewImpl类,详细探讨其构造方法、接口设计,包括CordovaWebViewEngine的创建、生命周期方法以及CordovaWebViewEngine.Client接口的实现,揭示Cordova中WebView的封装策略。
摘要由CSDN通过智能技术生成

上一章讲述了Cordova的入口CordovaActivity类的实现方法,在这个类中实现了webview的创建和对Html的加载,那么这章就来分析下Cordova中webview是如何封装的。

CordovaWebViewImpl类

CordovaWebViewImpl类实现了CordovaWebView接口,实现了接口方法。这个类在上一章的CordovaActivity中被调用,此类从上到下暴露出的主要接口设计分为以下几类:

  • 构造方法
  • init方法
  • loadUrl方法
  • CordovaWebViewEngine的方法实现
  • 生命周期方法
  • CordovaWebViewEngine.Client 接口的实现

构造方法

public CordovaWebViewImpl(CordovaWebViewEngine cordovaWebViewEngine) {
   
    this.engine = cordovaWebViewEngine;
}

构造方法需要传递一个CordovaWebViewEngine的实例,在CordovaActivity中已经初始化了CordovaWebViewEngine 实例,并传递过来,而创建CordovaWebViewEngine 实例的方法就是调用了CordovaWebViewImpl类的createEngine方法:

public static CordovaWebViewEngine createEngine(Context context, CordovaPreferences preferences) {
   
    String className = preferences.getString("webview", SystemWebViewEngine.class.getCanonicalName());
    try {
   
        Class<?> webViewClass = Class.forName(className);
        Constructor<?> constructor = webViewClass.getConstructor(Context.class, CordovaPreferences.class);
        return (CordovaWebViewEngine) constructor.newInstance(context, preferences);
    } catch (Exception e) {
   
        throw new RuntimeException("Failed to create webview. ", e);
    }
}

这个方法使用反射的方式初始化了SystemWebViewEngine类,为何使用反射,是因为cordova在config.xml中可以配置自己的WebViewEngine,而cordova自带的是SystemWebViewEngine类(TODO 后续分析)。

init方法

CordovaActivity中不仅调用了CordovaWebViewImpl的loadUrl方法,还调用了CordovaWebViewImpl的init方法,重现一下调用代码:

if (!appView.isInitialized()) {
   
    appView.init(cordovaInterface, pluginEntries, preferences);
}

回过头来查看CordovaWebViewImpl的init方法,分析做了哪些工作。

@SuppressLint("Assert")
@Override
public void init(CordovaInterface cordova, List<PluginEntry> pluginEntries, CordovaPreferences preferences) {
   
   if (this.cordova != null) {
   
       throw new IllegalStateException();
   }
   // 初始化CordovaInterface
   this.cordova = cor
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ruiurrui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值