Cordova Android源碼分析系列2(CordovaWebView相關(guān)類分析)
Cordova Android源碼分析系列二(CordovaWebView相關(guān)類分析)
本篇文章是Cordova Android源碼分析系列文章的第二篇,主要分析CordovaWebView和CordovaWebViewClient類,通過分析代碼可以知道Web網(wǎng)頁加載的過程,錯(cuò)誤出來,多線程處理等。 CordovaWebView類分析CordovaWebView類繼承了Android WebView類,這是一個(gè)很自然的實(shí)現(xiàn),共1000多行代碼。包含了PluginManager pluginManager,BroadcastReceiver receiver,CordovaInterface cordova, CordovaWebViewClient viewClient,CordovaChromeClient chromeClient,NativeToJsMessageQueue jsMessageQueue ,ExposedJsApi exposedJsApi,CordovaResourceApi resourceApi等重要的成員變量,與其它核心類關(guān)聯(lián)起來。 提供了4個(gè)構(gòu)造函數(shù):CordovaWebView(Context context),CordovaWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) ,分別對(duì)應(yīng)Android WebView類的相應(yīng)構(gòu)造函數(shù)。這些構(gòu)造函數(shù)首先調(diào)用WebView的相應(yīng)構(gòu)造函數(shù),然后初始化cordova類變量,按情況依次調(diào)用自身的setWebChromeClient,initWebViewClient,loadConfiguration,setup方法。 setWebChromeClient方法設(shè)置WebChromeClient,調(diào)用了WebView類的setWebChromeClient方法。 initWebViewClient方法根據(jù)Android SDK版本的不同,分別調(diào)用setWebViewClient,針對(duì)IceCreamSandwich版本,調(diào)用setWebViewClient(new IceCreamCordovaWebViewClient(this.cordova, this))。暫時(shí)不知道具體的原因。
/**
* 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時(shí)一樣:
WebSettings settings = this.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
針對(duì)HTC 2.x devices系列設(shè)備設(shè)置nav dump(針對(duì)ICS禁用,針對(duì)Jellybean 4.2降級(jí)),具體實(shí)現(xiàn)是通過反射拿到setNavDump方法,如果設(shè)備型號(hào)中包含HTC并且SDK版本小于11(HONEYCOMB),設(shè)置setNavDump(true)。 設(shè)置不保存網(wǎng)頁表單數(shù)據(jù),大家可以放心了! //We don't save any form data in the application settings.setSaveFormData(false); settings.setSavePassword(false); 后面是設(shè)置databasePath,是否打開調(diào)試模式等,依次調(diào)用了setDomStorageEnabled,setGeolocationEnabled,setAppCacheMaxSize,setAppCachePath,setAppCacheEnabled等WebSetting的方法,從名字就可以很容易的理解作用。 然后注冊(cè)了一個(gè)receiver,監(jiān)聽的IntentFilter action是ACTION_CONFIGURATION_CHANGED。 最后初始化了pluginManager,jsMessageQueue,exposedJsApi,resourceApi成員變量。 下面我們看下Web頁面載入的流程,先看loadUrl方法,它其實(shí)是調(diào)用了loadUrlIntoView方法。loadUrlIntoView方法首先會(huì)初始化插件管理器pluginManager,然后創(chuàng)建了2個(gè)Runnable對(duì)象loadError和timeoutCheck,loadError用于通知客戶端viewClient錯(cuò)誤信息,timeoutCheck用于檢查頁面超時(shí),最后在UI線程中調(diào)用loadUrlNow(url)。注意timeoutCheck任務(wù)是在線程池中運(yùn)行的。loadUrlNow方法最終調(diào)用了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,實(shí)現(xiàn)了CordovaWebView的回掉函數(shù),這些回掉函數(shù)在渲染文檔的過程中會(huì)被觸發(fā),例如onPageStarted(),shouldOverrideUrlLoading()等方法。 方法public boolean shouldOverrideUrlLoading(WebView view, String url) 為上層的web應(yīng)用提供了url加載時(shí)處理的機(jī)會(huì),js中的exec方法會(huì)被攔截,交給handleExecUrl(url)方法處理。如果uri是以tel,sms,geo,market等開頭,這里會(huì)通過Intent啟用相關(guān)的App處理。如果是我們自己App或文件,則會(huì)啟動(dòng)一個(gè)新的Activity,包含一個(gè)新的CordovaWebView,主要當(dāng)按返回鍵時(shí),可以返回我們的應(yīng)用。 方法public void onPageStarted(WebView view, String url, Bitmap favicon) 通知應(yīng)用頁面開始加載,如果頁面中包含frameset或iframe,這些嵌入的頁面加載時(shí)是不會(huì)觸發(fā)onPageStarted的。通過this.appView.postMessage方法發(fā)送通知給所有插件。
@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) 的實(shí)現(xiàn)類似onPageStarted,需要注意的是頁面加載完成后延時(shí)2秒才會(huì)停止進(jìn)度條,目的是防止出現(xiàn)js錯(cuò)誤或cordova沒有初始化的情況,做法是創(chuàng)建一個(gè)線程,sleep 2s,然后在UI線程發(fā)送通知spinner stop給所有插件。
CordovaResourceApi類分析Cordova Android使用okhttp框架作為網(wǎng)絡(luò)訪問基礎(chǔ)框架,okhttp 是一個(gè) Java 的 HTTP+SPDY 客戶端開發(fā)包,同時(shí)也支持 Android。 CordovaResourceApi封裝了okhttp,主要提供了3個(gè)功能: 1.讀寫Url的助手方法 例如處理assets, resources, content providers, files, data URIs, http[s] 可以用來查詢文件類型和內(nèi)容長(zhǎng)度 2.允許插件重定向Url 3.通過createHttpConnection()方法暴露Cordova自帶的okhttp庫(kù) 這個(gè)類比較簡(jiǎn)單,先是創(chuàng)建了靜態(tài)變量OkHttpClient httpClient和jsThread,這樣整個(gè)應(yīng)用只有一個(gè)okhttp實(shí)例,是輕量級(jí)實(shí)現(xiàn)。 然后構(gòu)造函數(shù)是 public CordovaResourceApi(Context context, PluginManager pluginManager) { this.contentResolver = context.getContentResolver(); this.assetManager = context.getAssets(); this.pluginManager = pluginManager; } 可以看到初始化了contentResolver,assetManager和pluginManager這個(gè)類成員變量。 public Uri remapUri(Uri uri) { assertNonRelative(uri); Uri pluginUri = pluginManager.remapUri(uri); return pluginUri != null ? pluginUri : uri; } 重定向Url的方法,url必須是絕對(duì)路徑,最終實(shí)現(xiàn)在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格式,這個(gè)方法必須運(yùn)行的后臺(tái)線程的斷言,不能運(yùn)行在UI線程和WebCore線程。 方法public OpenForReadResult openForRead(Uri uri, boolean skipThreadCheck) throws IOException 用來打開指定的uri,skipThreadCheck設(shè)置是否檢查是否在后臺(tái)線程運(yùn)行,返回值OpenForReadResult是個(gè)靜態(tài)內(nèi)部類,提供了uri,inputStream,mimeType,內(nèi)容長(zhǎng)度length,AssetFileDescriptor參數(shù),方便我們使用。 下篇文章分析Cordova插件架構(gòu),主要涉及CordovaPlugin和PluginManager類。 |
|
|