Skip to content

Commit

Permalink
add: promise support; fix: dialogs and setClip ANR on ui thread;
Browse files Browse the repository at this point in the history
  • Loading branch information
hyb1996 committed Dec 1, 2017
1 parent 45bbc33 commit 1413bf2
Show file tree
Hide file tree
Showing 12 changed files with 593 additions and 170 deletions.
6 changes: 2 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.stardust.scriptdroid"
minSdkVersion 17
targetSdkVersion 23
versionCode 226
versionName "3.0.0 Alpha26"
versionCode 227
versionName "3.0.0 Alpha27"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
ndk {
Expand Down Expand Up @@ -115,8 +115,6 @@ dependencies {
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
//JDeferred
compile 'org.jdeferred:jdeferred-android-aar:1.2.6'
//Glide
compile('com.github.bumptech.glide:glide:4.2.0', {
exclude group: 'com.android.support'
Expand Down
70 changes: 70 additions & 0 deletions app/src/main/assets/sample/对话框/UI模式下使用对话框.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"ui";

ui.layout(
<vertical>
<button id="callback" align="center">回调形式</button>
<button id="promise" align="center">Promise形式</button>
<button id="calc" align="center">简单计算器</button>
</vertical>
);

ui.callback.click(()=>{
dialogs.confirm("要弹出输入框吗?", "", function(b){
if(b){
dialogs.rawInput("输入", "", function(str){
alert("您输入的是:" + str);
});
}else{
ui.finish();
}
});
});

ui.promise.click(()=>{
dialogs.confirm("要弹出输入框吗")
.then(function(b){
if(b){
return dialogs.rawInput("输入");
}else{
ui.finish();
}
}).then(function(str){
alert("您输入的是:" + str);
});
});


ui.calc.click(()=>{
let num1, num2, op;
dialogs.input("请输入第一个数字")
.then(n => {
num1 = n;
return dialogs.singleChoice("请选择运算", ["加", "减", "乘", "除", "幂"]);
})
.then(o => {
op = o;
return dialogs.input("请输入第二个数字");
})
.then(n => {
num2 = n;
var result;
switch(op){
case 0:
result = num1 + num2;
break;
case 1:
result = num1 - num2;
break;
case 2:
result = num1 * num2;
break;
case 3:
result = num1 / num2;
break;
case 4:
result = Math.pow(num1, num2);
break;
}
alert("运算结果", result);
});
});
3 changes: 3 additions & 0 deletions autojs/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ dependencies {
compile 'com.github.hyb1996:EnhancedFloaty:0.17'
compile 'com.github.hyb1996:OpenCvLib:2.4.13.4-imgproc'
compile 'com.makeramen:roundedimageview:2.3.0'
//JDeferred
compile 'org.jdeferred:jdeferred-android-aar:1.2.6'

// Gson
compile 'com.google.code.gson:gson:2.8.0'
// Terminal emulator
Expand Down
5 changes: 3 additions & 2 deletions autojs/src/main/assets/javascript_engine_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ __runtime__.bridges.setBridges({
});

var __that__ = this;
JSON = require('__json2__.js');
util = require('__util__.js');
var Promise = require('promise.js');
var JSON = require('__json2__.js');
var util = require('__util__.js');

var __asGlobal__ = function(obj, functions){
var len = functions.length;
Expand Down
121 changes: 95 additions & 26 deletions autojs/src/main/assets/modules/__dialogs__.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
if(isUiThread() && !callback){
return new Promise(function(resolve, reject){
rtDialogs().alert(title, prefill, function(){
resolve.apply(null, Array.prototype.slice.call(arguments));
});
});
}
return rtDialogs().alert(title, prefill, callback ? callback : null);
}

dialogs.confirm = function(title, prefill){
dialogs.confirm = function(title, prefill, callback){
prefill = prefill || "";
return __runtime__.dialogs.confirm(title, 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){
dialogs.select = function(title, items, callback){
if(items instanceof Array){
return __runtime__.dialogs.select(title, items);
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 __runtime__.dialogs.select(title, [].slice.call(arguments, 1));
return rtDialogs().select(title, [].slice.call(arguments, 1));
}

dialogs.singleChoice = function(title, items, index){
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++){
Expand All @@ -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;
}
Loading

0 comments on commit 1413bf2

Please sign in to comment.