Commit 5c526f6b authored by kirk's avatar kirk

fix bugs in how the qr code image is updated

parent 567ea4c4
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="us.rader.wyfy"
android:versionCode="15"
android:versionName="1.15" >
android:versionCode="16"
android:versionName="1.16" >
<uses-sdk
android:minSdkVersion="10"
......
......@@ -32,5 +32,8 @@
<string name="unrecognized_result_code">Unrecognized result code %1$d</string>
<string name="canceled">Canceled</string>
<string name="unrecognized_connection_outcome">Unrecognized connection outcome %1$s</string>
<string name="null_message">Status ok but message is null</string>
<string name="empty_message">Empty message</string>
<string name="unparseable_payload">Unparseable payload</string>
</resources>
......@@ -26,6 +26,7 @@ import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
......@@ -240,12 +241,6 @@ public final class MainActivity extends FragmentActivity implements
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState != null) {
return;
}
if (settings == null) {
Intent intent = getIntent();
......@@ -275,6 +270,12 @@ public final class MainActivity extends FragmentActivity implements
}
}
if (savedInstanceState != null) {
return;
}
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
......@@ -431,7 +432,37 @@ public final class MainActivity extends FragmentActivity implements
NdefMessage message = resultData
.getParcelableExtra(ForegroundDispatchActivity.EXTRA_RESULT);
alert(NdefReaderActivity.decodePayload(message.getRecords()[0]));
if (message == null) {
alert(getString(R.string.null_message));
} else {
NdefRecord[] records = message.getRecords();
if ((records == null) || (records.length < 1)) {
alert(getString(R.string.empty_message));
} else {
NdefRecord record = records[0];
String payload = NdefReaderActivity
.decodePayload(record);
if (payload == null) {
alert(getString(R.string.unparseable_payload));
} else {
alert(payload);
}
}
}
break;
default:
......
......@@ -36,8 +36,6 @@ 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
......@@ -50,40 +48,43 @@ public final class QrCodeFragment extends Fragment {
* Invoke {@link QrCodeFragment#updateQrCode(WifiSettings)} in the UI thread
* after a short delay in a worker thread.
*
* <p>
* Work around bugs due to the fact that Android doesn't finalize all of its
* layout properties until long after it is too late during initial activity
* / fragment initialization
* </p>
*
* <p>
* TODO: investigate some way to eliminate this
* </p>
*
* @author Kirk
*/
private class LazyUpdateQrCode extends AsyncTask<Void, Void, Void> {
private class UpdateQrCodeTask extends
AsyncTask<WifiSettings, Void, Bitmap> {
/**
* Sleep briefly
*
* @param settings
* return <code>settings[0]</code>
* Get QR code image from <code>settings[0]</code>
*
* @return <code>settings[0]</code>
* @return {@link Bitmap} for QR code image
*
* @see android.os.AsyncTask#doInBackground(WifiSettings...)
*/
@Override
protected Void doInBackground(Void... settings) {
protected Bitmap doInBackground(WifiSettings... settings) {
try {
Thread.sleep(1);
int size = getQrCodeSize();
if (size > 0) {
if (settings[0] == null) {
return createBlankBitmap(size);
}
return settings[0].getQrCode(size);
} catch (InterruptedException e) {
}
} catch (Exception e) {
// nothing to do here
Log.e(getClass().getName(), "doInBackground", e); //$NON-NLS-1$
}
......@@ -92,14 +93,17 @@ public final class QrCodeFragment extends Fragment {
}
/**
* invoke {@link QrCodeFragment#updateQrCode()} in the UI thread
* Set the {@link Bitmap} for {@link QrCodeFragment#qrCode}
*
* @param bitmap
* the {@link Bitmap}
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Void result) {
protected void onPostExecute(Bitmap bitmap) {
updateQrCode();
qrCode.setImageBitmap(bitmap);
}
......@@ -216,15 +220,15 @@ public final class QrCodeFragment extends Fragment {
}
/**
* display the QR code image
* Update the QR code when resuming
*
* @see android.support.v4.app.Fragment#onResume()
*/
@Override
public void onStart() {
public void onResume() {
super.onStart();
new LazyUpdateQrCode().execute();
super.onResume();
new UpdateQrCodeTask().execute(settings);
}
......@@ -284,7 +288,7 @@ public final class QrCodeFragment extends Fragment {
public void updateQrCode(WifiSettings settings) {
this.settings = settings;
updateQrCode();
new UpdateQrCodeTask().execute(settings);
}
......@@ -319,37 +323,4 @@ public final class QrCodeFragment extends Fragment {
}
/**
* Update {@link #qrCode} to display the current value of {@link #settings}
*/
private void updateQrCode() {
int size = getQrCodeSize();
if (size > 0) {
try {
Bitmap bitmap;
if (settings == null) {
bitmap = createBlankBitmap(size);
} else {
bitmap = settings.getQrCode(size);
}
qrCode.setImageBitmap(bitmap);
} catch (WriterException e) {
Log.e(getClass().getName(), "updateQrCode", e); //$NON-NLS-1$
}
}
}
}
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