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






