Merge "Tweak Ndef.getType() API. Rename constants, expand javadoc." into gingerbread
diff --git a/core/java/android/nfc/technology/MifareClassic.java b/core/java/android/nfc/technology/MifareClassic.java
index 8a9ebf1..799f0a78 100644
--- a/core/java/android/nfc/technology/MifareClassic.java
+++ b/core/java/android/nfc/technology/MifareClassic.java
@@ -205,6 +205,15 @@
         return getBlockCount(sector) * 16;
     }
 
+    public int getTotalBlockCount() {
+        int totalBlocks = 0;
+        for (int sec = 0; sec < getSectorCount(); sec++) {
+            totalBlocks += getSectorSize(sec);
+        }
+
+        return totalBlocks;
+    }
+
     public int getBlockCount(int sector) {
         if (sector >= getSectorCount()) {
             throw new IllegalArgumentException("this card only has " + getSectorCount() +
@@ -343,9 +352,27 @@
         checkConnected();
 
         byte addr = (byte) block;
-        byte[] incr_cmd = { (byte) 0xC0, (byte) block };
+        byte[] decr_cmd = { (byte) 0xC0, (byte) block };
 
-        transceive(incr_cmd);
+        transceive(decr_cmd);
+    }
+
+    public void transfer(int block) throws IOException {
+        checkConnected();
+
+        byte addr = (byte) block;
+        byte[] trans_cmd = { (byte) 0xB0, (byte) block };
+
+        transceive(trans_cmd);
+    }
+
+    public void restore(int block) throws IOException {
+        checkConnected();
+
+        byte addr = (byte) block;
+        byte[] rest_cmd = { (byte) 0xC2, (byte) block };
+
+        transceive(rest_cmd);
     }
 
     /**
diff --git a/telephony/java/com/android/internal/telephony/RIL.java b/telephony/java/com/android/internal/telephony/RIL.java
index 3d410fd..7d31687 100644
--- a/telephony/java/com/android/internal/telephony/RIL.java
+++ b/telephony/java/com/android/internal/telephony/RIL.java
@@ -370,23 +370,26 @@
                 case EVENT_WAKE_LOCK_TIMEOUT:
                     // Haven't heard back from the last request.  Assume we're
                     // not getting a response and  release the wake lock.
-                    // TODO should we clean up mRequestList and mRequestPending
                     synchronized (mWakeLock) {
                         if (mWakeLock.isHeld()) {
-                            if (RILJ_LOGD) {
-                                synchronized (mRequestsList) {
-                                    int count = mRequestsList.size();
-                                    Log.d(LOG_TAG, "WAKE_LOCK_TIMEOUT " +
-                                        " mReqPending=" + mRequestMessagesPending +
-                                        " mRequestList=" + count);
-
-                                    for (int i = 0; i < count; i++) {
-                                        rr = mRequestsList.get(i);
-                                        Log.d(LOG_TAG, i + ": [" + rr.mSerial + "] " +
-                                            requestToString(rr.mRequest));
-
-                                    }
-                                }
+                            // The timer of WAKE_LOCK_TIMEOUT is reset with each
+                            // new send request. So when WAKE_LOCK_TIMEOUT occurs
+                            // all requests in mRequestList already waited at
+                            // least DEFAULT_WAKE_LOCK_TIMEOUT but no response.
+                            // Therefore all should be treated as lost requests.
+                            // Those lost requests return GENERIC_FAILURE and
+                            // request list is cleared.
+                            //
+                            // Note: mRequestMessagesPending shows how many
+                            //       requests are waiting to be sent (and before
+                            //       to be added in request list) since star the
+                            //       timer. It should be
+                            //       zero here since all request should already
+                            //       be put in request list while TIMEOUT occurs.
+                            clearRequestsList(GENERIC_FAILURE, true);
+                            if (mRequestMessagesPending != 0) {
+                                Log.e(LOG_TAG, "ERROR: mReqPending is NOT 0 at TIMEOUT, "
+                                    + "mReqPending = " + mRequestMessagesPending);
                             }
                             mWakeLock.release();
                         }
@@ -558,15 +561,7 @@
                 RILRequest.resetSerial();
 
                 // Clear request list on close
-                synchronized (mRequestsList) {
-                    for (int i = 0, sz = mRequestsList.size() ; i < sz ; i++) {
-                        RILRequest rr = mRequestsList.get(i);
-                        rr.onError(RADIO_NOT_AVAILABLE, null);
-                        rr.release();
-                    }
-
-                    mRequestsList.clear();
-                }
+                clearRequestsList(RADIO_NOT_AVAILABLE, false);
             }} catch (Throwable tr) {
                 Log.e(LOG_TAG,"Uncaught exception", tr);
             }
@@ -2061,6 +2056,34 @@
         releaseWakeLockIfDone();
     }
 
+    /**
+     * Release each request in mReqeustsList then clear the list
+     * @param error is the RIL_Errno sent back
+     * @param loggable true means to print all requests in mRequestslist
+     */
+    private void clearRequestsList(int error, boolean loggable) {
+        RILRequest rr;
+        synchronized (mRequestsList) {
+            int count = mRequestsList.size();
+            if (RILJ_LOGD && loggable) {
+                Log.d(LOG_TAG, "WAKE_LOCK_TIMEOUT " +
+                        " mReqPending=" + mRequestMessagesPending +
+                        " mRequestList=" + count);
+            }
+
+            for (int i = 0; i < count ; i++) {
+                rr = mRequestsList.get(i);
+                if (RILJ_LOGD && loggable) {
+                    Log.d(LOG_TAG, i + ": [" + rr.mSerial + "] " +
+                            requestToString(rr.mRequest));
+                }
+                rr.onError(error, null);
+                rr.release();
+            }
+            mRequestsList.clear();
+        }
+    }
+
     private RILRequest findAndRemoveRequestFromList(int serial) {
         synchronized (mRequestsList) {
             for (int i = 0, s = mRequestsList.size() ; i < s ; i++) {