Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for MIME types and URIs to the NFC api #740

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 68 additions & 31 deletions app/src/main/java/com/termux/api/apis/NfcAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import android.os.Bundle;
import android.os.Parcelable;
import android.util.JsonWriter;
import android.net.Uri;
import android.util.Base64;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -30,7 +32,6 @@ public static void onReceive(final Context context, final Intent intent) {
}



public static class NfcActivity extends AppCompatActivity {

private NfcAdapter adapter;
Expand All @@ -39,7 +40,7 @@ public static class NfcActivity extends AppCompatActivity {
String mode;
String param;
String value;

String mime;
private static final String LOG_TAG = "NfcActivity";

//Check for NFC
Expand All @@ -52,7 +53,7 @@ public void writeJson(JsonWriter out) throws Exception {
if (error.length() > 0)
out.name("error").value(error);
out.name("nfcPresent").value(null != adapter);
if(null!=adapter)
if (null != adapter)
out.name("nfcActive").value(adapter.isEnabled());
out.endObject();
}
Expand All @@ -69,21 +70,32 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
mode = intent.getStringExtra("mode");
if (null == mode)
mode = "noData";
param =intent.getStringExtra("param");
param = intent.getStringExtra("param");
if (null == param)
param = "noData";
value=intent.getStringExtra("value");
value = intent.getStringExtra("value");
if (null == socket_input) socket_input = intent.getStringExtra("socket_input");
if (null == socket_output) socket_output = intent.getStringExtra("socket_output");
// this is being jankily hacked on because
// - i'm stupid
// - i'm lazy
// - the person who wrote this was stupid
// sorry that was mean
// -doskel, 2024
if (mode.equals("mimeWrite")) {
mime = intent.getStringExtra("mime");
mode = "write";
}

if (mode.equals("noData")) {
errorNfc(this, intent,"");
errorNfc(this, intent, "");
finish();
}
}

NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);
if((null==adapter)||(!adapter.isEnabled())){
errorNfc(this,intent,"");
if ((null == adapter) || (!adapter.isEnabled())) {
errorNfc(this, intent, "");
finish();
}
}
Expand Down Expand Up @@ -114,7 +126,7 @@ protected void onNewIntent(Intent intent) {
try {
postResult(this, intent);
} catch (Exception e) {
Logger.logStackTraceWithMessage(LOG_TAG, "Error posting result" ,e);
Logger.logStackTraceWithMessage(LOG_TAG, "Error posting result", e);
}
finish();
}
Expand Down Expand Up @@ -149,7 +161,7 @@ public void writeJson(JsonWriter out) throws Exception {
switch (param) {
case "text":
Logger.logVerbose(LOG_TAG, "Write start");
onReceiveNfcWrite(context, intent);
onReceiveNfcWrite(context, intent, out);
Logger.logVerbose(LOG_TAG, "Write end");
break;
default:
Expand All @@ -158,15 +170,15 @@ public void writeJson(JsonWriter out) throws Exception {
}
break;
case "read":
switch (param){
switch (param) {
case "short":
readNDEFTag(intent,out);
readNDEFTag(intent, out);
break;
case "full":
readFullNDEFTag(intent,out);
readFullNDEFTag(intent, out);
break;
case "noData":
readNDEFTag(intent,out);
readNDEFTag(intent, out);
break;
default:
onUnexpectedAction(out, "Wrong Params", "Should be correct param value");
Expand All @@ -177,20 +189,42 @@ public void writeJson(JsonWriter out) throws Exception {
onUnexpectedAction(out, "Wrong Params", "Should be correct mode value ");
break;
}
} catch (Exception e){
} catch (Exception e) {
onUnexpectedAction(out, "exception", e.getMessage());
}
}
});
}

public void onReceiveNfcWrite( final Context context, Intent intent) throws Exception {
public void onReceiveNfcWrite(final Context context, Intent intent, JsonWriter out) throws Exception {
Logger.logVerbose(LOG_TAG, "onReceiveNfcWrite");

NfcAdapter adapter = NfcAdapter.getDefaultAdapter(context);
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefRecord record = NdefRecord.createTextRecord("en", value);
NdefMessage msg = new NdefMessage(new NdefRecord[]{record});
NdefMessage msg;
if (mime == null) {
NdefRecord record = NdefRecord.createTextRecord("en", value);
msg = new NdefMessage(new NdefRecord[]{record});
} else if (mime.equals("uri")) {
Uri uri;
try {
uri = Uri.parse(value);
} catch (Exception e) {
onUnexpectedAction(out, "Bad URI", "Should be a valid URI");
return;
}
NdefRecord record = NdefRecord.createUri(uri);
msg = new NdefMessage(new NdefRecord[]{record});
} else {
try {
byte[] data = Base64.decode(value, Base64.DEFAULT);
NdefRecord record = NdefRecord.createMime(mime, data);
msg = new NdefMessage(new NdefRecord[]{record});
} catch (Exception e) {
onUnexpectedAction(out, "Other error", "Unknown error");
return;
}
}
Ndef ndef = Ndef.get(tag);
ndef.connect();
ndef.writeNdefMessage(msg);
Expand All @@ -207,30 +241,30 @@ public void readNDEFTag(Intent intent, JsonWriter out) throws Exception {
Ndef ndefTag = Ndef.get(tag);
boolean bNdefPresent = false;
String[] strs = tag.getTechList();
for (String s: strs){
for (String s : strs) {
if (s.equals("android.nfc.tech.Ndef")) {
bNdefPresent = true;
break;
}
}
if (!bNdefPresent){
onUnexpectedAction(out, "Wrong Technology","termux API support only NFEF Tag");
if (!bNdefPresent) {
onUnexpectedAction(out, "Wrong Technology", "termux API support only NDEF Tag");
return;
}
NdefMessage[] nmsgs = new NdefMessage[msgs.length];
if (msgs.length == 1) {
nmsgs[0] = (NdefMessage) msgs[0];
NdefRecord[] records = nmsgs[0].getRecords();
out.beginObject();
if (records.length >0 ) {
if (records.length > 0) {
{
out.name("Record");
if (records.length > 1)
out.beginArray();
for (NdefRecord record: records){
for (NdefRecord record : records) {
out.beginObject();
int pos = 1 + record.getPayload()[0];
pos = (NdefRecord.TNF_WELL_KNOWN==record.getTnf())?(int)record.getPayload()[0]+1:0;
pos = (NdefRecord.TNF_WELL_KNOWN == record.getTnf()) ? (int) record.getPayload()[0] + 1 : 0;
int len = record.getPayload().length - pos;
byte[] msg = new byte[len];
System.arraycopy(record.getPayload(), pos, msg, 0, len);
Expand All @@ -255,22 +289,24 @@ public void readFullNDEFTag(Intent intent, JsonWriter out) throws Exception {

String[] strs = tag.getTechList();
boolean bNdefPresent = false;
for (String s: strs){
for (String s : strs) {
if (s.equals("android.nfc.tech.Ndef")) {
bNdefPresent = true;
break;
}
}
if (!bNdefPresent){
onUnexpectedAction(out, "Wrong Technology","termux API support only NFEF Tag");
if (!bNdefPresent) {
onUnexpectedAction(out, "Wrong Technology", "termux API support only NFEF Tag");
return;
}
NdefMessage[] nmsgs = new NdefMessage[msgs.length];
out.beginObject();
{
byte[] tagID = tag.getId();
StringBuilder sp = new StringBuilder();
for (byte tagIDpart : tagID) { sp.append(String.format("%02x", tagIDpart)); }
for (byte tagIDpart : tagID) {
sp.append(String.format("%02x", tagIDpart));
}
out.name("id").value(sp.toString());
out.name("typeTag").value(ndefTag.getType());
out.name("maxSize").value(ndefTag.getMaxSize());
Expand All @@ -295,10 +331,11 @@ public void readFullNDEFTag(Intent intent, JsonWriter out) throws Exception {
out.beginObject();
out.name("type").value(new String(record.getType()));
out.name("tnf").value(record.getTnf());
if (records[0].toUri() != null) out.name("URI").value(record.toUri().toString());
if (records[0].toUri() != null)
out.name("URI").value(record.toUri().toString());
out.name("mime").value(record.toMimeType());
int pos = 1 + record.getPayload()[0];
pos = (NdefRecord.TNF_WELL_KNOWN==record.getTnf())?(int)record.getPayload()[0]+1:0;
pos = (NdefRecord.TNF_WELL_KNOWN == record.getTnf()) ? (int) record.getPayload()[0] + 1 : 0;
int len = record.getPayload().length - pos;
byte[] msg = new byte[len];
System.arraycopy(record.getPayload(), pos, msg, 0, len);
Expand All @@ -313,7 +350,7 @@ public void readFullNDEFTag(Intent intent, JsonWriter out) throws Exception {
out.endObject();
}

protected void onUnexpectedAction(JsonWriter out,String error, String description) throws Exception {
protected void onUnexpectedAction(JsonWriter out, String error, String description) throws Exception {
out.beginObject();
out.name("error").value(error);
out.name("description").value(description);
Expand Down