From b3bc738cae19af347ebeca24285b40dbcc1c9ad5 Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 12 Nov 2013 19:06:26 -0800 Subject: [PATCH] move nfc stuff into separate utility class, and check API version for whether to support it. Oddly the app runs on 2.1 emulator without classnotfound error -- I was expecting to have to use the interface trick. --- .../org/eehouse/android/xw4/DlgDelegate.java | 2 +- .../org/eehouse/android/xw4/GameUtils.java | 17 +---- .../org/eehouse/android/xw4/GamesList.java | 13 +--- .../src/org/eehouse/android/xw4/NFCUtils.java | 70 +++++++++++++++++++ .../src/org/eehouse/android/xw4/XWApp.java | 1 - 5 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java index 94f68bbf1..e209dcf4b 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java @@ -411,7 +411,7 @@ public class DlgDelegate { .setPositiveButton( R.string.button_text, lstnr ) .setNegativeButton( R.string.button_html, lstnr ); - if ( XWApp.NFC_ENABLED ) { + if ( NFCUtils.nfcAvail() ) { builder.setNeutralButton( R.string.button_nfc, lstnr ); } Dialog dialog = builder.create(); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java index b6e3606cb..e2f89a64b 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java @@ -27,16 +27,11 @@ import android.graphics.Bitmap; import android.graphics.Rect; import android.net.Uri; -import android.nfc.NdefMessage; -import android.nfc.NdefRecord; -import android.nfc.NfcAdapter; - import android.text.Html; import android.text.TextUtils; import android.view.Display; import java.io.File; import java.io.FileOutputStream; -import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; @@ -563,17 +558,7 @@ public class GameUtils { if ( null != msgString ) { if ( DlgDelegate.NFC_BTN == chosen ) { - NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter( activity ); - NdefMessage msg = new NdefMessage( new NdefRecord[] { - new NdefRecord(NdefRecord.TNF_MIME_MEDIA, - "application/org.eehouse.android.xw4" - .getBytes(Charset.forName("US-ASCII")), - new byte[0], msgString. - getBytes(Charset.forName("US-ASCII"))) - ,NdefRecord. - createApplicationRecord("org.eehouse.android.xw4") - }); - nfcAdapter.setNdefPushMessage( msg, activity ); + NFCUtils.buildAndPush( activity, msgString ); Utils.showToast( activity, "Tap the receiving device now" ); } else { boolean choseEmail = DlgDelegate.EMAIL_BTN == chosen; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java index bbc802f6d..0342b8907 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesList.java @@ -28,13 +28,10 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; -import android.nfc.NdefMessage; -import android.nfc.NfcAdapter; import android.os.Bundle; import android.os.Handler; import android.os.Message; -import android.os.Parcelable; import android.preference.PreferenceManager; import android.view.Menu; import android.view.MenuInflater; @@ -1129,14 +1126,8 @@ public class GamesList extends XWExpandableListActivity private void tryNFCIntent( Intent intent ) { - if ( NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction()) ) { - Parcelable[] rawMsgs = - intent.getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES ); - // only one message sent during the beam - NdefMessage msg = (NdefMessage) rawMsgs[0]; - // record 0 contains the MIME type, record 1 is the AAR, if present - String data = new String( msg.getRecords()[0].getPayload() ); - + String data = NFCUtils.getFromIntent( intent ); + if ( null != data ) { NetLaunchInfo nli = new NetLaunchInfo( data ); if ( nli.isValid() ) { startNewNetGame( nli ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java new file mode 100644 index 000000000..8f4ba702a --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NFCUtils.java @@ -0,0 +1,70 @@ +/* -*- compile-command: "cd ../../../../../; ant debug install"; -*- */ +/* + * Copyright 2013 by Eric House (xwords@eehouse.org). All rights + * reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.eehouse.android.xw4; + +import android.app.Activity; +import android.content.Intent; +import android.nfc.NdefMessage; +import android.nfc.NdefRecord; +import android.nfc.NfcAdapter; +import android.os.Parcelable; +import java.nio.charset.Charset; + +public class NFCUtils { + private static boolean s_inSDK = + 14 <= Integer.valueOf( android.os.Build.VERSION.SDK ); + + public static boolean nfcAvail() + { + return s_inSDK /*&& turnedOn*/; + } + + public static void buildAndPush( Activity activity, String data ) + { + NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter( activity ); + NdefMessage msg = new NdefMessage( new NdefRecord[] { + new NdefRecord(NdefRecord.TNF_MIME_MEDIA, + "application/org.eehouse.android.xw4" + .getBytes(Charset.forName("US-ASCII")), + new byte[0], + data.getBytes(Charset.forName("US-ASCII"))) + ,NdefRecord. + createApplicationRecord("org.eehouse.android.xw4") + }); + nfcAdapter.setNdefPushMessage( msg, activity ); + } + + public static String getFromIntent( Intent intent ) + { + String result = null; + + if ( NfcAdapter.ACTION_NDEF_DISCOVERED.equals( intent.getAction() ) ) { + Parcelable[] rawMsgs = + intent.getParcelableArrayExtra( NfcAdapter.EXTRA_NDEF_MESSAGES ); + // only one message sent during the beam + NdefMessage msg = (NdefMessage)rawMsgs[0]; + // record 0 contains the MIME type, record 1 is the AAR, if present + result = new String( msg.getRecords()[0].getPayload() ); + } + + return result; + } +} diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java index bdb9588a8..27fba48bf 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java @@ -40,7 +40,6 @@ public class XWApp extends Application { public static final boolean DEBUG_EXP_TIMERS = false && DEBUG; public static final boolean GCM_IGNORED = false; public static final boolean UDP_ENABLED = true; - public static final boolean NFC_ENABLED = true; public static final String SMS_PUBLIC_HEADER = "-XW4"; public static final int MAX_TRAY_TILES = 7; // comtypes.h