Merge "Fix cursor leak."
diff --git a/src/com/android/contacts/dialpad/DialpadFragment.java b/src/com/android/contacts/dialpad/DialpadFragment.java
index 0de0f11..5f5a855 100644
--- a/src/com/android/contacts/dialpad/DialpadFragment.java
+++ b/src/com/android/contacts/dialpad/DialpadFragment.java
@@ -100,7 +100,7 @@
     private static final int TONE_RELATIVE_VOLUME = 80;
 
     /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
-    private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_MUSIC;
+    private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
 
     /**
      * View (usually FrameLayout) containing mDigits field. This can be null, in which mDigits
@@ -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;
@@ -476,17 +483,15 @@
         synchronized (mToneGeneratorLock) {
             if (mToneGenerator == null) {
                 try {
-                    // we want the user to be able to control the volume of the dial tones
-                    // outside of a call, so we use the stream type that is also mapped to the
-                    // volume control keys for this activity
                     mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
-                    getActivity().setVolumeControlStream(DIAL_TONE_STREAM_TYPE);
                 } catch (RuntimeException e) {
                     Log.w(TAG, "Exception caught while creating local tone generator: " + e);
                     mToneGenerator = null;
                 }
             }
         }
+        // Prevent unnecessary confusion. Reset the press count anyway.
+        mDialpadPressCount = 0;
 
         Activity parent = getActivity();
         if (parent instanceof DialtactsActivity) {
@@ -539,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();
@@ -788,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();
+            }
         }
     }