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
QtScreenGrabber.java
Go to the documentation of this file.
1// Copyright (C) 2024 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.multimedia;
5
6import android.app.Activity;
7import android.content.ComponentName;
8import android.content.Context;
9import android.content.Intent;
10import android.content.ServiceConnection;
11import android.media.projection.MediaProjectionManager;
12import android.os.Build;
13import android.os.IBinder;
14import android.util.Log;
15import android.util.Size;
16import android.util.DisplayMetrics;
17import android.view.Display;
18import android.view.WindowManager;
19import android.view.WindowMetrics;
20
21class QtScreenGrabber {
22 // Lock for synchronization
23 private final Object mServiceLock = new Object();
24 private QtScreenCaptureService mService = null;
25 private final Context m_activity;
26 static final String DATA = "data";
27 static final String HEIGHT = "height";
28 static final String ID = "id";
29 static final String RESULT_CODE = "resultCode";
30 static final String WIDTH = "width";
31 private static final String QtTAG = "QtScreenGrabber";
32
33 private ServiceConnection mConnection = new ServiceConnection() {
35 public void onServiceConnected(ComponentName name, IBinder service) {
36 synchronized (mServiceLock) {
37 QtScreenCaptureService.ScreenCaptureBinder binder =
38 (QtScreenCaptureService.ScreenCaptureBinder) service;
39 mService = binder.getService();
40 mServiceLock.notify();
41 }
42 }
43
45 public void onServiceDisconnected(ComponentName name) {
46 synchronized (mServiceLock) {
47 mService = null;
48 }
49 }
50 };
51
52 QtScreenGrabber(Activity activity, int requestCode) {
53 m_activity = activity;
54 MediaProjectionManager mgr = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
55 activity.startActivityForResult(mgr.createScreenCaptureIntent(), requestCode);
56 }
57
58 public static Size getScreenCaptureSize(Activity activity) {
59 WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
60 if (windowManager == null) {
61 Log.w(QtTAG, "WindowManager is null. Invalid screen size");
62 return new Size(0, 0);
63 }
64
65 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
66 WindowMetrics metrics = windowManager.getCurrentWindowMetrics();
67 return new Size(metrics.getBounds().width(), metrics.getBounds().height());
68 } else {
69 Display display = windowManager.getDefaultDisplay();
70 DisplayMetrics metrics = new DisplayMetrics();
71 display.getRealMetrics(metrics);
72 return new Size(metrics.widthPixels, metrics.heightPixels);
73 }
74 }
75
76 public boolean startScreenCaptureService(int resultCode, long id, int width, int height, Intent data) {
77 try {
78 Intent serviceIntent = new Intent(m_activity, QtScreenCaptureService.class);
79 serviceIntent.putExtra(RESULT_CODE, resultCode);
80 serviceIntent.putExtra(DATA, data);
81 serviceIntent.putExtra(ID, id);
82 serviceIntent.putExtra(WIDTH, width);
83 serviceIntent.putExtra(HEIGHT, height);
84 m_activity.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE);
85 m_activity.startService(serviceIntent);
86 } catch (Exception e) {
87 Log.w(QtTAG, "Cannot start QtScreenCaptureService: " + e);
88 return false;
89 }
90
91 return true;
92 }
93
94 public boolean stopScreenCaptureService() {
95 try {
96 Intent serviceIntent = new Intent(m_activity, QtScreenCaptureService.class);
97 synchronized (mServiceLock) {
98 if (mService == null)
99 // Service wasn't started at all or was not bound yet.
100 // Lets wait to make sure that we will call stopScreenCapture if it is needed
101 mServiceLock.wait(1000);
102
103 if (mService != null)
104 mService.stopScreenCapture();
105 m_activity.unbindService(mConnection);
106 }
107 m_activity.stopService(serviceIntent);
108 } catch (Exception e) {
109 Log.w(QtTAG, "Cannot stop QtScreenCaptureService: " + e);
110 return false;
111 }
112
113 return true;
114 }
115}
QPainter Context
struct wl_display * display
Definition linuxdmabuf.h:46
Q_CORE_EXPORT QtJniTypes::Activity activity()
static void onServiceConnected(JNIEnv *, jclass, jlong id, jstring name, jobject service)
static void onServiceDisconnected(JNIEnv *, jclass, jlong id, jstring name)
#define Size(name)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
[0]
GLsizei GLenum const void GLuint GLsizei GLfloat * metrics
GLint GLsizei width
EGLImageKHR EGLint * name
struct _XDisplay Display