From 7dc68522a25183a336a493fb26dec14fd8e0ff61 Mon Sep 17 00:00:00 2001 From: Marc Schink Date: Sun, 10 Dec 2017 12:36:21 +0100 Subject: [PATCH] Handle NFC action in UriHandlerActivity --- src/main/AndroidManifest.xml | 20 +++------- .../ui/StartConversationActivity.java | 31 ---------------- .../conversations/ui/UriHandlerActivity.java | 37 +++++++++++++++++++ 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 3c17bf714..fbc403b58 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -69,14 +69,6 @@ - - - - - - - - @@ -88,20 +80,20 @@ - - + - + + = Build.VERSION_CODES.JELLY_BEAN) { - return getInviteJellyBean(record).invite(); - } else { - byte[] payload = record.getPayload(); - if (payload[0] == 0) { - return new Invite(Uri.parse(new String(Arrays.copyOfRange( - payload, 1, payload.length)))).invite(); - } - } - } - } - } - } - } } return false; } diff --git a/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java b/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java index 043bf247e..9ffa1b11f 100644 --- a/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/UriHandlerActivity.java @@ -1,8 +1,14 @@ package eu.siacs.conversations.ui; +import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.net.Uri; +import android.nfc.NdefMessage; +import android.nfc.NdefRecord; +import android.nfc.NfcAdapter; +import android.os.Build; +import android.os.Parcelable; import com.google.zxing.integration.android.IntentIntegrator; import com.google.zxing.integration.android.IntentResult; @@ -27,6 +33,11 @@ public class UriHandlerActivity extends Activity { handleIntent(intent); } + @TargetApi(Build.VERSION_CODES.JELLY_BEAN) + Uri getInviteJellyBean(NdefRecord record) { + return record.toUri(); + } + private void handleUri(Uri uri) { final Intent intent; final XmppUri xmppUri = new XmppUri(uri); @@ -62,6 +73,30 @@ public class UriHandlerActivity extends Activity { startActivity(intent); } + private void handleNfcIntent(Intent data) { + for (Parcelable message : data.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES)) { + if (message instanceof NdefMessage) { + for (NdefRecord record : ((NdefMessage) message).getRecords()) { + switch (record.getTnf()) { + case NdefRecord.TNF_WELL_KNOWN: + if (Arrays.equals(record.getType(), NdefRecord.RTD_URI)) { + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + handleUri(getInviteJellyBean(record)); + } else { + byte[] payload = record.getPayload(); + if (payload[0] == 0) { + Uri uri = Uri.parse(new String(Arrays.copyOfRange( + payload, 1, payload.length))); + handleUri(uri); + } + } + } + } + } + } + } + } + private void handleIntent(Intent data) { if (data == null) { finish(); @@ -76,6 +111,8 @@ public class UriHandlerActivity extends Activity { case ACTION_SCAN_QR_CODE: new IntentIntegrator(this).initiateScan(Arrays.asList("AZTEC", "QR_CODE")); return; + case NfcAdapter.ACTION_NDEF_DISCOVERED: + handleNfcIntent(data); } finish();