From 33ff2405581271adf14ed4e45597a3b0b80a46ab Mon Sep 17 00:00:00 2001 From: Jeff Hamilton Date: Mon, 17 Jan 2011 07:59:03 -0800 Subject: [PATCH] New APIs for NDEF Push Protocol. The NPP is only usable by the foregorund activity to prevent dispatching confusion on the far end. Change-Id: I08475a52083fd7f81b79b7fe2faf4e126121a809 --- api/current.xml | 30 ++++++++++- core/java/android/nfc/INfcAdapter.aidl | 2 + core/java/android/nfc/NfcAdapter.java | 74 ++++++++++++++++++++++---- 3 files changed, 95 insertions(+), 11 deletions(-) diff --git a/api/current.xml b/api/current.xml index 4006e67a0f058..aefa9321eab56 100644 --- a/api/current.xml +++ b/api/current.xml @@ -100830,6 +100830,19 @@ + + + + + + + + + + - + diff --git a/core/java/android/nfc/INfcAdapter.aidl b/core/java/android/nfc/INfcAdapter.aidl index cb9fc9d05fbe4..cfeff523581dd 100644 --- a/core/java/android/nfc/INfcAdapter.aidl +++ b/core/java/android/nfc/INfcAdapter.aidl @@ -50,6 +50,8 @@ interface INfcAdapter void enableForegroundDispatch(in ComponentName activity, in PendingIntent intent, in IntentFilter[] filters); void disableForegroundDispatch(in ComponentName activity); + void enableForegroundNdefPush(in ComponentName activity, in NdefMessage msg); + void disableForegroundNdefPush(in ComponentName activity); // Non-public methods // TODO: check and complete diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java index c0c0462bfe8c4..28d53f567f62f 100644 --- a/core/java/android/nfc/NfcAdapter.java +++ b/core/java/android/nfc/NfcAdapter.java @@ -413,13 +413,6 @@ public final class NfcAdapter { } } - class ForegroundDispatchPausedListener implements OnActivityPausedListener { - @Override - public void onPaused(Activity activity) { - disableForegroundDispatchInternal(activity, true); - } - } - /** * Enables foreground dispatching to the given Activity. This will force all NFC Intents that * match the given filters to be delivered to the activity bypassing the standard dispatch @@ -438,7 +431,7 @@ public final class NfcAdapter { throw new NullPointerException(); } if (!activity.isResumed()) { - throw new IllegalStateException("Foregorund dispatching can onlly be enabled " + + throw new IllegalStateException("Foregorund dispatching can only be enabled " + "when your activity is resumed"); } try { @@ -452,15 +445,24 @@ public final class NfcAdapter { /** * Disables foreground activity dispatching setup with - * {@link #enableForegroundDispatch}. This must be called before the Activity returns from + * {@link #enableForegroundDispatch}. + * + *

This must be called before the Activity returns from * it's onPause() or this method will throw an IllegalStateException. * - * This method must be called from the main thread. + *

This method must be called from the main thread. */ public void disableForegroundDispatch(Activity activity) { disableForegroundDispatchInternal(activity, false); } + class ForegroundDispatchPausedListener implements OnActivityPausedListener { + @Override + public void onPaused(Activity activity) { + disableForegroundDispatchInternal(activity, true); + } + } + void disableForegroundDispatchInternal(Activity activity, boolean force) { try { sService.disableForegroundDispatch(activity.getComponentName()); @@ -473,6 +475,58 @@ public final class NfcAdapter { } } + /** + * Enable NDEF messages push while this Activity is in the foreground. + */ + public void enableForegroundNdefPush(Activity activity, NdefMessage msg) { + if (activity == null || msg == null) { + throw new NullPointerException(); + } + if (!activity.isResumed()) { + throw new IllegalStateException("Foregorund NDEF push can only be enabled " + + "when your activity is resumed"); + } + try { + ActivityThread.currentActivityThread().registerOnActivityPausedListener(activity, + new ForegroundDispatchPausedListener()); + sService.enableForegroundNdefPush(activity.getComponentName(), msg); + } catch (RemoteException e) { + attemptDeadServiceRecovery(e); + } + } + + /** + * Disables foreground NDEF push setup with + * {@link #enableForegroundNdefPush}. + * + *

This must be called before the Activity returns from + * it's onPause() or this method will throw an IllegalStateException. + * + *

This method must be called from the main thread. + */ + public void disableNdefPushDispatch(Activity activity) { + disableForegroundDispatchInternal(activity, false); + } + + class ForegroundNdefPushPausedListener implements OnActivityPausedListener { + @Override + public void onPaused(Activity activity) { + disableNdefPushDispatchInternal(activity, true); + } + } + + void disableNdefPushDispatchInternal(Activity activity, boolean force) { + try { + sService.disableForegroundNdefPush(activity.getComponentName()); + if (!force && !activity.isResumed()) { + throw new IllegalStateException("You must disable forgeground NDEF push " + + "while your activity is still resumed"); + } + } catch (RemoteException e) { + attemptDeadServiceRecovery(e); + } + } + /** * Retrieve a TagTechnology object used to interact with a Tag that is * in field.