├── example
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── mipmap-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ │ └── ic_launcher.png
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── lzyzsd
│ │ │ └── jsbridge
│ │ │ └── example
│ │ │ ├── MainJavascriptInterface.java
│ │ │ ├── CustomWebView.java
│ │ │ └── MainActivity.java
│ │ └── assets
│ │ └── demo.html
├── proguard-rules.pro
└── build.gradle
├── library
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ └── values
│ │ │ └── strings.xml
│ │ ├── java
│ │ └── com
│ │ │ └── github
│ │ │ └── lzyzsd
│ │ │ └── jsbridge
│ │ │ ├── BridgeHandler.java
│ │ │ ├── OnBridgeCallback.java
│ │ │ ├── CallBackFunction.java
│ │ │ ├── JSResponse.java
│ │ │ ├── JSRequest.java
│ │ │ ├── DefaultHandler.java
│ │ │ ├── WebViewJavascriptBridge.java
│ │ │ ├── IWebView.java
│ │ │ ├── BridgeWebViewClient.java
│ │ │ ├── BridgeUtil.java
│ │ │ ├── BaseJavascriptInterface.java
│ │ │ ├── BridgeWebView.java
│ │ │ ├── WebViewClientProxy.java
│ │ │ └── BridgeHelper.java
│ │ ├── AndroidManifest.xml
│ │ └── assets
│ │ └── WebViewJavascriptBridge.js
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── JsBridge.gif
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
└── gradlew
/example/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':example', ':library'
2 |
--------------------------------------------------------------------------------
/JsBridge.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kingwang666/JsBridge/HEAD/JsBridge.gif
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kingwang666/JsBridge/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | library
3 |
4 |
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kingwang666/JsBridge/HEAD/example/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kingwang666/JsBridge/HEAD/example/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kingwang666/JsBridge/HEAD/example/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/kingwang666/JsBridge/HEAD/example/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | JsBridge
3 | Java调用Web
4 |
5 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeHandler.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | @Deprecated
4 | public interface BridgeHandler {
5 |
6 | void handler(String data, CallBackFunction function);
7 |
8 | }
9 |
--------------------------------------------------------------------------------
/example/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/OnBridgeCallback.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | /**
4 | * Created on 2020/3/23
5 | * Author: bigwang
6 | * Description:
7 | */
8 | public interface OnBridgeCallback extends CallBackFunction {
9 | }
10 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/CallBackFunction.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | /**
4 | * use {@link OnBridgeCallback}
5 | */
6 | @Deprecated
7 | public interface CallBackFunction {
8 |
9 | void onCallBack(String data);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Sep 09 14:02:10 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
7 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/JSResponse.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | /**
4 | * Created on 2019/7/10.
5 | * Author: bigwang
6 | * Description:
7 | */
8 | class JSResponse {
9 |
10 | public String responseId;
11 |
12 | public String responseData;
13 | }
14 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/JSRequest.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | /**
4 | * Created on 2019/7/10.
5 | * Author: bigwang
6 | * Description:
7 | */
8 | class JSRequest {
9 |
10 | public String callbackId;
11 |
12 | public String data;
13 |
14 | public String handlerName;
15 | }
16 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/DefaultHandler.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | @Deprecated
4 | public class DefaultHandler implements BridgeHandler{
5 |
6 | @Override
7 | public void handler(String data, CallBackFunction function) {
8 | if(function != null){
9 | function.onCallBack("DefaultHandler response data");
10 | }
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/WebViewJavascriptBridge.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 |
4 | import android.webkit.ValueCallback;
5 |
6 | public interface WebViewJavascriptBridge {
7 |
8 | void sendToWeb(Object data);
9 |
10 | void sendToWeb(Object data, CallBackFunction responseCallback);
11 |
12 | void sendToWeb(String function, Object... values);
13 |
14 | void sendToWeb(String function, ValueCallback callback, Object... values);
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/IWebView.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | import android.webkit.ValueCallback;
4 | import android.webkit.WebViewClient;
5 |
6 | import androidx.annotation.Nullable;
7 |
8 | /**
9 | * WebView功能接口.
10 | *
11 | * @author ZhengAn
12 | * @date 2019-07-01
13 | */
14 | public interface IWebView {
15 |
16 | void addJavascriptInterface(Object obj, String interfaceName);
17 |
18 | void setWebViewClient(WebViewClient client);
19 |
20 | void evaluateJavascript(String script, @Nullable ValueCallback resultCallback);
21 |
22 | void loadUrl(String url);
23 |
24 | boolean post(Runnable runnable);
25 | }
26 |
--------------------------------------------------------------------------------
/example/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/bruce/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/bruce/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeWebViewClient.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | import android.webkit.WebView;
4 | import android.webkit.WebViewClient;
5 |
6 | /**
7 | * 如果要自定义WebViewClient必须要集成此类
8 | * Created by bruce on 10/28/15.
9 | */
10 |
11 | public class BridgeWebViewClient extends WebViewClient {
12 |
13 |
14 | @Override
15 | public void onPageFinished(WebView view, String url) {
16 | super.onPageFinished(view, url);
17 | onBridgeLoadStart();
18 | BridgeUtil.webViewLoadLocalJs(view, BridgeUtil.JAVA_SCRIPT);
19 | onBridgeLoadFinished();
20 | }
21 |
22 | public void onBridgeLoadStart() {
23 |
24 | }
25 |
26 | public void onBridgeLoadFinished() {
27 |
28 | }
29 |
30 | }
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | version = "1.0.0"
4 |
5 | android {
6 | compileSdkVersion 29
7 | // buildToolsVersion "25.0.3"
8 |
9 | defaultConfig {
10 | minSdkVersion 17
11 | targetSdkVersion 29
12 | versionCode 1
13 | versionName version
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | lintOptions { // 消除lint警告
22 | abortOnError false
23 | checkReleaseBuilds false
24 | }
25 | }
26 |
27 | dependencies {
28 | implementation 'androidx.appcompat:appcompat:1.1.0'
29 | implementation 'com.google.code.gson:gson:2.8.6'
30 | }
31 |
32 |
--------------------------------------------------------------------------------
/example/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 29
5 | // buildToolsVersion "25.0.3"
6 |
7 | defaultConfig {
8 | applicationId "com.github.lzyzsd.jsbridge.example"
9 | minSdkVersion 17
10 | targetSdkVersion 29
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation project(':library')
25 | implementation 'androidx.appcompat:appcompat:1.1.0'
26 | implementation 'com.google.code.gson:gson:2.8.6'
27 | }
28 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
14 |
15 |
16 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/example/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
13 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | #built application files
2 | *.apk
3 | *.ap_
4 |
5 |
6 | # files for the dex VM
7 | *.dex
8 |
9 |
10 | # Java class files
11 | *.class
12 |
13 |
14 | # generated files
15 | bin/
16 | gen/
17 |
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 |
23 | # Windows thumbnail db
24 | Thumbs.db
25 |
26 |
27 | # OSX files
28 | .DS_Store
29 |
30 |
31 | # Eclipse project files
32 | .classpath
33 | .project
34 |
35 |
36 | # Android Studio
37 | .idea
38 | #.idea/workspace.xml - remove # and delete .idea if it better suit your needs.
39 | .gradle
40 | build/
41 |
42 |
43 | # Signing files
44 | .signing/
45 |
46 |
47 | # User-specific configurations
48 | .idea/libraries/
49 | .idea/workspace.xml
50 | .idea/tasks.xml
51 | .idea/.name
52 | .idea/compiler.xml
53 | .idea/copyright/profiles_settings.xml
54 | .idea/encodings.xml
55 | .idea/misc.xml
56 | .idea/modules.xml
57 | .idea/scopes/scope_settings.xml
58 | .idea/vcs.xml
59 | *.iml
60 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | ## Project-wide Gradle settings.
2 | #
3 | # For more details on how to configure your build environment visit
4 | # http://www.gradle.org/docs/current/userguide/build_environment.html
5 | #
6 | # Specifies the JVM arguments used for the daemon process.
7 | # The setting is particularly useful for tweaking memory settings.
8 | # Default value: -Xmx1024m -XX:MaxPermSize=256m
9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
10 | #
11 | # When configured, Gradle will run in incubating parallel mode.
12 | # This option should only be used with decoupled projects. More details, visit
13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
14 | # org.gradle.parallel=true
15 | #Thu Jul 27 13:52:48 CST 2017
16 | #systemProp.http.proxyHost=127.0.0.1
17 | #systemProp.https.proxyPort=10809
18 | #systemProp.https.proxyHost=127.0.0.1
19 | #systemProp.http.proxyPort=10809
20 | android.enableJetifier=true
21 | android.useAndroidX=true
--------------------------------------------------------------------------------
/example/src/main/java/com/github/lzyzsd/jsbridge/example/MainJavascriptInterface.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge.example;
2 |
3 | import android.util.Log;
4 | import android.webkit.JavascriptInterface;
5 |
6 | import androidx.annotation.NonNull;
7 |
8 | import com.github.lzyzsd.jsbridge.BaseJavascriptInterface;
9 | import com.github.lzyzsd.jsbridge.BridgeHelper;
10 | import com.github.lzyzsd.jsbridge.BridgeWebView;
11 | import com.github.lzyzsd.jsbridge.CallBackFunction;
12 |
13 | import java.util.Map;
14 |
15 | /**
16 | * Created on 2019/7/10.
17 | * Author: bigwang
18 | * Description:
19 | */
20 | public class MainJavascriptInterface extends BaseJavascriptInterface {
21 |
22 |
23 | public MainJavascriptInterface(@NonNull BridgeWebView webView) {
24 | super(webView);
25 | }
26 |
27 | public MainJavascriptInterface(BridgeHelper helper) {
28 | super(helper);
29 | }
30 |
31 |
32 | public String send(String data) {
33 | return "it is default response";
34 | }
35 |
36 |
37 | @JavascriptInterface
38 | public String submitFromWeb(String data, String callbackId) {
39 | Log.d("chromium data", data + ", callbackId: " + callbackId + " " + Thread.currentThread().getName());
40 | // mWebView.sendResponse("submitFromWeb response", callbackId);
41 | return "submitFromWeb response";
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeUtil.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | import android.content.Context;
4 | import android.webkit.WebView;
5 |
6 | import java.io.BufferedReader;
7 | import java.io.IOException;
8 | import java.io.InputStream;
9 | import java.io.InputStreamReader;
10 |
11 | class BridgeUtil {
12 |
13 | public static final String JAVA_SCRIPT = "WebViewJavascriptBridge.js";
14 | public final static String UNDERLINE_STR = "_";
15 | public final static String CALLBACK_ID_FORMAT = "JAVA_CB_%s";
16 | public final static String JS_HANDLE_MESSAGE_FROM_JAVA = "javascript:WebViewJavascriptBridge._handleMessageFromNative('%s');";
17 | public final static String JAVASCRIPT_STR = "javascript:%s";
18 |
19 | /**
20 | * js 文件将注入为第一个script引用
21 | * @param view WebView
22 | * @param url url
23 | */
24 | public static void webViewLoadJs(WebView view, String url){
25 | String js = "var newscript = document.createElement(\"script\");";
26 | js += "newscript.src=\"" + url + "\";";
27 | js += "document.scripts[0].parentNode.insertBefore(newscript,document.scripts[0]);";
28 | view.loadUrl("javascript:" + js);
29 | }
30 |
31 | /**
32 | * 这里只是加载lib包中assets中的 WebViewJavascriptBridge.js
33 | * @param view webview
34 | * @param path 路径
35 | */
36 | public static void webViewLoadLocalJs(WebView view, String path){
37 | String jsContent = assetFile2Str(view.getContext(), path);
38 | view.loadUrl("javascript:" + jsContent);
39 | }
40 |
41 | /**
42 | * 解析assets文件夹里面的代码,去除注释,取可执行的代码
43 | * @param c context
44 | * @param urlStr 路径
45 | * @return 可执行代码
46 | */
47 | public static String assetFile2Str(Context c, String urlStr){
48 | InputStream in = null;
49 | try{
50 | in = c.getAssets().open(urlStr);
51 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
52 | String line = null;
53 | StringBuilder sb = new StringBuilder();
54 | do {
55 | line = bufferedReader.readLine();
56 | if (line != null && !line.matches("^\\s*\\/\\/.*")) { // 去除注释
57 | sb.append(line);
58 | }
59 | } while (line != null);
60 |
61 | bufferedReader.close();
62 | in.close();
63 |
64 | return sb.toString();
65 | } catch (Exception e) {
66 | e.printStackTrace();
67 | } finally {
68 | if(in != null) {
69 | try {
70 | in.close();
71 | } catch (IOException e) {
72 | e.printStackTrace();
73 | }
74 | }
75 | }
76 | return null;
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/BaseJavascriptInterface.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | import android.text.TextUtils;
4 | import android.util.Log;
5 | import android.webkit.JavascriptInterface;
6 |
7 | import androidx.annotation.NonNull;
8 |
9 | import java.util.Map;
10 |
11 | /**
12 | * Created on 2020/3/23
13 | * Author: bigwang
14 | * Description:
15 | */
16 | public class BaseJavascriptInterface {
17 |
18 | private Map mCallbacks;
19 | private Map mMessageHandlers;
20 | private BridgeHelper mBridgeHelper;
21 |
22 | public BaseJavascriptInterface(@NonNull BridgeHelper helper) {
23 | mCallbacks = helper.getResponseCallbacks();
24 | mMessageHandlers = helper.getMessageHandlers();
25 | mBridgeHelper = helper;
26 | }
27 |
28 | public BaseJavascriptInterface(@NonNull BridgeWebView webView){
29 | this(webView.getBridgeHelper());
30 | }
31 |
32 |
33 | @JavascriptInterface
34 | public final String send(String data, String callbackId) {
35 | Log.d("chromium", data + ", callbackId: " + callbackId + " " + Thread.currentThread().getName());
36 | return receiveMessage(data, callbackId);
37 | }
38 |
39 | @JavascriptInterface
40 | @Deprecated
41 | public final void send(String handlerName, String data, final String callbackId) {
42 | Log.d("chromium", "handlerName: " + handlerName + " " + data + ", callbackId: " + callbackId + " " + Thread.currentThread().getName());
43 | BridgeHandler handler = mMessageHandlers.get(handlerName);
44 | final BridgeHelper webView = mBridgeHelper;
45 | if (handler == null && webView != null) {
46 | handler = webView.getDefaultHandler();
47 | }
48 | if (handler != null) {
49 | handler.handler(data, new CallBackFunction() {
50 | @Override
51 | public void onCallBack(String data) {
52 | if (webView != null) {
53 | webView.sendResponse(data, callbackId);
54 | }
55 | }
56 | });
57 | }
58 | }
59 |
60 | @JavascriptInterface
61 | public final void response(String data, String responseId) {
62 | Log.d("chromium", data + ", responseId: " + responseId + " " + Thread.currentThread().getName());
63 | responseFormWeb(data, responseId);
64 | }
65 |
66 | protected String receiveMessage(String data, final String callbackId) {
67 | BridgeHandler handler = null;
68 | final BridgeHelper webView = mBridgeHelper;
69 | if (webView != null) {
70 | handler = webView.getDefaultHandler();
71 | }
72 | if (handler != null) {
73 | handler.handler(data, new CallBackFunction() {
74 | @Override
75 | public void onCallBack(String data) {
76 | webView.sendResponse(data, callbackId);
77 | }
78 | });
79 | }
80 | return null;
81 | }
82 |
83 | protected void responseFormWeb(String data, String responseId) {
84 | if (!TextUtils.isEmpty(responseId)) {
85 | CallBackFunction function = mCallbacks.remove(responseId);
86 | if (function != null) {
87 | function.onCallBack(data);
88 | }
89 | }
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/example/src/main/java/com/github/lzyzsd/jsbridge/example/CustomWebView.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge.example;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.Context;
5 | import android.os.Build;
6 | import android.util.AttributeSet;
7 | import android.webkit.ValueCallback;
8 | import android.webkit.WebView;
9 |
10 | import androidx.annotation.RequiresApi;
11 |
12 | import com.github.lzyzsd.jsbridge.BridgeHandler;
13 | import com.github.lzyzsd.jsbridge.BridgeHelper;
14 | import com.github.lzyzsd.jsbridge.CallBackFunction;
15 | import com.github.lzyzsd.jsbridge.IWebView;
16 | import com.github.lzyzsd.jsbridge.WebViewJavascriptBridge;
17 |
18 | /**
19 | * 采用BridgeHelper集成JsBridge功能示例.定制WebView,可只添加实际需要的JsBridge接口.
20 | *
21 | * @author ZhengAn
22 | * @date 2019-07-07
23 | */
24 | @SuppressLint("SetJavaScriptEnabled")
25 | public class CustomWebView extends WebView implements WebViewJavascriptBridge, IWebView {
26 |
27 | private BridgeHelper bridgeHelper;
28 |
29 | public CustomWebView(Context context, AttributeSet attrs) {
30 | super(context, attrs);
31 | init();
32 | }
33 |
34 | public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
35 | super(context, attrs, defStyle);
36 | init();
37 | }
38 |
39 | public CustomWebView(Context context) {
40 | super(context);
41 | init();
42 | }
43 |
44 | private void init() {
45 | this.setVerticalScrollBarEnabled(false);
46 | this.setHorizontalScrollBarEnabled(false);
47 | this.getSettings().setJavaScriptEnabled(true);
48 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
49 | WebView.setWebContentsDebuggingEnabled(true);
50 | }
51 |
52 | bridgeHelper = new BridgeHelper(this);
53 | }
54 |
55 | /**
56 | * @param handler default handler,handle messages send by js without assigned handler name,
57 | * if js message has handler name, it will be handled by named handlers registered by native
58 | */
59 | public void setDefaultHandler(BridgeHandler handler) {
60 | bridgeHelper.setDefaultHandler(handler);
61 | }
62 |
63 | /**
64 | * register handler,so that javascript can call it
65 | * 注册处理程序,以便javascript调用它
66 | *
67 | * @param handlerName handlerName
68 | * @param handler BridgeHandler
69 | */
70 | public void registerHandler(String handlerName, BridgeHandler handler) {
71 | bridgeHelper.registerHandler(handlerName, handler);
72 | }
73 |
74 | /**
75 | * unregister handler
76 | *
77 | * @param handlerName
78 | */
79 | public void unregisterHandler(String handlerName) {
80 | bridgeHelper.unregisterHandler(handlerName);
81 | }
82 |
83 | /**
84 | * call javascript registered handler
85 | * 调用javascript处理程序注册
86 | *
87 | * @param handlerName handlerName
88 | * @param data data
89 | * @param callBack CallBackFunction
90 | */
91 | public void callHandler(String handlerName, String data, CallBackFunction callBack) {
92 | bridgeHelper.callHandler(handlerName, data, callBack);
93 | }
94 |
95 | @Override
96 | public void sendToWeb(Object data) {
97 | bridgeHelper.sendToWeb(data);
98 | }
99 |
100 | @Override
101 | public void sendToWeb(Object data, CallBackFunction responseCallback) {
102 | bridgeHelper.sendToWeb(data, responseCallback);
103 | }
104 |
105 | @Override
106 | public void sendToWeb(String function, Object... values) {
107 | bridgeHelper.sendToWeb(function, values);
108 | }
109 |
110 | @Override
111 | @RequiresApi(api = Build.VERSION_CODES.KITKAT)
112 | public void sendToWeb(String function, ValueCallback callback, Object... values) {
113 | bridgeHelper.sendToWeb(function, callback, values);
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #JsBridge
2 |
3 | -----
4 |
5 | inspired and modified from [this](https://github.com/jacin1/JsBridge) and wechat jsBridge file, with some bugs fix and feature enhancement.
6 |
7 | This project make a bridge between Java and JavaScript.
8 |
9 | It provides safe and convenient way to call Java code from js and call js code from java.
10 |
11 | ## Demo
12 | 
13 |
14 | ## Usage
15 |
16 | ## JitPack.io
17 |
18 | I strongly recommend https://jitpack.io
19 |
20 | ```groovy
21 | repositories {
22 | // ...
23 | maven { url "https://jitpack.io" }
24 | }
25 |
26 | dependencies {
27 | compile 'com.github.lzyzsd:jsbridge:1.0.4'
28 | }
29 | ```
30 |
31 | ## Use it in Java
32 |
33 | add com.github.lzyzsd.jsbridge.BridgeWebView to your layout, it is inherited from WebView.
34 |
35 | ### Register a Java handler function so that js can call
36 |
37 | ```java
38 |
39 | webView.registerHandler("submitFromWeb", new BridgeHandler() {
40 | @Override
41 | public void handler(String data, CallBackFunction function) {
42 | Log.i(TAG, "handler = submitFromWeb, data from web = " + data);
43 | function.onCallBack("submitFromWeb exe, response data from Java");
44 | }
45 | });
46 |
47 | ```
48 |
49 | js can call this Java handler method "submitFromWeb" through:
50 |
51 | ```javascript
52 |
53 | WebViewJavascriptBridge.callHandler(
54 | 'submitFromWeb'
55 | , {'param': str1}
56 | , function(responseData) {
57 | document.getElementById("show").innerHTML = "send get responseData from java, data = " + responseData
58 | }
59 | );
60 |
61 | ```
62 |
63 | You can set a default handler in Java, so that js can send message to Java without assigned handlerName
64 |
65 | ```java
66 |
67 | webView.setDefaultHandler(new DefaultHandler());
68 |
69 | ```
70 |
71 | ```javascript
72 |
73 | window.WebViewJavascriptBridge.send(
74 | data
75 | , function(responseData) {
76 | document.getElementById("show").innerHTML = "repsonseData from java, data = " + responseData
77 | }
78 | );
79 |
80 | ```
81 |
82 | ### Register a JavaScript handler function so that Java can call
83 |
84 | ```javascript
85 |
86 | WebViewJavascriptBridge.registerHandler("functionInJs", function(data, responseCallback) {
87 | document.getElementById("show").innerHTML = ("data from Java: = " + data);
88 | var responseData = "Javascript Says Right back aka!";
89 | responseCallback(responseData);
90 | });
91 |
92 | ```
93 |
94 | Java can call this js handler function "functionInJs" through:
95 |
96 | ```java
97 |
98 | webView.callHandler("functionInJs", new Gson().toJson(user), new CallBackFunction() {
99 | @Override
100 | public void onCallBack(String data) {
101 |
102 | }
103 | });
104 |
105 | ```
106 | You can also define a default handler use init method, so that Java can send message to js without assigned handlerName
107 |
108 | for example:
109 |
110 | ```javascript
111 |
112 | bridge.init(function(message, responseCallback) {
113 | console.log('JS got a message', message);
114 | var data = {
115 | 'Javascript Responds': 'Wee!'
116 | };
117 | console.log('JS responding with', data);
118 | responseCallback(data);
119 | });
120 |
121 | ```
122 |
123 | ```java
124 | webView.send("hello");
125 | ```
126 |
127 | will print 'JS got a message hello' and 'JS responding with' in webview console.
128 |
129 | ## Notice
130 |
131 | This lib will inject a WebViewJavascriptBridge Object to window object.
132 | So in your js, before use WebViewJavascriptBridge, you must detect if WebViewJavascriptBridge exist.
133 | If WebViewJavascriptBridge does not exit, you can listen to WebViewJavascriptBridgeReady event, as the blow code shows:
134 |
135 | ```javascript
136 |
137 | if (window.WebViewJavascriptBridge) {
138 | //do your work here
139 | } else {
140 | document.addEventListener(
141 | 'WebViewJavascriptBridgeReady'
142 | , function() {
143 | //do your work here
144 | },
145 | false
146 | );
147 | }
148 |
149 | ```
150 |
151 | ## License
152 |
153 | This project is licensed under the terms of the MIT license.
154 |
--------------------------------------------------------------------------------
/example/src/main/assets/demo.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | js调用java
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
131 |
132 |
133 |
134 |
--------------------------------------------------------------------------------
/example/src/main/java/com/github/lzyzsd/jsbridge/example/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge.example;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.net.Uri;
6 | import android.os.Bundle;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.view.View.OnClickListener;
10 | import android.webkit.ValueCallback;
11 | import android.webkit.WebChromeClient;
12 | import android.webkit.WebView;
13 | import android.widget.Button;
14 |
15 | import com.github.lzyzsd.jsbridge.BridgeHandler;
16 | import com.github.lzyzsd.jsbridge.BridgeWebView;
17 | import com.github.lzyzsd.jsbridge.CallBackFunction;
18 | import com.github.lzyzsd.jsbridge.OnBridgeCallback;
19 | import com.google.gson.Gson;
20 |
21 | public class MainActivity extends Activity implements OnClickListener {
22 |
23 | private final String TAG = "MainActivity";
24 |
25 | BridgeWebView webView;
26 |
27 | Button button;
28 |
29 | int RESULT_CODE = 0;
30 |
31 | ValueCallback mUploadMessage;
32 |
33 | ValueCallback mUploadMessageArray;
34 |
35 | static class Location {
36 | String address;
37 | }
38 |
39 | static class User {
40 | String name;
41 | Location location;
42 | String testStr;
43 | }
44 |
45 | @Override
46 | protected void onCreate(Bundle savedInstanceState) {
47 | super.onCreate(savedInstanceState);
48 | setContentView(R.layout.activity_main);
49 |
50 | webView = (BridgeWebView) findViewById(R.id.webView);
51 |
52 | button = (Button) findViewById(R.id.button);
53 |
54 | button.setOnClickListener(this);
55 |
56 |
57 | webView.setWebChromeClient(new WebChromeClient() {
58 |
59 | @SuppressWarnings("unused")
60 | public void openFileChooser(ValueCallback uploadMsg, String AcceptType, String capture) {
61 | this.openFileChooser(uploadMsg);
62 | }
63 |
64 | @SuppressWarnings("unused")
65 | public void openFileChooser(ValueCallback uploadMsg, String AcceptType) {
66 | this.openFileChooser(uploadMsg);
67 | }
68 |
69 | public void openFileChooser(ValueCallback uploadMsg) {
70 | mUploadMessage = uploadMsg;
71 | pickFile();
72 | }
73 |
74 | @Override
75 | public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
76 | mUploadMessageArray = filePathCallback;
77 | pickFile();
78 | return true;
79 | }
80 | });
81 |
82 | webView.addJavascriptInterface(new MainJavascriptInterface(webView), "android");
83 | webView.setGson(new Gson());
84 | webView.loadUrl("file:///android_asset/demo.html");
85 |
86 | User user = new User();
87 | Location location = new Location();
88 | location.address = "SDU";
89 | user.location = location;
90 | user.name = "大头鬼";
91 |
92 | webView.registerHandler("submitFromWeb2", new BridgeHandler() {
93 | @Override
94 | public void handler(String data, CallBackFunction function) {
95 | Log.d(TAG, "submitFromWeb2, data" + data);
96 | function.onCallBack("java submitFromWeb2 back to you");
97 | }
98 | });
99 |
100 | webView.callHandler("functionInJs", new Gson().toJson(user), new CallBackFunction() {
101 | @Override
102 | public void onCallBack(String data) {
103 | Log.d(TAG, "onCallBack: " + data);
104 | }
105 | });
106 |
107 | webView.sendToWeb("hello");
108 |
109 | }
110 |
111 | public void pickFile() {
112 | Intent chooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
113 | chooserIntent.setType("image/*");
114 | startActivityForResult(chooserIntent, RESULT_CODE);
115 | }
116 |
117 | @Override
118 | protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
119 | if (requestCode == RESULT_CODE) {
120 | if (null == mUploadMessage && null == mUploadMessageArray){
121 | return;
122 | }
123 | if(null!= mUploadMessage && null == mUploadMessageArray){
124 | Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
125 | mUploadMessage.onReceiveValue(result);
126 | mUploadMessage = null;
127 | }
128 |
129 | if(null == mUploadMessage && null != mUploadMessageArray){
130 | Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
131 | if (result != null) {
132 | mUploadMessageArray.onReceiveValue(new Uri[]{result});
133 | }
134 | mUploadMessageArray = null;
135 | }
136 |
137 | }
138 | }
139 |
140 | @Override
141 | public void onClick(View v) {
142 | if (button.equals(v)) {
143 | webView.callHandler("functionInJs", "data from Java", new OnBridgeCallback() {
144 |
145 | @Override
146 | public void onCallBack(String data) {
147 | // TODO Auto-generated method stub
148 | Log.i(TAG, "reponse data from js " + data);
149 | }
150 |
151 | });
152 | }
153 |
154 | }
155 |
156 | }
157 |
--------------------------------------------------------------------------------
/library/src/main/assets/WebViewJavascriptBridge.js:
--------------------------------------------------------------------------------
1 | //notation: js file can only use this kind of comments
2 | //since comments will cause error when use in webview.loadurl,
3 | //comments will be remove by java use regexp
4 | (function() {
5 | if (window.WebViewJavascriptBridge) {
6 | return;
7 | }
8 |
9 | var receiveMessageQueue = [];
10 | var messageHandlers = {};
11 |
12 | var responseCallbacks = {};
13 | var uniqueId = 1;
14 |
15 | //set default messageHandler 初始化默认的消息线程
16 | function init(messageHandler) {
17 | if (WebViewJavascriptBridge._messageHandler) {
18 | throw new Error('WebViewJavascriptBridge.init called twice');
19 | }
20 | WebViewJavascriptBridge._messageHandler = messageHandler;
21 | var receivedMessages = receiveMessageQueue;
22 | receiveMessageQueue = null;
23 | for (var i = 0; i < receivedMessages.length; i++) {
24 | _dispatchMessageFromNative(receivedMessages[i]);
25 | }
26 | }
27 |
28 | // 发送
29 | function send(data, responseCallback) {
30 | _doSend('send', data, responseCallback);
31 | }
32 |
33 | // 注册线程 往数组里面添加值
34 | function registerHandler(handlerName, handler) {
35 | messageHandlers[handlerName] = handler;
36 | }
37 | // 调用线程
38 | function callHandler(handlerName, data, responseCallback) {
39 |
40 | _doSend(handlerName, data, responseCallback);
41 | }
42 |
43 | //sendMessage add message, 触发native处理 sendMessage
44 | function _doSend(handlerName, message, responseCallback) {
45 | var callbackId;
46 | if(typeof responseCallback === 'string'){
47 | callbackId = responseCallback;
48 | } else if (responseCallback) {
49 | callbackId = 'cb_' + (uniqueId++) + '_' + new Date().getTime();
50 | responseCallbacks[callbackId] = responseCallback;
51 | }else{
52 | callbackId = '';
53 | }
54 | try {
55 | var fn = eval('window.android.' + handlerName);
56 | } catch(e) {
57 | console.log(e);
58 | }
59 | if (typeof fn === 'function'){
60 | var responseData = fn.call(this, JSON.stringify(message), callbackId);
61 | if(responseData != null){
62 | console.log('response message: '+ responseData);
63 | responseCallback = responseCallbacks[callbackId];
64 | if (!responseCallback) {
65 | return;
66 | }
67 | responseCallback(responseData);
68 | delete responseCallbacks[callbackId];
69 | }
70 | }else{
71 | window.android.send(handlerName, JSON.stringify(message), callbackId)
72 | }
73 | }
74 |
75 | //提供给native使用,
76 | function _dispatchMessageFromNative(messageJSON) {
77 | setTimeout(function() {
78 | var message = JSON.parse(messageJSON);
79 | var responseCallback;
80 | //java call finished, now need to call js callback function
81 | if (message.responseId) {
82 | responseCallback = responseCallbacks[message.responseId];
83 | if (!responseCallback) {
84 | return;
85 | }
86 | responseCallback(message.responseData);
87 | delete responseCallbacks[message.responseId];
88 | } else {
89 | //直接发送
90 | if (message.callbackId) {
91 | var callbackResponseId = message.callbackId;
92 | responseCallback = function(responseData) {
93 | _doSend('response', responseData, callbackResponseId);
94 | };
95 | }
96 |
97 | var handler = WebViewJavascriptBridge._messageHandler;
98 | if (message.handlerName) {
99 | handler = messageHandlers[message.handlerName];
100 | }
101 | //查找指定handler
102 | try {
103 | handler(message.data, responseCallback);
104 | } catch (exception) {
105 | if (typeof console != 'undefined') {
106 | console.log("WebViewJavascriptBridge: WARNING: javascript handler threw.", message, exception);
107 | }
108 | }
109 | }
110 | });
111 | }
112 |
113 | //提供给native调用,receiveMessageQueue 在会在页面加载完后赋值为null,所以
114 | function _handleMessageFromNative(messageJSON) {
115 | console.log('handle message: '+ messageJSON);
116 | if (receiveMessageQueue) {
117 | receiveMessageQueue.push(messageJSON);
118 | }
119 | _dispatchMessageFromNative(messageJSON);
120 |
121 | }
122 |
123 | var WebViewJavascriptBridge = window.WebViewJavascriptBridge = {
124 | init: init,
125 | send: send,
126 | registerHandler: registerHandler,
127 | callHandler: callHandler,
128 | _handleMessageFromNative: _handleMessageFromNative
129 | };
130 |
131 | var doc = document;
132 | var readyEvent = doc.createEvent('Events');
133 | readyEvent.initEvent('WebViewJavascriptBridgeReady');
134 | readyEvent.bridge = WebViewJavascriptBridge;
135 | doc.dispatchEvent(readyEvent);
136 | })();
137 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeWebView.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.content.Context;
5 | import android.os.Build;
6 | import android.os.Looper;
7 | import android.os.SystemClock;
8 | import android.text.TextUtils;
9 | import android.util.AttributeSet;
10 | import android.util.Log;
11 | import android.webkit.ValueCallback;
12 | import android.webkit.WebSettings;
13 | import android.webkit.WebView;
14 | import android.webkit.WebViewClient;
15 |
16 | import androidx.annotation.NonNull;
17 | import androidx.annotation.RequiresApi;
18 |
19 | import com.github.lzyzsd.library.BuildConfig;
20 | import com.google.gson.Gson;
21 |
22 | import java.net.URLEncoder;
23 |
24 | @SuppressLint("SetJavaScriptEnabled")
25 | public class BridgeWebView extends WebView implements WebViewJavascriptBridge, IWebView {
26 |
27 | private BridgeHelper mBridgeHelper;
28 |
29 | public BridgeWebView(Context context, AttributeSet attrs) {
30 | super(context, attrs);
31 | init();
32 | }
33 |
34 | public BridgeWebView(Context context, AttributeSet attrs, int defStyle) {
35 | super(context, attrs, defStyle);
36 | init();
37 | }
38 |
39 | public BridgeWebView(Context context) {
40 | super(context);
41 | init();
42 | }
43 |
44 | private void init() {
45 |
46 | clearCache(true);
47 | WebSettings webSettings = getSettings();
48 | webSettings.setUseWideViewPort(true);
49 | // webView.getSettings().setLoadWithOverviewMode(true);
50 | webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
51 | webSettings.setJavaScriptEnabled(true);
52 |
53 | // mContent.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
54 | webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
55 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && BuildConfig.DEBUG) {
56 | WebView.setWebContentsDebuggingEnabled(true);
57 | }
58 |
59 | mBridgeHelper = new BridgeHelper(this);
60 | }
61 |
62 | @NonNull
63 | final BridgeHelper getBridgeHelper() {
64 | return mBridgeHelper;
65 | }
66 |
67 | public void setGson(Gson gson) {
68 | mBridgeHelper.setGson(gson);
69 | }
70 |
71 | public boolean isJSLoaded() {
72 | return mBridgeHelper.isJSLoaded();
73 | }
74 |
75 | @Override
76 | public void setWebViewClient(WebViewClient client) {
77 | if (client instanceof BridgeWebViewClient){
78 | super.setWebViewClient(client);
79 | }else {
80 | Log.w("BridgeWebView", "this client is deprecated, you should use BridgeWebViewClient");
81 | super.setWebViewClient(new WebViewClientProxy(mBridgeHelper, client));
82 | }
83 | }
84 |
85 |
86 | @Override
87 | public void sendToWeb(Object data) {
88 | mBridgeHelper.sendToWeb(data);
89 | }
90 |
91 | @Override
92 | public void sendToWeb(Object data, CallBackFunction responseCallback) {
93 | mBridgeHelper.sendToWeb(data, responseCallback);
94 | }
95 |
96 | /**
97 | * call javascript registered handler
98 | * 调用javascript处理程序注册
99 | *
100 | * @param handlerName handlerName
101 | * @param data data
102 | * @param callBack OnBridgeCallback
103 | */
104 | public void callHandler(String handlerName, String data, CallBackFunction callBack) {
105 | mBridgeHelper.callHandler(handlerName, data, callBack);
106 | }
107 |
108 | /**
109 | * @param handler default handler,handle messages send by js without assigned handler name,
110 | * if js message has handler name, it will be handled by named handlers registered by native
111 | */
112 | @Deprecated
113 | public void setDefaultHandler(BridgeHandler handler) {
114 | mBridgeHelper.setDefaultHandler(handler);
115 | }
116 |
117 | /**
118 | * register handler,so that javascript can call it
119 | * 注册处理程序,以便javascript调用它
120 | *
121 | * @param handlerName handlerName
122 | * @param handler BridgeHandler
123 | */
124 | @Deprecated
125 | public void registerHandler(String handlerName, BridgeHandler handler) {
126 | mBridgeHelper.registerHandler(handlerName, handler);
127 | }
128 |
129 | /**
130 | * unregister handler
131 | *
132 | * @param handlerName
133 | */
134 | @Deprecated
135 | public void unregisterHandler(String handlerName) {
136 | mBridgeHelper.unregisterHandler(handlerName);
137 | }
138 |
139 | /**
140 | * Experimental. Use with caution
141 | *
142 | * @param function method name
143 | * @param values method parameters
144 | */
145 | @Override
146 | public void sendToWeb(String function, Object... values) {
147 | mBridgeHelper.sendToWeb(function, values);
148 | }
149 |
150 |
151 | /**
152 | * Experimental. Use with caution
153 | *
154 | * @param function method name
155 | * @param callback value callback
156 | * @param values method parameters
157 | */
158 | @Override
159 | @RequiresApi(api = Build.VERSION_CODES.KITKAT)
160 | public void sendToWeb(String function, ValueCallback callback, Object... values) {
161 | mBridgeHelper.sendToWeb(function, callback, values);
162 | }
163 |
164 |
165 | @Override
166 | public void destroy() {
167 | super.destroy();
168 | mBridgeHelper.destroy();
169 | }
170 |
171 |
172 |
173 |
174 | }
175 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/WebViewClientProxy.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | import android.graphics.Bitmap;
4 | import android.net.http.SslError;
5 | import android.os.Build;
6 | import android.view.KeyEvent;
7 | import android.webkit.ClientCertRequest;
8 | import android.webkit.HttpAuthHandler;
9 | import android.webkit.RenderProcessGoneDetail;
10 | import android.webkit.SafeBrowsingResponse;
11 | import android.webkit.SslErrorHandler;
12 | import android.webkit.WebResourceError;
13 | import android.webkit.WebResourceRequest;
14 | import android.webkit.WebResourceResponse;
15 | import android.webkit.WebView;
16 | import android.webkit.WebViewClient;
17 |
18 | import androidx.annotation.Nullable;
19 |
20 | /**
21 | * Created on 2020/3/23
22 | * Author: bigwang
23 | * Description:
24 | */
25 | @Deprecated
26 | class WebViewClientProxy extends WebViewClient {
27 |
28 | private final OnLoadJSListener mListener;
29 |
30 | private final WebViewClient mClient;
31 |
32 | public WebViewClientProxy(OnLoadJSListener listener, WebViewClient client) {
33 | mListener = listener;
34 | mClient = client;
35 | }
36 |
37 |
38 | @Override
39 | public boolean shouldOverrideUrlLoading(WebView view, String url) {
40 |
41 | if (mClient != null) {
42 | return mClient.shouldOverrideUrlLoading(view, url);
43 | }
44 | return super.shouldOverrideUrlLoading(view, url);
45 | }
46 |
47 | @Override
48 | public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
49 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && mClient != null) {
50 | return mClient.shouldOverrideUrlLoading(view, request);
51 | }
52 | return super.shouldOverrideUrlLoading(view, request);
53 | }
54 |
55 | @Override
56 | public void onPageStarted(WebView view, String url, Bitmap favicon) {
57 | if (mClient != null) {
58 | mClient.onPageStarted(view, url, favicon);
59 | } else {
60 | super.onPageStarted(view, url, favicon);
61 | }
62 |
63 | }
64 |
65 | @Override
66 | public void onPageFinished(WebView view, String url) {
67 | if (mClient != null) {
68 | mClient.onPageFinished(view, url);
69 | } else {
70 | super.onPageFinished(view, url);
71 | }
72 | mListener.onLoadStart();
73 | BridgeUtil.webViewLoadLocalJs(view, BridgeUtil.JAVA_SCRIPT);
74 | mListener.onLoadFinished();
75 | }
76 |
77 | @Override
78 | public void onLoadResource(WebView view, String url) {
79 | if (mClient != null) {
80 | mClient.onLoadResource(view, url);
81 | } else {
82 | super.onLoadResource(view, url);
83 | }
84 | }
85 |
86 | @Override
87 | public void onPageCommitVisible(WebView view, String url) {
88 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mClient != null) {
89 | mClient.onPageCommitVisible(view, url);
90 | } else {
91 | super.onPageCommitVisible(view, url);
92 | }
93 | }
94 |
95 | @Nullable
96 | @Override
97 | public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
98 | if (mClient != null) {
99 | return mClient.shouldInterceptRequest(view, url);
100 | }
101 | return super.shouldInterceptRequest(view, url);
102 | }
103 |
104 | @Nullable
105 | @Override
106 | public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
107 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mClient != null) {
108 | return mClient.shouldInterceptRequest(view, request);
109 | }
110 | return super.shouldInterceptRequest(view, request);
111 | }
112 |
113 | @Override
114 | public void onTooManyRedirects(WebView view, android.os.Message cancelMsg, android.os.Message continueMsg) {
115 | if (mClient != null) {
116 | mClient.onTooManyRedirects(view, cancelMsg, continueMsg);
117 | } else {
118 | super.onTooManyRedirects(view, cancelMsg, continueMsg);
119 | }
120 | }
121 |
122 | @Override
123 | public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
124 | if (mClient != null) {
125 | mClient.onReceivedError(view, errorCode, description, failingUrl);
126 | } else {
127 | super.onReceivedError(view, errorCode, description, failingUrl);
128 | }
129 | }
130 |
131 | @Override
132 | public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
133 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mClient != null) {
134 | mClient.onReceivedError(view, request, error);
135 | } else {
136 | super.onReceivedError(view, request, error);
137 | }
138 | }
139 |
140 | @Override
141 | public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
142 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && mClient != null) {
143 | mClient.onReceivedHttpError(view, request, errorResponse);
144 | } else {
145 | super.onReceivedHttpError(view, request, errorResponse);
146 | }
147 |
148 | }
149 |
150 | @Override
151 | public void onFormResubmission(WebView view, android.os.Message dontResend, android.os.Message resend) {
152 | if (mClient != null) {
153 | mClient.onFormResubmission(view, dontResend, resend);
154 | } else {
155 | super.onFormResubmission(view, dontResend, resend);
156 | }
157 |
158 | }
159 |
160 | @Override
161 | public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
162 | if (mClient != null) {
163 | mClient.doUpdateVisitedHistory(view, url, isReload);
164 | } else {
165 | super.doUpdateVisitedHistory(view, url, isReload);
166 | }
167 | }
168 |
169 | @Override
170 | public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
171 | if (mClient != null) {
172 | mClient.onReceivedSslError(view, handler, error);
173 | } else {
174 | super.onReceivedSslError(view, handler, error);
175 | }
176 | }
177 |
178 | @Override
179 | public void onReceivedClientCertRequest(WebView view, ClientCertRequest request) {
180 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mClient != null) {
181 | mClient.onReceivedClientCertRequest(view, request);
182 | } else {
183 | super.onReceivedClientCertRequest(view, request);
184 | }
185 | }
186 |
187 | @Override
188 | public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
189 | if (mClient != null) {
190 | mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
191 | } else {
192 | super.onReceivedHttpAuthRequest(view, handler, host, realm);
193 | }
194 | }
195 |
196 | @Override
197 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
198 | if (mClient != null) {
199 | return mClient.shouldOverrideKeyEvent(view, event);
200 | }
201 | return super.shouldOverrideKeyEvent(view, event);
202 | }
203 |
204 | @Override
205 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
206 | if (mClient != null) {
207 | mClient.onUnhandledKeyEvent(view, event);
208 | } else {
209 | super.onUnhandledKeyEvent(view, event);
210 | }
211 | }
212 |
213 | @Override
214 | public void onScaleChanged(WebView view, float oldScale, float newScale) {
215 | if (mClient != null) {
216 | mClient.onScaleChanged(view, oldScale, newScale);
217 | } else {
218 | super.onScaleChanged(view, oldScale, newScale);
219 | }
220 | }
221 |
222 | @Override
223 | public void onReceivedLoginRequest(WebView view, String realm, @Nullable String account, String args) {
224 | if (mClient != null) {
225 | mClient.onReceivedLoginRequest(view, realm, account, args);
226 | } else {
227 | super.onReceivedLoginRequest(view, realm, account, args);
228 | }
229 | }
230 |
231 | @Override
232 | public boolean onRenderProcessGone(WebView view, RenderProcessGoneDetail detail) {
233 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && mClient != null){
234 | return mClient.onRenderProcessGone(view, detail);
235 | }
236 | return super.onRenderProcessGone(view, detail);
237 | }
238 |
239 | @Override
240 | public void onSafeBrowsingHit(WebView view, WebResourceRequest request, int threatType, SafeBrowsingResponse callback) {
241 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 && mClient != null){
242 | mClient.onSafeBrowsingHit(view, request, threatType, callback);
243 | }else {
244 | super.onSafeBrowsingHit(view, request, threatType, callback);
245 | }
246 | }
247 |
248 | @Deprecated
249 | public interface OnLoadJSListener {
250 |
251 | void onLoadStart();
252 |
253 | void onLoadFinished();
254 |
255 | }
256 | }
257 |
--------------------------------------------------------------------------------
/library/src/main/java/com/github/lzyzsd/jsbridge/BridgeHelper.java:
--------------------------------------------------------------------------------
1 | package com.github.lzyzsd.jsbridge;
2 |
3 | import android.os.Build;
4 | import android.os.Looper;
5 | import android.os.SystemClock;
6 | import android.text.TextUtils;
7 | import android.webkit.ValueCallback;
8 |
9 | import androidx.annotation.RequiresApi;
10 | import androidx.collection.ArrayMap;
11 |
12 | import com.google.gson.Gson;
13 |
14 | import java.net.URLEncoder;
15 | import java.util.ArrayList;
16 | import java.util.List;
17 | import java.util.Map;
18 |
19 | /**
20 | * JsBridge辅助类,帮助集成JsBridge功能.
21 | *
22 | * @author ZhengAn
23 | * @date 2019-06-30
24 | */
25 | public class BridgeHelper implements WebViewJavascriptBridge, WebViewClientProxy.OnLoadJSListener {
26 |
27 | @Deprecated
28 | private Map mMessageHandlers = new ArrayMap<>();
29 | private Map mCallbacks = new ArrayMap<>();
30 | @Deprecated
31 | private BridgeHandler mDefaultHandler = new DefaultHandler();
32 |
33 | private List