mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-11 08:48:06 +01:00
use interface trick to avoid NFC code crashing on 2.1
This commit is contained in:
parent
7d1a1228f0
commit
85a533c74f
1 changed files with 40 additions and 21 deletions
|
@ -31,10 +31,41 @@ import android.nfc.NfcManager;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|
||||||
public class NFCUtils {
|
public class NFCUtils {
|
||||||
private static boolean s_inSDK =
|
private static boolean s_inSDK;
|
||||||
14 <= Integer.valueOf( android.os.Build.VERSION.SDK );
|
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 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 )
|
public static boolean nfcAvail( Context context )
|
||||||
{
|
{
|
||||||
boolean result = s_inSDK;
|
boolean result = s_inSDK;
|
||||||
|
@ -47,25 +78,6 @@ public class NFCUtils {
|
||||||
return result;
|
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 )
|
public static void buildAndPush( Activity activity, String data )
|
||||||
{
|
{
|
||||||
s_data = data;
|
s_data = data;
|
||||||
|
@ -90,6 +102,13 @@ public class NFCUtils {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void register( Activity activity )
|
||||||
|
{
|
||||||
|
if ( null != s_safeNFC ) {
|
||||||
|
s_safeNFC.register( activity );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static NdefMessage makeMessage( Activity activity, String data )
|
private static NdefMessage makeMessage( Activity activity, String data )
|
||||||
{
|
{
|
||||||
String mimeType = activity.getString( R.string.xwords_nfc_mime );
|
String mimeType = activity.getString( R.string.xwords_nfc_mime );
|
||||||
|
|
Loading…
Add table
Reference in a new issue