fix(floaty): bugs of floaty's view inflation

This commit is contained in:
hyb1996
2018-03-31 20:55:33 +08:00
parent 3dffb02bad
commit e3335f4b79
3 changed files with 34 additions and 26 deletions

View File

@@ -22,7 +22,7 @@ module.exports = function(runtime, global){
if(typeof(value) == 'undefined'){ if(typeof(value) == 'undefined'){
value = floaty.__view_cache__[name]; value = floaty.__view_cache__[name];
if(!value){ if(!value){
value = window.getView(name); value = window.findView(name);
if(value){ if(value){
value = ui.__decorate__(value); value = ui.__decorate__(value);
floaty.__view_cache__[name] = value; floaty.__view_cache__[name] = value;

View File

@@ -1,9 +1,9 @@
package com.stardust.autojs.core.floaty; package com.stardust.autojs.core.floaty;
import android.content.Context;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.view.Gravity; import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager; import android.view.WindowManager;
@@ -23,6 +23,12 @@ import com.stardust.enhancedfloaty.gesture.ResizeGesture;
public class FloatyWindow extends ResizableFloatyWindow { public class FloatyWindow extends ResizableFloatyWindow {
public interface ViewSupplier {
View inflate(Context context, ViewGroup parent);
}
private final Object mLock = new Object(); private final Object mLock = new Object();
private boolean mCreated = false; private boolean mCreated = false;
private View mCloseButton; private View mCloseButton;
@@ -37,8 +43,8 @@ public class FloatyWindow extends ResizableFloatyWindow {
private MyFloaty mFloaty; private MyFloaty mFloaty;
public FloatyWindow(View view) { public FloatyWindow(Context context, ViewSupplier viewSupplier) {
this(new MyFloaty(view)); this(new MyFloaty(context, viewSupplier));
} }
private FloatyWindow(MyFloaty floaty) { private FloatyWindow(MyFloaty floaty) {
@@ -162,18 +168,20 @@ public class FloatyWindow extends ResizableFloatyWindow {
private static class MyFloaty implements ResizableFloaty { private static class MyFloaty implements ResizableFloaty {
private View mContentView; private ViewSupplier mContentViewSupplier;
private View mRootView; private View mRootView;
private Context mContext;
public MyFloaty(Context context, ViewSupplier supplier) {
public MyFloaty(View view) { mContentViewSupplier = supplier;
mContentView = view; mContext = context;
} }
@Override @Override
public View inflateView(FloatyService floatyService, ResizableFloatyWindow resizableFloatyWindow) { public View inflateView(FloatyService floatyService, ResizableFloatyWindow resizableFloatyWindow) {
mRootView = View.inflate(mContentView.getContext(), R.layout.floaty_window, null); mRootView = View.inflate(mContext, R.layout.floaty_window, null);
((FrameLayout) mRootView.findViewById(R.id.container)).addView(mContentView); FrameLayout container = (FrameLayout) mRootView.findViewById(R.id.container);
View contentView = mContentViewSupplier.inflate(mContext, container);
return mRootView; return mRootView;
} }

View File

@@ -5,6 +5,7 @@ import android.content.Intent;
import android.os.Looper; import android.os.Looper;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import com.stardust.autojs.R; import com.stardust.autojs.R;
import com.stardust.autojs.core.floaty.FloatyWindow; import com.stardust.autojs.core.floaty.FloatyWindow;
@@ -40,7 +41,14 @@ public class Floaty {
} }
public JsFloatyWindow window(String xml) { 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) { public JsFloatyWindow window(View view) {
@@ -49,7 +57,7 @@ public class Floaty {
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new ScriptInterruptedException(); throw new ScriptInterruptedException();
} }
JsFloatyWindow window = new JsFloatyWindow(view); JsFloatyWindow window = new JsFloatyWindow((context, parent) -> view);
addWindow(window); addWindow(window);
return window; return window;
} }
@@ -62,16 +70,6 @@ public class Floaty {
return mWindows.remove(window); return mWindows.remove(window);
} }
private View inflate(String xml) {
if (Looper.getMainLooper() == Looper.myLooper()) {
return mLayoutInflater.inflate(xml);
} else {
VolatileDispose<View> dispose = new VolatileDispose<>();
mUiHandler.post(() -> dispose.setAndNotify(mLayoutInflater.inflate(xml)));
return dispose.blockedGetOrThrow(ScriptInterruptedException.class);
}
}
public synchronized void closeAll() { public synchronized void closeAll() {
for (JsFloatyWindow window : mWindows) { for (JsFloatyWindow window : mWindows) {
window.close(false); window.close(false);
@@ -85,8 +83,11 @@ public class Floaty {
private volatile FloatyWindow mWindow; private volatile FloatyWindow mWindow;
private boolean mExitOnClose = false; private boolean mExitOnClose = false;
public JsFloatyWindow(View view) { public JsFloatyWindow(FloatyWindow.ViewSupplier supplier) {
mWindow = new FloatyWindow(view); mWindow = new FloatyWindow(mContext, (context, parent) -> {
mView = supplier.inflate(context, parent);
return mView;
});
mUiHandler.post(() -> { mUiHandler.post(() -> {
mUiHandler.getContext().startService(new Intent(mUiHandler.getContext(), FloatyService.class)); mUiHandler.getContext().startService(new Intent(mUiHandler.getContext(), FloatyService.class));
FloatyService.addWindow(mWindow); FloatyService.addWindow(mWindow);
@@ -94,10 +95,9 @@ public class Floaty {
mWindow.waitFor(); mWindow.waitFor();
mWindow.setOnCloseButtonClickListener(v -> close()); mWindow.setOnCloseButtonClickListener(v -> close());
//setSize(mWindow.getWindowBridge().getScreenWidth() / 2, mWindow.getWindowBridge().getScreenHeight() / 2); //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); return JsViewHelper.findViewByStringId(mView, id);
} }