Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QtActivityDelegate.java
Go to the documentation of this file.
1// Copyright (C) 2017 BogDan Vatra <bogdan@kde.org>
2// Copyright (C) 2023 The Qt Company Ltd.
3// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5
6package org.qtproject.qt.android;
7
8import android.app.Activity;
9import android.content.pm.ActivityInfo;
10import android.content.pm.PackageManager;
11import android.content.res.Configuration;
12import android.graphics.drawable.ColorDrawable;
13import android.graphics.drawable.Drawable;
14import android.graphics.Rect;
15import android.util.DisplayMetrics;
16import android.util.Log;
17import android.util.TypedValue;
18import android.view.ViewTreeObserver;
19import android.view.animation.AccelerateInterpolator;
20import android.view.animation.AlphaAnimation;
21import android.view.animation.Animation;
22import android.view.Menu;
23import android.view.View;
24import android.view.ViewConfiguration;
25import android.view.ViewGroup;
26import android.widget.ImageView;
27import android.widget.PopupMenu;
28
29import java.util.HashMap;
30
31class QtActivityDelegate extends QtActivityDelegateBase
33 QtNative.AppStateDetailsListener
34{
35 private static final String QtTAG = "QtActivityDelegate";
36
37 private QtRootLayout m_layout = null;
38 private ImageView m_splashScreen = null;
39 private boolean m_splashScreenSticky = false;
40 private boolean m_backendsRegistered = false;
41
42 private View m_dummyView = null;
43
44
45 QtActivityDelegate(Activity activity)
46 {
47 super(activity);
48 }
49
51 void initMembers()
52 {
53 super.initMembers();
54 setActionBarVisibility(false);
55 setActivityBackgroundDrawable();
56 if (QtNativeAccessibility.accessibilitySupported())
57 m_accessibilityDelegate.initLayoutAccessibility(m_layout);
58 }
59
60 void registerBackends()
61 {
62 if (m_backendsRegistered || BackendRegister.isNull())
63 return;
64
65 m_backendsRegistered = true;
66 BackendRegister.registerBackend(QtWindowInterface.class, QtActivityDelegate.this);
67 BackendRegister.registerBackend(QtAccessibilityInterface.class, QtActivityDelegate.this);
68 BackendRegister.registerBackend(QtMenuInterface.class, QtActivityDelegate.this);
69 BackendRegister.registerBackend(QtInputInterface.class, m_inputDelegate);
70 }
71
72 void unregisterBackends()
73 {
74 if (!m_backendsRegistered)
75 return;
76
77 m_backendsRegistered = false;
78
79 if (BackendRegister.isNull())
80 return;
81
82 BackendRegister.unregisterBackend(QtWindowInterface.class);
83 BackendRegister.unregisterBackend(QtAccessibilityInterface.class);
84 BackendRegister.unregisterBackend(QtMenuInterface.class);
85 BackendRegister.unregisterBackend(QtInputInterface.class);
86 }
87
89 final public void onAppStateDetailsChanged(QtNative.ApplicationStateDetails details) {
90 if (details.isStarted)
91 registerBackends();
92 else
93 unregisterBackends();
94 }
95
97 void startNativeApplicationImpl(String appParams, String mainLib)
98 {
99 if (m_layout == null) {
100 Log.e(QtTAG, "Unable to start native application with a null layout");
101 return;
102 }
103
104 m_layout.getViewTreeObserver().addOnGlobalLayoutListener(
105 new ViewTreeObserver.OnGlobalLayoutListener() {
106 @Override
107 public void onGlobalLayout() {
108 if (m_layout != null) {
109 QtNative.startApplication(appParams, mainLib);
110 m_layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
111 }
112 }
113 });
114 }
115
116 @Override
117 protected void setUpLayout()
118 {
119 // This should be assigned only once, otherwise, we'd have to check
120 // for null everywhere including before and inside runAction() Runnables.
121 m_layout = new QtRootLayout(m_activity);
122
123 int orientation = m_activity.getResources().getConfiguration().orientation;
124 setUpSplashScreen(orientation);
125 m_activity.registerForContextMenu(m_layout);
126 ViewGroup.LayoutParams rootParams = new ViewGroup.LayoutParams(
127 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
128 m_activity.setContentView(m_layout, rootParams);
129
130 handleUiModeChange();
131
132 m_displayManager.initDisplayProperties();
133
134 m_layout.getViewTreeObserver().addOnPreDrawListener(() -> {
135 if (!m_inputDelegate.isKeyboardVisible())
136 return true;
137
138 Rect r = new Rect();
139 m_activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
140 DisplayMetrics metrics = new DisplayMetrics();
141 QtDisplayManager.getDisplay(m_activity).getMetrics(metrics);
142 final int kbHeight = metrics.heightPixels - r.bottom;
143 if (kbHeight < 0) {
144 m_inputDelegate.setKeyboardVisibility(false, System.nanoTime());
145 return true;
146 }
147 final int[] location = new int[2];
148 m_layout.getLocationOnScreen(location);
149 QtInputDelegate.keyboardGeometryChanged(location[0], r.bottom - location[1],
150 r.width(), kbHeight);
151 return true;
152 });
153 }
154
155 @Override
156 protected void setUpSplashScreen(int orientation)
157 {
158 if (m_layout == null) {
159 Log.e(QtTAG, "Unable to setup splash screen with a null layout");
160 return;
161 }
162
163 try {
164 ActivityInfo info = m_activity.getPackageManager().getActivityInfo(
165 m_activity.getComponentName(),
166 PackageManager.GET_META_DATA);
167
168 String splashScreenKey = "android.app.splash_screen_drawable_"
169 + (orientation == Configuration.ORIENTATION_LANDSCAPE ? "landscape" : "portrait");
170 if (!info.metaData.containsKey(splashScreenKey))
171 splashScreenKey = "android.app.splash_screen_drawable";
172
173 if (info.metaData.containsKey(splashScreenKey)) {
174 m_splashScreenSticky =
175 info.metaData.containsKey("android.app.splash_screen_sticky") &&
176 info.metaData.getBoolean("android.app.splash_screen_sticky");
177
178 int id = info.metaData.getInt(splashScreenKey);
179 m_splashScreen = new ImageView(m_activity);
180 m_splashScreen.setImageDrawable(m_activity.getResources().getDrawable(
181 id, m_activity.getTheme()));
182 m_splashScreen.setScaleType(ImageView.ScaleType.FIT_XY);
183 m_splashScreen.setLayoutParams(new ViewGroup.LayoutParams(
184 ViewGroup.LayoutParams.MATCH_PARENT,
185 ViewGroup.LayoutParams.MATCH_PARENT));
186 m_layout.addView(m_splashScreen);
187 }
188 } catch (Exception e) {
189 e.printStackTrace();
190 }
191 }
192
193 @Override
194 protected void hideSplashScreen(final int duration)
195 {
196 QtNative.runAction(() -> {
197 if (m_splashScreen == null)
198 return;
199
200 if (m_layout != null && duration <= 0) {
201 m_layout.removeView(m_splashScreen);
202 m_splashScreen = null;
203 return;
204 }
205
206 final Animation fadeOut = new AlphaAnimation(1, 0);
207 fadeOut.setInterpolator(new AccelerateInterpolator());
208 fadeOut.setDuration(duration);
209
210 fadeOut.setAnimationListener(new Animation.AnimationListener() {
211 @Override
212 public void onAnimationEnd(Animation animation) {
213 hideSplashScreen(0);
214 }
215
216 @Override
217 public void onAnimationRepeat(Animation animation) {
218 }
219
220 @Override
221 public void onAnimationStart(Animation animation) {
222 }
223 });
224
225 m_splashScreen.startAnimation(fadeOut);
226 });
227 }
228
229 @Override
230 public void notifyLocationChange(int viewId)
231 {
232 m_accessibilityDelegate.notifyLocationChange(viewId);
233 }
234
235 @Override
236 public void notifyObjectHide(int viewId, int parentId)
237 {
238 m_accessibilityDelegate.notifyObjectHide(viewId, parentId);
239 }
240
241 @Override
242 public void notifyObjectShow(int parentId)
243 {
244 m_accessibilityDelegate.notifyObjectShow(parentId);
245 }
246
247 @Override
248 public void notifyObjectFocus(int viewId)
249 {
250 m_accessibilityDelegate.notifyObjectFocus(viewId);
251 }
252
253 @Override
254 public void notifyObjectDestroyed(int viewId, int parentId)
255 {
256 m_accessibilityDelegate.notifyObjectDestroyed(viewId, parentId);
257 }
258
259 @Override
260 public void notifyObjectCreated(int parentId)
261 {
262 m_accessibilityDelegate.notifyObjectCreated(parentId);
263 }
264
265 @Override
266 public void notifyValueChanged(int viewId)
267 {
268 m_accessibilityDelegate.notifyValueChanged(viewId);
269 }
270
271 @Override
272 public void notifyDescriptionOrNameChanged(int viewId)
273 {
274 m_accessibilityDelegate.notifyDescriptionOrNameChanged(viewId);
275 }
276
277 @Override
278 public void notifyScrolledEvent(int viewId)
279 {
280 m_accessibilityDelegate.notifyScrolledEvent(viewId);
281 }
282
283 @Override
284 public void notifyAnnouncementEvent(int viewId, String message)
285 {
286 m_accessibilityDelegate.notifyAnnouncementEvent(viewId, message);
287 }
288
289 @Override
290 public void notifyTextChanged(int viewId, String text, String beforeText,
291 int fromIndex, int addedCount, int removedCount)
292 {
293 m_accessibilityDelegate.notifyTextChanged(viewId, text, beforeText,
294 fromIndex, addedCount, removedCount);
295 }
296
297 // QtMenuInterface implementation begin
298 @Override
299 public void resetOptionsMenu()
300 {
301 QtNative.runAction(m_activity::invalidateOptionsMenu);
302 }
303
304 @Override
305 public void openOptionsMenu()
306 {
307 QtNative.runAction(m_activity::openOptionsMenu);
308 }
309
310 @Override
311 public void closeContextMenu()
312 {
313 QtNative.runAction(m_activity::closeContextMenu);
314 }
315
316 @Override
317 public void openContextMenu(final int x, final int y, final int w, final int h)
318 {
319 if (m_layout == null) {
320 Log.e(QtTAG, "Unable to open context menu with a null layout");
321 return;
322 }
323
324 m_layout.postDelayed(() -> {
325 if (m_layout == null) {
326 Log.w(QtTAG, "Unable to open context menu on null layout");
327 return;
328 }
329 final QtEditText focusedEditText = m_inputDelegate.getCurrentQtEditText();
330 if (focusedEditText == null) {
331 Log.w(QtTAG, "No focused view when trying to open context menu");
332 return;
333 }
334 m_layout.setLayoutParams(focusedEditText, new QtLayout.LayoutParams(w, h, x, y), false);
335 focusedEditText.requestLayout();
336 focusedEditText.getViewTreeObserver().addOnGlobalLayoutListener(
337 new ViewTreeObserver.OnGlobalLayoutListener() {
338 @Override
339 public void onGlobalLayout() {
340 focusedEditText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
341 PopupMenu popup = new PopupMenu(m_activity, focusedEditText);
342 QtActivityDelegate.this.onCreatePopupMenu(popup.getMenu());
343 popup.setOnMenuItemClickListener(m_activity::onContextItemSelected);
344 popup.setOnDismissListener(popupMenu ->
345 m_activity.onContextMenuClosed(popupMenu.getMenu()));
346 popup.show();
347 }
348 });
349 }, 100);
350 }
351 // QtMenuInterface implementation end
352
353 void onCreatePopupMenu(Menu menu)
354 {
355 QtNative.fillContextMenu(menu);
356 setContextMenuVisible(true);
357 }
358
359 @Override
360 void setActionBarVisibility(boolean visible)
361 {
362 if (m_activity.getActionBar() == null)
363 return;
364 if (ViewConfiguration.get(m_activity).hasPermanentMenuKey() || !visible)
365 m_activity.getActionBar().hide();
366 else
367 m_activity.getActionBar().show();
368 }
369
370 @UsedFromNativeCode
371 @Override
372 public void addTopLevelWindow(final QtWindow window)
373 {
374 if (m_layout == null || window == null)
375 return;
376
377 QtNative.runAction(()-> {
378 if (m_layout == null)
379 return;
380
381 if (m_topLevelWindows.isEmpty()) {
382 if (m_dummyView != null) {
383 m_layout.removeView(m_dummyView);
384 m_dummyView = null;
385 }
386 }
387
388 m_layout.addView(window, m_topLevelWindows.size());
389 m_topLevelWindows.put(window.getId(), window);
390 if (!m_splashScreenSticky)
391 hideSplashScreen();
392 });
393 }
394
395 @UsedFromNativeCode
396 @Override
397 public void removeTopLevelWindow(final int id)
398 {
399 QtNative.runAction(()-> {
400 if (m_topLevelWindows.containsKey(id)) {
401 QtWindow window = m_topLevelWindows.remove(id);
402 window.setOnApplyWindowInsetsListener(null); // Set in QtWindow for safe margins
403 if (m_topLevelWindows.isEmpty()) {
404 // Keep last frame in stack until it is replaced to get correct
405 // shutdown transition
406 m_dummyView = window;
407 } else if (m_layout != null) {
408 m_layout.removeView(window);
409 }
410 }
411 });
412 }
413
414 @UsedFromNativeCode
415 @Override
416 public void bringChildToFront(final int id)
417 {
418 if (m_layout != null) {
419 QtNative.runAction(() -> {
420 QtWindow window = m_topLevelWindows.get(id);
421 if (window != null && m_layout != null)
422 m_layout.moveChild(window, m_topLevelWindows.size() - 1);
423 });
424 }
425 }
426
427 @UsedFromNativeCode
428 @Override
429 public void bringChildToBack(int id)
430 {
431 if (m_layout != null) {
432 QtNative.runAction(() -> {
433 QtWindow window = m_topLevelWindows.get(id);
434 if (window != null && m_layout != null)
435 m_layout.moveChild(window, 0);
436 });
437 }
438 }
439
440 private void setActivityBackgroundDrawable()
441 {
442 TypedValue attr = new TypedValue();
443 m_activity.getTheme().resolveAttribute(android.R.attr.windowBackground,
444 attr, true);
445 Drawable backgroundDrawable;
446 if (attr.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
447 attr.type <= TypedValue.TYPE_LAST_COLOR_INT) {
448 backgroundDrawable = new ColorDrawable(attr.data);
449 } else {
450 backgroundDrawable = m_activity.getResources().
451 getDrawable(attr.resourceId, m_activity.getTheme());
452 }
453
454 m_activity.getWindow().setBackgroundDrawable(backgroundDrawable);
455 }
456}
void notifyDescriptionOrNameChanged(uint accessibilityObjectId)
void notifyObjectShow(uint accessibilityObjectId)
void notifyLocationChange(uint accessibilityObjectId)
void notifyObjectFocus(uint accessibilityObjectId)
void notifyObjectDestroyed(uint accessibilityObjectId)
void notifyObjectCreated(uint accessibilityObjectId)
void notifyObjectHide(uint accessibilityObjectId)
void notifyValueChanged(uint accessibilityObjectId)
void notifyAnnouncementEvent(uint accessibilityObjectId, const QString &message)
void notifyScrolledEvent(uint accessiblityObjectId)
self window
[2]
GLuint GLsizei const GLchar * message
GLint GLint GLint GLint GLint x
GLfloat GLfloat GLfloat w
[0]
GLboolean r
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
GLint location
GLint y
GLfloat GLfloat GLfloat GLfloat h
QMenu menu
[5]
QHostInfo info
[0]