Skip to content

Commit

Permalink
WJC: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mgttt committed Oct 24, 2016
1 parent beb9a9b commit e58d286
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 195 deletions.
132 changes: 132 additions & 0 deletions assetsJsBridge/WebViewJavascriptBridge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//NOTES: the comments will be removed
(function(win,doc,PROTOCOL_SCHEME){
if (win.WebViewJavascriptBridge) {
//in case of already init-ed.
return;
}
//o2s/s2o
function o2s(o){
if(null==o)return "null";
f=arguments.callee;
t=typeof o;
if('object'==t){if(Array==o.constructor)t='array';else if(RegExp==o.constructor)t='regexp';}
switch(t){
case 'undefined':case 'unknown':return;
case 'function':return !('prototype' in o)?"function(){}":(""+o);break;
case 'boolean':case 'regexp':return o.toString(); break;
case 'number':return isFinite(o)?o.toString():'null';break;
case 'string':return '"'+o.replace(/(\\|\")/g,"\\$1").replace(/\n/g,"\\n").replace(/\r/g,"\\r")+'"';break;
case 'object':var r=[];try{for(var p in o){v=f(o[p]);if(v!==undefined)r.push('"'+p+'":'+v);}}catch(e){};
return '{'+r.join(',')+'}';break;
case 'array':var r=[];
if(o.length>=0){
for(var i=0;i<o.length;i++){var v=f(o[i]);if (v!==undefined)r.push(v);};return '['+r.join(',')+']';
}
else{
for(var k in o){var v=f(o[k]);if(v!==undefined)r.push('"'+k+'":'+v);};return '{'+r.join(',')+'}';
}
}
};
function s2o(s){ try{ return (new Function('return '+s))(); }catch(ex){} };

var msgIfrm;
var send_Q = [];
var msgHandlerSet = {};

var responseCallbacks = {};
var msgId = 1;

//NOTES: can't remove yet, old bridge codes might use it... will remove in future...
function init() {
console.log('deprecated WebViewJavascriptBridge.init() is called.');
}

function _js2app(msg, cb) {
if (cb) {
msgId=(msgId + 1) % 1000000;
var callbackId = 'cb_' + msgId + '_' + new Date().getTime();
responseCallbacks[callbackId] = cb;
msg.callbackId = callbackId;
}
send_Q.push(msg);

//notify app a msg is Q-ed
msgIfrm.src = PROTOCOL_SCHEME + '://__QUEUE_MESSAGE__/';
//the __QUEUE_MESSAGE__ is just a what-ever word, @ref to shouldOverrideUrlLoading
}

function _fetchQueue() {
var messageQueueString = o2s(send_Q);
send_Q = [];
//TODO to improve the mechanism
//reload iframe src to communicate with java
msgIfrm.src = PROTOCOL_SCHEME + '://return/_fetchQueue/' + encodeURIComponent(messageQueueString);
}

function _app2js(msg) {
setTimeout(function(){
//console.log("_app2js", msg);
var callback=null;
//java call finished, now need to call js callback function
if (msg.responseId) {
callback = responseCallbacks[msg.responseId];
if (!callback) {
return;
}
callback(msg.responseData);
delete responseCallbacks[msg.responseId];
} else {
var handler = null;
if (msg.handlerName) {
//find the handler
handler = msgHandlerSet[msg.handlerName];
if(handler==null){
console.log("WebViewJavascriptBridge: not found handler name="+msg.handlerName);
}
}
try {
if(handler!=null){
if (msg.callbackId) {
var callbackResponseId = msg.callbackId;
callback = function(responseData) {
_js2app({
responseId: callbackResponseId,
responseData: responseData
});
};
}
handler(msg.data, callback);
}
} catch (exception) {
console.log("WebViewJavascriptBridge: WARNING: javascript handler threw.", message, exception);
}
}
},1);
}

function registerHandler(handlerName, handler) {
msgHandlerSet[handlerName] = handler;
}

function callHandler(handlerName, data, cb) {
_js2app({
handlerName: handlerName,
data: data
}, cb);
}
//init msg iframe:
msgIfrm = doc.createElement('iframe');
msgIfrm.style.display = 'none';
doc.documentElement.appendChild(msgIfrm);

win.WebViewJavascriptBridge = {
_fetchQueue: _fetchQueue,
_js2app: _js2app,
_app2js: _app2js,

init: init,

registerHandler: registerHandler,
callHandler: callHandler
};
})(window,document,'jsb1');
15 changes: 13 additions & 2 deletions lib-android/szu.bdi.hybrid.core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ org.gradle.daemon=true
android {
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
//useLibrary 'org.apache.http.legacy'
useLibrary 'org.apache.http.legacy'
defaultConfig {
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
Expand All @@ -21,7 +21,18 @@ android {
// sourceCompatibility JavaVersion.VERSION_1_7
// targetCompatibility JavaVersion.VERSION_1_7
// }

sourceSets {
main {
//java.srcDirs = ['src/main/java', 'src/main/java/NewStylePassword', 'src/main/java/Fragment', 'src/main/java/MyImageButton', 'src/main/java/com.bignerdranch.android.mfailab/Receivers', 'src/main/java/2', 'src/main/java/com.bignerdranch.android.mfailab/GestruePassword', 'src/main/java/com.bignerdranch.android.mfailab/Fragments', 'src/main/java/com.bignerdranch.android.mfailab/ButtonStyles', 'src/main/java/com.bignerdranch.android.mfailab/ActivitiesForExtends', 'src/main/java/com.bignerdranch.android.mfailab/InternetClassesAndThreads', 'src/main/java/com.bignerdranch.android.mfailab/SharedPreference', 'src/main/java/com.bignerdranch.android.mfailab/ExitApplication', 'src/main/java/com.bignerdranch.android.mfailab/Keyboard', 'src/main/java/com.bignerdranch.android.mfailab/setting']
//res.srcDirs = ['src/main/res', 'src/main/res/drawable/Function', 'src/main/res/xml']
//java.srcDirs = ['src']
//assets.srcDirs = ['assets']
//res.srcDirs = ['res']
//manifest.srcFile 'AndroidManifest.xml'
//wanjo: important, we need the folders as assets:
assets.srcDirs = ['../../assetsJsBridge']
}
}
}

dependencies {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ public static void bindWebViewApi(JsBridgeWebView wv, final HybridUi callerAct)
String address = optString(callerAct.getUiData("address"));
JSONArray foundAuth = findSubAuth(authObj, address);
if (foundAuth == null) {
HybridTools.quickShowMsgMain("ConfigError: not found match auth for address\n " + address + " !!!");
//TODO
HybridTools.quickShowMsgMain("ConfigError: not found match auth for address (" + address + ") !!!");
return;
}
Log.v(LOGTAG, " foundAuth=" + foundAuth);
Expand Down
5 changes: 3 additions & 2 deletions lib-android/szu.bdi.hybrid.core/src/main/java/szu/bdi/hybrid/core/ICallBackFunction.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package szu.bdi.hybrid.core;

public interface ICallBackFunction {
void onCallBack(String cbStr);
}
//NOTES:如果不override就什么都不做
void onCallBack(String cbStr);
}
Loading

0 comments on commit e58d286

Please sign in to comment.