Android Open Source - NFCAndroid Uri Ndef Record






From Project

Back to project page NFCAndroid.

License

The source code is released under:

Copyright (c) 2012, Wireless Sensor Technologies, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following ...

If you think the Android project NFCAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gototags.nfc.ndef;
/*  www  .j  ava2  s  .  co  m*/
import java.nio.charset.Charset;
import java.util.Arrays;

import com.google.common.base.Preconditions;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.primitives.Bytes;

import android.net.Uri;
import android.nfc.NdefRecord;

public class UriNdefRecord extends ParsedNdefRecord {
  private static final byte ZERO = (byte)0x00;
  
  private static final BiMap<Byte, String> URI_PREFIX_MAP = ImmutableBiMap.<Byte, String>builder()
            .put(ZERO, "")
            .put((byte)0x01, "http://www.")
            .put((byte)0x02, "https://www.")
            .put((byte)0x03, "http://")
            .put((byte)0x04, "https://")
            .put((byte)0x05, "tel:")
            .put((byte)0x06, "mailto:")
            .put((byte)0x07, "ftp://anonymous:anonymous@")
            .put((byte)0x08, "ftp://ftp.")
            .put((byte)0x09, "ftps://")
            .put((byte)0x0A, "sftp://")
            .put((byte)0x0B, "smb://")
            .put((byte)0x0C, "nfs://")
            .put((byte)0x0D, "ftp://")
            .put((byte)0x0E, "dav://")
            .put((byte)0x0F, "news:")
            .put((byte)0x10, "telnet://")
            .put((byte)0x11, "imap:")
            .put((byte)0x12, "rtsp://")
            .put((byte)0x13, "urn:")
            .put((byte)0x14, "pop:")
            .put((byte)0x15, "sip:")
            .put((byte)0x16, "sips:")
            .put((byte)0x17, "tftp:")
            .put((byte)0x18, "btspp://")
            .put((byte)0x19, "btl2cap://")
            .put((byte)0x1A, "btgoep://")
            .put((byte)0x1B, "tcpobex://")
            .put((byte)0x1C, "irdaobex://")
            .put((byte)0x1D, "file://")
            .put((byte)0x1E, "urn:epc:id:")
            .put((byte)0x1F, "urn:epc:tag:")
            .put((byte)0x20, "urn:epc:pat:")
            .put((byte)0x21, "urn:epc:raw:")
            .put((byte)0x22, "urn:epc:")
            .put((byte)0x23, "urn:nfc:")
            .build();
  
  public static String getPrefixValue(Uri uri) {
    if (uri != null) {
      String uriString = uri.toString().toLowerCase();
      
      for (byte prefixByte : URI_PREFIX_MAP.keySet()) {
        if (prefixByte != ZERO) {
          String prefixValue = URI_PREFIX_MAP.get(prefixByte);
          
          if (uriString.startsWith(prefixValue))
            return prefixValue;
        }
      }
    }
    
    return URI_PREFIX_MAP.get(ZERO);
  }
  
  public static byte getPrefixByte(Uri uri) {
    if (uri != null) {
      String uriString = uri.toString().toLowerCase();
      
      for (byte prefixByte : URI_PREFIX_MAP.keySet()) {
        if (prefixByte != ZERO) {
          if (uriString.startsWith(URI_PREFIX_MAP.get(prefixByte)))
            return prefixByte;
        }
      }
    }
    
    return ZERO;
  }
  
  private Uri uri;
  
  public UriNdefRecord() {
  }
  
  public UriNdefRecord(Uri uri) {
    this.setUri(uri);
  }
  
  public UriNdefRecord(String uriString) {
    this.setUri(Uri.parse(uriString));
  }
  
  public Uri getUri() {
    return uri;
  }

  public void setUri(Uri uri) {
    this.uri = uri;
  }
  
  @Override
  public NdefRecord toNdefRecord() {
    Preconditions.checkNotNull(this.getUri());
    
    try {
      String prefixValue = getPrefixValue(this.getUri());
      
      String value = this.getUri().toString().substring(prefixValue.length());
      byte[] valueField = value.getBytes(Charset.forName("UTF-8"));
      
      byte[] payload = new byte[valueField.length + 1];
      
      payload[0] = UriNdefRecord.getPrefixByte(this.getUri());
      
      System.arraycopy(valueField, 0, payload, 1, valueField.length);
      
      return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);
    } catch (Exception exception) {
      throw new IllegalArgumentException(exception);
    }
  }
  
  public static UriNdefRecord fromNdefRecord(NdefRecord ndefRecord) {
    Preconditions.checkNotNull(ndefRecord);
    
    short tnf = ndefRecord.getTnf();
    Preconditions.checkArgument(tnf == NdefRecord.TNF_WELL_KNOWN || tnf == NdefRecord.TNF_ABSOLUTE_URI);
    
    byte[] payload = ndefRecord.getPayload();
    
    try {
        if (tnf == NdefRecord.TNF_WELL_KNOWN) {
          String prefix = URI_PREFIX_MAP.get(payload[0]);
            
            byte[] fullUri = Bytes.concat(
                    prefix.getBytes(Charset.forName("UTF-8")),
                    Arrays.copyOfRange(payload, 1, payload.length));
    
            Uri uri = Uri.parse(new String(fullUri, Charset.forName("UTF-8")));
            
            return new UriNdefRecord(uri);
        } else if (tnf == NdefRecord.TNF_ABSOLUTE_URI) {
          Uri uri = Uri.parse(new String(payload, Charset.forName("UTF-8")));
            
            return new UriNdefRecord(uri);
        } else {
          throw new IllegalArgumentException("Unknown TNF '" + tnf + "'");
        }
    } catch (Exception exception) {
      throw new IllegalArgumentException(exception);
    }
  }
  
  public static boolean isUriNdefRecord(NdefRecord ndefRecord) {
    try {
      UriNdefRecord.fromNdefRecord(ndefRecord);
      return true;
    } catch (IllegalArgumentException illegalArgumentException) {
      return false;
    }
  }
}




Java Source Code List

com.gototags.nfc.Helper.java
com.gototags.nfc.NfcTag.java
com.gototags.nfc.app.BaseActivity.java
com.gototags.nfc.app.MainActivity.java
com.gototags.nfc.ndef.ParsedNdefRecord.java
com.gototags.nfc.ndef.TextNdefRecord.java
com.gototags.nfc.ndef.UriNdefRecord.java
com.gototags.nfc.records.NdefRecordParser.java
com.gototags.nfc.records.PhoneRecord.java
com.gototags.nfc.records.RecordType.java
com.gototags.nfc.records.Record.java
com.gototags.nfc.records.TextRecord.java
com.gototags.nfc.records.WebsiteRecord.java