本篇文章是Cordova Android源码分析系列文章的第二篇,主要分析CordovaWebView和CordovaWebViewClient类,通过分析代码可以知道Web网页加载的过程,错误出来,多线程处理等。
CordovaWebView类分析
CordovaWebView类继承了Android WebView类,这是一个很自然的实现,共1000多行代码。包含了PluginManager pluginManager,BroadcastReceiver receiver,CordovaInterface cordova, CordovaWebViewClient viewClient,CordovaChromeClient chromeClient,NativeToJsMessageQueue jsMessageQueue ,ExposedJsApi exposedJsApi,CordovaResourceApi resourceApi等重要的成员变量,与其它核心类关联起来。
提供了4个构造函数:CordovaWebView(Context context),CordovaWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) ,分别对应Android WebView类的相应构造函数。这些构造函数首先调用WebView的相应构造函数,然后初始化cordova类变量,按情况依次调用自身的setWebChromeClient,initWebViewClient,loadConfiguration,setup方法。
setWebChromeClient方法设置WebChromeClient,调用了WebView类的setWebChromeClient方法。
initWebViewClient方法根据Android SDK版本的不同,分别调用setWebViewClient,针对IceCreamSandwich版本,调用setWebViewClient(new IceCreamCordovaWebViewClient(this.cordova, this))。暂时不知道具体的原因。
/**
* set the WebViewClient, but provide special case handling for IceCreamSandwich.
*/
private void initWebViewClient(CordovaInterface cordova) {
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB ||
android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)
{
this.setWebViewClient(new CordovaWebViewClient(this.cordova, this));
}
else
{
this.setWebViewClient(new IceCreamCordovaWebViewClient(this.cordova, this));
}
}
setup方法的作用是初始化WebView。首先启用JavaScript,就像我们自己使用WebView时一样:
WebSettings settings = this.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
针对HTC 2.x devices系列设备设置nav dump(针对ICS禁用,针对Jellybean 4.2降级),具体实现是通过反射拿到setNavDump方法,如果设备型号中包含HTC并且SDK版本小于11(HONEYCOMB),设置setNavDump(true)。
设置不保存网页表单数据,大家可以放心了!
//We don't save any form data in the application
settings.setSaveFormData(false);
settings.setSavePassword(false);
后面是设置databasePath,是否打开调试模式等,依次调用了setDomStorageEnabled,setGeolocationEnabled,setAppCacheMaxSize,setAppCachePath,setAppCacheEnabled等WebSetting的方法,从名字就可以很容易的理解作用。
然后注册了一个receiver,监听的IntentFilter action是ACTION_CONFIGURATION_CHANGED。
最后初始化了pluginManager,jsMessageQueue,exposedJsApi,resourceApi成员变量。
下面我们看下Web页面载入的流程,先看loadUrl方法,它其实是调用了loadUrlIntoView方法。loadUrlIntoView方法首先会初始化插件管理器pluginManager,然后创建了2个Runnable对象loadError和timeoutCheck,loadError用于通知客户端viewClient错误信息,timeoutCheck用于检查页面超时,最后在UI线程中调用loadUrlNow(url)。注意timeoutCheck任务是在线程池中运行的。loadUrlNow方法最终调用了WebView的loadUrl(url)方法。
// Load url
this.cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
cordova.getThreadPool().execute(timeoutCheck);
me.loadUrlNow(url);
}
});
CordovaWebViewClient类分析
CordovaWebViewClient类继承了android WebViewClient,实现了CordovaWebView的回掉函数,这些回掉函数在渲染文档的过程中会被触发,例如onPageStarted(),shouldOverrideUrlLoading()等方法。
方法public boolean shouldOverrideUrlLoading(WebView view, String url) 为上层的web应用提供了url加载时处理的机会,js中的exec方法会被拦截,交给handleExecUrl(url)方法处理。如果uri是以tel,sms,geo,market等开头,这里会通过Intent启用相关的App处理。如果是我们自己App或文件,则会启动一个新的Activity,包含一个新的CordovaWebView,主要当按返回键时,可以返回我们的应用。
方法public void onPageStarted(WebView view, String url, Bitmap favicon) 通知应用页面开始加载,如果页面中包含frameset或iframe,这些嵌入的页面加载时是不会触发onPageStarted的。通过this.appView.postMessage方法发送通知给所有插件。
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
isCurrentlyLoading = true;
LOG.d(TAG, "onPageStarted(" + url + ")");
// Flush stale messages.
this.appView.jsMessageQueue.reset();
// Broadcast message that page has loaded
this.appView.postMessage("onPageStarted", url);
// Notify all plugins of the navigation, so they can clean up if necessary.
if (this.appView.pluginManager != null) {
this.appView.pluginManager.onReset();
}
}
方法public void onPageFinished(WebView view, String url) 的实现类似onPageStarted,需要注意的是页面加载完成后延时2秒才会停止进度条,目的是防止出现js错误或cordova没有初始化的情况,做法是创建一个线程,sleep 2s,然后在UI线程发送通知spinner stop给所有插件。
CordovaResourceApi类分析
Cordova Android使用okhttp框架作为网络访问基础框架,okhttp 是一个 Java 的 HTTP+SPDY 客户端开发包,同时也支持 Android。
CordovaResourceApi封装了okhttp,主要提供了3个功能:
1.读写Url的助手方法
例如处理assets, resources, content providers, files, data URIs, http[s]
可以用来查询文件类型和内容长度
2.允许插件重定向Url
3.通过createHttpConnection()方法暴露Cordova自带的okhttp库
这个类比较简单,先是创建了静态变量OkHttpClient httpClient和jsThread,这样整个应用只有一个okhttp实例,是轻量级实现。
然后构造函数是
public CordovaResourceApi(Context context, PluginManager pluginManager) {
this.contentResolver = context.getContentResolver();
this.assetManager = context.getAssets();
this.pluginManager = pluginManager;
}
可以看到初始化了contentResolver,assetManager和pluginManager这个类成员变量。
public Uri remapUri(Uri uri) {
assertNonRelative(uri);
Uri pluginUri = pluginManager.remapUri(uri);
return pluginUri != null ? pluginUri : uri;
}
重定向Url的方法,url必须是绝对路径,最终实现在pluginManager.remapUri(uri)
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Ignore excessive calls.
if (!isCurrentlyLoading) {
return;
}
isCurrentlyLoading = false;
LOG.d(TAG, "onPageFinished(" + url + ")");
/**
* Because of a timing issue we need to clear this history in onPageFinished as well as
* onPageStarted. However we only want to do this if the doClearHistory boolean is set to
* true. You see when you load a url with a # in it which is common in jQuery applications
* onPageStared is not called. Clearing the history at that point would break jQuery apps.
*/
if (this.doClearHistory) {
view.clearHistory();
this.doClearHistory = false;
}
// Clear timeout flag
this.appView.loadUrlTimeout++;
// Broadcast message that page has loaded
this.appView.postMessage("onPageFinished", url);
// Make app visible after 2 sec in case there was a JS error and Cordova JS never initialized correctly
if (this.appView.getVisibility() == View.INVISIBLE) {
Thread t = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
appView.postMessage("spinner", "stop");
}
});
} catch (InterruptedException e) {
}
}
});
t.start();
}
// Shutdown if blank loaded
if (url.equals("about:blank")) {
appView.postMessage("exit", null);
}
}
public File mapUriToFile(Uri uri) 返回url指向的文件,url可以是file://或content格式,这个方法必须运行的后台线程的断言,不能运行在UI线程和WebCore线程。
方法public OpenForReadResult openForRead(Uri uri, boolean skipThreadCheck) throws IOException 用来打开指定的uri,skipThreadCheck设置是否检查是否在后台线程运行,返回值OpenForReadResult是个静态内部类,提供了uri,inputStream,mimeType,内容长度length,AssetFileDescriptor参数,方便我们使用。
下篇文章分析Cordova插件架构,主要涉及CordovaPlugin和PluginManager类。