diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java index 1d898a060..8ff5f9c4a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java @@ -31,10 +31,41 @@ 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; + private static interface SafeNFC { + public void register( Activity activity ); + } + + private static class SafeNFCImpl implements SafeNFC { + public void register( final Activity activity ) + { + NfcAdapter.CreateNdefMessageCallback cb = + new NfcAdapter.CreateNdefMessageCallback() { + public NdefMessage createNdefMessage( NfcEvent event ) { + NdefMessage msg = null; + if ( null != s_data ) { + msg = makeMessage( activity, s_data ); + } + return msg; + } + }; + + NfcManager manager = + (NfcManager)activity.getSystemService( Context.NFC_SERVICE ); + NfcAdapter adapter = manager.getDefaultAdapter(); + adapter.setNdefPushMessageCallback( cb, activity ); + } + } + public static boolean nfcAvail( Context context ) { boolean result = s_inSDK; @@ -47,25 +78,6 @@ public class NFCUtils { return result; } - public static void register( final Activity activity ) - { - NfcAdapter.CreateNdefMessageCallback cb = - new NfcAdapter.CreateNdefMessageCallback() { - public NdefMessage createNdefMessage( NfcEvent event ) { - NdefMessage msg = null; - if ( null != s_data ) { - msg = makeMessage( activity, s_data ); - } - return msg; - } - }; - - NfcManager manager = - (NfcManager)activity.getSystemService( Context.NFC_SERVICE ); - NfcAdapter adapter = manager.getDefaultAdapter(); - adapter.setNdefPushMessageCallback( cb, activity ); - } - public static void buildAndPush( Activity activity, String data ) { s_data = 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 );