From ccb247cf2d7111c2080414741a7531905ce280f3 Mon Sep 17 00:00:00 2001 From: Nick Pelly Date: Tue, 23 Aug 2011 18:58:05 -0700 Subject: [PATCH] Update for NDEF push API change. Change-Id: I2a51c8c39cd23f90bc35133b2f50b01a82fe1ce4 --- .../android/email/activity/NfcHandler.java | 47 +++++++------------ .../email/activity/UIControllerBase.java | 13 +++-- 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/src/com/android/email/activity/NfcHandler.java b/src/com/android/email/activity/NfcHandler.java index f41dc24ea..775ec83d1 100644 --- a/src/com/android/email/activity/NfcHandler.java +++ b/src/com/android/email/activity/NfcHandler.java @@ -20,6 +20,7 @@ import android.app.Activity; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; +import android.nfc.NfcEvent; import android.text.TextUtils; import com.android.emailcommon.provider.Account; @@ -35,17 +36,25 @@ import java.net.URLEncoder; * will be called to create the data to be sent over the link, * which is a vCard in this case. */ -public class NfcHandler implements NfcAdapter.NdefPushCallback { - private NfcAdapter mNfcAdapter; - private UIControllerBase mUiController; - private Activity mActivity; - private String mCurrentEmail; +public class NfcHandler implements NfcAdapter.CreateNdefMessageCallback { + final UIControllerBase mUiController; + final Activity mActivity; - public NfcHandler(UIControllerBase controller, Activity - activity) { + String mCurrentEmail; + + public static NfcHandler register(UIControllerBase controller, Activity activity) { + NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity); + if (adapter == null) { + return null; // NFC not available on this device + } + NfcHandler nfcHandler = new NfcHandler(controller, activity); + adapter.setNdefPushMessageCallback(nfcHandler, activity); + return nfcHandler; + } + + public NfcHandler(UIControllerBase controller, Activity activity) { mUiController = controller; mActivity = activity; - mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity); } public void onAccountChanged() { @@ -57,22 +66,6 @@ public class NfcHandler implements NfcAdapter.NdefPushCallback { } else { mCurrentEmail = null; } - - } - - public void onPause() { - if (mNfcAdapter != null) { - mNfcAdapter.disableForegroundNdefPush( - mActivity); - } - } - - public void onResume() { - if (mNfcAdapter != null) { - mNfcAdapter.enableForegroundNdefPush( - mActivity, this); - onAccountChanged(); // Fetch current account - } } private static NdefMessage buildMailtoNdef(String address) { @@ -95,15 +88,11 @@ public class NfcHandler implements NfcAdapter.NdefPushCallback { } @Override - public NdefMessage createMessage() { + public NdefMessage createNdefMessage(NfcEvent event) { if (mCurrentEmail != null) { return buildMailtoNdef(mCurrentEmail); } else { return null; } } - - @Override - public void onMessagePushed() { - } } diff --git a/src/com/android/email/activity/UIControllerBase.java b/src/com/android/email/activity/UIControllerBase.java index a70bbb907..aebda550a 100644 --- a/src/com/android/email/activity/UIControllerBase.java +++ b/src/com/android/email/activity/UIControllerBase.java @@ -97,7 +97,7 @@ abstract class UIControllerBase implements MailboxListFragment.Callback, * The NfcHandler implements Near Field Communication sharing features * whenever the activity is in the foreground. */ - private final NfcHandler mNfcHandler; + private NfcHandler mNfcHandler; /** * The active context for the current MessageList. @@ -149,7 +149,6 @@ abstract class UIControllerBase implements MailboxListFragment.Callback, if (DEBUG_FRAGMENTS) { FragmentManager.enableDebugLogging(true); } - mNfcHandler = new NfcHandler(this, activity); } /** @@ -181,6 +180,7 @@ abstract class UIControllerBase implements MailboxListFragment.Callback, } mRefreshManager.registerListener(mRefreshListener); mActionBarController.onActivityCreated(); + mNfcHandler = NfcHandler.register(this, mActivity); } /** @@ -203,7 +203,9 @@ abstract class UIControllerBase implements MailboxListFragment.Callback, Log.d(Logging.LOG_TAG, this + " onActivityResume"); } refreshActionBar(); - mNfcHandler.onResume(); + if (mNfcHandler != null) { + mNfcHandler.onAccountChanged(); // workaround for email not set on initial load + } } /** @@ -213,7 +215,6 @@ abstract class UIControllerBase implements MailboxListFragment.Callback, if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) { Log.d(Logging.LOG_TAG, this + " onActivityPause"); } - mNfcHandler.onPause(); } /** @@ -552,7 +553,9 @@ abstract class UIControllerBase implements MailboxListFragment.Callback, openMailbox(accountId, inboxId); } } - mNfcHandler.onAccountChanged(); + if (mNfcHandler != null) { + mNfcHandler.onAccountChanged(); + } } /**