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 notifyValueChanged(int viewId, String value)
255 {
256 m_accessibilityDelegate.notifyValueChanged(viewId, value);
257 }
258
259 @Override public void notifyDescriptionOrNameChanged(int viewId, String value)
260 {
261 m_accessibilityDelegate.notifyDescriptionOrNameChanged(viewId, value);
262 }
263
264 @Override
265 public void notifyScrolledEvent(int viewId)
266 {
267 m_accessibilityDelegate.notifyScrolledEvent(viewId);
268 }
269
270 @Override
271 public void notifyAnnouncementEvent(int viewId, String message)
272 {
273 m_accessibilityDelegate.notifyAnnouncementEvent(viewId, message);
274 }
275
276 @Override
277 public void notifyTextChanged(int viewId, String text, String beforeText,
278 int fromIndex, int addedCount, int removedCount)
279 {
280 m_accessibilityDelegate.notifyTextChanged(viewId, text, beforeText,
281 fromIndex, addedCount, removedCount);
282 }
283
284 // QtMenuInterface implementation begin
285 @Override
286 public void resetOptionsMenu()
287 {
288 QtNative.runAction(m_activity::invalidateOptionsMenu);
289 }
290
291 @Override
292 public void openOptionsMenu()
293 {
294 QtNative.runAction(m_activity::openOptionsMenu);
295 }
296
297 @Override
298 public void closeContextMenu()
299 {
300 QtNative.runAction(m_activity::closeContextMenu);
301 }
302
303 @Override
304 public void openContextMenu(final int x, final int y, final int w, final int h)
305 {
306 if (m_layout == null) {
307 Log.e(QtTAG, "Unable to open context menu with a null layout");
308 return;
309 }
310
311 m_layout.postDelayed(() -> {
312 if (m_layout == null) {
313 Log.w(QtTAG, "Unable to open context menu on null layout");
314 return;
315 }
316 final QtEditText focusedEditText = m_inputDelegate.getCurrentQtEditText();
317 if (focusedEditText == null) {
318 Log.w(QtTAG, "No focused view when trying to open context menu");
319 return;
320 }
321 m_layout.setLayoutParams(focusedEditText, new QtLayout.LayoutParams(w, h, x, y), false);
322 focusedEditText.requestLayout();
323 focusedEditText.getViewTreeObserver().addOnGlobalLayoutListener(
324 new ViewTreeObserver.OnGlobalLayoutListener() {
325 @Override
326 public void onGlobalLayout() {
327 focusedEditText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
328 PopupMenu popup = new PopupMenu(m_activity, focusedEditText);
329 QtActivityDelegate.this.onCreatePopupMenu(popup.getMenu());
330 popup.setOnMenuItemClickListener(m_activity::onContextItemSelected);
331 popup.setOnDismissListener(popupMenu ->
332 m_activity.onContextMenuClosed(popupMenu.getMenu()));
333 popup.show();
334 }
335 });
336 }, 100);
337 }
338 // QtMenuInterface implementation end
339
340 void onCreatePopupMenu(Menu menu)
341 {
342 QtNative.fillContextMenu(menu);
343 setContextMenuVisible(true);
344 }
345
346 @Override
347 void setActionBarVisibility(boolean visible)
348 {
349 if (m_activity.getActionBar() == null)
350 return;
351 if (ViewConfiguration.get(m_activity).hasPermanentMenuKey() || !visible)
352 m_activity.getActionBar().hide();
353 else
354 m_activity.getActionBar().show();
355 }
356
357 @UsedFromNativeCode
358 @Override
359 public void addTopLevelWindow(final QtWindow window)
360 {
361 if (m_layout == null || window == null)
362 return;
363
364 QtNative.runAction(()-> {
365 if (m_layout == null)
366 return;
367
368 if (m_topLevelWindows.isEmpty()) {
369 if (m_dummyView != null) {
370 m_layout.removeView(m_dummyView);
371 m_dummyView = null;
372 }
373 }
374
375 m_layout.addView(window, m_topLevelWindows.size());
376 m_topLevelWindows.put(window.getId(), window);
377 if (!m_splashScreenSticky)
378 hideSplashScreen();
379 });
380 }
381
382 @UsedFromNativeCode
383 @Override
384 public void removeTopLevelWindow(final int id)
385 {
386 QtNative.runAction(()-> {
387 if (m_topLevelWindows.containsKey(id)) {
388 QtWindow window = m_topLevelWindows.remove(id);
389 window.setOnApplyWindowInsetsListener(null); // Set in QtWindow for safe margins
390 if (m_topLevelWindows.isEmpty()) {
391 // Keep last frame in stack until it is replaced to get correct
392 // shutdown transition
393 m_dummyView = window;
394 } else if (m_layout != null) {
395 m_layout.removeView(window);
396 }
397 }
398 });
399 }
400
401 @UsedFromNativeCode
402 @Override
403 public void bringChildToFront(final int id)
404 {
405 if (m_layout != null) {
406 QtNative.runAction(() -> {
407 QtWindow window = m_topLevelWindows.get(id);
408 if (window != null && m_layout != null)
409 m_layout.moveChild(window, m_topLevelWindows.size() - 1);
410 });
411 }
412 }
413
414 @UsedFromNativeCode
415 @Override
416 public void bringChildToBack(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, 0);
423 });
424 }
425 }
426
427 private void setActivityBackgroundDrawable()
428 {
429 TypedValue attr = new TypedValue();
430 m_activity.getTheme().resolveAttribute(android.R.attr.windowBackground,
431 attr, true);
432 Drawable backgroundDrawable;
433 if (attr.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
434 attr.type <= TypedValue.TYPE_LAST_COLOR_INT) {
435 backgroundDrawable = new ColorDrawable(attr.data);
436 } else {
437 backgroundDrawable = m_activity.getResources().
438 getDrawable(attr.resourceId, m_activity.getTheme());
439 }
440
441 m_activity.getWindow().setBackgroundDrawable(backgroundDrawable);
442 }
443}
void notifyDescriptionOrNameChanged(uint accessibilityObjectId)
void notifyObjectShow(uint accessibilityObjectId)
void notifyLocationChange(uint accessibilityObjectId)
void notifyObjectFocus(uint accessibilityObjectId)
void notifyObjectHide(uint accessibilityObjectId)
void notifyValueChanged(uint accessibilityObjectId)
void notifyAnnouncementEvent(uint accessibilityObjectId, const QString &message)
void notifyScrolledEvent(uint accessiblityObjectId)
self window
[2]
EGLOutputLayerEXT EGLint EGLAttrib value
[3]
GLint GLint GLint GLint GLint x
GLfloat GLfloat GLfloat w
[0]
GLboolean r
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
GLint location
GLuint GLsizei const GLchar * message
GLint y
GLfloat GLfloat GLfloat GLfloat h
QMenu menu
[5]
QHostInfo info
[0]