Make remove-previous-digit logic aware of cursor position

If we long-press '0' at the middle of digits intending to insert
'+', we cannot it easily because current logic doesn't consider the
current cursor position. This change will fix the behavior.

Bug: 6041729
Change-Id: Iaaea5addf17f332710c3522e27279a7d2448286b
diff --git a/src/com/android/contacts/dialpad/DialpadFragment.java b/src/com/android/contacts/dialpad/DialpadFragment.java
index 10e59fc..a49ce8a 100644
--- a/src/com/android/contacts/dialpad/DialpadFragment.java
+++ b/src/com/android/contacts/dialpad/DialpadFragment.java
@@ -888,7 +888,7 @@
                 // Just for safety we also check if the digits field is empty or not.
                 if (isDigitsEmpty() || TextUtils.equals(mDigits.getText(), "1")) {
                     // We'll try to initiate voicemail and thus we want to remove irrelevant string.
-                    removeLastOneDigitIfPossible();
+                    removePreviousDigitIfPossible();
 
                     if (isVoicemailAvailable()) {
                         callVoicemail();
@@ -915,7 +915,7 @@
             }
             case R.id.zero: {
                 // Remove tentative input ('0') done by onTouch().
-                removeLastOneDigitIfPossible();
+                removePreviousDigitIfPossible();
                 keyPressed(KeyEvent.KEYCODE_PLUS);
                 return true;
             }
@@ -930,11 +930,16 @@
         return false;
     }
 
-    private void removeLastOneDigitIfPossible() {
+    /**
+     * Remove the digit just before the current position. This can be used if we want to replace
+     * the previous digit or cancel previously entered character.
+     */
+    private void removePreviousDigitIfPossible() {
         final Editable editable = mDigits.getText();
-        final int length = editable.length();
-        if (length > 0) {
-            mDigits.getText().delete(length - 1, length);
+        final int currentPosition = mDigits.getSelectionStart();
+        if (currentPosition > 0) {
+            mDigits.setSelection(currentPosition);
+            mDigits.getText().delete(currentPosition - 1, currentPosition);
         }
     }