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.
This commit is contained in:
Eric House 2013-11-12 19:06:26 -08:00
parent f291ef7377
commit b3bc738cae
5 changed files with 74 additions and 29 deletions

View file

@ -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();

View file

@ -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;

View file

@ -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 );

View file

@ -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;
}
}

View file

@ -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