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