diff --git a/autojs/src/main/assets/modules/__floaty__.js b/autojs/src/main/assets/modules/__floaty__.js index 88db8b91..2fc1ddb3 100644 --- a/autojs/src/main/assets/modules/__floaty__.js +++ b/autojs/src/main/assets/modules/__floaty__.js @@ -22,7 +22,7 @@ module.exports = function(runtime, global){ if(typeof(value) == 'undefined'){ value = floaty.__view_cache__[name]; if(!value){ - value = window.getView(name); + value = window.findView(name); if(value){ value = ui.__decorate__(value); floaty.__view_cache__[name] = value; diff --git a/autojs/src/main/java/com/stardust/autojs/core/floaty/FloatyWindow.java b/autojs/src/main/java/com/stardust/autojs/core/floaty/FloatyWindow.java index 039c1dd4..d15d596b 100644 --- a/autojs/src/main/java/com/stardust/autojs/core/floaty/FloatyWindow.java +++ b/autojs/src/main/java/com/stardust/autojs/core/floaty/FloatyWindow.java @@ -1,9 +1,9 @@ package com.stardust.autojs.core.floaty; +import android.content.Context; import android.graphics.PixelFormat; import android.support.annotation.Nullable; import android.view.Gravity; -import android.view.KeyEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; @@ -23,6 +23,12 @@ import com.stardust.enhancedfloaty.gesture.ResizeGesture; public class FloatyWindow extends ResizableFloatyWindow { + public interface ViewSupplier { + + View inflate(Context context, ViewGroup parent); + + } + private final Object mLock = new Object(); private boolean mCreated = false; private View mCloseButton; @@ -37,8 +43,8 @@ public class FloatyWindow extends ResizableFloatyWindow { private MyFloaty mFloaty; - public FloatyWindow(View view) { - this(new MyFloaty(view)); + public FloatyWindow(Context context, ViewSupplier viewSupplier) { + this(new MyFloaty(context, viewSupplier)); } private FloatyWindow(MyFloaty floaty) { @@ -162,18 +168,20 @@ public class FloatyWindow extends ResizableFloatyWindow { private static class MyFloaty implements ResizableFloaty { - private View mContentView; + private ViewSupplier mContentViewSupplier; private View mRootView; + private Context mContext; - - public MyFloaty(View view) { - mContentView = view; + public MyFloaty(Context context, ViewSupplier supplier) { + mContentViewSupplier = supplier; + mContext = context; } @Override public View inflateView(FloatyService floatyService, ResizableFloatyWindow resizableFloatyWindow) { - mRootView = View.inflate(mContentView.getContext(), R.layout.floaty_window, null); - ((FrameLayout) mRootView.findViewById(R.id.container)).addView(mContentView); + mRootView = View.inflate(mContext, R.layout.floaty_window, null); + FrameLayout container = (FrameLayout) mRootView.findViewById(R.id.container); + View contentView = mContentViewSupplier.inflate(mContext, container); return mRootView; } diff --git a/autojs/src/main/java/com/stardust/autojs/runtime/api/Floaty.java b/autojs/src/main/java/com/stardust/autojs/runtime/api/Floaty.java index e8b89347..9fa072ed 100644 --- a/autojs/src/main/java/com/stardust/autojs/runtime/api/Floaty.java +++ b/autojs/src/main/java/com/stardust/autojs/runtime/api/Floaty.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.os.Looper; import android.view.ContextThemeWrapper; import android.view.View; +import android.view.ViewGroup; import com.stardust.autojs.R; import com.stardust.autojs.core.floaty.FloatyWindow; @@ -40,7 +41,14 @@ public class Floaty { } public JsFloatyWindow window(String xml) { - return window(inflate(xml)); + try { + FloatingPermission.waitForPermissionGranted(mContext); + } catch (InterruptedException e) { + throw new ScriptInterruptedException(); + } + JsFloatyWindow window = new JsFloatyWindow((context, parent) -> mLayoutInflater.inflate(xml, parent)); + addWindow(window); + return window; } public JsFloatyWindow window(View view) { @@ -49,7 +57,7 @@ public class Floaty { } catch (InterruptedException e) { throw new ScriptInterruptedException(); } - JsFloatyWindow window = new JsFloatyWindow(view); + JsFloatyWindow window = new JsFloatyWindow((context, parent) -> view); addWindow(window); return window; } @@ -62,16 +70,6 @@ public class Floaty { return mWindows.remove(window); } - private View inflate(String xml) { - if (Looper.getMainLooper() == Looper.myLooper()) { - return mLayoutInflater.inflate(xml); - } else { - VolatileDispose dispose = new VolatileDispose<>(); - mUiHandler.post(() -> dispose.setAndNotify(mLayoutInflater.inflate(xml))); - return dispose.blockedGetOrThrow(ScriptInterruptedException.class); - } - } - public synchronized void closeAll() { for (JsFloatyWindow window : mWindows) { window.close(false); @@ -85,8 +83,11 @@ public class Floaty { private volatile FloatyWindow mWindow; private boolean mExitOnClose = false; - public JsFloatyWindow(View view) { - mWindow = new FloatyWindow(view); + public JsFloatyWindow(FloatyWindow.ViewSupplier supplier) { + mWindow = new FloatyWindow(mContext, (context, parent) -> { + mView = supplier.inflate(context, parent); + return mView; + }); mUiHandler.post(() -> { mUiHandler.getContext().startService(new Intent(mUiHandler.getContext(), FloatyService.class)); FloatyService.addWindow(mWindow); @@ -94,10 +95,9 @@ public class Floaty { mWindow.waitFor(); mWindow.setOnCloseButtonClickListener(v -> close()); //setSize(mWindow.getWindowBridge().getScreenWidth() / 2, mWindow.getWindowBridge().getScreenHeight() / 2); - mView = view; } - public View getView(String id) { + public View findView(String id) { return JsViewHelper.findViewByStringId(mView, id); }