mExpectingUpdateSelection was out of sync when nothing to delete.

cherripick of I9c6a948331726a821bd3ccec9c1d02dec2c4703a
(forward cherrypicking this because the automerger is stuck now.)

This bug was leading to corrupted rendering of surrogate pairs in the following
scenario.

1. Type some emojis
2. Move the cursor at the beginning of the text field
3. Hit backspace even though there's nothing to delete
4. Move the cursor after some emoji
5. Hit backspace

The root cause of this issue was the out-of-sync mExpectingUpdateSelection if
handleBackspace() gets called when the cursor reaches at the beginning of the
TextView. In such case, mExpectingUpdateSelection shouldn't be set true because
there's nothing to delete, so there will be no onUpdateSelection() calls associated
with it. Due to this bug, the cache in RichInputConnection could get stale at step 4
described above. Then the following handleBackspace() that should delete a surrogate
pair was not working correctly because of the stale cache.

bug: 11181913
Change-Id: I1cbf444d8d105416e7de75c16d80b3797f470495
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index a850510..e8ebf88 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1623,8 +1623,6 @@
         case Constants.CODE_DELETE:
             mSpaceState = SPACE_STATE_NONE;
             handleBackspace(spaceState);
-            mDeleteCount++;
-            mExpectingUpdateSelection = true;
             LatinImeLogger.logOnDelete(x, y);
             break;
         case Constants.CODE_SHIFT:
@@ -2052,6 +2050,10 @@
     }
 
     private void handleBackspace(final int spaceState) {
+        // We revert these in this method if the deletion doesn't happen.
+        mDeleteCount++;
+        mExpectingUpdateSelection = true;
+
         // In many cases, we may have to put the keyboard in auto-shift state again. However
         // we want to wait a few milliseconds before doing it to avoid the keyboard flashing
         // during key repeat.
@@ -2141,8 +2143,16 @@
                     // This should never happen.
                     Log.e(TAG, "Backspace when we don't know the selection position");
                 }
-                final int lengthToDelete = Character.isSupplementaryCodePoint(
-                        mConnection.getCodePointBeforeCursor()) ? 2 : 1;
+                final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
+                if (codePointBeforeCursor == Constants.NOT_A_CODE) {
+                    // Nothing to delete before the cursor. We have to revert the deletion states
+                    // that were updated at the beginning of this method.
+                    mDeleteCount--;
+                    mExpectingUpdateSelection = false;
+                    return;
+                }
+                final int lengthToDelete =
+                        Character.isSupplementaryCodePoint(codePointBeforeCursor) ? 2 : 1;
                 if (mAppWorkAroundsUtils.isBeforeJellyBean() ||
                         currentSettings.mInputAttributes.isTypeNull()) {
                     // There are two possible reasons to send a key event: either the field has
@@ -2161,12 +2171,16 @@
                             true /* shouldUncommitLogUnit */);
                 }
                 if (mDeleteCount > DELETE_ACCELERATE_AT) {
-                    final int lengthToDeleteAgain = Character.isSupplementaryCodePoint(
-                            mConnection.getCodePointBeforeCursor()) ? 2 : 1;
-                    mConnection.deleteSurroundingText(lengthToDeleteAgain, 0);
-                    if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
-                        ResearchLogger.latinIME_handleBackspace(lengthToDeleteAgain,
-                                true /* shouldUncommitLogUnit */);
+                    final int codePointBeforeCursorToDeleteAgain =
+                            mConnection.getCodePointBeforeCursor();
+                    if (codePointBeforeCursorToDeleteAgain != Constants.NOT_A_CODE) {
+                        final int lengthToDeleteAgain = Character.isSupplementaryCodePoint(
+                                codePointBeforeCursorToDeleteAgain) ? 2 : 1;
+                        mConnection.deleteSurroundingText(lengthToDeleteAgain, 0);
+                        if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
+                            ResearchLogger.latinIME_handleBackspace(lengthToDeleteAgain,
+                                    true /* shouldUncommitLogUnit */);
+                        }
                     }
                 }
             }