use interface trick to avoid NFC code crashing on 2.1

This commit is contained in:
Eric House 2013-11-14 08:03:28 -08:00
parent 7d1a1228f0
commit 85a533c74f

View file

@ -31,23 +31,22 @@ import android.nfc.NfcManager;
import android.os.Parcelable;
public class NFCUtils {
private static boolean s_inSDK =
14 <= Integer.valueOf( android.os.Build.VERSION.SDK );
private static boolean s_inSDK;
private static SafeNFC s_safeNFC;
static {
s_inSDK = 14 <= Integer.valueOf( android.os.Build.VERSION.SDK );
if ( s_inSDK ) {
s_safeNFC = new SafeNFCImpl();
}
}
private static String s_data = null;
public static boolean nfcAvail( Context context )
{
boolean result = s_inSDK;
if ( result ) {
NfcManager manager =
(NfcManager)context.getSystemService( Context.NFC_SERVICE );
NfcAdapter adapter = manager.getDefaultAdapter();
result = null != adapter && adapter.isEnabled();
}
return result;
private static interface SafeNFC {
public void register( Activity activity );
}
public static void register( final Activity activity )
private static class SafeNFCImpl implements SafeNFC {
public void register( final Activity activity )
{
NfcAdapter.CreateNdefMessageCallback cb =
new NfcAdapter.CreateNdefMessageCallback() {
@ -65,6 +64,19 @@ public class NFCUtils {
NfcAdapter adapter = manager.getDefaultAdapter();
adapter.setNdefPushMessageCallback( cb, activity );
}
}
public static boolean nfcAvail( Context context )
{
boolean result = s_inSDK;
if ( result ) {
NfcManager manager =
(NfcManager)context.getSystemService( Context.NFC_SERVICE );
NfcAdapter adapter = manager.getDefaultAdapter();
result = null != adapter && adapter.isEnabled();
}
return result;
}
public static void buildAndPush( Activity activity, String data )
{
@ -90,6 +102,13 @@ public class NFCUtils {
return result;
}
public static void register( Activity activity )
{
if ( null != s_safeNFC ) {
s_safeNFC.register( activity );
}
}
private static NdefMessage makeMessage( Activity activity, String data )
{
String mimeType = activity.getString( R.string.xwords_nfc_mime );