Fix 2402303: Split Keyboard widget from LatinIME into reusable PasswordEntryKeyboardView

- Added new PasswordEntryKeyboardView to internal/widgets.  Widget supports:
	- alpha mode with symbols (latin-1 only).
	- a numeric keyboard
	- IME emulation that applies keyboard input to arbitrary top-level view widget.
- Added new transparent assets to framework resources.
- Modified Keyguard and Keyguard layouts to use new PasswordEntryKeyboardView.
diff --git a/core/java/com/android/internal/widget/PasswordEntryKeyboard.java b/core/java/com/android/internal/widget/PasswordEntryKeyboard.java
new file mode 100644
index 0000000..51f7f69
--- /dev/null
+++ b/core/java/com/android/internal/widget/PasswordEntryKeyboard.java
@@ -0,0 +1,323 @@
+/*
+ * Copyright (C) 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.internal.widget;
+
+import java.util.Locale;
+import android.content.Context;
+import android.content.res.Resources;
+import android.content.res.XmlResourceParser;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.PorterDuff;
+import android.graphics.Paint.Align;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+import android.inputmethodservice.Keyboard;
+import android.inputmethodservice.KeyboardView;
+import android.util.Log;
+import com.android.internal.R;
+
+/**
+ * A basic, embed-able keyboard designed for password entry. Allows entry of all Latin-1 characters.
+ *
+ * It has two modes: alpha and numeric. In alpha mode, it allows all Latin-1 characters and enables
+ * an additional keyboard with symbols.  In numeric mode, it shows a 12-key DTMF dialer-like
+ * keypad with alpha characters hints.
+ */
+public class PasswordEntryKeyboard extends Keyboard {
+    private static final String TAG = "PasswordEntryKeyboard";
+    private static final int SHIFT_OFF = 0;
+    private static final int SHIFT_ON = 1;
+    private static final int SHIFT_LOCKED = 2;
+    public static final int KEYCODE_SPACE = ' ';
+
+    private Drawable mShiftIcon;
+    private Drawable mShiftLockIcon;
+    private Drawable mShiftLockPreviewIcon;
+    private Drawable mOldShiftIcon;
+    private Drawable mOldShiftPreviewIcon;
+    private Drawable mSpaceIcon;
+    private Key mShiftKey;
+    private Key mEnterKey;
+    private Key mF1Key;
+    private Key mSpaceKey;
+    private Locale mLocale;
+    private Resources mRes;
+    private int mExtensionResId;
+    private int mShiftState = SHIFT_OFF;
+
+    static int sSpacebarVerticalCorrection;
+
+    public PasswordEntryKeyboard(Context context, int xmlLayoutResId) {
+        this(context, xmlLayoutResId, 0);
+    }
+
+    public PasswordEntryKeyboard(Context context, int xmlLayoutResId, int mode) {
+        super(context, xmlLayoutResId, mode);
+        final Resources res = context.getResources();
+        mRes = res;
+        mShiftIcon = res.getDrawable(R.drawable.sym_keyboard_shift);
+        mShiftLockIcon = res.getDrawable(R.drawable.sym_keyboard_shift_locked);
+        mShiftLockPreviewIcon = res.getDrawable(R.drawable.sym_keyboard_feedback_shift_locked);
+        mShiftLockPreviewIcon.setBounds(0, 0,
+                mShiftLockPreviewIcon.getIntrinsicWidth(),
+                mShiftLockPreviewIcon.getIntrinsicHeight());
+        mSpaceIcon = res.getDrawable(R.drawable.sym_keyboard_space);
+        sSpacebarVerticalCorrection = res.getDimensionPixelOffset(
+                R.dimen.password_keyboard_spacebar_vertical_correction);
+    }
+
+    public PasswordEntryKeyboard(Context context, int layoutTemplateResId,
+            CharSequence characters, int columns, int horizontalPadding) {
+        super(context, layoutTemplateResId, characters, columns, horizontalPadding);
+    }
+
+    @Override
+    protected Key createKeyFromXml(Resources res, Row parent, int x, int y,
+            XmlResourceParser parser) {
+        LatinKey key = new LatinKey(res, parent, x, y, parser);
+        final int code = key.codes[0];
+        if (code >=0 && code != '\n' && (code < 32 || code > 127)) {
+            Log.w(TAG, "Key code for " + key.label + " is not latin-1");
+            key.label = " ";
+            key.setEnabled(false);
+        }
+        switch (key.codes[0]) {
+            case 10:
+                mEnterKey = key;
+                break;
+            case PasswordEntryKeyboardView.KEYCODE_F1:
+                mF1Key = key;
+                break;
+            case 32:
+                mSpaceKey = key;
+                break;
+        }
+        return key;
+    }
+
+    /**
+     * Allows enter key resources to be overridden
+     * @param res resources to grab given items from
+     * @param previewId preview drawable shown on enter key
+     * @param iconId normal drawable shown on enter key
+     * @param labelId string shown on enter key
+     */
+    void setEnterKeyResources(Resources res, int previewId, int iconId, int labelId) {
+        if (mEnterKey != null) {
+            // Reset some of the rarely used attributes.
+            mEnterKey.popupCharacters = null;
+            mEnterKey.popupResId = 0;
+            mEnterKey.text = null;
+
+            mEnterKey.iconPreview = res.getDrawable(previewId);
+            mEnterKey.icon = res.getDrawable(iconId);
+            mEnterKey.label = res.getText(labelId);
+
+            // Set the initial size of the preview icon
+            if (mEnterKey.iconPreview != null) {
+                mEnterKey.iconPreview.setBounds(0, 0,
+                        mEnterKey.iconPreview.getIntrinsicWidth(),
+                        mEnterKey.iconPreview.getIntrinsicHeight());
+            }
+        }
+    }
+
+    /**
+     * Allows shiftlock to be turned on.  See {@link #setShiftLocked(boolean)}
+     *
+     */
+    void enableShiftLock() {
+        int index = getShiftKeyIndex();
+        if (index >= 0) {
+            mShiftKey = getKeys().get(index);
+            if (mShiftKey instanceof LatinKey) {
+                ((LatinKey)mShiftKey).enableShiftLock();
+            }
+            mOldShiftIcon = mShiftKey.icon;
+            mOldShiftPreviewIcon = mShiftKey.iconPreview;
+        }
+    }
+
+    /**
+     * Turn on shift lock. This turns on the LED for this key, if it has one.
+     * It should be followed by a call to {@link KeyboardView#invalidateKey(int)}
+     * or {@link KeyboardView#invalidateAllKeys()}
+     *
+     * @param shiftLocked
+     */
+    void setShiftLocked(boolean shiftLocked) {
+        if (mShiftKey != null) {
+            if (shiftLocked) {
+                mShiftKey.on = true;
+                mShiftKey.icon = mShiftLockIcon;
+                mShiftState = SHIFT_LOCKED;
+            } else {
+                mShiftKey.on = false;
+                mShiftKey.icon = mShiftLockIcon;
+                mShiftState = SHIFT_ON;
+            }
+        }
+    }
+
+    /**
+     * Turn on shift mode. Sets shift mode and turns on icon for shift key.
+     * It should be followed by a call to {@link KeyboardView#invalidateKey(int)}
+     * or {@link KeyboardView#invalidateAllKeys()}
+     *
+     * @param shiftLocked
+     */
+    @Override
+    public boolean setShifted(boolean shiftState) {
+        boolean shiftChanged = false;
+        if (mShiftKey != null) {
+            if (shiftState == false) {
+                shiftChanged = mShiftState != SHIFT_OFF;
+                mShiftState = SHIFT_OFF;
+                mShiftKey.on = false;
+                mShiftKey.icon = mOldShiftIcon;
+            } else if (mShiftState == SHIFT_OFF) {
+                shiftChanged = mShiftState == SHIFT_OFF;
+                mShiftState = SHIFT_ON;
+                mShiftKey.on = false;
+                mShiftKey.icon = mShiftIcon;
+            }
+        } else {
+            return super.setShifted(shiftState);
+        }
+        return shiftChanged;
+    }
+
+    /**
+     * Whether or not keyboard is shifted.
+     * @return true if keyboard state is shifted.
+     */
+    @Override
+    public boolean isShifted() {
+        if (mShiftKey != null) {
+            return mShiftState != SHIFT_OFF;
+        } else {
+            return super.isShifted();
+        }
+    }
+
+    /**
+     * Sets keyboard extension. Keyboard extension is shown when input is detected above keyboard
+     * while keyboard has focus.
+     *
+     * @param resId
+     */
+    public void setExtension(int resId) {
+        mExtensionResId = resId;
+    }
+
+    /**
+     * Get current extesion resource id.
+     *
+     * @return resource id, 0 if not set.
+     */
+    public int getExtension() {
+        return mExtensionResId;
+    }
+
+    private void updateSpaceBarForLocale() {
+        if (mLocale != null) {
+            // Create the graphic for spacebar
+            Bitmap buffer = Bitmap.createBitmap(mSpaceKey.width, mSpaceIcon.getIntrinsicHeight(),
+                    Bitmap.Config.ARGB_8888);
+            Canvas canvas = new Canvas(buffer);
+            canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
+            Paint paint = new Paint();
+            paint.setAntiAlias(true);
+            // TODO: Make the text size a customizable attribute
+            paint.setTextSize(22);
+            paint.setTextAlign(Align.CENTER);
+            // Draw a drop shadow for the text
+            paint.setShadowLayer(1f, 0, 0, 0xFF000000);
+            paint.setColor(0x80C0C0C0);
+            canvas.drawText(mLocale.getDisplayLanguage(mLocale),
+                    buffer.getWidth() / 2, - paint.ascent() + 2, paint);
+            int x = (buffer.getWidth() - mSpaceIcon.getIntrinsicWidth()) / 2;
+            int y = buffer.getHeight() - mSpaceIcon.getIntrinsicHeight();
+            mSpaceIcon.setBounds(x, y,
+                    x + mSpaceIcon.getIntrinsicWidth(), y + mSpaceIcon.getIntrinsicHeight());
+            mSpaceIcon.draw(canvas);
+            mSpaceKey.icon = new BitmapDrawable(mRes, buffer);
+            mSpaceKey.repeatable = false;
+        } else {
+            mSpaceKey.icon = mRes.getDrawable(R.drawable.sym_keyboard_space);
+            mSpaceKey.repeatable = true;
+        }
+    }
+
+    public void setLanguage(Locale locale) {
+        if (mLocale != null && mLocale.equals(locale)) return;
+        mLocale = locale;
+        updateSpaceBarForLocale();
+    }
+
+    static class LatinKey extends Keyboard.Key {
+        private boolean mShiftLockEnabled;
+        private boolean mEnabled = true;
+
+        public LatinKey(Resources res, Keyboard.Row parent, int x, int y,
+                XmlResourceParser parser) {
+            super(res, parent, x, y, parser);
+            if (popupCharacters != null && popupCharacters.length() == 0) {
+                // If there is a keyboard with no keys specified in popupCharacters
+                popupResId = 0;
+            }
+        }
+
+        void setEnabled(boolean enabled) {
+            mEnabled = enabled;
+        }
+
+        void enableShiftLock() {
+            mShiftLockEnabled = true;
+        }
+
+        @Override
+        public void onReleased(boolean inside) {
+            if (!mShiftLockEnabled) {
+                super.onReleased(inside);
+            } else {
+                pressed = !pressed;
+            }
+        }
+
+        /**
+         * Overriding this method so that we can reduce the target area for certain keys.
+         */
+        @Override
+        public boolean isInside(int x, int y) {
+            if (!mEnabled) {
+                return false;
+            }
+            final int code = codes[0];
+            if (code == KEYCODE_SHIFT || code == KEYCODE_DELETE) {
+                y -= height / 10;
+                if (code == KEYCODE_SHIFT) x += width / 6;
+                if (code == KEYCODE_DELETE) x -= width / 6;
+            } else if (code == KEYCODE_SPACE) {
+                y += PasswordEntryKeyboard.sSpacebarVerticalCorrection;
+            }
+            return super.isInside(x, y);
+        }
+    }
+}
diff --git a/core/java/com/android/internal/widget/PasswordEntryKeyboardHelper.java b/core/java/com/android/internal/widget/PasswordEntryKeyboardHelper.java
new file mode 100644
index 0000000..b809afc
--- /dev/null
+++ b/core/java/com/android/internal/widget/PasswordEntryKeyboardHelper.java
@@ -0,0 +1,230 @@
+/*
+ * Copyright (C) 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.internal.widget;
+
+import android.content.Context;
+import android.inputmethodservice.Keyboard;
+import android.inputmethodservice.KeyboardView;
+import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
+import android.os.Handler;
+import android.os.SystemClock;
+import android.text.Editable;
+import android.text.Selection;
+import android.util.Log;
+import android.view.KeyCharacterMap;
+import android.view.KeyEvent;
+import android.view.View;
+import android.view.ViewRoot;
+import android.view.inputmethod.InputConnection;
+import android.widget.EditText;
+import com.android.internal.R;
+
+public class PasswordEntryKeyboardHelper implements OnKeyboardActionListener {
+
+    public static final int KEYBOARD_MODE_ALPHA = 0;
+    public static final int KEYBOARD_MODE_NUMERIC = 1;
+    private static final int KEYBOARD_STATE_NORMAL = 0;
+    private static final int KEYBOARD_STATE_SHIFTED = 1;
+    private static final int KEYBOARD_STATE_CAPSLOCK = 2;
+    private static final String TAG = "PasswordEntryKeyboardHelper";
+    private int mKeyboardMode = KEYBOARD_MODE_ALPHA;
+    private int mKeyboardState = KEYBOARD_STATE_NORMAL;
+    private PasswordEntryKeyboard mQwertyKeyboard;
+    private PasswordEntryKeyboard mQwertyKeyboardShifted;
+    private PasswordEntryKeyboard mSymbolsKeyboard;
+    private PasswordEntryKeyboard mSymbolsKeyboardShifted;
+    private PasswordEntryKeyboard mNumericKeyboard;
+    private Context mContext;
+    private View mTargetView;
+    private KeyboardView mKeyboardView;
+
+    public PasswordEntryKeyboardHelper(Context context, KeyboardView keyboardView, View targetView) {
+        mContext = context;
+        mTargetView = targetView;
+        mKeyboardView = keyboardView;
+        createKeyboards();
+        mKeyboardView.setOnKeyboardActionListener(this);
+    }
+
+    public boolean isAlpha() {
+        return mKeyboardMode == KEYBOARD_MODE_ALPHA;
+    }
+
+    private void createKeyboards() {
+        mNumericKeyboard = new PasswordEntryKeyboard(mContext, R.xml.password_kbd_numeric);
+        mQwertyKeyboard = new PasswordEntryKeyboard(mContext,
+                R.xml.password_kbd_qwerty, R.id.mode_normal);
+        mQwertyKeyboard.enableShiftLock();
+
+        mQwertyKeyboardShifted = new PasswordEntryKeyboard(mContext,
+                R.xml.password_kbd_qwerty_shifted,
+                R.id.mode_normal);
+        mQwertyKeyboardShifted.enableShiftLock();
+        mQwertyKeyboardShifted.setShifted(true); // always shifted.
+
+        mSymbolsKeyboard = new PasswordEntryKeyboard(mContext, R.xml.password_kbd_symbols);
+        mSymbolsKeyboard.enableShiftLock();
+
+        mSymbolsKeyboardShifted = new PasswordEntryKeyboard(mContext,
+                R.xml.password_kbd_symbols_shift);
+        mSymbolsKeyboardShifted.enableShiftLock();
+        mSymbolsKeyboardShifted.setShifted(true); // always shifted
+    }
+
+    public void setKeyboardMode(int mode) {
+        switch (mode) {
+            case KEYBOARD_MODE_ALPHA:
+                mKeyboardView.setKeyboard(mQwertyKeyboard);
+                mKeyboardState = KEYBOARD_STATE_NORMAL;
+                break;
+            case KEYBOARD_MODE_NUMERIC:
+                mKeyboardView.setKeyboard(mNumericKeyboard);
+                mKeyboardState = KEYBOARD_STATE_NORMAL;
+                break;
+        }
+        mKeyboardMode = mode;
+    }
+
+    private void sendKeyEventsToTarget(int keyEventCode) {
+        Handler handler = mTargetView.getHandler();
+        KeyEvent[] events = KeyCharacterMap.load(KeyCharacterMap.ALPHA).getEvents(
+                new char[] { (char) keyEventCode });
+        if (events != null) {
+            for (KeyEvent event : events) {
+                handler.sendMessage(handler.obtainMessage(ViewRoot.DISPATCH_KEY, event));
+            }
+        }
+    }
+
+    public void sendDownUpKeyEvents(int keyEventCode) {
+        long eventTime = SystemClock.uptimeMillis();
+        Handler handler = mTargetView.getHandler();
+        handler.sendMessage(handler.obtainMessage(ViewRoot.DISPATCH_KEY_FROM_IME,
+                new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyEventCode, 0, 0, 0, 0,
+                    KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE)));
+        handler.sendMessage(handler.obtainMessage(ViewRoot.DISPATCH_KEY_FROM_IME,
+                new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyEventCode, 0, 0, 0, 0,
+                        KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE)));
+    }
+
+    public void onKey(int primaryCode, int[] keyCodes) {
+        Log.v(TAG, "Key code = " + Integer.toHexString(primaryCode));
+        if (primaryCode == Keyboard.KEYCODE_DELETE) {
+            handleBackspace();
+        } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
+            handleShift();
+        } else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
+            handleClose();
+            return;
+        } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE && mKeyboardView != null) {
+            handleModeChange();
+        } else {
+            handleCharacter(primaryCode, keyCodes);
+            // Switch back to old keyboard if we're not in capslock mode
+            if (mKeyboardState == KEYBOARD_STATE_SHIFTED) {
+                // skip to the unlocked state
+                mKeyboardState = KEYBOARD_STATE_CAPSLOCK;
+                handleShift();
+            }
+        }
+    }
+
+    private void handleModeChange() {
+        final Keyboard current = mKeyboardView.getKeyboard();
+        Keyboard next = null;
+        if (current == mQwertyKeyboard || current == mQwertyKeyboardShifted) {
+            next = mSymbolsKeyboard;
+        } else if (current == mSymbolsKeyboard || current == mSymbolsKeyboardShifted) {
+            next = mQwertyKeyboard;
+        }
+        if (next != null) {
+            mKeyboardView.setKeyboard(next);
+            mKeyboardState = KEYBOARD_STATE_NORMAL;
+        }
+    }
+
+    private void handleBackspace() {
+        sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
+    }
+
+    private void handleShift() {
+        if (mKeyboardView == null) {
+            return;
+        }
+        Keyboard current = mKeyboardView.getKeyboard();
+        PasswordEntryKeyboard next = null;
+        final boolean isAlphaMode = current == mQwertyKeyboard
+                || current == mQwertyKeyboardShifted;
+        if (mKeyboardState == KEYBOARD_STATE_NORMAL) {
+            mKeyboardState = isAlphaMode ? KEYBOARD_STATE_SHIFTED : KEYBOARD_STATE_CAPSLOCK;
+            next = isAlphaMode ? mQwertyKeyboardShifted : mSymbolsKeyboardShifted;
+        } else if (mKeyboardState == KEYBOARD_STATE_SHIFTED) {
+            mKeyboardState = KEYBOARD_STATE_CAPSLOCK;
+            next = isAlphaMode ? mQwertyKeyboardShifted : mSymbolsKeyboardShifted;
+        } else if (mKeyboardState == KEYBOARD_STATE_CAPSLOCK) {
+            mKeyboardState = KEYBOARD_STATE_NORMAL;
+            next = isAlphaMode ? mQwertyKeyboard : mSymbolsKeyboard;
+        }
+        if (next != null) {
+            if (next != current) {
+                mKeyboardView.setKeyboard(next);
+            }
+            next.setShiftLocked(mKeyboardState == KEYBOARD_STATE_CAPSLOCK);
+            mKeyboardView.setShifted(mKeyboardState != KEYBOARD_STATE_NORMAL);
+        }
+    }
+
+    private void handleCharacter(int primaryCode, int[] keyCodes) {
+        // Maybe turn off shift if not in capslock mode.
+        if (mKeyboardView.isShifted() && primaryCode != ' ' && primaryCode != '\n') {
+            primaryCode = Character.toUpperCase(primaryCode);
+        }
+        sendKeyEventsToTarget(primaryCode);
+    }
+
+    private void handleClose() {
+
+    }
+
+    public void onPress(int primaryCode) {
+
+    }
+
+    public void onRelease(int primaryCode) {
+
+    }
+
+    public void onText(CharSequence text) {
+
+    }
+
+    public void swipeDown() {
+
+    }
+
+    public void swipeLeft() {
+
+    }
+
+    public void swipeRight() {
+
+    }
+
+    public void swipeUp() {
+
+    }
+};
diff --git a/core/java/com/android/internal/widget/PasswordEntryKeyboardView.java b/core/java/com/android/internal/widget/PasswordEntryKeyboardView.java
new file mode 100644
index 0000000..9b93fc2
--- /dev/null
+++ b/core/java/com/android/internal/widget/PasswordEntryKeyboardView.java
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2010 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.internal.widget;
+
+import android.content.Context;
+import android.inputmethodservice.Keyboard;
+import android.inputmethodservice.KeyboardView;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.MotionEvent;
+import android.widget.PopupWindow;
+import com.android.internal.R;
+
+public class PasswordEntryKeyboardView extends KeyboardView {
+
+    public static final int KEYCODE_OPTIONS = -100;
+    static final int KEYCODE_SHIFT_LONGPRESS = -101;
+    static final int KEYCODE_VOICE = -102;
+    static final int KEYCODE_F1 = -103;
+    static final int KEYCODE_NEXT_LANGUAGE = -104;
+
+    private boolean mExtensionVisible;
+    private PasswordEntryKeyboardView mExtension;
+    private PopupWindow mExtensionPopup;
+    private boolean mFirstEvent;
+
+    public PasswordEntryKeyboardView(Context context, AttributeSet attrs) {
+        super(context, attrs);
+    }
+
+    public PasswordEntryKeyboardView(Context context, AttributeSet attrs, int defStyle) {
+        super(context, attrs, defStyle);
+    }
+
+    @Override
+    public boolean onTouchEvent(MotionEvent me) {
+        if (((PasswordEntryKeyboard) getKeyboard()).getExtension() == 0) {
+            return super.onTouchEvent(me);
+        }
+        if (me.getY() < 0) {
+            if (mExtensionVisible) {
+                int action = me.getAction();
+                if (mFirstEvent) action = MotionEvent.ACTION_DOWN;
+                mFirstEvent = false;
+                MotionEvent translated = MotionEvent.obtain(me.getEventTime(), me.getEventTime(),
+                        action,
+                        me.getX(), me.getY() + mExtension.getHeight(), me.getMetaState());
+                boolean result = mExtension.onTouchEvent(translated);
+                translated.recycle();
+                if (me.getAction() == MotionEvent.ACTION_UP
+                        || me.getAction() == MotionEvent.ACTION_CANCEL) {
+                    closeExtension();
+                }
+                return result;
+            } else {
+                if (openExtension()) {
+                    MotionEvent cancel = MotionEvent.obtain(me.getDownTime(), me.getEventTime(),
+                            MotionEvent.ACTION_CANCEL, me.getX() - 100, me.getY() - 100, 0);
+                    super.onTouchEvent(cancel);
+                    cancel.recycle();
+                    if (mExtension.getHeight() > 0) {
+                        MotionEvent translated = MotionEvent.obtain(me.getEventTime(),
+                                me.getEventTime(),
+                                MotionEvent.ACTION_DOWN,
+                                me.getX(), me.getY() + mExtension.getHeight(),
+                                me.getMetaState());
+                        mExtension.onTouchEvent(translated);
+                        translated.recycle();
+                    } else {
+                        mFirstEvent = true;
+                    }
+                }
+                return true;
+            }
+        } else if (mExtensionVisible) {
+            closeExtension();
+            // Send a down event into the main keyboard first
+            MotionEvent down = MotionEvent.obtain(me.getEventTime(), me.getEventTime(),
+                    MotionEvent.ACTION_DOWN, me.getX(), me.getY(), me.getMetaState());
+            super.onTouchEvent(down);
+            down.recycle();
+            // Send the actual event
+            return super.onTouchEvent(me);
+        } else {
+            return super.onTouchEvent(me);
+        }
+    }
+
+    private boolean openExtension() {
+        if (((PasswordEntryKeyboard) getKeyboard()).getExtension() == 0) return false;
+        makePopupWindow();
+        mExtensionVisible = true;
+        return true;
+    }
+
+    private void makePopupWindow() {
+        if (mExtensionPopup == null) {
+            int[] windowLocation = new int[2];
+            mExtensionPopup = new PopupWindow(getContext());
+            mExtensionPopup.setBackgroundDrawable(null);
+            LayoutInflater li = (LayoutInflater) getContext().getSystemService(
+                    Context.LAYOUT_INFLATER_SERVICE);
+            mExtension = (PasswordEntryKeyboardView) li.inflate(
+                    R.layout.password_keyboard_input, null);
+            mExtension.setOnKeyboardActionListener(getOnKeyboardActionListener());
+            mExtension.setPopupParent(this);
+            mExtension.setPopupOffset(0, -windowLocation[1]);
+            Keyboard keyboard;
+            mExtension.setKeyboard(keyboard = new PasswordEntryKeyboard(getContext(),
+                    ((PasswordEntryKeyboard) getKeyboard()).getExtension()));
+            mExtensionPopup.setContentView(mExtension);
+            mExtensionPopup.setWidth(getWidth());
+            mExtensionPopup.setHeight(keyboard.getHeight());
+            getLocationInWindow(windowLocation);
+            // TODO: Fix the "- 30".
+            mExtension.setPopupOffset(0, -windowLocation[1] - 30);
+            mExtensionPopup.showAtLocation(this, 0, 0, -keyboard.getHeight()
+                    + windowLocation[1]);
+        } else {
+            mExtension.setVisibility(VISIBLE);
+        }
+    }
+
+    @Override
+    public void closing() {
+        super.closing();
+        if (mExtensionPopup != null && mExtensionPopup.isShowing()) {
+            mExtensionPopup.dismiss();
+            mExtensionPopup = null;
+        }
+    }
+
+    private void closeExtension() {
+        mExtension.setVisibility(INVISIBLE);
+        mExtension.closing();
+        mExtensionVisible = false;
+    }
+}