opt: floaty
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
package com.stardust.autojs.core.floaty;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.stardust.autojs.R;
|
||||
import com.stardust.autojs.runtime.api.Floaty;
|
||||
import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.enhancedfloaty.ResizableFloaty;
|
||||
import com.stardust.enhancedfloaty.ResizableFloatyWindow;
|
||||
@@ -18,7 +16,7 @@ import com.stardust.enhancedfloaty.ResizableFloatyWindow;
|
||||
|
||||
public class FloatyWindow extends ResizableFloatyWindow {
|
||||
|
||||
private static final Object LOCK = new Object();
|
||||
private final Object mLock = new Object();
|
||||
private View mView;
|
||||
private boolean mCreated = false;
|
||||
private View mMoveCursor;
|
||||
@@ -38,12 +36,12 @@ public class FloatyWindow extends ResizableFloatyWindow {
|
||||
}
|
||||
|
||||
public void waitFor() {
|
||||
synchronized (LOCK) {
|
||||
synchronized (mLock) {
|
||||
if (mCreated) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
LOCK.wait();
|
||||
mLock.wait();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -53,17 +51,17 @@ public class FloatyWindow extends ResizableFloatyWindow {
|
||||
@Override
|
||||
public void onCreate(FloatyService service, WindowManager manager) {
|
||||
super.onCreate(service, manager);
|
||||
synchronized (LOCK) {
|
||||
mCreated = true;
|
||||
LOCK.notify();
|
||||
}
|
||||
View root = (View) mView.getParent().getParent();
|
||||
mMoveCursor = root.findViewById(R.id.move_cursor);
|
||||
mResizer = root.findViewById(R.id.resizer);
|
||||
mCloseButton = root.findViewById(R.id.close);
|
||||
synchronized (mLock) {
|
||||
mCreated = true;
|
||||
mLock.notify();
|
||||
}
|
||||
}
|
||||
|
||||
public void setOnCloseButtonClickListener(View.OnClickListener listener){
|
||||
public void setOnCloseButtonClickListener(View.OnClickListener listener) {
|
||||
mCloseButton.setOnClickListener(listener);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,9 +17,7 @@ import com.stardust.enhancedfloaty.FloatyService;
|
||||
import com.stardust.util.UiHandler;
|
||||
import com.stardust.util.ViewUtil;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
/**
|
||||
@@ -31,7 +29,7 @@ public class Floaty {
|
||||
private JsLayoutInflater mJsLayoutInflater;
|
||||
private Context mContext;
|
||||
private UiHandler mUiHandler;
|
||||
private Set<JsFloatyWindow> mWindows = new CopyOnWriteArraySet<>();
|
||||
private CopyOnWriteArraySet<JsFloatyWindow> mWindows = new CopyOnWriteArraySet<>();
|
||||
private ScriptRuntime mRuntime;
|
||||
|
||||
public Floaty(UiHandler uiHandler, UI ui, ScriptRuntime runtime) {
|
||||
@@ -73,7 +71,7 @@ public class Floaty {
|
||||
public class JsFloatyWindow {
|
||||
|
||||
private View mView;
|
||||
private FloatyWindow mWindow;
|
||||
private volatile FloatyWindow mWindow;
|
||||
private boolean mExitOnClose = false;
|
||||
|
||||
public JsFloatyWindow(View view) {
|
||||
@@ -109,31 +107,29 @@ public class Floaty {
|
||||
}
|
||||
|
||||
public void setSize(int w, int h) {
|
||||
runWithWindow(() -> ViewUtil.setViewMeasure(mWindow.getRootView(), w, h));
|
||||
}
|
||||
|
||||
private void runWithWindow(Runnable r) {
|
||||
if (mWindow == null)
|
||||
return;
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
ViewUtil.setViewMeasure(mWindow.getRootView(), w, h);
|
||||
// mWindow.getWindowBridge().updateMeasure(w, h);
|
||||
} else {
|
||||
mUiHandler.post(() -> {
|
||||
ViewUtil.setViewMeasure(mWindow.getRootView(), w, h);
|
||||
// mWindow.getWindowBridge().updateMeasure(w, h);
|
||||
});
|
||||
r.run();
|
||||
return;
|
||||
}
|
||||
mUiHandler.post(() -> {
|
||||
if (mWindow == null)
|
||||
return;
|
||||
r.run();
|
||||
});
|
||||
}
|
||||
|
||||
public void setPosition(int x, int y) {
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
mWindow.getWindowBridge().updatePosition(x, y);
|
||||
} else {
|
||||
mUiHandler.post(() -> mWindow.getWindowBridge().updatePosition(x, y));
|
||||
}
|
||||
runWithWindow(() -> mWindow.getWindowBridge().updatePosition(x, y));
|
||||
}
|
||||
|
||||
public void setAdjustEnabled(boolean enabled) {
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
mWindow.setAdjustEnabled(enabled);
|
||||
} else {
|
||||
mUiHandler.post(() -> mWindow.setAdjustEnabled(enabled));
|
||||
}
|
||||
runWithWindow(() -> mWindow.setAdjustEnabled(enabled));
|
||||
}
|
||||
|
||||
public boolean isAdjustEnabled() {
|
||||
@@ -145,17 +141,16 @@ public class Floaty {
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (!mWindows.remove(this)) {
|
||||
if (mWindow == null || !mWindows.remove(this)) {
|
||||
return;
|
||||
}
|
||||
if (Looper.myLooper() == Looper.getMainLooper()) {
|
||||
runWithWindow(() -> {
|
||||
mWindow.close();
|
||||
} else {
|
||||
mUiHandler.post(() -> mWindow.close());
|
||||
}
|
||||
if (mExitOnClose) {
|
||||
mRuntime.exit();
|
||||
}
|
||||
mWindow = null;
|
||||
if (mExitOnClose) {
|
||||
mRuntime.exit();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user