diff options
Diffstat (limited to 'src/android/nfc/src')
-rw-r--r-- | src/android/nfc/src/org/qtproject/qt/android/nfc/QtNfc.java (renamed from src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java) | 115 | ||||
-rw-r--r-- | src/android/nfc/src/org/qtproject/qt/android/nfc/QtNfcBroadcastReceiver.java | 38 | ||||
-rw-r--r-- | src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfcBroadcastReceiver.java | 72 |
3 files changed, 71 insertions, 154 deletions
diff --git a/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java b/src/android/nfc/src/org/qtproject/qt/android/nfc/QtNfc.java index 19e645f5..7753f182 100644 --- a/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfc.java +++ b/src/android/nfc/src/org/qtproject/qt/android/nfc/QtNfc.java @@ -1,49 +1,10 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Centria research and development -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -package org.qtproject.qt5.android.nfc; - -import java.lang.Thread; +// Copyright (C) 2016 Centria research and development +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +package org.qtproject.qt.android.nfc; + import java.lang.Runnable; -import android.os.Parcelable; -import android.os.Looper; import android.content.Context; import android.app.Activity; import android.app.PendingIntent; @@ -51,22 +12,20 @@ import android.content.Intent; import android.content.IntentFilter; import android.nfc.NfcAdapter; import android.content.IntentFilter.MalformedMimeTypeException; -import android.os.Bundle; +import android.os.Build; +import android.os.Parcelable; import android.util.Log; -import android.content.BroadcastReceiver; import android.content.pm.PackageManager; -public class QtNfc +class QtNfc { - /* static final QtNfc m_nfc = new QtNfc(); */ static private final String TAG = "QtNfc"; - static public NfcAdapter m_adapter = null; - static public PendingIntent m_pendingIntent = null; - static public IntentFilter[] m_filters; - static public Context m_context = null; - static public Activity m_activity = null; + static private NfcAdapter m_adapter = null; + static private PendingIntent m_pendingIntent = null; + static private Context m_context = null; + static private Activity m_activity = null; - static public void setContext(Context context) + static void setContext(Context context) { m_context = context; if (context instanceof Activity) m_activity = (Activity) context; @@ -78,42 +37,30 @@ public class QtNfc } if (m_adapter == null) { - //Log.e(TAG, "No NFC available"); return; } + // Since Android 12 (API level 31) it's mandatory to specify mutability + // of PendingIntent. We need a mutable intent, which was a default + // option earlier. + int flags = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? PendingIntent.FLAG_MUTABLE + : 0; m_pendingIntent = PendingIntent.getActivity( m_activity, 0, new Intent(m_activity, m_activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), - 0); - - //Log.d(TAG, "Pending intent:" + m_pendingIntent); - - IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED); - - m_filters = new IntentFilter[]{ - filter - }; - - try { - filter.addDataType("*/*"); - } catch(MalformedMimeTypeException e) { - throw new RuntimeException("Fail", e); - } - - //Log.d(TAG, "Thread:" + Thread.currentThread().getId()); + flags); } - static public boolean start() + static boolean startDiscovery() { if (m_adapter == null || m_activity == null || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) return false; m_activity.runOnUiThread(new Runnable() { + @Override public void run() { - //Log.d(TAG, "Enabling NFC"); IntentFilter[] filters = new IntentFilter[3]; filters[0] = new IntentFilter(); filters[0].addAction(NfcAdapter.ACTION_TAG_DISCOVERED); @@ -124,9 +71,9 @@ public class QtNfc try { filters[1].addDataType("*/*"); } catch (MalformedMimeTypeException e) { - throw new RuntimeException("Check your mime type."); + throw new RuntimeException("IntentFilter.addDataType() failed"); } - // some tags will report as tech, even if they are ndef formated/formatable. + // some tags will report as tech, even if they are ndef formatted/formattable. filters[2] = new IntentFilter(); filters[2].addAction(NfcAdapter.ACTION_TECH_DISCOVERED); String[][] techList = new String[][]{ @@ -144,15 +91,15 @@ public class QtNfc return true; } - static public boolean stop() + static boolean stopDiscovery() { if (m_adapter == null || m_activity == null || !m_activity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) return false; m_activity.runOnUiThread(new Runnable() { + @Override public void run() { - //Log.d(TAG, "Disabling NFC"); try { m_adapter.disableForegroundDispatch(m_activity); } catch(IllegalStateException e) { @@ -164,22 +111,21 @@ public class QtNfc return true; } - static public boolean isAvailable() + static boolean isEnabled() { if (m_adapter == null) { - //Log.e(TAG, "No NFC available (Adapter is null)"); return false; } return m_adapter.isEnabled(); } - static public boolean isSupported() + static boolean isSupported() { return (m_adapter != null); } - static public Intent getStartIntent() + static Intent getStartIntent() { Log.d(TAG, "getStartIntent"); if (m_activity == null) return null; @@ -193,4 +139,9 @@ public class QtNfc return null; } } + + static Parcelable getTag(Intent intent) + { + return intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); + } } diff --git a/src/android/nfc/src/org/qtproject/qt/android/nfc/QtNfcBroadcastReceiver.java b/src/android/nfc/src/org/qtproject/qt/android/nfc/QtNfcBroadcastReceiver.java new file mode 100644 index 00000000..cd6b6a43 --- /dev/null +++ b/src/android/nfc/src/org/qtproject/qt/android/nfc/QtNfcBroadcastReceiver.java @@ -0,0 +1,38 @@ +// Copyright (C) 2018 Governikus GmbH & Co. KG +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +package org.qtproject.qt.android.nfc; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.nfc.NfcAdapter; + +class QtNfcBroadcastReceiver extends BroadcastReceiver +{ + final private long qtObject; + final private Context qtContext; + + QtNfcBroadcastReceiver(long obj, Context context) + { + qtObject = obj; + qtContext = context; + IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); + qtContext.registerReceiver(this, filter); + } + + void unregisterReceiver() + { + qtContext.unregisterReceiver(this); + } + + @Override + public void onReceive(Context context, Intent intent) + { + final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_OFF); + jniOnReceive(qtObject, state); + } + + native void jniOnReceive(long qtObject, int state); +} diff --git a/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfcBroadcastReceiver.java b/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfcBroadcastReceiver.java deleted file mode 100644 index ea650ede..00000000 --- a/src/android/nfc/src/org/qtproject/qt5/android/nfc/QtNfcBroadcastReceiver.java +++ /dev/null @@ -1,72 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2018 Governikus GmbH & Co. KG -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtNfc module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -package org.qtproject.qt5.android.nfc; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.nfc.NfcAdapter; - - -public class QtNfcBroadcastReceiver extends BroadcastReceiver -{ - private Context qtContext; - - public QtNfcBroadcastReceiver(Context context) - { - qtContext = context; - IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); - qtContext.registerReceiver(this, filter); - } - - public void unregisterReceiver() - { - qtContext.unregisterReceiver(this); - } - - public void onReceive(Context context, Intent intent) - { - final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE, NfcAdapter.STATE_OFF); - jniOnReceive(state); - } - - public native void jniOnReceive(int state); -} |