add: promise support; fix: dialogs and setClip ANR on ui thread;

This commit is contained in:
hyb1996
2017-12-01 20:52:51 +08:00
parent 45bbc33ab7
commit 1413bf21a5
12 changed files with 607 additions and 184 deletions

View File

@@ -2,43 +2,107 @@
module.exports = function(__runtime__, scope){
var dialogs = {};
dialogs.rawInput = function(title, prefill){
dialogs.rawInput = function(title, prefill, callback){
prefill = prefill || "";
var s = __runtime__.dialogs.rawInput(title, prefill);
return s ? String(s) : null;
if(isUiThread() && !callback){
return new Promise(function(resolve, reject){
rtDialogs().rawInput(title, prefill, function(){
resolve.apply(null, Array.prototype.slice.call(arguments));
});
});
}
return rtDialogs().rawInput(title, prefill, callback ? callback : null);
};
dialogs.input = function(title, prefill){
return eval(dialogs.rawInput(title, prefill));
dialogs.input = function(title, prefill, callback){
prefill = prefill || "";
if(isUiThread() && !callback){
return new Promise(function(resolve, reject){
rtDialogs().rawInput(title, prefill, function(str){
resolve(eval(str));
});
});
}
if(callback){
dialogs.rawInput(title, prefill, function(str){
callback(eval(str));
});
return;
}
return eval(dialogs.rawInput(title, prefill), callback ? callback : null);
}
dialogs.prompt = dialogs.rawInput;
dialogs.alert = function(title, prefill){
dialogs.alert = function(title, prefill, callback){
prefill = prefill || "";
return __runtime__.dialogs.alert(title, prefill);
}
dialogs.confirm = function(title, prefill){
prefill = prefill || "";
return __runtime__.dialogs.confirm(title, prefill);
}
dialogs.select = function(title, items){
if(items instanceof Array){
return __runtime__.dialogs.select(title, items);
if(isUiThread() && !callback){
return new Promise(function(resolve, reject){
rtDialogs().alert(title, prefill, function(){
resolve.apply(null, Array.prototype.slice.call(arguments));
});
});
}
return __runtime__.dialogs.select(title, [].slice.call(arguments, 1));
return rtDialogs().alert(title, prefill, callback ? callback : null);
}
dialogs.singleChoice = function(title, items, index){
dialogs.confirm = function(title, prefill, callback){
prefill = prefill || "";
if(isUiThread() && !callback){
return new Promise(function(resolve, reject){
rtDialogs().confirm(title, prefill, function(){
resolve.apply(null, Array.prototype.slice.call(arguments));
});
});
}
return rtDialogs().confirm(title, prefill, callback ? callback : null);
}
dialogs.select = function(title, items, callback){
if(items instanceof Array){
if(isUiThread() && !callback){
return new Promise(function(resolve, reject){
rtDialogs().select(title, items, function(){
resolve.apply(null, Array.prototype.slice.call(arguments));
});
});
}
return rtDialogs().select(title, items, callback ? callback : null);
}
return rtDialogs().select(title, [].slice.call(arguments, 1));
}
dialogs.singleChoice = function(title, items, index, callback){
index = index || 0;
return __runtime__.dialogs.singleChoice(title, index, items);
if(isUiThread() && !callback){
return new Promise(function(resolve, reject){
rtDialogs().singleChoice(title, index, items, function(){
resolve.apply(null, Array.prototype.slice.call(arguments));
});
});
}
return rtDialogs().singleChoice(title, index, items, callback ? callback : null);
}
dialogs.multiChoice = function(title, items, index){
dialogs.multiChoice = function(title, items, index, callback){
index = index || [];
var javaArray = __runtime__.dialogs.multiChoice(title, index, items);
if(isUiThread() && !callback){
return new Promise(function(resolve, reject){
rtDialogs().singleChoice(title, index, items, function(r){
resolve.apply(null, toJsArray(r));
});
});
}
if(callback){
return rtDialogs().multiChoice(title, index, items, function(r){
callback(toJsArray(r));
});
}
return toJsArray(rtDialogs().multiChoice(title, index, items, null));
}
function toJsArray(javaArray){
var jsArray = [];
var len = javaArray.length;
for (var i = 0;i < len;i++){
@@ -47,21 +111,26 @@ module.exports = function(__runtime__, scope){
return jsArray;
}
scope.rawInput = function(title, prefill){
return dialogs.rawInput(title, prefill);
function rtDialogs(){
var d = __runtime__.dialogs;
if(!isUiThread()){
return d.nonUiDialogs;
}else{
return d;
}
}
scope.alert = function(title, prefill){
dialogs.alert(title, prefill);
function isUiThread(){
return android.os.Looper.myLooper() == android.os.Looper.getMainLooper();
}
scope.confirm = function(title, prefill){
return dialogs.confirm(title, prefill);
}
scope.rawInput = dialogs.rawInput.bind(dialogs);
scope.prompt = function(title, prefill){
return dialogs.prompt(title, prefill);
}
scope.alert = dialogs.alert.bind(dialogs);
scope.confirm = dialogs.confirm.bind(dialogs);
scope.prompt = dialogs.prompt.bind(dialogs);
return dialogs;
}