Android Open Source - NFCAndroid 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.records;
/*from  ww  w . j a  v a2 s . c om*/
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;

import com.google.common.base.Preconditions;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;

public abstract class Record {  
  protected abstract NdefRecord toNdefRecord();
  
  public NdefRecord getNdefRecord() {
    this.Validate();
    
    return this.toNdefRecord();
  }
  
  public void Validate() {
  }
  
  public RecordType getRecordType() {
    return Record.getRecordType(this.getClass());
  }
  
  public static Record[] fromNdefMessage(NdefMessage ndefMessage) {
    ArrayList<Record> records = new ArrayList<Record>();
    
    for (NdefRecord ndefRecord : ndefMessage.getRecords()) {
      Record record = Record.lookupFromNdefRecord(ndefRecord);
      
      if (record != null) {
        records.add(record);
      }
    }
    
    return records.toArray(new Record[records.size()]);
  }
  
  public static Record lookupFromNdefRecord(NdefRecord ndefRecord) {
    if (ndefRecord != null) {
      for (RecordType recordType : Record.RecordTypes) {
        NdefRecordParser ndefRecordParser = recordType.getNdefRecordParser();
        Preconditions.checkNotNull(ndefRecordParser);
        
        if (ndefRecordParser.isRecordType(ndefRecord)) {
          Record record = ndefRecordParser.fromNdefRecord(ndefRecord);
          Preconditions.checkNotNull(record);
          
          return record;
        }
      }
    }
    
    return null;
  }
  
  private static ArrayList<RecordType> RecordTypes = new ArrayList<RecordType>();

  public static void registerRecordType(RecordType recordType) {  
    Record.RecordTypes.add(recordType);
    
    Collections.sort(Record.RecordTypes);
  }
  
  public static void unregisterRecordType(Type type) {
    for (Integer i = 0; i < Record.RecordTypes.size(); i++) {
      RecordType recordType = Record.RecordTypes.get(i);
      
      if (recordType.getType().equals(type)) {
        Record.RecordTypes.remove(i);
        break;
      }
    }
  }
  
  public static void unregisterAllRecordTypes() {
    Record.RecordTypes.clear();
  }
  
  public static ArrayList<RecordType> getRecordTypes() {
    return Record.RecordTypes;
  }
  
  public static RecordType getRecordType(Type type) {
    for (RecordType recordType : Record.RecordTypes) {
      if (recordType.getType().equals(type))
        return recordType;
    }
    return null;
  }
  
  static {
    Record.registerRecordType(new RecordType("Text", TextRecord.class, 1, new NdefRecordParser(){
      public Record fromNdefRecord(NdefRecord ndefRecord) {
        return TextRecord.fromNdefRecord(ndefRecord);
      }

      public boolean isRecordType(NdefRecord ndefRecord) {
        return TextRecord.isTextRecord(ndefRecord);
      }
    }));
    
    Record.registerRecordType(new RecordType("Phone", PhoneRecord.class, 1, new NdefRecordParser(){
      public Record fromNdefRecord(NdefRecord ndefRecord) {
        return PhoneRecord.fromNdefRecord(ndefRecord);
      }

      public boolean isRecordType(NdefRecord ndefRecord) {
        return PhoneRecord.isPhoneRecord(ndefRecord);
      }
    }));
    
    Record.registerRecordType(new RecordType("Website", WebsiteRecord.class, 1, new NdefRecordParser(){
      public Record fromNdefRecord(NdefRecord ndefRecord) {
        return WebsiteRecord.fromNdefRecord(ndefRecord);
      }

      public boolean isRecordType(NdefRecord ndefRecord) {
        return WebsiteRecord.isWebsiteRecord(ndefRecord);
      }
    }));  
  }
}




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