Android Open Source - NFCAndroid Website 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   www. j  av  a2 s  . c o m
import com.google.common.base.Preconditions;
import com.gototags.nfc.ndef.UriNdefRecord;

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

public class WebsiteRecord extends Record {
  private String url;
  
  public WebsiteRecord() {
  }
  
  public WebsiteRecord(String url) {
    this.setUrl(url);
  }
  
  public String getUrl() {
    return this.url;
  }
  
  public void setUrl(String url) {
    this.url = url;
  }
  
  @Override
  public void Validate() {
    Preconditions.checkNotNull(this.getUrl(), "The url must not be null.");
  }
  
  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    else if (!(this instanceof WebsiteRecord))
      return false;
    else {
      WebsiteRecord websiteRecord = (WebsiteRecord)obj;
      
      return 
        (this.getUrl() == null && websiteRecord.getUrl() == null) ||  
        (this.getUrl().equals(websiteRecord.getUrl()));
    }
  }
  
  @Override
  public int hashCode() {
    if (this.getUrl() == null) 
      return 0;
    else
      return this.getUrl().hashCode();
  }
  
  @Override
  public String toString() {
    return this.getUrl();
  }
  
  @Override
  protected NdefRecord toNdefRecord() {
    String str = this.getUrl();
    
    try {
      if (!(str.startsWith("http") || !str.startsWith("https"))) {
        str = "http://" + str;
      }
      
      UriNdefRecord uriNdefRecord = new UriNdefRecord(Uri.parse(url));
      
      return uriNdefRecord.toNdefRecord();
    } catch (Exception exception) {
      throw new IllegalArgumentException(exception);
    }
  }

  public static WebsiteRecord fromNdefRecord(NdefRecord ndefRecord) {
    Preconditions.checkNotNull(ndefRecord);
    
    try {
      UriNdefRecord uriNdefRecord = UriNdefRecord.fromNdefRecord(ndefRecord);
      
      Uri uri = uriNdefRecord.getUri();
      
      String scheme = uri.getScheme().toLowerCase();
      
      if ("http".equals(scheme) || "https".equals(scheme))
        return new WebsiteRecord(uri.toString());
      else 
        throw new IllegalArgumentException("Invalid scheme '" + scheme + "'");
    } catch (Exception exception) {
      throw new IllegalArgumentException(exception);
    }
  }
  
  public static boolean isWebsiteRecord(NdefRecord ndefRecord) {
    try {
      WebsiteRecord.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