support pre JELLY_BEAN ndef

This commit is contained in:
M. Dietrich 2014-11-04 09:30:03 +01:00
parent 7a9327d960
commit 32396b3bde

View File

@ -16,6 +16,7 @@ import android.net.Uri;
import android.nfc.NdefMessage; import android.nfc.NdefMessage;
import android.nfc.NdefRecord; import android.nfc.NdefRecord;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.v13.app.FragmentPagerAdapter; import android.support.v13.app.FragmentPagerAdapter;
@ -44,7 +45,9 @@ import com.google.zxing.integration.android.IntentResult;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -580,9 +583,9 @@ public class StartConversationActivity extends XmppActivity {
if (intent == null || intent.getAction() == null) { if (intent == null || intent.getAction() == null) {
return false; return false;
} }
String jid; String jid = null;
Uri uri; Uri uri = null;
Invite invite; Invite invite = null;
switch (intent.getAction()) { switch (intent.getAction()) {
case Intent.ACTION_SENDTO: case Intent.ACTION_SENDTO:
try { try {
@ -603,11 +606,18 @@ public class StartConversationActivity extends XmppActivity {
return handleJid(invite.jid); return handleJid(invite.jid);
} }
case NfcAdapter.ACTION_NDEF_DISCOVERED: case NfcAdapter.ACTION_NDEF_DISCOVERED:
if (android.os.Build.VERSION.SDK_INT >= 16) {
Parcelable[] messages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); Parcelable[] messages = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage message = (NdefMessage) messages[0]; NdefMessage message = (NdefMessage) messages[0];
NdefRecord record = message.getRecords()[0]; NdefRecord record = message.getRecords()[0];
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
invite = parseInviteUri(record.toUri()); invite = parseInviteUri(record.toUri());
} else {
byte[] mPayload = record.getPayload();
if (mPayload[0] == 0) {
invite = parseInviteUri(Uri.parse(new String(Arrays.copyOfRange(
mPayload, 1, mPayload.length), StandardCharsets.UTF_8)));
}
}
if (invite != null) { if (invite != null) {
if (invite.muc) { if (invite.muc) {
showJoinConferenceDialog(invite.jid); showJoinConferenceDialog(invite.jid);
@ -616,9 +626,7 @@ public class StartConversationActivity extends XmppActivity {
return handleJid(invite.jid); return handleJid(invite.jid);
} }
} }
} else {
return false; return false;
}
default: default:
return false; return false;
} }