Skip to content

Commit f5deb1f

Browse files
author
niuxiaowei
committed
init
0 parents  commit f5deb1f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+3918
-0
lines changed

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
.svn
2+
.svn/*
3+
*.class
4+
*.apk
5+
bin/
6+
bin/*
7+
gen/
8+
gen/*
9+
lint.xml
10+
.DS_Store
11+
.settings/
12+
.settings/*
13+
.gradle/*
14+
*.iml
15+
build/*
16+
import-summary.txt
17+
.idea/*
18+
local.properties
19+
.git
20+
obj/
21+
22+
.gradle/
23+
.idea/
24+
build/
25+

README.md

Lines changed: 352 additions & 0 deletions
Large diffs are not rendered by default.

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 23
5+
buildToolsVersion "23.0.2"
6+
7+
defaultConfig {
8+
applicationId "com.bridge"
9+
minSdkVersion 14
10+
targetSdkVersion 23
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+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
testCompile 'junit:junit:4.12'
25+
compile 'com.android.support:appcompat-v7:23.4.0'
26+
compile 'com.android.support:design:23.4.0'
27+
}

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/niuxiaowei/Library/Android/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+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.bridge;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

app/src/main/AndroidManifest.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest package="com.bridge"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity
12+
android:exported="false"
13+
android:name=".view.MainActivity"
14+
android:label="@string/app_name"
15+
android:theme="@style/AppTheme.NoActionBar">
16+
<intent-filter>
17+
<action android:name="android.intent.action.MAIN"/>
18+
19+
<category android:name="android.intent.category.LAUNCHER"/>
20+
</intent-filter>
21+
</activity>
22+
</application>
23+
24+
</manifest>

app/src/main/assets/bridge.js

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// Uses Node, AMD or browser globals to create a module. This example creates
2+
// a global even when AMD is used. This is useful if you have some scripts
3+
// that are loaded by an AMD loader, but they still want access to globals.
4+
// If you do not need to export a global for the AMD case,
5+
// see returnExports.js.
6+
7+
// If you want something that will work in other stricter CommonJS environments,
8+
// or if you need to create a circular dependency, see commonJsStrictGlobal.js
9+
10+
// Defines a module "returnExportsGlobal" that depends another module called
11+
// "b". Note that the name of the module is implied by the file name. It is
12+
// best if the file name and the exported global have matching names.
13+
14+
// If the 'b' module also uses this type of boilerplate, then
15+
// in the browser, it will create a global .b that is used below.
16+
17+
18+
//document.write('<script src=""></script>');
19+
20+
21+
22+
23+
(function (root, factory) {
24+
root.JsBridge = factory(root);
25+
}(typeof window == "undefined" ? this : window, function (win) {
26+
if (!win.document) { return {};}
27+
28+
29+
var doc = win.document,
30+
title = doc.title,
31+
ua = navigator.userAgent.toLowerCase(),
32+
platform = navigator.platform.toLowerCase(),
33+
isMacorWin = !(!platform.match("mac") && !platform.match("win")),
34+
isandroid = -1 != ua.indexOf("android"),
35+
isphoneorpad = -1 != ua.indexOf("iphone") || -1 != ua.indexOf("ipad"),
36+
JsBridge = {
37+
usable: false,
38+
init: function (bridge) {
39+
/*注册提供给native的接口*/
40+
bridge.registerHandler("exam",function(message, responseCallback){
41+
42+
var result=document.getElementById("result");
43+
result.innerHTML = 'native传递的数据:'+JSON.stringify(message);
44+
responseCallback({
45+
status: "1",
46+
msg: "ok",
47+
values:{
48+
msg: "js回调native"
49+
}
50+
});
51+
});
52+
53+
bridge.registerHandler("exam1",function(message, responseCallback){
54+
55+
var result=document.getElementById("result");
56+
result.innerHTML = 'native传递的数据:'+JSON.stringify(message);
57+
responseCallback({
58+
status: "1",
59+
msg: "ok",
60+
values:{
61+
cityName:message.cityName,
62+
cityProvince: message.cityProvince
63+
}
64+
});
65+
});
66+
67+
bridge.registerHandler("exam2",function(message, responseCallback){
68+
69+
var result=document.getElementById("result");
70+
result.innerHTML = 'native传递的数据:'+JSON.stringify(message);
71+
responseCallback({
72+
status: "1",
73+
msg: "ok",
74+
values:{
75+
city:{
76+
cityName:message.cityName,
77+
cityProvince: message.cityProvince
78+
}
79+
80+
}
81+
});
82+
});
83+
84+
bridge.registerHandler("exam3",function(message, responseCallback){
85+
86+
var result=document.getElementById("result");
87+
result.innerHTML = 'native传递的数据:'+JSON.stringify(message);
88+
responseCallback({
89+
status: "1",
90+
msg: "ok",
91+
values:{
92+
cityName:'北京',
93+
cityProvince: '北京'
94+
95+
}
96+
});
97+
});
98+
99+
bridge.registerHandler("exam4",function(message, responseCallback){
100+
101+
var result=document.getElementById("result");
102+
result.innerHTML = 'native传递的数据:'+JSON.stringify(message);
103+
responseCallback({
104+
status: "1",
105+
msg: "ok",
106+
values:{
107+
108+
}
109+
});
110+
});
111+
112+
return this;
113+
},
114+
checkUsable: function (methodName, params, cb) {
115+
var _this = this;
116+
if (!window._JSNativeBridge) {
117+
//JS not be injected success
118+
cb({
119+
status: "-1",
120+
msg: "window._JSNativeBridge is undefined"
121+
}, {});
122+
return;
123+
}
124+
125+
try {
126+
window._JSNativeBridge._doSendRequest(methodName, params, cb);
127+
} catch (e) {
128+
cb({status: "-1", msg: e},{});
129+
}
130+
},
131+
132+
test: function(params,cb){
133+
this.checkUsable("test",{
134+
"msg": "js发送的请求"
135+
},cb);
136+
},
137+
138+
test1: function(params,cb){
139+
this.checkUsable("test1",{
140+
"age": 10,
141+
"name":"wangwu"
142+
},cb);
143+
},
144+
145+
test2: function(params,cb){
146+
this.checkUsable("test2",{
147+
person:{
148+
"age": 10,
149+
"name":"wangwu"
150+
}
151+
152+
},cb);
153+
},
154+
155+
test3: function(params,cb){
156+
this.checkUsable("test3",{
157+
"jiguan":"北京",
158+
person:{
159+
"age": 10,
160+
"name":"wangwu"
161+
}
162+
163+
},cb);
164+
},
165+
test4: function(params,cb){
166+
this.checkUsable("test4",{
167+
168+
},cb);
169+
}
170+
171+
172+
};
173+
174+
if (window._JSNativeBridge) {
175+
console.log("native injection js success!");
176+
window._JSNativeBridge.protocol.scheme = 'niu';
177+
window._JSNativeBridge.protocol.host = 'receive_msg';
178+
window._JSNativeBridge.debug = true;
179+
JsBridge.init(window._JSNativeBridge);
180+
} else {
181+
console.log("native injection js wrong!");
182+
document.addEventListener(
183+
'JsBridgeInit',
184+
function(event) {
185+
console.log('------------------bridge');
186+
window._JSNativeBridge.protocol.scheme = 'niu';
187+
window._JSNativeBridge.protocol.host = 'receive_msg';
188+
event.bridge.debug = true;
189+
JsBridge.init(event.bridge);
190+
},
191+
false
192+
);
193+
}
194+
195+
return JsBridge;
196+
}));

0 commit comments

Comments
 (0)