Commit 96a41356 authored by kirk's avatar kirk

Support legacy tags written with earlier versions of this app

parent 57ac70ca
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="us.rader.wyfy"
android:versionCode="16"
android:versionName="1.16" >
android:versionCode="17"
android:versionName="1.17" >
<uses-sdk
android:minSdkVersion="10"
......@@ -34,7 +34,16 @@
<data android:scheme="wifi" />
</intent-filter>
</activity>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="application/x-wyfy" />
</intent-filter>
</activity>
<activity
android:name="us.rader.wyfy.WriteTagActivity"
android:label="@string/title_activity_write_tag"
......
......@@ -27,8 +27,10 @@ import android.net.Uri;
import android.net.wifi.WifiManager;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
......@@ -251,19 +253,16 @@ public final class MainActivity extends FragmentActivity implements
if (uri != null) {
try {
initialize(uri.toString());
settings = WifiSettings.parse(uri);
if (settings != null) {
} else {
connectOnStart = true;
Parcelable[] ndefMessages = intent
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
}
if ((ndefMessages != null) && (ndefMessages.length > 0)) {
} catch (Exception e) {
Log.e(getClass().getName(), "error parsing URI", e); //$NON-NLS-1$
initialize((NdefMessage) ndefMessages[0]);
}
}
......@@ -298,6 +297,70 @@ public final class MainActivity extends FragmentActivity implements
}
/**
* Initialize from a legacy {@link NdefMessage}
*
* Provide backward compatibility for tags written with older versions of
* this app
*
* @param ndefMessage
* legacy {@link NdefMessage}
*/
private void initialize(NdefMessage ndefMessage) {
try {
NdefRecord[] records = ndefMessage.getRecords();
if (records.length > 0) {
NdefRecord record = records[0];
if (record.getTnf() != NdefRecord.TNF_MIME_MEDIA) {
return;
}
String type = new String(record.getType(), "US-ASCII"); //$NON-NLS-1$
if ("application/x-wyfy".equals(type)) { //$NON-NLS-1$
String payload = new String(record.getPayload(), "US-ASCII"); //$NON-NLS-1$
initialize(payload);
}
}
} catch (Exception e) {
Log.e(getClass().getName(), "initializeNdefMessage", e); //$NON-NLS-1$
}
}
/**
* Initialize {@link #settings} from the given WIFI: {@link Uri}
*
* @param uri
* WIFI: {@link Uri}
*/
private void initialize(String uri) {
try {
settings = WifiSettings.parse(uri);
if (settings != null) {
connectOnStart = true;
}
} catch (Exception e) {
Log.e(getClass().getName(), "error parsing URI", e); //$NON-NLS-1$
}
}
/**
* Call {@link WifiSettings#connect(WifiManager)} if {@link #settings} is
* not <code>null</code>
......
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