Skip to content

Commit

Permalink
JsCallback关联的WebView改为软引用,同时增加其apply时的异常情况
Browse files Browse the repository at this point in the history
  • Loading branch information
pedant committed Nov 27, 2014
1 parent 004006e commit 23fed5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
27 changes: 22 additions & 5 deletions library/src/main/java/cn/pedant/SafeWebViewBridge/JsCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,33 @@

package cn.pedant.SafeWebViewBridge;

import android.webkit.WebView;
import android.util.Log;
import android.webkit.WebView;

import java.lang.ref.SoftReference;

public class JsCallback {
private static final String CALLBACK_JS_FORMAT = "javascript:%s.callback(%d, %d %s);";
private int mIndex;
private WebView mWebView;
private boolean mCouldGoOn;
private SoftReference<WebView> mWebViewRef;
private int mIsPermanent;
private String mInjectedName;

public JsCallback (WebView view, String injectedName, int index) {
mWebView = view;
mCouldGoOn = true;
mWebViewRef = new SoftReference<WebView>(view);
mInjectedName = injectedName;
mIndex = index;
}

public void apply (Object... args) {
public void apply (Object... args) throws JsCallbackException {
if (mWebViewRef.get() == null) {
throw new JsCallbackException("the WebView related to the JsCallback has been recycled");
}
if (!mCouldGoOn) {
throw new JsCallbackException("the JsCallback isn't permanent,cannot be called more than once");
}
StringBuilder sb = new StringBuilder();
for (Object arg : args){
sb.append(",");
Expand All @@ -39,10 +49,17 @@ public void apply (Object... args) {
}
String execJs = String.format(CALLBACK_JS_FORMAT, mInjectedName, mIndex, mIsPermanent, sb.toString());
Log.d("JsCallBack", execJs);
mWebView.loadUrl(execJs);
mWebViewRef.get().loadUrl(execJs);
mCouldGoOn = mIsPermanent > 0;
}

public void setPermanent (boolean value) {
mIsPermanent = value ? 1 : 0;
}

public static class JsCallbackException extends Exception {
public JsCallbackException (String msg) {
super(msg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import android.telephony.TelephonyManager;
import android.webkit.WebView;
import android.widget.Toast;
import cn.pedant.SafeWebViewBridge.JsCallback;
import cn.pedant.SafeWebViewBridge.sample.util.TaskExecutor;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -26,6 +24,9 @@
import java.util.Iterator;
import java.util.List;

import cn.pedant.SafeWebViewBridge.JsCallback;
import cn.pedant.SafeWebViewBridge.sample.util.TaskExecutor;

//HostJsScope中需要被JS调用的函数,必须定义成public static,且必须包含WebView这个参数
public class HostJsScope {
/**
Expand Down Expand Up @@ -174,7 +175,11 @@ public static void delayJsCallBack(WebView view, int ms, final String backMsg, f
TaskExecutor.scheduleTaskOnUiThread(ms * 1000, new Runnable() {
@Override
public void run() {
jsCallback.apply(backMsg);
try {
jsCallback.apply(backMsg);
} catch (JsCallback.JsCallbackException je) {
je.printStackTrace();
}
}
});
}
Expand Down

0 comments on commit 23fed5d

Please sign in to comment.