Don't call disconnect() twice to avoid emergency recovery.

When a tag is disconnected, first the watchdog is stopped, then disconnect
is called. However, the watchdog stop also did a disconnect() itself.
Now that is only done if the watchdog itself concludes the tag is gone.
Also made sure we catch an early return when the handle is -1;
in that case, just restart the discovery.

Also removed some old code.

Change-Id: Ib851115eecaea65d88cffc7771e273258a404f87
diff --git a/jni/com_android_nfc_NativeNfcTag.cpp b/jni/com_android_nfc_NativeNfcTag.cpp
index 63334df..21525c9 100644
--- a/jni/com_android_nfc_NativeNfcTag.cpp
+++ b/jni/com_android_nfc_NativeNfcTag.cpp
@@ -135,49 +135,6 @@
    sem_post(&pCallbackData->sem);
 }
 
-static void nfc_jni_async_presence_check_callback(void* pContext,NFCSTATUS  status)
-{
-   NFCSTATUS ret;
-   JNIEnv* env = (JNIEnv*)pContext;
-
-   LOG_CALLBACK("nfc_jni_async_presence_check_callback", status);
-
-   if(status != NFCSTATUS_SUCCESS)
-   {
-      /* Disconnect & Restart Polling loop */
-      TRACE("Tag removed from the RF Field\n");
-
-      TRACE("phLibNfc_RemoteDev_Disconnect(async)");
-      REENTRANCE_LOCK();
-      ret = phLibNfc_RemoteDev_Disconnect(handle, NFC_DISCOVERY_CONTINUE, nfc_jni_async_disconnect_callback,(void*)handle);
-      REENTRANCE_UNLOCK();
-      if(ret != NFCSTATUS_PENDING)
-      {
-         LOGE("phLibNfc_RemoteDev_Disconnect() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
-         /* concurrency lock held while in callback */
-         nfc_jni_restart_discovery_locked(nfc_jni_get_nat_ext(env));
-         return;
-      }
-      TRACE("phLibNfc_RemoteDev_Disconnect() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
-   }
-   else
-   {
-      TRACE("phLibNfc_RemoteDev_CheckPresence(async)");
-      /* Presence Check */
-      REENTRANCE_LOCK();
-      ret = phLibNfc_RemoteDev_CheckPresence(handle,nfc_jni_async_presence_check_callback, (void*)env);
-      REENTRANCE_UNLOCK();
-      if(ret != NFCSTATUS_PENDING)
-      {
-         LOGE("phLibNfc_RemoteDev_CheckPresence() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
-         return;
-      }
-      TRACE("phLibNfc_RemoteDev_CheckPresence() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
-   }
-}
-
-
-
 static phNfc_sData_t *nfc_jni_transceive_buffer;
 
 static void nfc_jni_transceive_callback(void *pContext,
@@ -624,6 +581,14 @@
    /* Disconnect */
    TRACE("Disconnecting from tag (%x)", handle);
    
+   if (handle == -1) {
+       // Was never connected to any tag, exit
+       result = JNI_TRUE;
+       LOGE("doDisconnect() - Target already disconnected");
+       nfc_jni_restart_discovery_locked(nfc_jni_get_nat_ext(e));
+       goto clean_and_return;
+   }
+
    /* Presence Check */
    do
    {
diff --git a/src/com/android/nfc/NativeNfcTag.java b/src/com/android/nfc/NativeNfcTag.java
index 4f23d15..3155ace 100755
--- a/src/com/android/nfc/NativeNfcTag.java
+++ b/src/com/android/nfc/NativeNfcTag.java
@@ -71,7 +71,7 @@
                 }
             }
             // Restart the polling loop if the tag is not here any more
-            if (!isPresent) {
+            if (isRunning && !isPresent) {
                 Log.d(TAG, "Tag lost, restarting polling loop");
                 doDisconnect();
             }
@@ -142,8 +142,9 @@
         if (mWatchdog != null) {
             mWatchdog.end();
         }
+        boolean result = doDisconnect();
         mConnectedTechnology = -1;
-        return doDisconnect();
+        return result;
     }
 
     native boolean doReconnect();