Fixed API for active NDEF reading and NDEF formatting.

- Added getNdefCached() to return the message read at discovery time.
- Fixed format() to check ndef before doing the write:
  libnfc actually requires a checkNdef to be done before writing.

Change-Id: I9b3108299c05539bdef92dd74f62f911fb5a16bf
diff --git a/core/java/android/nfc/technology/Ndef.java b/core/java/android/nfc/technology/Ndef.java
index aa3f04b..7e194aa 100644
--- a/core/java/android/nfc/technology/Ndef.java
+++ b/core/java/android/nfc/technology/Ndef.java
@@ -56,6 +56,7 @@
 
     private final int mMaxNdefSize;
     private final int mCardState;
+    private final NdefMessage mNdefMsg;
 
     /**
      * Internal constructor, to be used by NfcAdapter
@@ -66,6 +67,7 @@
         if (extras != null) {
             mMaxNdefSize = extras.getInt(EXTRA_NDEF_MAXLENGTH);
             mCardState = extras.getInt(EXTRA_NDEF_CARDSTATE);
+            mNdefMsg = extras.getParcelable(EXTRA_NDEF_MSG);
         } else {
             throw new NullPointerException("NDEF tech extras are null.");
         }
@@ -76,27 +78,8 @@
      * Get the primary NDEF message on this tag. This data is read at discovery time
      * and does not require a connection.
      */
-    public NdefMessage getNdefMessage() throws IOException, FormatException {
-        try {
-            int serviceHandle = mTag.getServiceHandle();
-            NdefMessage msg = mTagService.ndefRead(serviceHandle);
-            if (msg == null) {
-                int errorCode = mTagService.getLastError(serviceHandle);
-                switch (errorCode) {
-                    case ErrorCodes.ERROR_IO:
-                        throw new IOException();
-                    case ErrorCodes.ERROR_INVALID_PARAM:
-                        throw new FormatException();
-                    default:
-                        // Should not happen
-                        throw new IOException();
-                }
-            }
-            return msg;
-        } catch (RemoteException e) {
-            attemptDeadServiceRecovery(e);
-            return null;
-        }
+    public NdefMessage getCachedNdefMessage() {
+        return mNdefMsg;
     }
 
     /**
@@ -126,6 +109,36 @@
 
     // Methods that require connect()
     /**
+     * Get the primary NDEF message on this tag. This data is read actively
+     * and requires a connection.
+     */
+    public NdefMessage getNdefMessage() throws IOException, FormatException {
+        try {
+            int serviceHandle = mTag.getServiceHandle();
+            if (mTagService.isNdef(serviceHandle)) {
+                NdefMessage msg = mTagService.ndefRead(serviceHandle);
+                if (msg == null) {
+                    int errorCode = mTagService.getLastError(serviceHandle);
+                    switch (errorCode) {
+                        case ErrorCodes.ERROR_IO:
+                            throw new IOException();
+                        case ErrorCodes.ERROR_INVALID_PARAM:
+                            throw new FormatException();
+                        default:
+                            // Should not happen
+                            throw new IOException();
+                    }
+                }
+                return msg;
+            } else {
+                return null;
+            }
+        } catch (RemoteException e) {
+            attemptDeadServiceRecovery(e);
+            return null;
+        }
+    }
+    /**
      * Overwrite the primary NDEF message
      * @throws IOException
      */
diff --git a/core/java/android/nfc/technology/NdefFormatable.java b/core/java/android/nfc/technology/NdefFormatable.java
index 05fd67c..bd21e58 100644
--- a/core/java/android/nfc/technology/NdefFormatable.java
+++ b/core/java/android/nfc/technology/NdefFormatable.java
@@ -73,17 +73,22 @@
                     // Should not happen
                     throw new IOException();
             }
-            errorCode = mTagService.ndefWrite(serviceHandle, firstMessage);
-            switch (errorCode) {
-                case ErrorCodes.SUCCESS:
-                    break;
-                case ErrorCodes.ERROR_IO:
-                    throw new IOException();
-                case ErrorCodes.ERROR_INVALID_PARAM:
-                    throw new FormatException();
-                default:
-                    // Should not happen
-                    throw new IOException();
+            // Now check and see if the format worked
+            if (mTagService.isNdef(serviceHandle)) {
+                errorCode = mTagService.ndefWrite(serviceHandle, firstMessage);
+                switch (errorCode) {
+                    case ErrorCodes.SUCCESS:
+                        break;
+                    case ErrorCodes.ERROR_IO:
+                        throw new IOException();
+                    case ErrorCodes.ERROR_INVALID_PARAM:
+                        throw new FormatException();
+                    default:
+                        // Should not happen
+                        throw new IOException();
+                }
+            } else {
+                throw new IOException();
             }
         } catch (RemoteException e) {
             attemptDeadServiceRecovery(e);