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
QtCameraActivation.java
Go to the documentation of this file.
1// Copyright (C) 2026 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
3package org.qtproject.qt.android.multimedia.qffmpeg;
4
5import android.annotation.TargetApi;
6import android.os.Build;
7import android.util.Log;
8
9import java.lang.ref.Cleaner;
10
11import org.qtproject.qt.android.UsedFromNativeCode;
12
13// Wrapper around the native counterpart QAndroidCameraActivation.
14//
15// In the case this class is never signalled with a success/failure result,
16// we make sure garbage collector will eventually destroy the underlying
17// C++ object.
18class QtCameraActivation {
19 private static native void onActivationFinished(
20 long activationPtr,
21 boolean success,
22 String errorMessage);
23
24 // Separate from QtCameraActivation, so that the cleanup can run once it is unreachable.
25 private static final class State implements Runnable {
26 private long mActivationPtr = 0;
27
28 State(long activationPtr) {
29 mActivationPtr = activationPtr;
30 }
31
32 synchronized boolean report(boolean success, String errorMessage) {
33 if (mActivationPtr == 0)
34 return false;
35
36 final long activationPtr = mActivationPtr;
37 mActivationPtr = 0;
38 onActivationFinished(activationPtr, success, errorMessage);
39 return true;
40 }
41
43 public void run() {
44 if (report(false, "Failed to start camera"))
45 Log.w(QtCamera2.LOG_TAG, "Camera activation was abandoned without a result");
46 }
47 }
48
49 // Separate class, so that Cleaner is never loaded before Android 13.
50 @TargetApi(Build.VERSION_CODES.TIRAMISU)
51 private static final class ModernCleanup {
52 private static final Cleaner sCleaner = Cleaner.create();
53
54 static Object register(Object owner, State state) {
55 return sCleaner.register(owner, state);
56 }
57 }
58
59 private static final class LegacyCleanup {
60 private final State mState;
61
62 LegacyCleanup(State state) {
63 mState = state;
64 }
65
67 @SuppressWarnings("deprecation")
68 protected void finalize() throws Throwable {
69 try {
70 mState.run();
71 } finally {
72 super.finalize();
73 }
74 }
75 }
76
77 private final State mState;
78 // Keeps the cleanup alive for as long as this object is reachable.
79 private final Object mCleanup;
80
81 @UsedFromNativeCode
82 QtCameraActivation(long activationPtr) {
83 assert(activationPtr != 0);
84 mState = new State(activationPtr);
85 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
86 mCleanup = ModernCleanup.register(this, mState);
87 else
88 mCleanup = new LegacyCleanup(mState);
89 }
90
91 boolean reportSuccess() {
92 return mState.report(true, "");
93 }
94
95 boolean reportFailure(String errorMessage) {
96 return mState.report(false, errorMessage);
97 }
98}
PeripheralState state
#define assert
#define register