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
androidmainnewintentlistener.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 BasysKom GmbH
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
6#include "android/androidjninfc_p.h"
7#include <QtGui/QGuiApplication>
8#include <QtCore/QJniObject>
9
11
12QMainNfcNewIntentListener::QMainNfcNewIntentListener() : paused(true), receiving(false)
13{
14 QtAndroidPrivate::registerNewIntentListener(this);
15 QtAndroidPrivate::registerResumePauseListener(this);
16}
17
19{
20 QtAndroidPrivate::unregisterNewIntentListener(this);
21 QtAndroidPrivate::unregisterResumePauseListener(this);
22}
23
24bool QMainNfcNewIntentListener::handleNewIntent(JNIEnv * /*env*/, jobject intentObject)
25{
26 QtJniTypes::Intent intent = intentObject;
27 // Only intents with a tag are relevant
28 if (!QtNfc::getTag(intent).isValid())
29 return false;
30
31 listenersLock.lockForRead();
32 for (auto listener : std::as_const(listeners))
33 listener->newIntent(intent);
34 listenersLock.unlock();
35 return true;
36}
37
39{
40 static bool firstListener = true;
41 if (firstListener) {
42 QtJniTypes::Intent intent = QtNfc::getStartIntent();
43 if (intent.isValid())
44 listener->newIntent(intent);
45 paused = static_cast<QGuiApplication*>(QGuiApplication::instance())->applicationState() != Qt::ApplicationActive;
46 }
47 firstListener = false;
48 listenersLock.lockForWrite();
49 if (!listeners.contains(listener))
50 listeners.push_back(listener);
51 listenersLock.unlock();
52 updateReceiveState();
53 return true;
54}
55
57{
58 listenersLock.lockForWrite();
59 listeners.removeOne(listener);
60 listenersLock.unlock();
61 updateReceiveState();
62 return true;
63}
64
66{
67 paused = false;
68 updateReceiveState();
69}
70
72{
73 paused = true;
74 updateReceiveState();
75}
76
77void QMainNfcNewIntentListener::updateReceiveState()
78{
79 if (paused) {
80 // We were paused while receiving, so we stop receiving.
81 if (receiving) {
83 receiving = false;
84 }
85 return;
86 }
87
88 // We reach here, so we are not paused.
89 listenersLock.lockForRead();
90 // We have nfc listeners and do not receive. Switch on.
91 if (!listeners.isEmpty() && !receiving)
92 receiving = QtNfc::startDiscovery();
93
94 // we have no nfc listeners and do receive. Switch off.
95 if (listeners.isEmpty() && receiving) {
97 receiving = false;
98 }
99 listenersLock.unlock();
100}
101
102QT_END_NAMESPACE
virtual void newIntent(QJniObject intent)=0
bool registerListener(QAndroidNfcListenerInterface *listener)
bool unregisterListener(QAndroidNfcListenerInterface *listener)
bool handleNewIntent(JNIEnv *env, jobject intent)
bool stopDiscovery()
bool startDiscovery()