Add public API to do NDEF push.

Change-Id: I102da9bbc6d78577a26fa03ee363a60abc389d6c
diff --git a/Android.mk b/Android.mk
index 4a994ed..c05bafe 100644
--- a/Android.mk
+++ b/Android.mk
@@ -114,6 +114,7 @@
 	core/java/android/nfc/ILlcpConnectionlessSocket.aidl \
 	core/java/android/nfc/ILlcpServiceSocket.aidl \
 	core/java/android/nfc/ILlcpSocket.aidl \
+	core/java/android/nfc/INdefPushCallback.aidl \
 	core/java/android/nfc/INfcAdapter.aidl \
 	core/java/android/nfc/INfcAdapterExtras.aidl \
 	core/java/android/nfc/INfcTag.aidl \
diff --git a/api/current.txt b/api/current.txt
index 3339497..c6d8083 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -11641,6 +11641,7 @@
     method public void disableForegroundNdefPush(android.app.Activity);
     method public void enableForegroundDispatch(android.app.Activity, android.app.PendingIntent, android.content.IntentFilter[], java.lang.String[][]);
     method public void enableForegroundNdefPush(android.app.Activity, android.nfc.NdefMessage);
+    method public void enableForegroundNdefPush(android.app.Activity, android.nfc.NfcAdapter.NdefPushCallback);
     method public static android.nfc.NfcAdapter getDefaultAdapter(android.content.Context);
     method public static deprecated android.nfc.NfcAdapter getDefaultAdapter();
     method public boolean isEnabled();
@@ -11652,6 +11653,11 @@
     field public static final java.lang.String EXTRA_TAG = "android.nfc.extra.TAG";
   }
 
+  public static abstract interface NfcAdapter.NdefPushCallback {
+    method public abstract android.nfc.NdefMessage createMessage();
+    method public abstract void onMessagePushed();
+  }
+
   public final class NfcManager {
     method public android.nfc.NfcAdapter getDefaultAdapter();
   }
diff --git a/core/java/android/nfc/INdefPushCallback.aidl b/core/java/android/nfc/INdefPushCallback.aidl
new file mode 100644
index 0000000..80ba2ed
--- /dev/null
+++ b/core/java/android/nfc/INdefPushCallback.aidl
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.nfc;
+
+import android.nfc.NdefMessage;
+
+/**
+ * @hide
+ */
+interface INdefPushCallback
+{
+    NdefMessage onConnect();
+    void onMessagePushed();
+}
diff --git a/core/java/android/nfc/INfcAdapter.aidl b/core/java/android/nfc/INfcAdapter.aidl
index 870127c..d11fea0 100644
--- a/core/java/android/nfc/INfcAdapter.aidl
+++ b/core/java/android/nfc/INfcAdapter.aidl
@@ -25,6 +25,7 @@
 import android.nfc.ILlcpSocket;
 import android.nfc.ILlcpServiceSocket;
 import android.nfc.ILlcpConnectionlessSocket;
+import android.nfc.INdefPushCallback;
 import android.nfc.INfcTag;
 import android.nfc.IP2pTarget;
 import android.nfc.IP2pInitiator;
@@ -51,6 +52,7 @@
             in IntentFilter[] filters, in TechListParcel techLists);
     void disableForegroundDispatch(in ComponentName activity);
     void enableForegroundNdefPush(in ComponentName activity, in NdefMessage msg);
+    void enableForegroundNdefPushWithCallback(in ComponentName activity, in INdefPushCallback callback);
     void disableForegroundNdefPush(in ComponentName activity);
 
     // Non-public methods
diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java
index 4689804..738e75f 100644
--- a/core/java/android/nfc/NfcAdapter.java
+++ b/core/java/android/nfc/NfcAdapter.java
@@ -124,7 +124,7 @@
      * Intent to start an activity when a tag is discovered.
      *
      * <p>This intent will not be started when a tag is discovered if any activities respond to
-     * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag. 
+     * {@link #ACTION_NDEF_DISCOVERED} or {@link #ACTION_TECH_DISCOVERED} for the current tag.
      */
     @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
     public static final String ACTION_TAG_DISCOVERED = "android.nfc.action.TAG_DISCOVERED";
@@ -235,6 +235,37 @@
      */
     private static final int DISCOVERY_MODE_CARD_EMULATION = 2;
 
+    /**
+     * Callback passed into {@link #enableForegroundNdefPush(Activity,NdefPushCallback)}. This
+     */
+    public interface NdefPushCallback {
+        /**
+         * Called when a P2P connection is created.
+         */
+        NdefMessage createMessage();
+        /**
+         * Called when the message is pushed.
+         */
+        void onMessagePushed();
+    }
+
+    private static class NdefPushCallbackWrapper extends INdefPushCallback.Stub {
+        private NdefPushCallback mCallback;
+
+        public NdefPushCallbackWrapper(NdefPushCallback callback) {
+            mCallback = callback;
+        }
+
+        @Override
+        public NdefMessage onConnect() {
+            return mCallback.createMessage();
+        }
+
+        @Override
+        public void onMessagePushed() {
+            mCallback.onMessagePushed();
+        }
+    }
 
     // Guarded by NfcAdapter.class
     private static boolean sIsInitialized = false;
@@ -575,6 +606,44 @@
     }
 
     /**
+     * Enable NDEF message push over P2P while this Activity is in the foreground.
+     *
+     * <p>For this to function properly the other NFC device being scanned must
+     * support the "com.android.npp" NDEF push protocol. Support for this
+     * protocol is currently optional for Android NFC devices.
+     *
+     * <p>This method must be called from the main thread.
+     *
+     * <p class="note"><em>NOTE:</em> While foreground NDEF push is active standard tag dispatch is disabled.
+     * Only the foreground activity may receive tag discovered dispatches via
+     * {@link #enableForegroundDispatch}.
+     *
+     * <p class="note">Requires the {@link android.Manifest.permission#NFC} permission.
+     *
+     * @param activity the foreground Activity
+     * @param callback is called on when the P2P connection is established
+     * @throws IllegalStateException if the Activity is not currently in the foreground
+     * @throws OperationNotSupportedException if this Android device does not support NDEF push
+     */
+    public void enableForegroundNdefPush(Activity activity, NdefPushCallback callback) {
+        if (activity == null || callback == 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,
+                    mForegroundNdefPushListener);
+            sService.enableForegroundNdefPushWithCallback(activity.getComponentName(),
+                    new NdefPushCallbackWrapper(callback));
+        } catch (RemoteException e) {
+            attemptDeadServiceRecovery(e);
+        }
+    }
+
+    /**
      * Disable NDEF message push over P2P.
      *
      * <p>After calling {@link #enableForegroundNdefPush}, an activity