b/7363553: fix keyboard interaction when re-entering Compose Message activity.

rework the fix for b/7350054, use recommendations from Satoshi to call getWindow().setSoftInputMode()
instead of explicitly showing the keyboard later.

Reduce the number places to call showKeyboard() from 3 places to 1. The new logic is, if there
is a draft for this conversation, we set the window mode to SOFT_INPUT_STATE_VISIBLE, otherwise,
set the window mode to SOFT_INPUT_STATE_HIDDEN. We check DraftCache to see if there is a draft,
and don't need to wait until the full content is loaded to determine that.

In addition, employ a hack to hide the keyboard in onPause(), to avoid a jank caused by
onSizeChanged() called twice when re-entering the Compose activity. This improves the jank
slightly, as we still have to adjust the scroll position, leading to the page jump after the
keyboard is opened. It's arguably a little better than before.

Change-Id: Ic4874c103ae235ce3c813a15bbd72283bc844b1b
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index eccff7d..b45abad 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -80,7 +80,7 @@
 
         <activity android:name=".ui.ComposeMessageActivity"
                   android:configChanges="orientation|screenSize|keyboardHidden"
-                  android:windowSoftInputMode="stateHidden"
+                  android:windowSoftInputMode="stateHidden|adjustResize"
                   android:theme="@style/MmsHoloTheme"
                   android:parentActivityName=".ui.ConversationList"
                   android:launchMode="singleTop" >
diff --git a/src/com/android/mms/ui/ComposeMessageActivity.java b/src/com/android/mms/ui/ComposeMessageActivity.java
index 9212749..a847d67 100644
--- a/src/com/android/mms/ui/ComposeMessageActivity.java
+++ b/src/com/android/mms/ui/ComposeMessageActivity.java
@@ -138,6 +138,7 @@
 import com.android.mms.ui.MessageListView.OnSizeChangedListener;
 import com.android.mms.ui.MessageUtils.ResizeImageResultCallback;
 import com.android.mms.ui.RecipientsEditor.RecipientContextMenuInfo;
+import com.android.mms.util.DraftCache;
 import com.android.mms.util.PhoneNumberFormatter;
 import com.android.mms.util.SendingProgressTokenManager;
 import com.android.mms.util.SmileyParser;
@@ -312,7 +313,6 @@
 
     // keys for extras and icicles
     public final static String THREAD_ID = "thread_id";
-    private final static String KEYBOARD_OPEN = "keyboardOpen";
     private final static String RECIPIENTS = "recipients";
 
     @SuppressWarnings("unused")
@@ -1931,11 +1931,6 @@
             // short-circuited.
             hideRecipientEditor();
             initRecipientsEditor();
-
-            // Bring up the softkeyboard so the user can immediately enter recipients. This
-            // call won't do anything on devices with a hard keyboard.
-            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
-                    WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
         } else {
             hideRecipientEditor();
         }
@@ -2140,7 +2135,6 @@
         super.onSaveInstanceState(outState);
 
         outState.putString(RECIPIENTS, getRecipients().serialize());
-        outState.putBoolean(KEYBOARD_OPEN, mIsKeyboardOpen);
 
         mWorkingMessage.writeStateToBundle(outState);
 
@@ -2179,10 +2173,27 @@
         mIsRunning = true;
         updateThreadIdIfRunning();
         mConversation.markAsRead();
+
+        int mode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
+        if (DraftCache.getInstance().hasDraft(mConversation.getThreadId())) {
+            mode |= WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE;
+        } else {
+            mode |= WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN;
+        }
+        getWindow().setSoftInputMode(mode);
     }
 
     @Override
     protected void onPause() {
+        // HACK: fix for getting double callback for onSizeChanged() after re-entering this
+        // activity. Wait 100ms until InputMethodManagerService handles hideSoftInput
+        // This should really be fixed in WindowManagerService
+        hideKeyboard();
+        try {
+            Thread.sleep(100);
+        } catch (InterruptedException ex) {
+        }
+
         super.onPause();
 
         if (DEBUG) {
@@ -3377,6 +3388,11 @@
 
         mMsgListView.setOnSizeChangedListener(new OnSizeChangedListener() {
             public void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
+                if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
+                    Log.v(TAG, "##### onSizeChanged: w=" + width + " h=" + height +
+                            " oldw=" + oldWidth + " oldh=" + oldHeight);
+                }
+
                 // The message list view changed size, most likely because the keyboard
                 // appeared or disappeared or the user typed/deleted chars in the message
                 // box causing it to change its height when expanding/collapsing to hold more
@@ -3505,9 +3521,6 @@
                         drawTopPanel(false);
                         drawBottomPanel();
                         updateSendButtonState();
-                        if (!TextUtils.isEmpty(mTextEditor.getText())) {
-                            showKeyboard();
-                        }
                     }
                 });
     }
@@ -3670,28 +3683,6 @@
         inputMethodManager.hideSoftInputFromWindow(mTextEditor.getWindowToken(), 0);
     }
 
-    @Override
-    public void onWindowFocusChanged(boolean hasFocus) {
-        super.onWindowFocusChanged(hasFocus);
-        if (hasFocus && !TextUtils.isEmpty(mTextEditor.getText())) {
-            showKeyboard();
-        }
-    }
-
-    private void showKeyboard() {
-        mMessageListItemHandler.postDelayed(new Runnable() {
-            @Override
-            public void run() {
-                InputMethodManager inputMethodManager =
-                    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
-
-                if (!inputMethodManager.showSoftInput(mTextEditor, 0)) {
-                    log("Keyboard NOT SHOWN!");
-                }
-            }
-        }, 0);
-    }
-
     private void updateSendButtonState() {
         boolean enable = false;
         if (isPreparedForSending()) {
@@ -3735,7 +3726,6 @@
         if (bundle != null) {
             setIntent(getIntent().setAction(Intent.ACTION_VIEW));
             String recipients = bundle.getString(RECIPIENTS);
-            boolean keyboardOpen = bundle.getBoolean(KEYBOARD_OPEN);
             if (LogTag.VERBOSE) log("get mConversation by recipients " + recipients);
             mConversation = Conversation.get(this,
                     ContactList.getByNumbers(recipients,
@@ -3744,9 +3734,6 @@
             mExitOnSent = bundle.getBoolean("exit_on_sent", false);
             mWorkingMessage.readStateFromBundle(bundle);
 
-            if (keyboardOpen) {
-                showKeyboard();
-            }
             return;
         }