Merge "Let users change ringer volume in dialer."
diff --git a/src/com/android/contacts/dialpad/DialpadFragment.java b/src/com/android/contacts/dialpad/DialpadFragment.java
index 87e50a0..5f5a855 100644
--- a/src/com/android/contacts/dialpad/DialpadFragment.java
+++ b/src/com/android/contacts/dialpad/DialpadFragment.java
@@ -116,6 +116,13 @@
     private ToneGenerator mToneGenerator;
     private final Object mToneGeneratorLock = new Object();
     private View mDialpad;
+    /**
+     * Remembers the number of dialpad buttons which are pressed at this moment.
+     * If it becomes 0, meaning no buttons are pressed, we'll call
+     * {@link ToneGenerator#stopTone()}; the method shouldn't be called unless the last key is
+     * released.
+     */
+    private int mDialpadPressCount;
 
     private View mDialButtonContainer;
     private View mDialButton;
@@ -483,6 +490,8 @@
                 }
             }
         }
+        // Prevent unnecessary confusion. Reset the press count anyway.
+        mDialpadPressCount = 0;
 
         Activity parent = getActivity();
         if (parent instanceof DialtactsActivity) {
@@ -535,6 +544,9 @@
 
         // Make sure we don't leave this activity with a tone still playing.
         stopTone();
+        // Just in case reset the counter too.
+        mDialpadPressCount = 0;
+
         synchronized (mToneGeneratorLock) {
             if (mToneGenerator != null) {
                 mToneGenerator.release();
@@ -784,9 +796,19 @@
                     break;
                 }
             }
+            mDialpadPressCount++;
         } else {
             view.jumpDrawablesToCurrentState();
-            stopTone();
+            mDialpadPressCount--;
+            if (mDialpadPressCount < 0) {
+                // This must be a very buggy situation in which the number of touch-down events
+                // don't match that of touch-up. We should tolerate the situation anyway.
+                Log.e(TAG, "mKeyPressCount become negative.");
+                stopTone();
+                mDialpadPressCount = 0;
+            } else if (mDialpadPressCount == 0) {
+                stopTone();
+            }
         }
     }