Commit 2a0af685 authored by Kirk Rader's avatar Kirk Rader

refactorings and documentation improvements to nfc boilerplate classes

parent d1700ae6
......@@ -39,5 +39,6 @@
<string name="saved_label">Saved</string>
<string name="title_activity_delete_rows">Database</string>
<string name="delete_ssid_prompt">Delete %1$s?</string>
<string name="error_processing_tag">Error processing tag</string>
</resources>
......@@ -27,29 +27,34 @@ import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.os.Parcelable;
import android.util.Log;
/**
* {@link ForegroundDispatchActivity} that extracts a {@link NdefMessage} from a
* NDEF-compatible {@link Tag}
*
* Also provides utility methods for parsing the contents of certain kinds of
* {@link NdefRecord}
*
* @author Kirk
*
* @see ForegroundDispatchActivity
* @see NdefWriterActivity
* @see NdefRecordConstants
* @see #decodePayload(NdefRecord)
* @see #processTag(Intent)
* @see #onTagProcessed(NdefMessage)
*/
public abstract class NdefReaderActivity extends
ForegroundDispatchActivity<NdefMessage> implements NdefRecordConstants {
/**
* {@link Intent} extras key used to return the value passed to
* {@link #onTagProcessed(NdefMessage, boolean)} to the {@link Activity}
* that launched this one
*/
public static final String EXTRA_RESULT = "us.rader.wyfy.nfc.result"; //$NON-NLS-1$
/**
* Result code passed to {@link #setResult(int)} or
* {@link #setResult(int, Intent)} to indicate an error
* {@link #onTagProcessed(NdefMessage)} to the {@link Activity} that
* launched this one
*/
public static final int RESULT_ERROR_PROCESSING_TAG = RESULT_FIRST_USER;
public static final String EXTRA_RESULT = "us.rader.wyfy.nfc.result"; //$NON-NLS-1$
/**
* Decode the payload of certain kinds of {@link NdefRecord}
......@@ -292,130 +297,89 @@ public abstract class NdefReaderActivity extends
}
/**
* Extract an {@link NdefMessage} from the given {@link Tag}
* Invoke {@link #setResult(int)} or {@link #setResult(int, Intent)}, as
* appropriate, and then {@link #finish()}
*
* @param tag
* the {@link Tag}
* @param result
* the {@link NdefMessage} returned by
* {@link #processTag(Intent)} or <code>null</code>
*
* @param task
* the {@link ForegroundDispatchActivity.ProcessTagTask} running
* this method
* @see ForegroundDispatchActivity#onTagProcessed(Object)
*/
@Override
protected void onTagProcessed(NdefMessage result) {
if (result == null) {
setResult(RESULT_CANCELED);
} else {
Intent intent = new Intent();
intent.putExtra(EXTRA_RESULT, result);
setResult(RESULT_OK, intent);
}
finish();
}
/**
* Extract an {@link NdefMessage} from the given {@link Tag}
*
* @param intent
* the {@link Intent}
*
* @return the {@link NdefMessage} or <code>null</code> if something goes
* wrong
*
* @see ForegroundDispatchActivity#processTag(android.nfc.Tag,
* ForegroundDispatchActivity.ProcessTagTask)
* @see ForegroundDispatchActivity#processTag(Intent)
*/
@Override
protected NdefMessage processTag(Tag tag, ProcessTagTask task) {
Ndef ndef = Ndef.get(tag);
protected NdefMessage processTag(Intent intent) {
if (ndef == null) {
if (intent == null) {
task.report(getString(R.string.incompatible_tag));
return null;
}
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
Parcelable[] ndefMessages = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (null == ndefMessage) {
if ((null != ndefMessages) && (ndefMessages.length > 0)) {
task.report(getString(R.string.empty_tag));
return (NdefMessage) ndefMessages[0];
}
return ndefMessage;
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
}
if (tag == null) {
/**
* Invoke {@link #setResult(int, Intent)} and then {@link #finish()}
*
* <p>
* The parameters passed to {@link #setResult(int, Intent)} or
* {@link #setResult(int)} will be determined as follows:
* </p>
*
* <table>
*
* <tr>
* <th><code>result</code></th>
* <th><code>cancelled</code></th>
* <th>Invoke</th>
* </tr>
*
* <tr>
* <td><code>null</code></td>
* <td><code>false</code></td>
* <td><code>setResult({@link #RESULT_ERROR_PROCESSING_TAG})</td>
* </tr>
*
* <tr>
* <td><code>null</code></td>
* <td><code>true</code></td>
* <td><code>setResult({@link #RESULT_CANCELED})</td>
* </tr>
*
* <tr>
* <td>non-<code>null</code></td>
* <td><code>false</code></td>
* <td><code>setResult({@link #RESULT_OK}, intent)</td>
* </tr>
*
* <tr>
* <td>non-<code>null</code></td>
* <td><code>true</code></td>
* <td><code>setResult({@link #RESULT_CANCELED}, intent)</td>
* </tr>
*
* </table>
*
* <p>
* In each of the cases where an <code>intent</code> is passed to
* {@link #setResult(int, Intent)}, <code>result</code> will be passed as a
* "parcelable extra" with the key {@link #EXTRA_RESULT}
*
* @param result
* the {@link NdefMessage} returned by
* {@link #processTag(Tag, us.rader.wyfy.nfc.ForegroundDispatchActivity.ProcessTagTask)}
* or <code>null</code>
*
* @param cancelled
* <code>true</code> if and only if the
* {@link ForegroundDispatchActivity.ProcessTagTask} invoking
* this method was cancelled
*
* @see us.rader.wyfy.nfc.ForegroundDispatchActivity#onTagProcessed(java.lang.Object,
* boolean)
*/
@Override
protected void onTagProcessed(NdefMessage result, boolean cancelled) {
return null;
int resultCode = (cancelled ? RESULT_CANCELED : RESULT_OK);
}
if (result == null) {
Ndef ndef = Ndef.get(tag);
if (resultCode != RESULT_CANCELED) {
if (ndef == null) {
Log.e(getClass().getName(), "result is null but not cancelled"); //$NON-NLS-1$
resultCode = RESULT_ERROR_PROCESSING_TAG;
toast(getString(R.string.incompatible_tag));
return null;
}
}
setResult(resultCode);
NdefMessage ndefMessage = ndef.getCachedNdefMessage();
} else {
if (null == ndefMessage) {
Intent intent = new Intent();
intent.putExtra(EXTRA_RESULT, result);
setResult(resultCode, intent);
toast(getString(R.string.empty_tag));
}
finish();
return ndefMessage;
}
......
......@@ -18,9 +18,11 @@ package us.rader.wyfy.nfc;
import java.io.UnsupportedEncodingException;
import us.rader.wyfy.R;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
......@@ -312,29 +314,26 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
* Write the value returned by {@link #createNdefMessage(NdefMessage)} to
* the given {@link Tag}
*
* @param tag
* the {@link Tag}
*
* @param task
* the {@link ForegroundDispatchActivity.ProcessTagTask} running
* this method
* @param intent
* the {@link Intent}
*
* @return the {@link NdefMessage} that was written to the {@link Tag} or
* <code>null</code>
*
* @see NdefReaderActivity#processTag(Tag,
* ForegroundDispatchActivity.ProcessTagTask)
* @see NdefReaderActivity#processTag(Intent)
*/
@Override
protected final NdefMessage processTag(Tag tag, ProcessTagTask task) {
protected final NdefMessage processTag(Intent intent) {
try {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndef = Ndef.get(tag);
if (ndef != null) {
return writeNdef(ndef, task);
return writeNdef(ndef);
}
......@@ -342,11 +341,11 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
if (formatable != null) {
return writeFormatable(formatable, task);
return writeFormatable(formatable);
}
task.report(getString(R.string.incompatible_tag));
toast(getString(R.string.incompatible_tag));
return null;
} catch (Exception e) {
......@@ -364,14 +363,9 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
* @param formatable
* the {@link NdefFormatable} tag
*
* @param task
* the {@link ForegroundDispatchActivity.ProcessTagTask} running
* this method
*
* @return the {@link NdefMessage} written to the tag
*/
private NdefMessage writeFormatable(NdefFormatable formatable,
ProcessTagTask task) {
private NdefMessage writeFormatable(NdefFormatable formatable) {
try {
......@@ -379,7 +373,6 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
if (ndefMessage == null) {
task.cancel(false);
return null;
}
......@@ -409,7 +402,7 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
} catch (Exception e) {
Log.e(NdefWriterActivity.class.getName(), "writeFormatable", e); //$NON-NLS-1$
task.report(getString(R.string.error_formatting_tag));
toast(getString(R.string.error_formatting_tag));
return null;
}
......@@ -422,19 +415,15 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
* @param ndef
* the {@link Ndef} tag
*
* @param task
* the {@link ForegroundDispatchActivity.ProcessTagTask} running
* this method
*
* @return the {@link NdefMessage} written to the tag
*/
private NdefMessage writeNdef(Ndef ndef, ProcessTagTask task) {
private NdefMessage writeNdef(Ndef ndef) {
try {
if (!ndef.isWritable()) {
task.report(getString(R.string.read_only_tag));
toast(getString(R.string.read_only_tag));
return null;
}
......@@ -444,7 +433,6 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
if (ndefMessage == null) {
task.cancel(false);
return null;
}
......@@ -454,8 +442,7 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
if (bytes.length > max) {
task.report(getString(R.string.tag_size_exceeded, bytes.length,
max));
toast(getString(R.string.tag_size_exceeded, bytes.length, max));
return null;
}
......@@ -470,7 +457,7 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
if (!ndef.makeReadOnly()) {
task.report(getString(R.string.failed_to_write_protect_tag));
toast(getString(R.string.failed_to_write_protect_tag));
return null;
}
......@@ -487,7 +474,7 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
} catch (Exception e) {
Log.e(NdefWriterActivity.class.getName(), "writeNdef", e); //$NON-NLS-1$
task.report(getString(R.string.error_writing_tag));
toast(getString(R.string.error_writing_tag));
return null;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment