From b3be08909b572e49c0396ae44350c628bdba7cdd Mon Sep 17 00:00:00 2001 From: beichen <826237710@qq.com> Date: Tue, 9 Jul 2019 09:22:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(project)=EF=BC=9A=E6=B7=BB=E5=8A=A0=20Andr?= =?UTF-8?q?oid=20=E7=AB=AF=20Java=20=E7=BC=96=E5=86=99=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=20JSBridgeAndroidJava?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + JSBridgeAndroidJava/.gitignore | 141 ++++++++++++ JSBridgeAndroidJava/app/.gitignore | 1 + JSBridgeAndroidJava/app/build.gradle | 33 +++ JSBridgeAndroidJava/app/proguard-rules.pro | 21 ++ .../ExampleInstrumentedTest.java | 26 +++ .../app/src/main/AndroidManifest.xml | 30 +++ .../jsbridgeandroidjava/MainActivity.java | 205 ++++++++++++++++++ .../jsbridgeandroidjava/MyWebViewClient.java | 12 + .../drawable-v24/ic_launcher_foreground.xml | 34 +++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 86 ++++++++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 2963 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4905 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2060 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2783 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4490 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6895 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6387 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10413 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9128 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15132 bytes .../app/src/main/res/values/colors.xml | 6 + .../app/src/main/res/values/strings.xml | 3 + .../app/src/main/res/values/styles.xml | 11 + .../jsbridgeandroidjava/ExampleUnitTest.java | 17 ++ JSBridgeAndroidJava/build.gradle | 27 +++ JSBridgeAndroidJava/gradle.properties | 15 ++ JSBridgeAndroidJava/gradlew | 172 +++++++++++++++ JSBridgeAndroidJava/gradlew.bat | 84 +++++++ JSBridgeAndroidJava/settings.gradle | 1 + 33 files changed, 1108 insertions(+) create mode 100644 JSBridgeAndroidJava/.gitignore create mode 100644 JSBridgeAndroidJava/app/.gitignore create mode 100644 JSBridgeAndroidJava/app/build.gradle create mode 100644 JSBridgeAndroidJava/app/proguard-rules.pro create mode 100644 JSBridgeAndroidJava/app/src/androidTest/java/com/example/jsbridgeandroidjava/ExampleInstrumentedTest.java create mode 100644 JSBridgeAndroidJava/app/src/main/AndroidManifest.xml create mode 100644 JSBridgeAndroidJava/app/src/main/java/com/beichen/jsbridgeandroidjava/MainActivity.java create mode 100644 JSBridgeAndroidJava/app/src/main/java/com/beichen/jsbridgeandroidjava/MyWebViewClient.java create mode 100644 JSBridgeAndroidJava/app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 JSBridgeAndroidJava/app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 JSBridgeAndroidJava/app/src/main/res/layout/activity_main.xml create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 JSBridgeAndroidJava/app/src/main/res/values/colors.xml create mode 100644 JSBridgeAndroidJava/app/src/main/res/values/strings.xml create mode 100644 JSBridgeAndroidJava/app/src/main/res/values/styles.xml create mode 100644 JSBridgeAndroidJava/app/src/test/java/com/example/jsbridgeandroidjava/ExampleUnitTest.java create mode 100644 JSBridgeAndroidJava/build.gradle create mode 100644 JSBridgeAndroidJava/gradle.properties create mode 100755 JSBridgeAndroidJava/gradlew create mode 100644 JSBridgeAndroidJava/gradlew.bat create mode 100644 JSBridgeAndroidJava/settings.gradle diff --git a/.gitignore b/.gitignore index e884fef..c54c76a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ .DS_Store .vscode .ideal +.idea + +*/gradle/wrapper npm-debug.log* yarn-debug.log* diff --git a/JSBridgeAndroidJava/.gitignore b/JSBridgeAndroidJava/.gitignore new file mode 100644 index 0000000..af3da95 --- /dev/null +++ b/JSBridgeAndroidJava/.gitignore @@ -0,0 +1,141 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild + + +# Android +# Built application files +*.apk +*.ap_ +*.aab + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ +release/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# IntelliJ +*.iml +.idea/workspace.xml +.idea/tasks.xml +.idea/gradle.xml +.idea/assetWizardSettings.xml +.idea/dictionaries +.idea/libraries +# Android Studio 3 in .gitignore file. +.idea/caches +.idea/modules.xml +# Comment next line if keeping position of elements in Navigation Editor is relevant for you +.idea/navEditor.xml + +# Keystore files +# Uncomment the following lines if you do not want to check your keystore files in. +#*.jks +#*.keystore + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +# google-services.json + +# Freeline +freeline.py +freeline/ +freeline_project_description.json + +# fastlane +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output +fastlane/readme.md + +# Version control +vcs.xml + +# lint +lint/intermediates/ +lint/generated/ +lint/outputs/ +lint/tmp/ +# lint/reports/ + + +# Gradle +.gradle +/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache + +# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 +# gradle/wrapper/gradle-wrapper.properties + + +# Java +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* \ No newline at end of file diff --git a/JSBridgeAndroidJava/app/.gitignore b/JSBridgeAndroidJava/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/JSBridgeAndroidJava/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/JSBridgeAndroidJava/app/build.gradle b/JSBridgeAndroidJava/app/build.gradle new file mode 100644 index 0000000..a5300ec --- /dev/null +++ b/JSBridgeAndroidJava/app/build.gradle @@ -0,0 +1,33 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + defaultConfig { + applicationId "com.beichen.jsbridgeandroidjava" + minSdkVersion 23 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + repositories { + // ... + maven { url "https://jitpack.io" } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.github.lzyzsd:jsbridge:1.0.4' + implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'com.android.support.constraint:constraint-layout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'com.android.support.test:runner:1.0.2' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' +} diff --git a/JSBridgeAndroidJava/app/proguard-rules.pro b/JSBridgeAndroidJava/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/JSBridgeAndroidJava/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/JSBridgeAndroidJava/app/src/androidTest/java/com/example/jsbridgeandroidjava/ExampleInstrumentedTest.java b/JSBridgeAndroidJava/app/src/androidTest/java/com/example/jsbridgeandroidjava/ExampleInstrumentedTest.java new file mode 100644 index 0000000..6a9065e --- /dev/null +++ b/JSBridgeAndroidJava/app/src/androidTest/java/com/example/jsbridgeandroidjava/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.beichen.jsbridgeandroidjava; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.beichen.jsbridgeandroidjava", appContext.getPackageName()); + } +} diff --git a/JSBridgeAndroidJava/app/src/main/AndroidManifest.xml b/JSBridgeAndroidJava/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d12545a --- /dev/null +++ b/JSBridgeAndroidJava/app/src/main/AndroidManifest.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/JSBridgeAndroidJava/app/src/main/java/com/beichen/jsbridgeandroidjava/MainActivity.java b/JSBridgeAndroidJava/app/src/main/java/com/beichen/jsbridgeandroidjava/MainActivity.java new file mode 100644 index 0000000..e2d68b5 --- /dev/null +++ b/JSBridgeAndroidJava/app/src/main/java/com/beichen/jsbridgeandroidjava/MainActivity.java @@ -0,0 +1,205 @@ +package com.beichen.jsbridgeandroidjava; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.KeyEvent; +import android.view.View; +import android.webkit.CookieManager; +import android.webkit.CookieSyncManager; +import android.webkit.WebChromeClient; +import android.widget.EditText; +import android.widget.TextView; +import android.widget.Toast; + +import com.github.lzyzsd.jsbridge.BridgeHandler; +import com.github.lzyzsd.jsbridge.BridgeWebView; +import com.github.lzyzsd.jsbridge.CallBackFunction; + + +public class MainActivity extends AppCompatActivity implements View.OnClickListener { + + private BridgeWebView mWebView; + private static final String URL = "http://xxx.xxx.xxx.xxx:xxxx/"; + + long exitTime = 0; + private TextView mTvUser; + private EditText mEditName; + private EditText mEditCookie; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + initWebView(); + + registerHandlers(); + + initViews(); + + } + + @Override + protected void onResume() { + super.onResume(); + mWebView.reload(); + } + + /** + * 初始化 WebView + */ + @SuppressLint("SetJavaScriptEnabled") + private void initWebView() { + mWebView = findViewById(R.id.main_wv); + + mWebView.getSettings().setAllowFileAccess(true); + mWebView.getSettings().setAppCacheEnabled(true); + mWebView.getSettings().setDatabaseEnabled(true); + // 开启 localStorage + mWebView.getSettings().setDomStorageEnabled(true); + // 设置支持javascript + mWebView.getSettings().setJavaScriptEnabled(true); + // 进行缩放 + mWebView.getSettings().setBuiltInZoomControls(true); + // 设置UserAgent + mWebView.getSettings().setUserAgentString(mWebView.getSettings().getUserAgentString() + "android"); + // 设置不用系统浏览器打开,直接显示在当前WebView + mWebView.setWebChromeClient(new WebChromeClient()); + mWebView.setWebViewClient(new MyWebViewClient(mWebView)); + + mWebView.loadUrl(URL); + } + + /** + * 注册与 H5 交互的事件函数 + */ + private void registerHandlers() { + // 设置默认接收函数 + mWebView.setDefaultHandler(new BridgeHandler() { + @Override + public void handler(String data, CallBackFunction function) { + Toast.makeText(MainActivity.this, data, Toast.LENGTH_LONG).show(); + function.onCallBack("安卓返回给 JS 的消息内容"); + } + }); + + // 注册刷新页面的 reloadUrl 函数 + mWebView.registerHandler("reloadUrl", new BridgeHandler() { + + @Override + public void handler(String data, CallBackFunction function) { + mWebView.reload(); + Toast.makeText(MainActivity.this, "刷新成功~", Toast.LENGTH_SHORT).show(); + function.onCallBack(""); + } + }); + + // 注册修改 User 名称的 changeUser 函数 + mWebView.registerHandler("changeUser", new BridgeHandler() { + + @Override + public void handler(String user, CallBackFunction function) { + mTvUser.setText(user); + function.onCallBack(""); + } + }); + } + + /** + * 初始化其他 View 组件 + */ + private void initViews() { + findViewById(R.id.btn_cookie).setOnClickListener(this); + findViewById(R.id.btn_name).setOnClickListener(this); + findViewById(R.id.btn_init).setOnClickListener(this); + mTvUser = findViewById(R.id.tv_user); + mEditCookie = findViewById(R.id.edit_cookie); + mEditName = findViewById(R.id.edit_name); + } + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.btn_init: + // 调用 H5 界面的默认接收函数 + mWebView.send("安卓传递给 JS 的消息", new CallBackFunction() { + @Override + public void onCallBack(String data) { + Toast.makeText(MainActivity.this, data, Toast.LENGTH_LONG).show(); + } + }); + break; + case R.id.btn_name: + // 调用 H5 界面的 changeName 事件函数 + mWebView.callHandler("changeName", mEditName.getText().toString(), new CallBackFunction() { + @Override + public void onCallBack(String data) { + Toast.makeText(MainActivity.this, "name 修改成功", Toast.LENGTH_SHORT).show(); + mEditName.setText(""); + } + }); + break; + case R.id.btn_cookie: + syncCookie(this, URL, "token=" + mEditCookie.getText().toString()); + // 调用 H5 界面的 syncCookie 事件函数 + mWebView.callHandler("syncCookie", "", new CallBackFunction() { + @Override + public void onCallBack(String data) { + Toast.makeText(MainActivity.this, "Cookie 同步成功", Toast.LENGTH_SHORT).show(); + mEditCookie.setText(""); + } + }); + break; + } + } + + @Override + public boolean onKeyDown(int keyCode, KeyEvent event) { + if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) { + // 返回前一个页面 + mWebView.goBack(); + return true; + } else if (keyCode == KeyEvent.KEYCODE_BACK) { + exit(); + return false; + } + return super.onKeyDown(keyCode, event); + } + + /** + * 退出应用 + */ + public void exit() { + if ((System.currentTimeMillis() - exitTime) > 2000) { + Toast.makeText(getApplicationContext(), "再按一次退出程序", + Toast.LENGTH_SHORT).show(); + exitTime = System.currentTimeMillis(); + } else { + finish(); + System.exit(0); + } + } + + /** + * 这只并同步 Cookie 的工具函数 + * @param context 上下文对象 + * @param url url 地址 + * @param cookie 需要设置的 cookie 值,例如:"token=azhd57hkslz" + */ + @SuppressWarnings("deprecation") + private static void syncCookie(Context context, String url, String cookie){ + CookieSyncManager.createInstance(context); + CookieManager cookieManager = CookieManager.getInstance(); + cookieManager.setAcceptCookie(true); + cookieManager.removeSessionCookie();// 移除 + cookieManager.removeAllCookie(); + cookieManager.setCookie(url, cookie); + String newCookie = cookieManager.getCookie(url); + Log.i("tag ", "newCookie == " + newCookie); + CookieSyncManager.getInstance().sync(); + } + +} diff --git a/JSBridgeAndroidJava/app/src/main/java/com/beichen/jsbridgeandroidjava/MyWebViewClient.java b/JSBridgeAndroidJava/app/src/main/java/com/beichen/jsbridgeandroidjava/MyWebViewClient.java new file mode 100644 index 0000000..ed95e60 --- /dev/null +++ b/JSBridgeAndroidJava/app/src/main/java/com/beichen/jsbridgeandroidjava/MyWebViewClient.java @@ -0,0 +1,12 @@ +package com.beichen.jsbridgeandroidjava; + +import com.github.lzyzsd.jsbridge.BridgeWebView; +import com.github.lzyzsd.jsbridge.BridgeWebViewClient; + +public class MyWebViewClient extends BridgeWebViewClient { + + public MyWebViewClient(BridgeWebView webView) { + super(webView); + } + +} diff --git a/JSBridgeAndroidJava/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/JSBridgeAndroidJava/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/JSBridgeAndroidJava/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/JSBridgeAndroidJava/app/src/main/res/drawable/ic_launcher_background.xml b/JSBridgeAndroidJava/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/JSBridgeAndroidJava/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/JSBridgeAndroidJava/app/src/main/res/layout/activity_main.xml b/JSBridgeAndroidJava/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..45a5a5c --- /dev/null +++ b/JSBridgeAndroidJava/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,86 @@ + + + + + + + +