Update for NDEF push API change.

Change-Id: I2a51c8c39cd23f90bc35133b2f50b01a82fe1ce4
This commit is contained in:
Nick Pelly 2011-08-23 18:58:05 -07:00
parent 1eb80b23fb
commit ccb247cf2d
2 changed files with 26 additions and 34 deletions

View File

@ -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() {
}
}

View File

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