1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
// Copyright (C) 2017 BogDan Vatra <bogdan@kde.org>
// Copyright (C) 2023 The Qt Company Ltd.
// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
package org.qtproject.qt.android;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.Rect;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.Menu;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowInsetsController;
import android.widget.ImageView;
import android.widget.PopupMenu;
import java.util.HashMap;
class QtActivityDelegate extends QtActivityDelegateBase
{
private static final String QtTAG = "QtActivityDelegate";
private QtRootLayout m_layout = null;
private ImageView m_splashScreen = null;
private boolean m_splashScreenSticky = false;
private View m_dummyView = null;
private HashMap<Integer, View> m_nativeViews = new HashMap<Integer, View>();
QtActivityDelegate(Activity activity)
{
super(activity);
setActionBarVisibility(false);
setActivityBackgroundDrawable();
}
@UsedFromNativeCode
@Override
QtLayout getQtLayout()
{
return m_layout;
}
@UsedFromNativeCode
@Override
void setSystemUiVisibility(int systemUiVisibility)
{
QtNative.runAction(() -> {
m_displayManager.setSystemUiVisibility(systemUiVisibility);
m_layout.requestLayout();
QtNative.updateWindow();
});
}
@Override
public boolean updateActivityAfterRestart(Activity activity) {
boolean updated = super.updateActivityAfterRestart(activity);
// TODO verify whether this is even needed, the last I checked the initMembers
// recreates the layout anyway
// update the new activity content view to old layout
ViewGroup layoutParent = (ViewGroup)m_layout.getParent();
if (layoutParent != null)
layoutParent.removeView(m_layout);
m_activity.setContentView(m_layout);
return updated;
}
@Override
void startNativeApplicationImpl(String appParams, String mainLib)
{
m_layout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
QtNative.startApplication(appParams, mainLib);
m_layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
@Override
protected void setUpLayout()
{
int orientation = m_activity.getResources().getConfiguration().orientation;
m_layout = new QtRootLayout(m_activity);
setUpSplashScreen(orientation);
m_activity.registerForContextMenu(m_layout);
m_activity.setContentView(m_layout,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
QtDisplayManager.handleOrientationChanges(m_activity);
handleUiModeChange(m_activity.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK);
Display display = (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)
? m_activity.getWindowManager().getDefaultDisplay()
: m_activity.getDisplay();
QtDisplayManager.handleRefreshRateChanged(QtDisplayManager.getRefreshRate(display));
m_layout.getViewTreeObserver().addOnPreDrawListener(() -> {
if (!m_inputDelegate.isKeyboardVisible())
return true;
Rect r = new Rect();
m_activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
DisplayMetrics metrics = new DisplayMetrics();
m_activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
final int kbHeight = metrics.heightPixels - r.bottom;
if (kbHeight < 0) {
m_inputDelegate.setKeyboardVisibility(false, System.nanoTime());
return true;
}
final int[] location = new int[2];
m_layout.getLocationOnScreen(location);
QtInputDelegate.keyboardGeometryChanged(location[0], r.bottom - location[1],
r.width(), kbHeight);
return true;
});
registerGlobalFocusChangeListener(m_layout);
m_inputDelegate.setEditPopupMenu(new EditPopupMenu(m_activity, m_layout));
}
@Override
protected void setUpSplashScreen(int orientation)
{
try {
ActivityInfo info = m_activity.getPackageManager().getActivityInfo(
m_activity.getComponentName(),
PackageManager.GET_META_DATA);
String splashScreenKey = "android.app.splash_screen_drawable_"
+ (orientation == Configuration.ORIENTATION_LANDSCAPE ? "landscape" : "portrait");
if (!info.metaData.containsKey(splashScreenKey))
splashScreenKey = "android.app.splash_screen_drawable";
if (info.metaData.containsKey(splashScreenKey)) {
m_splashScreenSticky =
info.metaData.containsKey("android.app.splash_screen_sticky") &&
info.metaData.getBoolean("android.app.splash_screen_sticky");
int id = info.metaData.getInt(splashScreenKey);
m_splashScreen = new ImageView(m_activity);
m_splashScreen.setImageDrawable(m_activity.getResources().getDrawable(
id, m_activity.getTheme()));
m_splashScreen.setScaleType(ImageView.ScaleType.FIT_XY);
m_splashScreen.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
m_layout.addView(m_splashScreen);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void hideSplashScreen(final int duration)
{
QtNative.runAction(() -> {
if (m_splashScreen == null)
return;
if (duration <= 0) {
m_layout.removeView(m_splashScreen);
m_splashScreen = null;
return;
}
final Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(duration);
fadeOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
hideSplashScreen(0);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
});
m_splashScreen.startAnimation(fadeOut);
});
}
@UsedFromNativeCode
public void initializeAccessibility()
{
QtNative.runAction(() -> {
// FIXME make QtAccessibilityDelegate window based
if (m_layout != null)
m_accessibilityDelegate = new QtAccessibilityDelegate(m_layout);
else
Log.w(QtTAG, "Null layout, failed to initialize accessibility delegate.");
});
}
@UsedFromNativeCode
public void resetOptionsMenu()
{
QtNative.runAction(() -> m_activity.invalidateOptionsMenu());
}
@UsedFromNativeCode
public void openOptionsMenu()
{
QtNative.runAction(() -> m_activity.openOptionsMenu());
}
private boolean m_contextMenuVisible = false;
public void onCreatePopupMenu(Menu menu)
{
QtNative.fillContextMenu(menu);
m_contextMenuVisible = true;
}
@UsedFromNativeCode
@Override
public void openContextMenu(final int x, final int y, final int w, final int h)
{
m_layout.postDelayed(() -> {
final QtEditText focusedEditText = m_inputDelegate.getCurrentQtEditText();
if (focusedEditText == null) {
Log.w(QtTAG, "No focused view when trying to open context menu");
return;
}
m_layout.setLayoutParams(focusedEditText, new QtLayout.LayoutParams(w, h, x, y), false);
PopupMenu popup = new PopupMenu(m_activity, focusedEditText);
QtActivityDelegate.this.onCreatePopupMenu(popup.getMenu());
popup.setOnMenuItemClickListener(menuItem ->
m_activity.onContextItemSelected(menuItem));
popup.setOnDismissListener(popupMenu ->
m_activity.onContextMenuClosed(popupMenu.getMenu()));
popup.show();
}, 100);
}
@UsedFromNativeCode
public void closeContextMenu()
{
QtNative.runAction(() -> m_activity.closeContextMenu());
}
@Override
void setActionBarVisibility(boolean visible)
{
if (m_activity.getActionBar() == null)
return;
if (ViewConfiguration.get(m_activity).hasPermanentMenuKey() || !visible)
m_activity.getActionBar().hide();
else
m_activity.getActionBar().show();
}
@UsedFromNativeCode
@Override
public void addTopLevelWindow(final QtWindow window)
{
if (window == null)
return;
QtNative.runAction(()-> {
if (m_topLevelWindows.size() == 0) {
if (m_dummyView != null) {
m_layout.removeView(m_dummyView);
m_dummyView = null;
}
}
window.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
m_layout.addView(window, m_topLevelWindows.size());
m_topLevelWindows.put(window.getId(), window);
if (!m_splashScreenSticky)
hideSplashScreen();
});
}
@UsedFromNativeCode
@Override
void removeTopLevelWindow(final int id)
{
QtNative.runAction(()-> {
if (m_topLevelWindows.containsKey(id)) {
QtWindow window = m_topLevelWindows.remove(id);
if (m_topLevelWindows.isEmpty()) {
// Keep last frame in stack until it is replaced to get correct
// shutdown transition
m_dummyView = window;
} else {
m_layout.removeView(window);
}
}
});
}
@UsedFromNativeCode
@Override
void bringChildToFront(final int id)
{
QtNative.runAction(() -> {
QtWindow window = m_topLevelWindows.get(id);
if (window != null)
m_layout.moveChild(window, m_topLevelWindows.size() - 1);
});
}
@UsedFromNativeCode
@Override
void bringChildToBack(int id)
{
QtNative.runAction(() -> {
QtWindow window = m_topLevelWindows.get(id);
if (window != null)
m_layout.moveChild(window, 0);
});
}
@Override
QtAccessibilityDelegate createAccessibilityDelegate()
{
if (m_layout != null)
return new QtAccessibilityDelegate(m_layout);
Log.w(QtTAG, "Null layout, failed to initialize accessibility delegate.");
return null;
}
private void setActivityBackgroundDrawable()
{
TypedValue attr = new TypedValue();
m_activity.getTheme().resolveAttribute(android.R.attr.windowBackground,
attr, true);
Drawable backgroundDrawable;
if (attr.type >= TypedValue.TYPE_FIRST_COLOR_INT &&
attr.type <= TypedValue.TYPE_LAST_COLOR_INT) {
backgroundDrawable = new ColorDrawable(attr.data);
} else {
backgroundDrawable = m_activity.getResources().
getDrawable(attr.resourceId, m_activity.getTheme());
}
m_activity.getWindow().setBackgroundDrawable(backgroundDrawable);
}
// TODO: QTBUG-122761 To be removed after QtAndroidAutomotive does not depend on it.
@UsedFromNativeCode
public void insertNativeView(int id, View view, int x, int y, int w, int h)
{
QtNative.runAction(()-> {
if (m_dummyView != null) {
m_layout.removeView(m_dummyView);
m_dummyView = null;
}
if (m_nativeViews.containsKey(id))
m_layout.removeView(m_nativeViews.remove(id));
if (w < 0 || h < 0) {
view.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
} else {
view.setLayoutParams(new QtLayout.LayoutParams(w, h, x, y));
}
view.setId(id);
m_layout.addView(view);
m_nativeViews.put(id, view);
});
}
// TODO: QTBUG-122761 To be removed after QtAndroidAutomotive does not depend on it.
@UsedFromNativeCode
public void setNativeViewGeometry(int id, int x, int y, int w, int h)
{
QtNative.runAction(() -> {
if (m_nativeViews.containsKey(id)) {
View view = m_nativeViews.get(id);
view.setLayoutParams(new QtLayout.LayoutParams(w, h, x, y));
} else {
Log.e(QtTAG, "View " + id + " not found!");
}
});
}
}
|