Commit 3888bc8f authored by kirk's avatar kirk

fix more bugs in qr code image update; add functionality to nfc boilerplate classes

parent 5c526f6b
......@@ -17,6 +17,8 @@ package us.rader.wyfy;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Timer;
import java.util.TimerTask;
import us.rader.wyfy.model.WifiSettings;
import us.rader.wyfy.provider.FileProvider;
......@@ -36,6 +38,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.google.zxing.WriterException;
/**
* {link Fragment} to display the QR code representation of the current WIFI:
* URI
......@@ -45,8 +49,8 @@ import android.widget.ImageView;
public final class QrCodeFragment extends Fragment {
/**
* Invoke {@link QrCodeFragment#updateQrCode(WifiSettings)} in the UI thread
* after a short delay in a worker thread.
* Create the QR image {@link Bitmap} in a worker thread and update
* {@link QrCodeFragment#qrCode} in the UI thread
*
* @author Kirk
*/
......@@ -54,7 +58,14 @@ public final class QrCodeFragment extends Fragment {
AsyncTask<WifiSettings, Void, Bitmap> {
/**
* Sleep briefly
* Return the {@link Bitmap} for the QR code image representing
* <code>settings[0]</code>
*
* TODO: this will return <code>null</code> when invoked before the UI
* is actually ready to be rendered, which happens in
* {@link QrCodeFragment#onResume()} in single-pane mode; investigate
* ways to eliminate the resulting kludgery in
* {@link #onPostExecute(Bitmap)}
*
* @param settings
* Get QR code image from <code>settings[0]</code>
......@@ -62,6 +73,8 @@ public final class QrCodeFragment extends Fragment {
* @return {@link Bitmap} for QR code image
*
* @see android.os.AsyncTask#doInBackground(WifiSettings...)
* @see WifiSettings#getQrCode(int)
* @see #onPostExecute(Bitmap)
*/
@Override
protected Bitmap doInBackground(WifiSettings... settings) {
......@@ -82,7 +95,7 @@ public final class QrCodeFragment extends Fragment {
}
} catch (Exception e) {
} catch (WriterException e) {
Log.e(getClass().getName(), "doInBackground", e); //$NON-NLS-1$
......@@ -99,12 +112,35 @@ public final class QrCodeFragment extends Fragment {
* the {@link Bitmap}
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
* @see #doInBackground(WifiSettings...)
*/
@Override
protected void onPostExecute(Bitmap bitmap) {
qrCode.setImageBitmap(bitmap);
if (bitmap == null) {
// TODO: eliminate this kludge to force another refresh after
// giving the UI time to settle down
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
new UpdateQrCodeTask().execute(settings);
}
};
timer.schedule(timerTask, 500);
} else {
qrCode.setImageBitmap(bitmap);
}
}
}
......
......@@ -15,8 +15,6 @@
*/
package us.rader.wyfy;
import java.io.UnsupportedEncodingException;
import us.rader.wyfy.nfc.NdefWriterActivity;
import android.annotation.TargetApi;
import android.app.Activity;
......@@ -29,7 +27,6 @@ import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
......@@ -114,24 +111,9 @@ public final class WriteTagActivity extends NdefWriterActivity {
@Override
protected NdefMessage createNdefMessage(NdefMessage currentMessage) {
try {
byte[] bytes = uri.toString().getBytes("US-ASCII"); //$NON-NLS-1$
byte[] payload = new byte[bytes.length + 1];
payload[0] = 0;
System.arraycopy(bytes, 0, payload, 1, bytes.length);
NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_URI, null, payload);
NdefMessage ndefMessage = new NdefMessage(
new NdefRecord[] { record });
return ndefMessage;
} catch (UnsupportedEncodingException e) {
Log.e(getClass().getName(), "createNdefMessage", e); //$NON-NLS-1$
return null;
}
NdefRecord record = createUri(uri);
NdefMessage ndefMessage = new NdefMessage(new NdefRecord[] { record });
return ndefMessage;
}
......
......@@ -325,16 +325,19 @@ public abstract class ForegroundDispatchActivity<ResultType extends Parcelable>
*/
protected void onTagProcessed(ResultType result, boolean cancelled) {
Intent intent = new Intent();
if (result == null) {
if (result != null) {
setResult(RESULT_CANCELED);
} else {
int resultCode = (cancelled ? RESULT_CANCELED : RESULT_OK);
Intent intent = new Intent();
intent.putExtra(EXTRA_RESULT, result);
setResult(resultCode, intent);
}
int resultCode = (cancelled ? RESULT_CANCELED : RESULT_OK);
setResult(resultCode, intent);
finish();
}
......
......@@ -45,7 +45,7 @@ public abstract class NdefReaderActivity extends
* TODO: copy the complete list of prefixes to this implementation
* </p>
*/
private static final String[] URI_PREFIXES = { "", //$NON-NLS-1$
protected static final String[] URI_PREFIXES = { "", //$NON-NLS-1$
// 1
"http://www.", //$NON-NLS-1$
......@@ -64,7 +64,7 @@ public abstract class NdefReaderActivity extends
// 6
"mailto:" //$NON-NLS-1$
};
};
/**
* Decode the payload of certain kinds of {@link NdefRecord}
......
......@@ -15,8 +15,12 @@
*/
package us.rader.wyfy.nfc;
import java.io.UnsupportedEncodingException;
import us.rader.wyfy.R;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
......@@ -28,12 +32,183 @@ import android.util.Log;
*/
public abstract class NdefWriterActivity extends NdefReaderActivity {
/**
* Return MIME {@link NdefRecord}
*
* @param type
* the MIME type string
*
* @param payload
* the payload bytes
*
* @return MIME {@link NdefRecord}
*
* @see #createMime(String, String, String)
*/
public static NdefRecord createMime(String type, byte[] payload) {
try {
byte[] bytes = type.getBytes("US-ASCII"); //$NON-NLS-1$
return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, bytes, null,
payload);
} catch (UnsupportedEncodingException e) {
Log.e(NdefWriterActivity.class.getName(), "createMime", e); //$NON-NLS-1$
throw new IllegalArgumentException(e);
}
}
/**
* Return MIME {@link NdefRecord}
*
* @param type
* the MIME type string
*
* @param payload
* the payload string
*
* @param encoding
* the encoding for the payload (e.g. "US-ASCII", "UTF-8" etc.)
*
* @return MIME {@link NdefRecord}
*
* @see #createMime(String, byte[])
*/
public static NdefRecord createMime(String type, String payload,
String encoding) {
try {
byte[] bytes = payload.getBytes(encoding);
return createMime(type, bytes);
} catch (UnsupportedEncodingException e) {
Log.e(NdefWriterActivity.class.getName(), "createMime", e); //$NON-NLS-1$
throw new IllegalArgumentException(e);
}
}
/**
* Return a "T" {@link NdefRecord}
*
* @param language
* the language code
*
* @param text
* the text string
*
* @return "T" {@link NdefRecord}
*/
public static NdefRecord createText(String language, String text) {
try {
byte[] languageBytes = language.getBytes("UTF-8"); //$NON-NLS-1$
byte[] textBytes = text.getBytes("UTF-8"); //$NON-NLS-1$
byte[] payload = new byte[languageBytes.length + textBytes.length
+ 1];
payload[0] = (byte) languageBytes.length;
System.arraycopy(languageBytes, 0, payload, 1, languageBytes.length);
System.arraycopy(textBytes, 0, payload, languageBytes.length + 1,
textBytes.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_TEXT, null, payload);
} catch (UnsupportedEncodingException e) {
Log.e(NdefWriterActivity.class.getName(), "createText", e); //$NON-NLS-1$
throw new IllegalArgumentException(e);
}
}
/**
* Create a "U" {@link NdefRecord}
*
* @param uri
* the URI string
*
* @return "U" {@link NdefRecord}
*
* @see #createUri(Uri)
*/
public static NdefRecord createUri(String uri) {
try {
String prefix = ""; //$NON-NLS-1$
String suffix = uri;
int code = 0;
for (int index = 1; index < URI_PREFIXES.length; ++index) {
if (uri.startsWith(URI_PREFIXES[index])) {
prefix = URI_PREFIXES[index];
suffix = uri.substring(prefix.length());
code = index;
break;
}
}
byte[] bytes = suffix.getBytes("US-ASCII"); //$NON-NLS-1$
byte[] payload = new byte[bytes.length + 1];
payload[0] = (byte) code;
System.arraycopy(bytes, 0, payload, 1, bytes.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_URI, null, payload);
} catch (UnsupportedEncodingException e) {
Log.e(NdefWriterActivity.class.getName(), "createUri", e); //$NON-NLS-1$
throw new IllegalArgumentException(e);
}
}
/**
* Create a "U" {@link NdefRecord}
*
* @param uri
* the {@link Uri}
*
* @return "U" {@link NdefRecord}
*
* @see #createUri(String)
*/
public static NdefRecord createUri(Uri uri) {
return createUri(uri.toString());
}
/**
* If <code>true</code>, write-protect a {@link Tag} after writing to it.
* Otherwise, leave the {@link Tag} writable
*/
private boolean writeProtectRequested;
/**
* Pass required parameter to super class constructor
*
* @param requestCode
* the foreground dispatch request code to pass to
* {@link NdefReaderActivity#NdefReaderActivity(int)}
*/
protected NdefWriterActivity(int requestCode) {
super(requestCode);
writeProtectRequested = false;
}
/**
* Return the current value of {@link #writeProtectRequested}
*
......@@ -69,20 +244,6 @@ public abstract class NdefWriterActivity extends NdefReaderActivity {
*/
protected abstract NdefMessage createNdefMessage(NdefMessage currentMessage);
/**
* Pass required parameter to super class constructor
*
* @param requestCode
* the foreground dispatch request code to pass to
* {@link NdefReaderActivity#NdefReaderActivity(int)}
*/
protected NdefWriterActivity(int requestCode) {
super(requestCode);
writeProtectRequested = false;
}
/**
* Write the value returned by {@link #createNdefMessage(NdefMessage)} to
* the given {@link Tag}
......
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