NFC: check received message, receive on API 9

This commit is contained in:
str4d
2014-11-25 00:49:33 +00:00
parent 3fc07ee7a4
commit ee3001cb3f
4 changed files with 26 additions and 15 deletions

View File

@@ -2,4 +2,10 @@ package i2p.bote.android;
public class Constants {
public static final String EMAILDEST_SCHEME = "bote";
public static final String NDEF_DOMAIN = "i2p.bote";
public static final String NDEF_TYPE_CONTACT = "contact";
public static final String NDEF_TYPE_CONTACT_DESTINATION = "contactDestination";
public static final String NDEF_LEGACY_TYPE_CONTACT = NDEF_DOMAIN + ":" + NDEF_TYPE_CONTACT;
public static final String NDEF_LEGACY_TYPE_CONTACT_DESTINATION = NDEF_DOMAIN + ":" + NDEF_TYPE_CONTACT_DESTINATION;
}

View File

@@ -4,12 +4,14 @@ import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import java.util.Arrays;
import i2p.bote.android.Constants;
import i2p.bote.android.InitActivities;
import i2p.bote.android.R;
@@ -45,13 +47,10 @@ public class EditContactActivity extends ActionBarActivity {
public void onResume() {
super.onResume();
// NFC receive only works on API 10+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) {
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction()) ||
NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
}
// Check to see that the Activity started due to an Android Beam
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction()) ||
NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
processIntent(getIntent());
}
}
@@ -64,7 +63,7 @@ public class EditContactActivity extends ActionBarActivity {
/**
* Parses the NDEF Message from the intent
*/
void processIntent(Intent intent) {
private void processIntent(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs == null || rawMsgs.length < 1)
@@ -72,7 +71,11 @@ public class EditContactActivity extends ActionBarActivity {
NdefMessage msg = (NdefMessage) rawMsgs[0];
NdefRecord[] records = msg.getRecords();
if (records.length != 2)
if (records.length != 2 ||
records[0].getTnf() != NdefRecord.TNF_EXTERNAL_TYPE ||
!Arrays.equals(records[0].getType(), Constants.NDEF_LEGACY_TYPE_CONTACT.getBytes()) ||
records[1].getTnf() != NdefRecord.TNF_EXTERNAL_TYPE ||
!Arrays.equals(records[1].getType(), Constants.NDEF_LEGACY_TYPE_CONTACT_DESTINATION.getBytes()))
return; // TODO notify user?
String name = new String(records[0].getPayload());
String destination = new String(records[1].getPayload());

View File

@@ -9,7 +9,6 @@ import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import i2p.bote.android.InitActivities;
import i2p.bote.android.R;
public class ViewContactActivity extends ActionBarActivity {
NfcAdapter mNfcAdapter;

View File

@@ -26,6 +26,7 @@ import android.widget.Toast;
import java.security.GeneralSecurityException;
import i2p.bote.android.Constants;
import i2p.bote.android.R;
import i2p.bote.android.util.BoteHelper;
import i2p.bote.fileencryption.PasswordException;
@@ -198,13 +199,14 @@ public class ViewContactFragment extends Fragment {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
return new NdefRecord(
NdefRecord.TNF_EXTERNAL_TYPE,
"i2p.bote:contact".getBytes(),
Constants.NDEF_LEGACY_TYPE_CONTACT.getBytes(),
new byte[0],
mContact.getName().getBytes()
);
else
return NdefRecord.createExternal(
"i2p.bote", "contact", mContact.getName().getBytes()
Constants.NDEF_DOMAIN, Constants.NDEF_TYPE_CONTACT,
mContact.getName().getBytes()
);
}
@@ -212,13 +214,14 @@ public class ViewContactFragment extends Fragment {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN)
return new NdefRecord(
NdefRecord.TNF_EXTERNAL_TYPE,
"i2p.bote:contactDestination".getBytes(),
Constants.NDEF_LEGACY_TYPE_CONTACT_DESTINATION.getBytes(),
new byte[0],
mContact.getDestination().getKey().getBytes()
);
else
return NdefRecord.createExternal(
"i2p.bote", "contactDestination", mContact.getDestination().getKey().getBytes()
Constants.NDEF_DOMAIN, Constants.NDEF_TYPE_CONTACT_DESTINATION,
mContact.getDestination().getKey().getBytes()
);
}
}