Skip to content

Commit

Permalink
精简项目结构,拆解为更易用的接口
Browse files Browse the repository at this point in the history
  • Loading branch information
pedant committed Sep 9, 2014
1 parent cafebdc commit e125e10
Show file tree
Hide file tree
Showing 20 changed files with 489 additions and 1,574 deletions.
5 changes: 2 additions & 3 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
<application
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:name=".MainApp">
android:icon="@drawable/ic_launcher">
<activity
android:name=".page.WebActivity"
android:name=".demo.WebActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand Down
39 changes: 1 addition & 38 deletions assets/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,6 @@ <h3>Java Interface 可用接口列表测试</h3>
<button onclick="HostApp.alert(HostApp.getIMSI());">测试</button>
</li>

<br/>
<li>
是否存在纵向滚动条<br/>
代码:<br/>
<div>
HostApp.alert(
HostApp.existVerticalScrollbar()
);
</div>
<button onclick="HostApp.alert(HostApp.existVerticalScrollbar());">测试</button>
</li>

<br/>
<li>
传入JSON对象,返回对象的一个键值字符串<br/>
Expand All @@ -121,7 +109,7 @@ <h3>Java Interface 可用接口列表测试</h3>

<br/>
<li>
传入回调的js函数给Java方法,设定3秒后回调<br/>
异步回调,传入js函数到Java方法,设定3秒后回调<br/>
代码:<br/>
<div>
HostApp.delayJsCallBack(3, 'call back haha', function (msg) {
Expand All @@ -131,20 +119,6 @@ <h3>Java Interface 可用接口列表测试</h3>
<button onclick="HostApp.delayJsCallBack(3, 'call back haha', function (msg) {HostApp.alert(msg);});">测试</button>
</li>

<br/>
<li>
注册浏览器滚动到底部时的JS回调函数<br/>
代码:<br/>
<div>
if (HostApp.existVerticalScrollbar()) {
HostApp.setOnScrollBottomListener (40, function (contentHeight) {
HostApp.toast("reach content(height " + contentHeight + ") bottom");
});
}
</div>
<button style="width: 150px;" onclick="HostApp.resetContentHeight();window.scroll(0, window.document.body.scrollHeight);">直接滚动到底部</button>
</li>

<br/>
<li>
重载方法测试-执行整型定义<br/>
Expand Down Expand Up @@ -194,16 +168,5 @@ <h3>Java Interface 可用接口列表测试</h3>
</li>
</ul>


<script type="text/javascript">
$(function () {
//HostApp.alert("document ready now");
HostApp.setOnScrollBottomListener (40, function (contentHeight) {
HostApp.toast("reach content(height " + contentHeight + ") bottom");
});
});
</script>


</body>
</html>
29 changes: 0 additions & 29 deletions res/layout/main.xml

This file was deleted.

41 changes: 0 additions & 41 deletions src/cn/pedant/SafeJava4WebviewJS/MainApp.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,60 +1,57 @@
/**
* Summary: 应用中使用的WebChromeClient基类
* Version 1.0
* Date: 13-11-8
* Time: 下午2:31
* Copyright: Copyright (c) 2013
*/

package cn.pedant.SafeJava4WebviewJS.webview;

import android.webkit.*;
import cn.pedant.SafeJava4WebviewJS.MainApp;
import cn.pedant.SafeJava4WebviewJS.util.Log;


public class BaseWebChromeClient extends WebChromeClient {
private static BaseWebChromeClient mInstance;

public static BaseWebChromeClient getInstance () {
if (mInstance == null) {
mInstance = new BaseWebChromeClient();
}
return mInstance;
}

// 处理Alert事件
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
result.confirm();
return true;
}

@Override
public void onProgressChanged (WebView view, int newProgress) {
BaseWebView baseWebView = (BaseWebView)view;
//为什么要在这里注入JS
//1 OnPageStarted中注入有可能全局注入不成功,导致页面脚本上所有接口任何时候都不可用
//2 OnPageFinished中注入,虽然最后都会全局注入成功,但是完成时间有可能太晚,当页面在初始化调用接口函数时会等待时间过长
//3 在进度变化时注入,刚好可以在上面两个问题中得到一个折中处理
//为什么是进度大于25%才进行注入,因为从测试看来只有进度大于这个数字页面才真正得到框架刷新加载,保证100%注入成功
if (newProgress > 25 && !baseWebView.isInjectedJS()) {
baseWebView.loadJS(MainApp.getIns().getJsCallJava().getPreloadInterfaceJS());
baseWebView.setIsInjectedJS(true);
Log.d(" inject js interface completely on progress " + newProgress);
}
super.onProgressChanged(view, newProgress);
}

@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
result.confirm(MainApp.getIns().getJsCallJava().call(view, message));
return true;
}

@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
// we MUST return true we are done with debugging
return !MainApp.IS_DEBUGING; // return false to enable console.log
}
}
/**
* Summary: 应用中使用的WebChromeClient基类
* Version 1.0
* Date: 13-11-8
* Time: 下午2:31
* Copyright: Copyright (c) 2013
*/

package cn.pedant.SafeJava4WebviewJS.bridge;

import android.util.Log;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;


public class InjectedChromeClient extends WebChromeClient {
private final String TAG = "InjectedChromeClient";
private JsCallJava mJsCallJava;
private boolean mIsInjectedJS;

public InjectedChromeClient (Class injectedCls) {
mJsCallJava = new JsCallJava(injectedCls);
}

// 处理Alert事件
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
result.confirm();
return true;
}

@Override
public void onProgressChanged (WebView view, int newProgress) {
//为什么要在这里注入JS
//1 OnPageStarted中注入有可能全局注入不成功,导致页面脚本上所有接口任何时候都不可用
//2 OnPageFinished中注入,虽然最后都会全局注入成功,但是完成时间有可能太晚,当页面在初始化调用接口函数时会等待时间过长
//3 在进度变化时注入,刚好可以在上面两个问题中得到一个折中处理
//为什么是进度大于25%才进行注入,因为从测试看来只有进度大于这个数字页面才真正得到框架刷新加载,保证100%注入成功
if (newProgress <= 25) {
mIsInjectedJS = false;
} else if (!mIsInjectedJS) {
view.loadUrl(mJsCallJava.getPreloadInterfaceJS());
mIsInjectedJS = true;
Log.d(TAG, " inject js interface completely on progress " + newProgress);

}
super.onProgressChanged(view, newProgress);
}

@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
result.confirm(mJsCallJava.call(view, message));
return true;
}
}
Loading

0 comments on commit e125e10

Please sign in to comment.