4package org.qtproject.qt.android;
6import android.annotation.SuppressLint;
7import android.app.ActionBar;
8import android.app.Activity;
9import android.content.Context;
10import android.content.res.TypedArray;
11import android.graphics.Insets;
13import android.view.DisplayCutout;
14import android.view.GestureDetector;
15import android.view.MotionEvent;
16import android.view.Surface;
17import android.view.View;
18import android.view.ViewGroup;
19import android.view.ViewTreeObserver;
20import android.view.Window;
21import android.view.WindowInsets;
22import android.view.WindowManager;
23import android.view.WindowInsetsController;
24import android.os.Build;
26import java.util.HashMap;
27import java.util.concurrent.ConcurrentLinkedQueue;
29@SuppressLint(
"ViewConstructor")
32 private View m_surfaceContainer;
34 private volatile Surface m_surface;
35 private boolean m_nativeSurfaceRetired =
false;
36 private final ConcurrentLinkedQueue<Runnable> m_pendingSurfaceReleases =
new ConcurrentLinkedQueue<>();
37 private View m_nativeView;
38 private final HashMap<Integer, QtWindow> m_childWindows =
new HashMap<>();
39 private QtWindow m_parentWindow;
40 private GestureDetector m_gestureDetector;
41 private final QtEditText m_editText;
42 private boolean m_editTextFocusInitialized =
false;
44 private boolean m_firstSafeMarginsDelivered =
false;
45 private WindowInsets m_pendingSafeAreaInsets;
46 private int m_actionBarHeight = -1;
48 private static native
void surfaceCreated(
int windowId, Surface surface);
49 private static native
void surfaceRedrawNeeded(
int windowId, Runnable drawingFinished);
50 private static native
void surfaceDestroyed(
int windowId, Runnable surfaceReleased);
51 private static native
void safeAreaMarginsChanged(Insets insets,
int id);
52 static native
void windowFocusChanged(
boolean hasFocus,
int id);
53 static native
void updateWindows();
55 QtWindow(
Context context,
boolean isForeignWindow, QtWindow parentWindow,
56 QtInputConnection.QtInputConnectionListener listener)
59 m_inputConnectionListener = listener;
61 setFocusableInTouchMode(
true);
62 setDefaultFocusHighlightEnabled(
false);
63 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
67 setVisibility(View.INVISIBLE);
69 if (!isForeignWindow && context instanceof Activity) {
71 m_editText =
new QtEditText(context, listener);
72 m_editText.setFocusable(
false);
73 m_editText.setFocusableInTouchMode(
false);
74 m_editText.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
75 LayoutParams layoutParams =
new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
76 ViewGroup.LayoutParams.WRAP_CONTENT);
77 QtNative.runAction(() -> addView(m_editText, layoutParams));
84 new GestureDetector(context,
new GestureDetector.SimpleOnGestureListener() {
86 public void onLongPress(MotionEvent event) {
87 QtInputDelegate.longPress(getId(), (int) event.getX(), (int) event.getY(),
88 event.getMetaState());
91 m_gestureDetector.setIsLongpressEnabled(
true);
94 registerSafeAreaMarginsListener();
99 void reportSafeAreaMarginsFromRoot(WindowInsets freshInsets)
101 if (!isAttachedToWindow())
105 getRootView().post(() -> {
106 WindowInsets insets = freshInsets !=
null ? freshInsets : getRootWindowInsets();
108 updateSafeAreaMargins(insets);
110 for (QtWindow
child : m_childWindows.values())
111 child.reportSafeAreaMarginsFromRoot(freshInsets);
114 void registerSafeAreaMarginsListener()
119 setOnApplyWindowInsetsListener((
view, insets) -> {
120 WindowInsets windowInsets =
view.onApplyWindowInsets(insets);
121 updateSafeAreaMargins(windowInsets);
127 if (isAttachedToWindow()) {
128 WindowInsets insets = getRootWindowInsets();
129 if (insets !=
null) {
130 getRootView().post(() -> updateSafeAreaMargins(insets));
131 m_firstSafeMarginsDelivered =
true;
134 addOnAttachStateChangeListener(
new View.OnAttachStateChangeListener() {
136 public void onViewAttachedToWindow(View view) {
137 view.removeOnAttachStateChangeListener(this);
138 view.requestApplyInsets();
142 public void onViewDetachedFromWindow(View
view) {}
147 if (!m_firstSafeMarginsDelivered) {
148 ViewTreeObserver.OnPreDrawListener listener =
new ViewTreeObserver.OnPreDrawListener() {
150 public boolean onPreDraw() {
151 if (isAttachedToWindow()) {
152 WindowInsets insets = getRootWindowInsets();
153 if (insets !=
null) {
154 getViewTreeObserver().removeOnPreDrawListener(
this);
155 getRootView().post(() -> updateSafeAreaMargins(insets));
156 m_firstSafeMarginsDelivered =
true;
161 requestApplyInsets();
166 getViewTreeObserver().addOnPreDrawListener(listener);
171 protected void onLayout(
boolean changed,
int l,
int t,
int r,
int b)
175 reportSafeAreaMargins();
176 super.onLayout(changed, l,
t,
r,
b);
179 @SuppressWarnings(
"deprecation")
180 private boolean isAdjustResize()
182 if (!(getContext() instanceof Activity))
184 int mode = ((Activity) getContext()).getWindow().getAttributes().softInputMode
185 & WindowManager.LayoutParams.SOFT_INPUT_MASK_ADJUST;
186 return mode == WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
189 @SuppressWarnings(
"deprecation")
190 Insets getSafeInsets(
View view, WindowInsets insets)
192 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
193 int types = WindowInsets.Type.displayCutout() | WindowInsets.Type.systemBars();
196 if (isAdjustResize())
197 types |= WindowInsets.Type.ime();
198 return insets.getInsets(types);
202 int left = insets.getSystemWindowInsetLeft();
203 int top = insets.getSystemWindowInsetTop();
204 int right = insets.getSystemWindowInsetRight();
205 int bottom = insets.getSystemWindowInsetBottom();
209 DisplayCutout cutout = insets.getDisplayCutout();
210 if (cutout !=
null) {
211 left = Math.max(
left, cutout.getSafeInsetLeft());
212 top = Math.max(
top, cutout.getSafeInsetTop());
213 right = Math.max(
right, cutout.getSafeInsetRight());
221 ActionBar actionBar = ((Activity) getContext()).getActionBar();
222 if (actionBar ==
null || !actionBar.isShowing()) {
223 int topWithoutActionBar = top - actionBarHeight();
224 if (topWithoutActionBar > 0)
225 top = topWithoutActionBar;
231 private void updateSafeAreaMargins(WindowInsets insets)
233 if (getVisibility() !=
View.VISIBLE)
236 m_pendingSafeAreaInsets = insets;
237 if (!isLayoutRequested() || isInLayout())
238 reportSafeAreaMargins();
241 private void reportSafeAreaMargins()
243 WindowInsets insets = m_pendingSafeAreaInsets;
244 m_pendingSafeAreaInsets =
null;
245 if (getVisibility() !=
View.VISIBLE || !(getContext() instanceof QtActivityBase))
248 insets = getRootWindowInsets();
251 View rootView = getRootView();
253 int[] rootLocation =
new int[2];
254 rootView.getLocationOnScreen(rootLocation);
255 int rootX = rootLocation[0];
256 int rootY = rootLocation[1];
258 int[] windowLocation =
new int[2];
259 getLocationOnScreen(windowLocation);
260 int windowX = windowLocation[0];
261 int windowY = windowLocation[1];
264 int leftOffset = windowX - rootX;
265 int topOffset = windowY - rootY;
266 int rightOffset = (rootX + rootView.getWidth()) - (windowX + getWidth());
267 int bottomOffset = (rootY + rootView.getHeight()) - (windowY + getHeight());
270 Insets safeInsets = getSafeInsets(rootView, insets);
271 int left = Math.max(0, Math.min(safeInsets.left, safeInsets.left - leftOffset));
272 int top = Math.max(0, Math.min(safeInsets.top, safeInsets.top - topOffset));
273 int right = Math.max(0, Math.min(safeInsets.right, safeInsets.right - rightOffset));
274 int bottom = Math.max(0, Math.min(safeInsets.bottom, safeInsets.bottom - bottomOffset));
279 private int actionBarHeight()
281 if (m_actionBarHeight == -1) {
282 TypedArray ta = getContext().getTheme().obtainStyledAttributes(
283 new int[] { android.R.attr.actionBarSize });
285 m_actionBarHeight = ta.getDimensionPixelSize(0, 0);
290 return m_actionBarHeight;
294 public void onSurfaceCreated(Surface surface)
297 surfaceCreated(getId(), surface);
301 public void onSurfaceRedrawNeeded(Runnable drawingFinished)
303 surfaceRedrawNeeded(getId(), drawingFinished);
307 public void onSurfaceDestroyed(Runnable surfaceReleased)
310 if (m_nativeSurfaceRetired) {
311 surfaceReleased.run();
314 m_pendingSurfaceReleases.add(surfaceReleased);
315 surfaceDestroyed(getId(), () -> {
316 if (m_pendingSurfaceReleases.remove(surfaceReleased))
317 surfaceReleased.run();
330 public boolean onTouchEvent(MotionEvent
event)
334 if (!m_editTextFocusInitialized) {
335 m_editTextFocusInitialized =
true;
336 if (m_editText !=
null) {
337 m_editText.setFocusable(
true);
338 m_editText.setFocusableInTouchMode(
true);
342 windowFocusChanged(
true, getId());
343 if (m_editText !=
null && m_inputConnectionListener !=
null)
344 m_inputConnectionListener.onEditTextChanged(m_editText);
346 QtInputDelegate.sendTouchEvent(
event, getId());
347 if (m_gestureDetector !=
null)
348 m_gestureDetector.onTouchEvent(
event);
353 public boolean onTrackballEvent(MotionEvent
event)
355 QtInputDelegate.sendTrackballEvent(
event, getId());
360 public boolean onGenericMotionEvent(MotionEvent
event)
362 return QtInputDelegate.sendGenericMotionEvent(
event, getId());
366 protected void onAttachedToWindow()
368 super.onAttachedToWindow();
369 setOnDragListener(QtDragManager.getInstance());
373 protected void onDetachedFromWindow()
375 QtDragManager.getInstance().onSourceWindowDetached(
this);
376 setOnDragListener(
null);
377 super.onDetachedFromWindow();
383 if (m_parentWindow !=
null)
384 m_parentWindow.removeChildWindow(getId());
389 private int m_x, m_y, m_width, m_height;
390 private boolean m_visible;
391 private boolean m_needsSurface, m_surfaceOnTop, m_surfaceOpaque, m_surfaceConfigChanged;
392 private int m_surfaceContainerType;
393 private final Runnable m_applyUpdate = this::applyUpdate;
394 private boolean m_surfaceContainerShown;
397 void configureSurface(
boolean onTop,
boolean isOpaque,
int containerType)
399 synchronized (
this) {
400 final boolean configDiffers = onTop != m_surfaceOnTop || isOpaque != m_surfaceOpaque
401 || containerType != m_surfaceContainerType;
403 if (m_needsSurface && configDiffers)
404 m_surfaceConfigChanged =
true;
405 m_needsSurface =
true;
406 m_surfaceOnTop = onTop;
407 m_surfaceOpaque = isOpaque;
408 m_surfaceContainerType = containerType;
413 void updateWindow(
int x,
int y,
int width,
int height,
boolean visible)
415 synchronized (
this) {
422 removeCallbacks(m_applyUpdate);
426 private void applyUpdate()
429 final boolean visible, needsSurface, onTop, isOpaque, configChanged;
430 synchronized (
this) {
431 configChanged = m_surfaceConfigChanged;
432 m_surfaceConfigChanged =
false;
438 needsSurface = m_needsSurface;
439 onTop = m_surfaceOnTop;
440 isOpaque = m_surfaceOpaque;
441 containerType = m_surfaceContainerType;
443 if (getContext() instanceof QtActivityBase && !hasLayoutGeometry(
x,
y,
width,
height))
444 setLayoutParams(
new QtLayout.LayoutParams(
width,
height,
x,
y));
446 if (configChanged && m_surfaceContainer !=
null && !m_surfaceContainerShown) {
447 removeView(m_surfaceContainer);
448 m_surfaceContainer =
null;
452 if (needsSurface && m_surfaceContainer ==
null)
453 addSurfaceContainer(onTop, isOpaque, containerType);
454 final boolean shown = visible && getVisibility() !=
View.VISIBLE;
455 setVisibility(visible ?
View.VISIBLE :
View.INVISIBLE);
456 if (visible && m_surfaceContainer !=
null)
457 m_surfaceContainerShown =
true;
463 private boolean hasLayoutGeometry(
int x,
int y,
int w,
int h)
465 if (!(getLayoutParams() instanceof QtLayout.LayoutParams))
467 QtLayout.LayoutParams
params = (QtLayout.LayoutParams) getLayoutParams();
468 return params.x ==
x && params.y ==
y && params.width ==
w && params.height ==
h;
471 private void addSurfaceContainer(
boolean onTop,
boolean isOpaque,
int surfaceContainerType)
473 if (surfaceContainerType == 0)
474 m_surfaceContainer =
new QtSurface(getContext(), QtWindow.this, onTop, 32);
476 m_surfaceContainer =
new QtTextureView(getContext(), QtWindow.this, isOpaque);
477 m_surfaceContainer.setLayoutParams(
new QtLayout.LayoutParams(
478 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
479 m_surfaceContainerShown =
false;
482 addView(m_surfaceContainer, 0);
486 void destroySurface()
488 QtNative.runAction(this::destroySurfaceOnUiThread,
false);
491 private void destroySurfaceOnUiThread()
494 m_nativeSurfaceRetired =
true;
495 Throwable removalFailure =
null;
497 if (m_surfaceContainer !=
null) {
498 removeView(m_surfaceContainer);
499 m_surfaceContainer =
null;
501 }
catch (RuntimeException |
Error failure) {
502 removalFailure = failure;
506 releasePendingSurfaces();
507 }
catch (RuntimeException |
Error failure) {
508 if (removalFailure ==
null)
510 if (removalFailure != failure)
511 removalFailure.addSuppressed(failure);
516 private void releasePendingSurfaces()
518 Throwable firstFailure =
null;
520 while ((released = m_pendingSurfaceReleases.poll()) !=
null) {
523 }
catch (RuntimeException |
Error failure) {
524 if (firstFailure ==
null)
525 firstFailure = failure;
526 else if (firstFailure != failure)
527 firstFailure.addSuppressed(failure);
530 if (firstFailure instanceof RuntimeException)
531 throw (RuntimeException) firstFailure;
532 if (firstFailure instanceof
Error)
533 throw (
Error) firstFailure;
536 void addChildWindow(QtWindow
window)
538 QtNative.runAction(()-> {
540 addView(
window, getChildCount());
544 void removeChildWindow(
int id)
546 QtNative.runAction(()-> {
547 if (m_childWindows.containsKey(
id))
548 removeView(m_childWindows.remove(
id));
555 QtNative.runAction(()-> {
556 if (m_nativeView !=
null)
557 removeView(m_nativeView);
560 m_nativeView.setLayoutParams(
new QtLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
561 ViewGroup.LayoutParams.MATCH_PARENT));
562 addView(m_nativeView);
567 void bringChildToFront(
int id)
569 QtNative.runAction(()-> {
572 if (getChildCount() > 0)
573 moveChild(
view, getChildCount() - 1);
579 void bringChildToBack(
int id) {
580 QtNative.runAction(()-> {
589 void removeNativeView()
591 QtNative.runAction(()-> {
592 if (m_nativeView !=
null) {
593 removeView(m_nativeView);
601 if (m_parentWindow == parentWindow)
604 if (m_parentWindow !=
null)
605 m_parentWindow.removeChildWindow(getId());
607 m_parentWindow = parentWindow;
608 if (m_parentWindow !=
null)
609 m_parentWindow.addChildWindow(
this);
613 void updateFocusedEditText()
615 if (m_editText !=
null && m_inputConnectionListener !=
null)
616 m_inputConnectionListener.onEditTextChanged(m_editText);
Napi::TypedArray TypedArray
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
GLfloat GLfloat GLfloat w
[0]
GLdouble GLdouble GLdouble GLdouble top
GLfloat GLfloat GLfloat GLfloat h
file setParent(multiPart)
manager post(request, myJson, this, [this](QRestReply &reply) { if(!reply.isSuccess()) { } if(std::optional json=reply.readJson()) { } })
if(foo.startsWith("("+type+") 0x")) ... QString hello("hello")
[0]