Skip to content
Browse files

fix: introduce Preconditions, fix: SmartPoster.getFirstIfExists

  • Loading branch information...
1 parent 015b49c commit 179c6d8a34280cd437c49fd56cb6c8343668a64e yoshihiro503 committed
View
20 src/main/scala/record/SmartPoster.scala
@@ -10,7 +10,8 @@ import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
-import org.proofcafe.nfcdemo.NdefMessageParser;
+import org.proofcafe.nfcdemo.NdefMessageParser
+import org.proofcafe.nfcdemo.util.Preconditions
import java.util.Arrays;
import java.util.NoSuchElementException;
@@ -39,7 +40,7 @@ class SmartPoster private (uri: UriRecord, title: TextRecord, action: SmartPoste
* records are just metadata about this record. There MUST be one URI record
* and there MUST NOT be more than one."
*/
- private val mUriRecord : UriRecord = uri //Preconditions.checkNotNull(uri)
+ private val mUriRecord : UriRecord = Preconditions.checkNotNull(uri)
/**
* NFC Forum Smart Poster Record Type Definition section 3.2.1.
@@ -52,7 +53,7 @@ class SmartPoster private (uri: UriRecord, title: TextRecord, action: SmartPoste
* the UI designer may ignore it, but doing so will induce a different user
* experience from device to device."
*/
- private val mAction : RecommendedAction = action //Preconditions.checkNotNull(action)
+ private val mAction : RecommendedAction = Preconditions.checkNotNull(action)
/**
* NFC Forum Smart Poster Record Type Definition section 3.2.1.
@@ -92,8 +93,8 @@ class SmartPoster private (uri: UriRecord, title: TextRecord, action: SmartPoste
object SmartPoster {
def parse(record : NdefRecord) : SmartPoster = {
- // Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_WELL_KNOWN);
- // Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_SMART_POSTER));
+ Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_WELL_KNOWN);
+ Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_SMART_POSTER));
try {
val subRecords = new NdefMessage(record.getPayload());
parse(subRecords.getRecords());
@@ -112,7 +113,8 @@ object SmartPoster {
case List(uri:UriRecord) => uri
case _ => throw new IllegalArgumentException()
}
- val title = getFirstIfExists[TextRecord](records)
+ val title =
+ getFirstIfExists(records, classOf[TextRecord]).orNull
val action = parseRecommendedAction(recordsRaw)
val typ = parseType(recordsRaw);
new SmartPoster(uri, title, action, typ);
@@ -135,10 +137,10 @@ object SmartPoster {
* Returns the first element of {@code elements} which is an instance of
* {@code type}, or {@code null} if no such element exists.
*/
- private def getFirstIfExists[T >: Null](elements: List[_]): T =
+ private def getFirstIfExists[T](elements: List[Any], t: Class[T]): Option[T] =
elements match {
- case (x:T) :: xs => x
- case _ => null
+ case x :: xs if (t.isInstance(x)) => Some(t.cast(x))
+ case _ => None
}
private def getByType(typ : Array[Byte], records : Array[NdefRecord]) : NdefRecord = {
View
10 src/main/scala/record/TextRecord.scala
@@ -7,6 +7,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
+import org.proofcafe.nfcdemo.util.Preconditions
+
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
@@ -16,9 +18,9 @@ import java.util.Arrays;
class TextRecord private (languageCode : String, text : String) extends ParsedNdefRecord {
/** ISO/IANA language code */
- private val mLanguageCode = languageCode //Preconditions.checkNotNull(languageCode);
+ private val mLanguageCode = Preconditions.checkNotNull(languageCode);
- private val mText = text //Preconditions.checkNotNull(text);
+ private val mText = Preconditions.checkNotNull(text);
def getView(activity : Activity, inflater : LayoutInflater, parent : ViewGroup, offset : Int) = {
val text : TextView = new TextView(activity) //TODO (TextView) inflater.inflate(R.layout.tag_text, parent, false);
@@ -41,8 +43,8 @@ object TextRecord {
// TODO: deal with text fields which span multiple NdefRecords
def parse(record : NdefRecord) : TextRecord = {
- // Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_WELL_KNOWN);
- // Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_TEXT));
+ Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_WELL_KNOWN);
+ Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_TEXT));
try {
val payload = record.getPayload();
/*
View
7 src/main/scala/record/UriRecord.scala
@@ -9,6 +9,8 @@ import android.view.ViewGroup;
import android.widget.TextView;
import android.util.Log
+import org.proofcafe.nfcdemo.util.Preconditions
+
import java.nio.charset.Charset;
import java.util.Arrays;
import scala.collection.immutable.HashMap
@@ -21,7 +23,7 @@ class UriRecord(uri : Uri) extends ParsedNdefRecord {
val TAG = "UriRecord"
val RECORD_TYPE = "UriRecord"
- private val mUri = uri //Preconditions.checkNotNull(uri);
+ private val mUri = Preconditions.checkNotNull(uri);
def getView(activity : Activity, inflater : LayoutInflater, parent : ViewGroup, offset : Int) = {
val text : TextView = new TextView(activity) //TODO inflater.inflate("Text", parent, false);
@@ -108,9 +110,8 @@ object UriRecord {
/** Parse an well known URI record */
private def parseWellKnown(record : NdefRecord) = {
- //Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_URI));
+ Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_URI));
val payload = record.getPayload()
- Log.d("TAG", "payload[0]="+payload(0))
/*
* payload[0] contains the URI Identifier Code, per the
* NFC Forum "URI Record Type Definition" section 3.2.2.
View
55 src/main/scala/simulator/FakeTagsActivity.scala
@@ -9,6 +9,9 @@ import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
+import android.util.Log
+
+import org.proofcafe.nfcdemo.util.Preconditions
import java.nio.charset.Charset;
import java.util.Locale;
@@ -25,32 +28,38 @@ class FakeTagsActivity extends ListActivity {
var mAdapter : ArrayAdapter[TagDescription] = null;
- override def onCreate(savedState : Bundle) {
- super.onCreate(savedState);
- val adapter = new ArrayAdapter[TagDescription](this, android.R.layout.simple_list_item_1, android.R.id.text1)
-
- adapter.add(
- new TagDescription("Broadcast NFC Text Tag", MockNdefMessages.ENGLISH_PLAIN_TEXT));
- adapter.add(new TagDescription(
- "Broadcast NFC SmartPoster URL & text", MockNdefMessages.SMART_POSTER_URL_AND_TEXT));
- adapter.add(new TagDescription(
- "Broadcast NFC SmartPoster URL", MockNdefMessages.SMART_POSTER_URL_NO_TEXT));
- setListAdapter(adapter);
- mAdapter = adapter;
- }
+ override def onCreate(savedState : Bundle) {
+ super.onCreate(savedState)
+ val adapter = new ArrayAdapter[TagDescription](this, android.R.layout.simple_list_item_1, android.R.id.text1)
+
+ adapter.add(new TagDescription(
+ "Broadcast NFC Text Tag",
+ MockNdefMessages.ENGLISH_PLAIN_TEXT
+ ))
+ adapter.add(new TagDescription(
+ "Broadcast NFC SmartPoster URL & text",
+ MockNdefMessages.SMART_POSTER_URL_AND_TEXT
+ ))
+ adapter.add(new TagDescription(
+ "Broadcast NFC SmartPoster URL",
+ MockNdefMessages.SMART_POSTER_URL_NO_TEXT
+ ))
+ setListAdapter(adapter)
+ mAdapter = adapter
+ }
- override def onListItemClick(l : ListView, v : View, position : Int, id : Long) {
- val description = mAdapter.getItem(position)
- val intent = new Intent(NfcAdapter.ACTION_TAG_DISCOVERED)
- intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, description.msgs)
- startActivity(intent)
- }
+ override def onListItemClick(l: ListView, v: View, position: Int, id: Long) {
+ val description = mAdapter.getItem(position)
+ val intent = new Intent(NfcAdapter.ACTION_TAG_DISCOVERED)
+ intent.putExtra(NfcAdapter.EXTRA_NDEF_MESSAGES, description.msgs)
+ startActivity(intent)
+ }
}
object FakeTagsActivity {
def newTextRecord(text : String, locale : Locale, encodeInUtf8 : Boolean) = {
- // Preconditions.checkNotNull(text);
- // Preconditions.checkNotNull(locale);
+ Preconditions.checkNotNull(text);
+ Preconditions.checkNotNull(locale);
val langBytes = locale.getLanguage().getBytes(Charset.forName("US_ASCII"));
val utfEncoding = if (encodeInUtf8) Charset.forName("UTF_8") else Charset.forName("UTF-16")
val textBytes = text.getBytes(utfEncoding);
@@ -61,8 +70,8 @@ object FakeTagsActivity {
}
def newMimeRecord(typ : String, data : Array[Byte]) = {
- // Preconditions.checkNotNull(typ);
- // Preconditions.checkNotNull(data);
+ Preconditions.checkNotNull(typ);
+ Preconditions.checkNotNull(data);
val typeBytes = typ.getBytes(Charset.forName("US_ASCII"));
new NdefRecord(NdefRecord.TNF_MIME_MEDIA, typeBytes, Array[Byte](), data);
}

0 comments on commit 179c6d8

Please sign in to comment.
Something went wrong with that request. Please try again.