Fix issue 2139634: DTMF tones on Sholes popping, hissing (audio latency too high).

This change is a complement to the main fix in kernel driver for the same issue (partner change #1250).
It removes clicks sometimes heard after the end of the tones while audio flinger is sending 0s to the audio output stream.
The problem was that the sleep time between two writes was more than the duration of one audio output stream buffer which could cause some underrun.

Also fixed a recent regression in ToneGenerator that made that the end of previous tone was repeated at the beginning of current one under certain timing circumstances when the maximum tone duration was specified.
diff --git a/include/media/ToneGenerator.h b/include/media/ToneGenerator.h
index e8df08e..c884c2c 100644
--- a/include/media/ToneGenerator.h
+++ b/include/media/ToneGenerator.h
@@ -248,6 +248,7 @@
     // only if tone duration is less than about 27 Hours(@44100Hz sampling rate). If this time is exceeded,
     // no crash will occur but tone sequence will show a glitch.
     unsigned int mMaxSmp;  // Maximum number of audio samples played (maximun tone duration)
+    int mDurationMs;  // Maximum tone duration in ms
 
     unsigned short mCurSegment;  // Current segment index in ToneDescriptor segments[]
     unsigned short mCurCount;  // Current sequence repeat count
diff --git a/media/libmedia/ToneGenerator.cpp b/media/libmedia/ToneGenerator.cpp
index d36bec9..3729eb3 100644
--- a/media/libmedia/ToneGenerator.cpp
+++ b/media/libmedia/ToneGenerator.cpp
@@ -879,6 +879,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 bool ToneGenerator::startTone(int toneType, int durationMs) {
     bool lResult = false;
+    status_t lStatus;
 
     if ((toneType < 0) || (toneType >= NUM_TONES))
         return lResult;
@@ -898,15 +899,16 @@
     toneType = getToneForRegion(toneType);
     mpNewToneDesc = &sToneDescriptors[toneType];
 
-    if (durationMs == -1) {
-        mMaxSmp = TONEGEN_INF;
-    } else {
-        if (durationMs > (int)(TONEGEN_INF / mSamplingRate)) {
-            mMaxSmp = (durationMs / 1000) * mSamplingRate;
-        } else {
-            mMaxSmp = (durationMs * mSamplingRate) / 1000;
+    mDurationMs = durationMs;
+
+    if (mState == TONE_STOPPED) {
+        LOGV("Start waiting for previous tone to stop");
+        lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
+        if (lStatus != NO_ERROR) {
+            LOGE("--- start wait for stop timed out, status %d", lStatus);
+            mState = TONE_IDLE;
+            return lResult;
         }
-        LOGV("startTone, duration limited to %d ms", durationMs);
     }
 
     if (mState == TONE_INIT) {
@@ -919,7 +921,7 @@
             mLock.lock();
             if (mState == TONE_STARTING) {
                 LOGV("Wait for start callback");
-                status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
+                lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
                 if (lStatus != NO_ERROR) {
                     LOGE("--- Immediate start timed out, status %d", lStatus);
                     mState = TONE_IDLE;
@@ -931,9 +933,8 @@
         }
     } else {
         LOGV("Delayed start\n");
-
         mState = TONE_RESTARTING;
-        status_t lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
+        lStatus = mWaitCbkCond.waitRelative(mLock, seconds(1));
         if (lStatus == NO_ERROR) {
             if (mState != TONE_IDLE) {
                 lResult = true;
@@ -1316,6 +1317,17 @@
 
     mpToneDesc = mpNewToneDesc;
 
+    if (mDurationMs == -1) {
+        mMaxSmp = TONEGEN_INF;
+    } else {
+        if (mDurationMs > (int)(TONEGEN_INF / mSamplingRate)) {
+            mMaxSmp = (mDurationMs / 1000) * mSamplingRate;
+        } else {
+            mMaxSmp = (mDurationMs * mSamplingRate) / 1000;
+        }
+        LOGV("prepareWave, duration limited to %d ms", mDurationMs);
+    }
+
     while (mpToneDesc->segments[segmentIdx].duration) {
         // Get total number of sine waves: needed to adapt sine wave gain.
         unsigned int lNumWaves = numWaves(segmentIdx);