Skip to content

Commit

Permalink
新增 dialogs.build()支持customView属性和wrapInScrollView属性
Browse files Browse the repository at this point in the history
  • Loading branch information
hyb1996 committed Jan 22, 2019
1 parent 1865ac8 commit f6bab9e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
8 changes: 8 additions & 0 deletions autojs/src/main/assets/modules/__dialogs__.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ module.exports = function(__runtime__, scope){
builder.getDialog().emit("check", checked, builder.getDialog());
});
}
if(properties.customView != undefined) {
let customView = properties.customView;
if(typeof(customView) == 'xml' || typeof(customView) == 'string') {
customView = ui.run(() => ui.inflate(customView));
}
let wrapInScrollView = (properties.wrapInScrollView === undefined) ? true : properties.wrapInScrollView;
builder.customView(customView, wrapInScrollView);
}
}

function wrapNonNullString(str){
Expand Down
13 changes: 8 additions & 5 deletions autojs/src/main/assets/modules/__ui__.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function (runtime, global) {
ui.__defineGetter__("emitter", ()=> activity ? activity.getEventEmitter() : null);

ui.layout = function (xml) {
if(!activity){
if(typeof(activity) == 'undefined'){
throw new Error("需要在ui模式下运行才能使用该函数");
}
runtime.ui.layoutInflater.setContext(activity);
Expand All @@ -24,15 +24,18 @@ module.exports = function (runtime, global) {
}

ui.inflate = function(xml, parent, attachToParent){
if(!activity){
throw new Error("需要在ui模式下运行才能使用该函数");
}
if(typeof(xml) == 'xml'){
xml = xml.toXMLString();
}
parent = parent || null;
attachToParent = !!attachToParent;
runtime.ui.layoutInflater.setContext(activity);
let ctx;
if(typeof(activity) == 'undefined') {
ctx = new android.view.ContextThemeWrapper(context, com.stardust.autojs.R.style.ScriptTheme);
} else {
ctx = activity;
}
runtime.ui.layoutInflater.setContext(ctx);
return runtime.ui.layoutInflater.inflate(xml.toString(), parent, attachToParent);
}

Expand Down
34 changes: 27 additions & 7 deletions autojs/src/main/java/com/stardust/autojs/core/looper/Loopers.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
import java.util.HashSet;
import java.util.concurrent.CopyOnWriteArrayList;

import androidx.annotation.Nullable;

/**
* Created by Stardust on 2017/7/29.
*/

@SuppressWarnings("ConstantConditions")
public class Loopers implements MessageQueue.IdleHandler {

private static final String LOG_TAG = "Loopers";
Expand All @@ -32,9 +35,27 @@ public interface LooperQuitHandler {
private static final Runnable EMPTY_RUNNABLE = () -> {
};

private volatile ThreadLocal<Boolean> waitWhenIdle = new ThreadLocal<>();
private volatile ThreadLocal<HashSet<Integer>> waitIds = new ThreadLocal<>();
private volatile ThreadLocal<Integer> maxWaitId = new ThreadLocal<>();
private volatile ThreadLocal<Boolean> waitWhenIdle = new ThreadLocal<Boolean>() {
@Nullable
@Override
protected Boolean initialValue() {
return Looper.myLooper() == Looper.getMainLooper();
}
};
private volatile ThreadLocal<HashSet<Integer>> waitIds = new ThreadLocal<HashSet<Integer>>() {
@Nullable
@Override
protected HashSet<Integer> initialValue() {
return new HashSet<>();
}
};
private volatile ThreadLocal<Integer> maxWaitId = new ThreadLocal<Integer>() {
@Nullable
@Override
protected Integer initialValue() {
return 0;
}
};
private volatile ThreadLocal<CopyOnWriteArrayList<LooperQuitHandler>> looperQuitHandlers = new ThreadLocal<>();
private volatile Looper mServantLooper;
private Timers mTimers;
Expand Down Expand Up @@ -126,20 +147,22 @@ public Looper getServantLooper() {
return mServantLooper;
}

public void quitServantLooper() {
private void quitServantLooper() {
if (mServantLooper == null)
return;
mServantLooper.quit();
}

public int waitWhenIdle() {
int id = maxWaitId.get();
Log.d(LOG_TAG, "waitWhenIdle: " + id);
maxWaitId.set(id + 1);
waitIds.get().add(id);
return id;
}

public void doNotWaitWhenIdle(int waitId) {
Log.d(LOG_TAG, "doNotWaitWhenIdle: " + waitId);
waitIds.get().remove(waitId);
}

Expand Down Expand Up @@ -181,9 +204,6 @@ public void prepare() {
if (Looper.myLooper() == null)
LooperHelper.prepare();
Looper.myQueue().addIdleHandler(this);
waitWhenIdle.set(Looper.myLooper() == Looper.getMainLooper());
waitIds.set(new HashSet<>());
maxWaitId.set(0);
}

public void notifyThreadExit(TimerThread thread) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public void postDelayed(Runnable r, long interval) {
}
}

public void post(Runnable r) {

}

public boolean clearInterval(int id) {
return clearCallback(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public JsDialogBuilder(Context context, ScriptRuntime runtime) {
super(context);
mTimer = runtime.timers.getTimerForCurrentThread();
mLoopers = runtime.loopers;
mEmitter = new EventEmitter(runtime.bridges, mTimer);
mEmitter = new EventEmitter(runtime.bridges);
mUiHandler = runtime.uiHandler;
setUpEvents();
}
Expand Down Expand Up @@ -70,7 +70,6 @@ private void setUpEvents() {

public void onShowCalled() {
mTimer.postDelayed(() -> mWaitId = mLoopers.waitWhenIdle(), 0);

}

public JsDialog getDialog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public MaterialDialog.Builder newBuilder() {
}
return new JsDialogBuilder(context, mRuntime)
.theme(Theme.LIGHT);

}

public class NonUiDialogs {
Expand Down

0 comments on commit f6bab9e

Please sign in to comment.