Merge "Remove password field special handling"
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index afbdd36..394414d 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -113,7 +113,7 @@
     // Key events coming any faster than this are long-presses.
     private static final int QUICK_PRESS = 200;
 
-    private static final int SCREEN_ORIENTATION_CHANGE_DETECTION_DELAY = 2;
+    private static final int START_INPUT_VIEW_DELAY_WHEN_SCREEN_ORIENTATION_STARTED = 10;
     private static final int ACCUMULATE_START_INPUT_VIEW_DELAY = 20;
     private static final int RESTORE_KEYBOARD_STATE_DELAY = 500;
 
@@ -198,8 +198,6 @@
 
     // Member variables for remembering the current device orientation.
     private int mDisplayOrientation;
-    private int mDisplayWidth;
-    private int mDisplayHeight;
 
     // Object for reacting to adding/removing a dictionary pack.
     private BroadcastReceiver mDictionaryPackInstallReceiver =
@@ -219,31 +217,10 @@
         private static final int MSG_DISMISS_LANGUAGE_ON_SPACEBAR = 5;
         private static final int MSG_SPACE_TYPED = 6;
         private static final int MSG_SET_BIGRAM_PREDICTIONS = 7;
-        private static final int MSG_CONFIRM_ORIENTATION_CHANGE = 8;
+        private static final int MSG_START_ORIENTATION_CHANGE = 8;
         private static final int MSG_START_INPUT_VIEW = 9;
         private static final int MSG_RESTORE_KEYBOARD_LAYOUT = 10;
 
-        private static class OrientationChangeArgs {
-            public final int mOldWidth;
-            public final int mOldHeight;
-            private int mRetryCount;
-
-            public OrientationChangeArgs(int oldw, int oldh) {
-                mOldWidth = oldw;
-                mOldHeight = oldh;
-                mRetryCount = 0;
-            }
-
-            public boolean hasTimedOut() {
-                mRetryCount++;
-                return mRetryCount >= 10;
-            }
-
-            public boolean hasOrientationChangeFinished(DisplayMetrics dm) {
-                return dm.widthPixels != mOldWidth && dm.heightPixels != mOldHeight;
-            }
-        }
-
         public UIHandler(LatinIME outerInstance) {
             super(outerInstance);
         }
@@ -291,18 +268,6 @@
                             (LatinKeyboard)msg.obj);
                 }
                 break;
-            case MSG_CONFIRM_ORIENTATION_CHANGE: {
-                final OrientationChangeArgs args = (OrientationChangeArgs)msg.obj;
-                final Resources res = latinIme.mResources;
-                final DisplayMetrics dm = res.getDisplayMetrics();
-                if (args.hasTimedOut() || args.hasOrientationChangeFinished(dm)) {
-                    latinIme.setDisplayGeometry(res.getConfiguration(), dm);
-                } else {
-                    // It seems orientation changing is on going.
-                    postConfirmOrientationChange(args);
-                }
-                break;
-            }
             case MSG_START_INPUT_VIEW:
                 latinIme.onStartInputView((EditorInfo)msg.obj, false);
                 break;
@@ -411,22 +376,16 @@
             }
         }
 
-        private void postConfirmOrientationChange(OrientationChangeArgs args) {
-            removeMessages(MSG_CONFIRM_ORIENTATION_CHANGE);
-            // Will confirm whether orientation change has finished or not again.
-            sendMessageDelayed(obtainMessage(MSG_CONFIRM_ORIENTATION_CHANGE, args),
-                    SCREEN_ORIENTATION_CHANGE_DETECTION_DELAY);
-        }
-
-        public void startOrientationChanging(int oldw, int oldh) {
-            postConfirmOrientationChange(new OrientationChangeArgs(oldw, oldh));
+        public void startOrientationChanging() {
+            sendMessageDelayed(obtainMessage(MSG_START_ORIENTATION_CHANGE),
+                    START_INPUT_VIEW_DELAY_WHEN_SCREEN_ORIENTATION_STARTED);
             final LatinIME latinIme = getOuterInstance();
             latinIme.mKeyboardSwitcher.getKeyboardState().save();
             postRestoreKeyboardLayout();
         }
 
         public boolean postStartInputView(EditorInfo attribute) {
-            if (hasMessages(MSG_CONFIRM_ORIENTATION_CHANGE) || hasMessages(MSG_START_INPUT_VIEW)) {
+            if (hasMessages(MSG_START_ORIENTATION_CHANGE) || hasMessages(MSG_START_INPUT_VIEW)) {
                 removeMessages(MSG_START_INPUT_VIEW);
                 // Postpone onStartInputView by ACCUMULATE_START_INPUT_VIEW_DELAY and see if
                 // orientation change has finished.
@@ -438,12 +397,6 @@
         }
     }
 
-    private void setDisplayGeometry(Configuration conf, DisplayMetrics metric) {
-        mDisplayOrientation = conf.orientation;
-        mDisplayWidth = metric.widthPixels;
-        mDisplayHeight = metric.heightPixels;
-    }
-
     @Override
     public void onCreate() {
         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
@@ -481,7 +434,7 @@
             }
         }
 
-        setDisplayGeometry(res.getConfiguration(), res.getDisplayMetrics());
+        mDisplayOrientation = res.getConfiguration().orientation;
 
         // Register to receive ringer mode change and network state change.
         // Also receive installation and removal of a dictionary pack.
@@ -609,8 +562,9 @@
     public void onConfigurationChanged(Configuration conf) {
         mSubtypeSwitcher.onConfigurationChanged(conf);
         // If orientation changed while predicting, commit the change
-        if (conf.orientation != mDisplayOrientation) {
-            mHandler.startOrientationChanging(mDisplayWidth, mDisplayHeight);
+        if (mDisplayOrientation != conf.orientation) {
+            mDisplayOrientation = conf.orientation;
+            mHandler.startOrientationChanging();
             final InputConnection ic = getCurrentInputConnection();
             commitTyped(ic);
             if (ic != null) ic.finishComposingText(); // For voice input
diff --git a/java/src/com/android/inputmethod/latin/StringBuilderPool.java b/java/src/com/android/inputmethod/latin/StringBuilderPool.java
index 66f1237..a663ed4 100644
--- a/java/src/com/android/inputmethod/latin/StringBuilderPool.java
+++ b/java/src/com/android/inputmethod/latin/StringBuilderPool.java
@@ -26,12 +26,20 @@
 public class StringBuilderPool {
     // Singleton
     private static final StringBuilderPool sInstance = new StringBuilderPool();
+    private static final boolean DEBUG = false;
     private StringBuilderPool() {}
-    // TODO: Make this a normal array with a size of 20
+    // TODO: Make this a normal array with a size of 20, or a ConcurrentQueue
     private final List<StringBuilder> mPool =
             Collections.synchronizedList(new ArrayList<StringBuilder>());
 
     public static StringBuilder getStringBuilder(final int initialSize) {
+        // TODO: although the pool is synchronized, the following is not thread-safe.
+        // Two threads entering this at the same time could take the same size of the pool and the
+        // second to attempt removing this index from the pool would crash with an
+        // IndexOutOfBoundsException.
+        // At the moment this pool is only used in Suggest.java and only in one thread so it's
+        // okay. The simplest thing to do here is probably to replace the ArrayList with a
+        // ConcurrentQueue.
         final int poolSize = sInstance.mPool.size();
         final StringBuilder sb = poolSize > 0 ? (StringBuilder) sInstance.mPool.remove(poolSize - 1)
                 : new StringBuilder(initialSize);
@@ -40,6 +48,12 @@
     }
 
     public static void recycle(final StringBuilder garbage) {
+        if (DEBUG) {
+            final int gid = garbage.hashCode();
+            for (final StringBuilder q : sInstance.mPool) {
+                if (gid == q.hashCode()) throw new RuntimeException("Duplicate id " + gid);
+            }
+        }
         sInstance.mPool.add(garbage);
     }
 
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index e3cb698..29b6295 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -284,7 +284,14 @@
     }
 
     protected void addBigramToSuggestions(CharSequence bigram) {
-        mSuggestions.add(bigram);
+        // TODO: Try to be a little more shrewd with resource allocation.
+        // At the moment we copy this object because the StringBuilders are pooled (see
+        // StringBuilderPool.java) and when we are finished using mSuggestions and
+        // mBigramSuggestions we will take everything from both and insert them back in the
+        // pool, so we can't allow the same object to be in both lists at the same time.
+        final StringBuilder sb = StringBuilderPool.getStringBuilder(getApproxMaxWordLength());
+        sb.append(bigram);
+        mSuggestions.add(sb);
     }
 
     // TODO: cleanup dictionaries looking up and suggestions building with SuggestedWords.Builder