1
0
Fork 0
mirror of https://github.com/MatomoCamp/matomocamp-companion-android.git synced 2024-09-19 16:13:46 +02:00

Improve NfcUtils to provide methods to convert data from and to NdefRecord

This commit is contained in:
Christophe Beyls 2019-01-28 20:47:44 +01:00
parent 5f3b8f945d
commit 75512c1364
4 changed files with 14 additions and 10 deletions

View file

@ -59,7 +59,7 @@ public class EventDetailsActivity extends AppCompatActivity implements Observer<
String eventIdString;
if (NfcUtils.hasAppData(intent)) {
// NFC intent
eventIdString = new String(NfcUtils.extractAppData(intent));
eventIdString = NfcUtils.toEventIdString((NfcUtils.extractAppData(intent)));
} else {
// Normal in-app intent
eventIdString = intent.getDataString();
@ -128,6 +128,6 @@ public class EventDetailsActivity extends AppCompatActivity implements Observer<
@Override
public NdefRecord createNfcAppData() {
return NfcUtils.createSingleEventAppData(this, event);
return NfcUtils.createEventAppData(this, event);
}
}

View file

@ -152,6 +152,6 @@ public class TrackScheduleActivity extends AppCompatActivity
if (lastSelectedEvent == null) {
return null;
}
return NfcUtils.createSingleEventAppData(this, lastSelectedEvent);
return NfcUtils.createEventAppData(this, lastSelectedEvent);
}
}

View file

@ -96,7 +96,7 @@ public class TrackScheduleEventActivity extends AppCompatActivity implements Obs
if (event == null) {
return null;
}
return NfcUtils.createSingleEventAppData(this, event);
return NfcUtils.createEventAppData(this, event);
}
@Override

View file

@ -59,12 +59,16 @@ public class NfcUtils {
return true;
}
public static NdefRecord createSingleEventAppData(@NonNull Context context, @NonNull Event event) {
public static NdefRecord createEventAppData(@NonNull Context context, @NonNull Event event) {
String mimeType = "application/" + context.getPackageName();
byte[] mimeData = String.valueOf(event.getId()).getBytes();
return NdefRecord.createMime(mimeType, mimeData);
}
public static String toEventIdString(@NonNull NdefRecord record) {
return new String(record.getPayload());
}
/**
* Determines if the intent contains NFC NDEF application-specific data to be extracted.
*/
@ -73,14 +77,14 @@ public class NfcUtils {
}
/**
* Extracts application-specific data sent through NFC from an intent. You must first ensure that the intent contains NFC data by calling hasAppData().
* Extracts application-specific data sent through NFC from an intent.
* You must first ensure that the intent contains NFC data by calling hasAppData().
*
* @param intent
* @return The extracted data
* @return The extracted app data as an NdefRecord
*/
public static byte[] extractAppData(Intent intent) {
public static NdefRecord extractAppData(Intent intent) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];
return msg.getRecords()[0].getPayload();
return msg.getRecords()[0];
}
}