Prevent Intent from initiating special-char handling

TESTED:
- Manually type "123#"
-> Should cause special-char-sequence handling

- Have Intent with "tel:123%23"
-> Should *not* cause the handling

- Try other USSD sequences
-> Should behave similarly as shown above

Bug: 6726923
Change-Id: Ic5b042f620b10931fedcaf12bb58be2405bf7390
diff --git a/src/com/android/contacts/dialpad/DialpadFragment.java b/src/com/android/contacts/dialpad/DialpadFragment.java
index 745c044..350fc28 100644
--- a/src/com/android/contacts/dialpad/DialpadFragment.java
+++ b/src/com/android/contacts/dialpad/DialpadFragment.java
@@ -191,6 +191,13 @@
 
     private boolean mWasEmptyBeforeTextChange;
 
+    /**
+     * This field is set to true while processing an incoming DIAL intent, in order to make sure
+     * that SpecialCharSequenceMgr actions can be triggered by user input but *not* by a
+     * tel: URI passed by some other app
+     */
+    private boolean mSkipSpecialCharSequenceHandling;
+
     @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {
         mWasEmptyBeforeTextChange = TextUtils.isEmpty(s);
@@ -214,7 +221,8 @@
         // When DTMF dialpad buttons are being pressed, we delay SpecialCharSequencMgr sequence,
         // since some of SpecialCharSequenceMgr's behavior is too abrupt for the "touch-down"
         // behavior.
-        if (SpecialCharSequenceMgr.handleChars(getActivity(), input.toString(), mDigits)) {
+        if (!mSkipSpecialCharSequenceHandling &&
+                SpecialCharSequenceMgr.handleChars(getActivity(), input.toString(), mDigits)) {
             // A special sequence was entered, clear the digits
             mDigits.getText().clear();
         }
@@ -436,17 +444,29 @@
         showDialpadChooser(needToShowDialpadChooser);
     }
 
+    /**
+     * Sets formatted digits to digits field.
+     *
+     * Watch out: this method assumes it is used only for loading the digits widget based on data
+     * in a DIAL or VIEW intent, and *not* ever called for digits typed by the user.
+     */
     private void setFormattedDigits(String data, String normalizedNumber) {
-        // strip the non-dialable numbers out of the data string.
-        String dialString = PhoneNumberUtils.extractNetworkPortion(data);
-        dialString =
-                PhoneNumberUtils.formatNumber(dialString, normalizedNumber, mCurrentCountryIso);
-        if (!TextUtils.isEmpty(dialString)) {
-            Editable digits = mDigits.getText();
-            digits.replace(0, digits.length(), dialString);
-            // for some reason this isn't getting called in the digits.replace call above..
-            // but in any case, this will make sure the background drawable looks right
-            afterTextChanged(digits);
+        try {
+            mSkipSpecialCharSequenceHandling = true;
+
+            // strip the non-dialable numbers out of the data string.
+            String dialString = PhoneNumberUtils.extractNetworkPortion(data);
+            dialString =
+                    PhoneNumberUtils.formatNumber(dialString, normalizedNumber, mCurrentCountryIso);
+            if (!TextUtils.isEmpty(dialString)) {
+                Editable digits = mDigits.getText();
+                digits.replace(0, digits.length(), dialString);
+                // for some reason this isn't getting called in the digits.replace call above..
+                // but in any case, this will make sure the background drawable looks right
+                afterTextChanged(digits);
+            }
+        } finally {
+            mSkipSpecialCharSequenceHandling = false;
         }
     }