Bug fix for: a spinner is shown on VoiceIME startup

The last change did not solve the bug completely, the spinner was still shown at times when no initialization dialog was shown

Change-Id: I43a5f8df4e333ffb6deb727c3bf45ed7b7a54069
diff --git a/src/com/android/inputmethod/voice/RecognitionView.java b/src/com/android/inputmethod/voice/RecognitionView.java
index 18310e9..5d4a0e0 100644
--- a/src/com/android/inputmethod/voice/RecognitionView.java
+++ b/src/com/android/inputmethod/voice/RecognitionView.java
@@ -52,12 +52,6 @@
 public class RecognitionView {
     private static final String TAG = "RecognitionView";
 
-    // If there's a significant delay between starting up voice search and the
-    // onset of audio recording, show the "initializing" screen first. If not,
-    // jump directly to the "speak now" screen to avoid flashing "initializing"
-    // quickly.
-    private static final boolean EXPECT_RECORDING_DELAY = true;
-
     private Handler mUiHandler;  // Reference to UI thread
     private View mView;
     private Context mContext;
@@ -148,29 +142,18 @@
     public void showInitializing() {
         mUiHandler.post(new Runnable() {
             public void run() {
-              mProgress.setVisibility(View.GONE); // make sure we show no spinner on startup
-              mText.setText(R.string.voice_initializing);
-              mImage.setImageDrawable(mInitializing);
-              mButtonText.setText(mContext.getText(R.string.cancel));
+                prepareDialog(false, mContext.getText(R.string.voice_initializing), mInitializing,
+                        mContext.getText(R.string.cancel)); 
             }
           });
     }
 
-    public void showStartState() {
-      if (EXPECT_RECORDING_DELAY) {
-          showInitializing();
-      } else {
-          showListening();
-      }
-    }
-
     public void showListening() {
-        mState = State.LISTENING;
         mUiHandler.post(new Runnable() {
             public void run() {
-              mText.setText(R.string.voice_listening);
-              mImage.setImageDrawable(mSpeakNow.get(0));
-              mButtonText.setText(mContext.getText(R.string.cancel));
+                mState = State.LISTENING;
+                prepareDialog(false, mContext.getText(R.string.voice_listening), mSpeakNow.get(0),
+                        mContext.getText(R.string.cancel));
             }
           });
         mUiHandler.postDelayed(mUpdateVolumeRunnable, 50);
@@ -181,13 +164,10 @@
     }
 
     public void showError(final String message) {
-        mState = State.READY;
         mUiHandler.post(new Runnable() {
             public void run() {
-              exitWorking();
-              mText.setText(message);
-              mImage.setImageDrawable(mError);
-              mButtonText.setText(mContext.getText(R.string.ok));
+                mState = State.READY;
+                prepareDialog(false, message, mError, mContext.getText(R.string.ok));
             }
           });
     }
@@ -197,21 +177,33 @@
         final int speechStartPosition,
         final int speechEndPosition) {
 
-        mState = State.WORKING;
-
         mUiHandler.post(new Runnable() {
             public void run() {
-              mText.setText(R.string.voice_working);
-              mImage.setVisibility(View.GONE);
-              mProgress.setVisibility(View.VISIBLE);
-              final ShortBuffer buf = ByteBuffer.wrap(waveBuffer.toByteArray())
-                      .order(ByteOrder.nativeOrder()).asShortBuffer();
-              buf.position(0);
-              waveBuffer.reset();
-              showWave(buf, speechStartPosition / 2, speechEndPosition / 2);
+                mState = State.WORKING;
+                prepareDialog(true, mContext.getText(R.string.voice_working), null, mContext
+                        .getText(R.string.cancel));
+                final ShortBuffer buf = ByteBuffer.wrap(waveBuffer.toByteArray()).order(
+                        ByteOrder.nativeOrder()).asShortBuffer();
+                buf.position(0);
+                waveBuffer.reset();
+                showWave(buf, speechStartPosition / 2, speechEndPosition / 2);
             }
           });
     }
+    
+    private void prepareDialog(boolean spinVisible, CharSequence text, Drawable image,
+            CharSequence btnTxt) {
+        if (spinVisible) {
+            mProgress.setVisibility(View.VISIBLE);
+            mImage.setVisibility(View.GONE);
+        } else {
+            mProgress.setVisibility(View.GONE);
+            mImage.setImageDrawable(image);
+            mImage.setVisibility(View.VISIBLE);
+        }
+        mText.setText(text);
+        mButtonText.setText(btnTxt);
+    }
 
     /**
      * @return an average abs of the specified buffer.
@@ -305,13 +297,12 @@
 
 
     public void finish() {
-        mState = State.READY;
         mUiHandler.post(new Runnable() {
             public void run() {
-              exitWorking();
+                mState = State.READY;
+                exitWorking();
             }
           });
-        showStartState();
     }
 
     private void exitWorking() {