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
QtDisplayManager.java
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4package org.qtproject.qt.android;
5
6import android.app.Activity;
7import android.content.Context;
8import android.content.res.Configuration;
9import android.content.res.Resources;
10import android.graphics.Rect;
11import android.hardware.display.DisplayManager;
12import android.os.Build;
13import android.util.DisplayMetrics;
14import android.util.Size;
15import android.view.Display;
16import android.view.Surface;
17import android.view.WindowManager;
18import android.view.WindowMetrics;
19
20import java.util.ArrayList;
21import java.util.Arrays;
22import java.util.List;
23
24import android.util.Log;
25
26class QtDisplayManager
27{
28 private static String QtTAG = "QtDisplayManager";
29 // screen methods
30 static native void handleLayoutSizeChanged(int availableWidth, int availableHeight);
31 static native void handleOrientationChanged(int newRotation, int nativeOrientation);
32 static native void handleRefreshRateChanged(float refreshRate);
33 static native void handleUiDarkModeChanged(int newUiMode);
34 static native void handleScreenAdded(int displayId);
35 static native void handleScreenChanged(int displayId);
36 static native void handleScreenRemoved(int displayId);
37 static native void handleScreenDensityChanged(double density);
38 // screen methods
39
40 private static int m_previousRotation = -1;
41
42 private final DisplayManager.DisplayListener m_displayListener;
43 private final Activity m_activity;
44
45 QtDisplayManager(Activity activity)
46 {
47 m_activity = activity;
48 m_displayListener = new DisplayManager.DisplayListener() {
49 @Override
50 public void onDisplayAdded(int displayId) {
51 QtDisplayManager.handleScreenAdded(displayId);
52 }
53
54 @Override
55 public void onDisplayChanged(int displayId) {
56 QtDisplayManager.updateRefreshRate(m_activity);
57 QtDisplayManager.updateScreenDensity(m_activity);
58 QtDisplayManager.handleScreenChanged(displayId);
59 }
60
61 @Override
62 public void onDisplayRemoved(int displayId) {
63 QtDisplayManager.handleScreenRemoved(displayId);
64 }
65 };
66 }
67
68 static void updateRefreshRate(Context context)
69 {
70 Display display = getDisplay(context);
71 float refreshRate = display != null ? display.getRefreshRate() : 60.0f;
72 QtDisplayManager.handleRefreshRateChanged(refreshRate);
73 }
74
75 static void handleOrientationChange(Activity activity)
76 {
77 Display display = QtDisplayManager.getDisplay(activity);
78 int rotation = display != null ? display.getRotation() : 0;
79 if (m_previousRotation == rotation)
80 return;
81 int nativeOrientation = getNativeOrientation(activity, rotation);
82 QtDisplayManager.handleOrientationChanged(rotation, nativeOrientation);
83 m_previousRotation = rotation;
84 }
85
86 static void updateScreenDensity(Activity activity)
87 {
88 Resources resources = activity == null ? Resources.getSystem() : activity.getResources();
89 double density = resources.getDisplayMetrics().density;
90 QtDisplayManager.handleScreenDensityChanged(density);
91 }
92
93 private static int getNativeOrientation(Activity activity, int rotation)
94 {
95 int orientation = activity.getResources().getConfiguration().orientation;
96 boolean rot90 = (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
97 boolean isLandscape = (orientation == Configuration.ORIENTATION_LANDSCAPE);
98 if ((isLandscape && !rot90) || (!isLandscape && rot90))
99 return Configuration.ORIENTATION_LANDSCAPE;
100
101 return Configuration.ORIENTATION_PORTRAIT;
102 }
103
104 void initDisplayProperties()
105 {
106 QtDisplayManager.handleOrientationChange(m_activity);
107 QtDisplayManager.updateRefreshRate(m_activity);
108 QtDisplayManager.updateScreenDensity(m_activity);
109 }
110
111 void registerDisplayListener()
112 {
113 DisplayManager displayManager =
114 (DisplayManager) m_activity.getSystemService(Context.DISPLAY_SERVICE);
115 displayManager.registerDisplayListener(m_displayListener, null);
116 }
117
118 void unregisterDisplayListener()
119 {
120 DisplayManager displayManager =
121 (DisplayManager) m_activity.getSystemService(Context.DISPLAY_SERVICE);
122 displayManager.unregisterDisplayListener(m_displayListener);
123 }
124
125 @SuppressWarnings("deprecation")
126 static Display getDisplay(Context context)
127 {
128 Activity activity = (Activity) context;
129 if (activity != null) {
130 return (Build.VERSION.SDK_INT < Build.VERSION_CODES.R)
131 ? activity.getWindowManager().getDefaultDisplay()
132 : activity.getDisplay();
133 }
134
135 final DisplayManager dm = context.getSystemService(DisplayManager.class);
136 return dm.getDisplay(Display.DEFAULT_DISPLAY);
137 }
138
139 @UsedFromNativeCode
140 static Display getDisplay(Context context, int displayId)
141 {
142 DisplayManager displayManager =
143 (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
144 if (displayManager != null) {
145 return displayManager.getDisplay(displayId);
146 }
147 return null;
148 }
149
150 @UsedFromNativeCode
151 static List<Display> getAvailableDisplays(Context context)
152 {
153 DisplayManager displayManager =
154 (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
155 if (displayManager != null) {
156 Display[] displays = displayManager.getDisplays();
157 return Arrays.asList(displays);
158 }
159 return new ArrayList<>();
160 }
161
162 @UsedFromNativeCode
163 @SuppressWarnings("deprecation")
164 static Size getDisplaySize(Context context, Display display)
165 {
166 if (display == null || context == null)
167 return new Size(0, 0);
168
169 if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
170 final DisplayMetrics metrics = new DisplayMetrics();
171 display.getRealMetrics(metrics);
172 return new Size(metrics.widthPixels, metrics.heightPixels);
173 } else {
174 try {
175 Context displayContext = context.createDisplayContext(display);
176 WindowManager windowManager = displayContext.getSystemService(WindowManager.class);
177 if (windowManager != null) {
178 WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
179 Rect areaBounds = metrics.getBounds();
180 return new Size(areaBounds.width(), areaBounds.height());
181 } else {
182 Log.e(QtTAG, "getDisplaySize(): WindowManager null, display ID" + display.getDisplayId());
183 }
184 } catch (Exception e) {
185 Log.e(QtTAG, "Failed to retrieve display metrics with " + e);
186 }
187 return new Size(0, 0);
188 }
189 }
190
192 static float getXDpi(final DisplayMetrics metrics) {
193 if (metrics.xdpi < android.util.DisplayMetrics.DENSITY_LOW)
194 return android.util.DisplayMetrics.DENSITY_LOW;
195 return metrics.xdpi;
196 }
197
199 static float getYDpi(final DisplayMetrics metrics) {
200 if (metrics.ydpi < android.util.DisplayMetrics.DENSITY_LOW)
201 return android.util.DisplayMetrics.DENSITY_LOW;
202 return metrics.ydpi;
203 }
204}
QPainter Context
static const QString context()
Definition java.cpp:398
struct wl_display * display
Definition linuxdmabuf.h:41
Q_CORE_EXPORT QtJniTypes::Activity activity()
#define Size(name)
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
struct _XDisplay Display