Skip to content

Commit

Permalink
Merge pull request commonsguy#8 from jonreeve/nfc_uri_prefix_fix
Browse files Browse the repository at this point in the history
Fixed buildUrlBytes so it would select a longer prefix
  • Loading branch information
commonsguy committed Jun 9, 2012
2 parents e0d2dde + 2e92145 commit d393430
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions NFC/URLTagger/src/com/commonsware/android/nfc/url/URLTagger.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected void onNewIntent(Intent intent) {
if (inWriteMode &&
NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
Tag tag=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
byte[] url=buildUrlBytes();
byte[] url=buildUrlBytes(intent.getStringExtra(Intent.EXTRA_TEXT));
NdefRecord record=new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_URI,
new byte[] {}, url);
Expand Down Expand Up @@ -105,29 +105,29 @@ public void onPause() {
super.onPause();
}

private byte[] buildUrlBytes() {
String raw=getIntent().getStringExtra(Intent.EXTRA_TEXT);
int prefix=0;
String subset=raw;
private byte[] buildUrlBytes(String url) {
byte prefixByte=0;
String subset=url;
int bestPrefixLength=0;

for (int i=0;i<PREFIXES.length;i++) {
if (raw.startsWith(PREFIXES[i])) {
prefix=i+1;
subset=raw.substring(PREFIXES[i].length());

break;
String prefix = PREFIXES[i];
if (url.startsWith(prefix) && prefix.length() > bestPrefixLength) {
prefixByte=(byte)(i+1);
bestPrefixLength=prefix.length();
subset=url.substring(bestPrefixLength);
}
}

byte[] subsetBytes=subset.getBytes();
byte[] result=new byte[subsetBytes.length+1];
final byte[] subsetBytes = subset.getBytes();
final byte[] result = new byte[subsetBytes.length+1];

result[0]=(byte)prefix;
result[0]=prefixByte;
System.arraycopy(subsetBytes, 0, result, 1, subsetBytes.length);

return(result);
}

static class WriteTask extends AsyncTask<Void, Void, Void> {
Activity host=null;
NdefMessage msg=null;
Expand Down Expand Up @@ -211,4 +211,4 @@ protected void onPostExecute(Void unused) {
host.finish();
}
}
}
}

0 comments on commit d393430

Please sign in to comment.