Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 840e42d7 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

Merge "Add getters and copy constructor to Key class"

parents 0663bc04 7dc60f9d
Loading
Loading
Loading
Loading
+4 −4
Original line number Original line Diff line number Diff line
@@ -172,7 +172,7 @@ public final class AccessibilityEntityProvider extends AccessibilityNodeProvider
            return null;
            return null;
        }
        }
        final String keyDescription = getKeyDescription(key);
        final String keyDescription = getKeyDescription(key);
        final Rect boundsInParent = key.mHitBox;
        final Rect boundsInParent = key.getHitBox();


        // Calculate the key's in-screen bounds.
        // Calculate the key's in-screen bounds.
        mTempBoundsInScreen.set(boundsInParent);
        mTempBoundsInScreen.set(boundsInParent);
@@ -208,8 +208,8 @@ public final class AccessibilityEntityProvider extends AccessibilityNodeProvider
     * @param key The key to press.
     * @param key The key to press.
     */
     */
    void simulateKeyPress(final Key key) {
    void simulateKeyPress(final Key key) {
        final int x = key.mHitBox.centerX();
        final int x = key.getHitBox().centerX();
        final int y = key.mHitBox.centerY();
        final int y = key.getHitBox().centerY();
        final long downTime = SystemClock.uptimeMillis();
        final long downTime = SystemClock.uptimeMillis();
        final MotionEvent downEvent = MotionEvent.obtain(
        final MotionEvent downEvent = MotionEvent.obtain(
                downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
                downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0);
@@ -325,6 +325,6 @@ public final class AccessibilityEntityProvider extends AccessibilityNodeProvider
        // The key x- and y-coordinates are stable between layout changes.
        // The key x- and y-coordinates are stable between layout changes.
        // Generate an identifier by bit-shifting the x-coordinate to the
        // Generate an identifier by bit-shifting the x-coordinate to the
        // left-half of the integer and OR'ing with the y-coordinate.
        // left-half of the integer and OR'ing with the y-coordinate.
        return ((0xFFFF & key.mX) << (Integer.SIZE / 2)) | (0xFFFF & key.mY);
        return ((0xFFFF & key.getX()) << (Integer.SIZE / 2)) | (0xFFFF & key.getY());
    }
    }
}
}
+9 −9
Original line number Original line Diff line number Diff line
@@ -97,7 +97,7 @@ public final class KeyCodeDescriptionMapper {
     */
     */
    public String getDescriptionForKey(final Context context, final Keyboard keyboard,
    public String getDescriptionForKey(final Context context, final Keyboard keyboard,
            final Key key, final boolean shouldObscure) {
            final Key key, final boolean shouldObscure) {
        final int code = key.mCode;
        final int code = key.getCode();


        if (code == Constants.CODE_SWITCH_ALPHA_SYMBOL) {
        if (code == Constants.CODE_SWITCH_ALPHA_SYMBOL) {
            final String description = getDescriptionForSwitchAlphaSymbol(context, keyboard);
            final String description = getDescriptionForSwitchAlphaSymbol(context, keyboard);
@@ -116,8 +116,8 @@ public final class KeyCodeDescriptionMapper {
            return getDescriptionForActionKey(context, keyboard, key);
            return getDescriptionForActionKey(context, keyboard, key);
        }
        }


        if (!TextUtils.isEmpty(key.mLabel)) {
        if (!TextUtils.isEmpty(key.getLabel())) {
            final String label = key.mLabel.toString().trim();
            final String label = key.getLabel().trim();


            // First, attempt to map the label to a pre-defined description.
            // First, attempt to map the label to a pre-defined description.
            if (mKeyLabelMap.containsKey(label)) {
            if (mKeyLabelMap.containsKey(label)) {
@@ -126,7 +126,7 @@ public final class KeyCodeDescriptionMapper {
        }
        }


        // Just attempt to speak the description.
        // Just attempt to speak the description.
        if (key.mCode != Constants.CODE_UNSPECIFIED) {
        if (key.getCode() != Constants.CODE_UNSPECIFIED) {
            return getDescriptionForKeyCode(context, keyboard, key, shouldObscure);
            return getDescriptionForKeyCode(context, keyboard, key, shouldObscure);
        }
        }
        return null;
        return null;
@@ -215,8 +215,8 @@ public final class KeyCodeDescriptionMapper {
        final int resId;
        final int resId;


        // Always use the label, if available.
        // Always use the label, if available.
        if (!TextUtils.isEmpty(key.mLabel)) {
        if (!TextUtils.isEmpty(key.getLabel())) {
            return key.mLabel.toString().trim();
            return key.getLabel().trim();
        }
        }


        // Otherwise, use the action ID.
        // Otherwise, use the action ID.
@@ -267,7 +267,7 @@ public final class KeyCodeDescriptionMapper {
     */
     */
    private String getDescriptionForKeyCode(final Context context, final Keyboard keyboard,
    private String getDescriptionForKeyCode(final Context context, final Keyboard keyboard,
            final Key key, final boolean shouldObscure) {
            final Key key, final boolean shouldObscure) {
        final int code = key.mCode;
        final int code = key.getCode();


        // If the key description should be obscured, now is the time to do it.
        // If the key description should be obscured, now is the time to do it.
        final boolean isDefinedNonCtrl = Character.isDefined(code) && !Character.isISOControl(code);
        final boolean isDefinedNonCtrl = Character.isDefined(code) && !Character.isISOControl(code);
@@ -280,8 +280,8 @@ public final class KeyCodeDescriptionMapper {
        if (isDefinedNonCtrl) {
        if (isDefinedNonCtrl) {
            return Character.toString((char) code);
            return Character.toString((char) code);
        }
        }
        if (!TextUtils.isEmpty(key.mLabel)) {
        if (!TextUtils.isEmpty(key.getLabel())) {
            return key.mLabel;
            return key.getLabel();
        }
        }
        return context.getString(R.string.spoken_description_unknown, code);
        return context.getString(R.string.spoken_description_unknown, code);
    }
    }
+98 −27
Original line number Original line Diff line number Diff line
@@ -58,12 +58,12 @@ public class Key implements Comparable<Key> {
    /**
    /**
     * The key code (unicode or custom code) that this key generates.
     * The key code (unicode or custom code) that this key generates.
     */
     */
    public final int mCode;
    private final int mCode;


    /** Label to display */
    /** Label to display */
    public final String mLabel;
    private final String mLabel;
    /** Hint label to display on the key in conjunction with the label */
    /** Hint label to display on the key in conjunction with the label */
    public final String mHintLabel;
    private final String mHintLabel;
    /** Flags of the label */
    /** Flags of the label */
    private final int mLabelFlags;
    private final int mLabelFlags;
    private static final int LABEL_FLAGS_ALIGN_LEFT = 0x01;
    private static final int LABEL_FLAGS_ALIGN_LEFT = 0x01;
@@ -95,18 +95,18 @@ public class Key implements Comparable<Key> {
    private final int mIconId;
    private final int mIconId;


    /** Width of the key, not including the gap */
    /** Width of the key, not including the gap */
    public final int mWidth;
    private final int mWidth;
    /** Height of the key, not including the gap */
    /** Height of the key, not including the gap */
    public final int mHeight;
    private final int mHeight;
    /** X coordinate of the key in the keyboard layout */
    /** X coordinate of the key in the keyboard layout */
    public final int mX;
    private final int mX;
    /** Y coordinate of the key in the keyboard layout */
    /** Y coordinate of the key in the keyboard layout */
    public final int mY;
    private final int mY;
    /** Hit bounding box of the key */
    /** Hit bounding box of the key */
    public final Rect mHitBox = new Rect();
    private final Rect mHitBox = new Rect();


    /** More keys. It is guaranteed that this is null or an array of one or more elements */
    /** More keys. It is guaranteed that this is null or an array of one or more elements */
    public final MoreKeySpec[] mMoreKeys;
    private final MoreKeySpec[] mMoreKeys;
    /** More keys column number and flags */
    /** More keys column number and flags */
    private final int mMoreKeysColumnAndFlags;
    private final int mMoreKeysColumnAndFlags;
    private static final int MORE_KEYS_COLUMN_MASK = 0x000000ff;
    private static final int MORE_KEYS_COLUMN_MASK = 0x000000ff;
@@ -121,7 +121,7 @@ public class Key implements Comparable<Key> {
    private static final String MORE_KEYS_NO_PANEL_AUTO_MORE_KEY = "!noPanelAutoMoreKey!";
    private static final String MORE_KEYS_NO_PANEL_AUTO_MORE_KEY = "!noPanelAutoMoreKey!";


    /** Background type that represents different key background visual than normal one. */
    /** Background type that represents different key background visual than normal one. */
    public final int mBackgroundType;
    private final int mBackgroundType;
    public static final int BACKGROUND_TYPE_EMPTY = 0;
    public static final int BACKGROUND_TYPE_EMPTY = 0;
    public static final int BACKGROUND_TYPE_NORMAL = 1;
    public static final int BACKGROUND_TYPE_NORMAL = 1;
    public static final int BACKGROUND_TYPE_FUNCTIONAL = 2;
    public static final int BACKGROUND_TYPE_FUNCTIONAL = 2;
@@ -135,7 +135,7 @@ public class Key implements Comparable<Key> {
    private static final int ACTION_FLAGS_ALT_CODE_WHILE_TYPING = 0x04;
    private static final int ACTION_FLAGS_ALT_CODE_WHILE_TYPING = 0x04;
    private static final int ACTION_FLAGS_ENABLE_LONG_PRESS = 0x08;
    private static final int ACTION_FLAGS_ENABLE_LONG_PRESS = 0x08;


    public final KeyVisualAttributes mKeyVisualAttributes;
    private final KeyVisualAttributes mKeyVisualAttributes;


    private final OptionalAttributes mOptionalAttributes;
    private final OptionalAttributes mOptionalAttributes;


@@ -151,7 +151,7 @@ public class Key implements Comparable<Key> {
        public final int mVisualInsetsLeft;
        public final int mVisualInsetsLeft;
        public final int mVisualInsetsRight;
        public final int mVisualInsetsRight;


        public OptionalAttributes(final String outputText, final int altCode,
        private OptionalAttributes(final String outputText, final int altCode,
                final int disabledIconId, final int previewIconId,
                final int disabledIconId, final int previewIconId,
                final int visualInsetsLeft, final int visualInsetsRight) {
                final int visualInsetsLeft, final int visualInsetsRight) {
            mOutputText = outputText;
            mOutputText = outputText;
@@ -161,6 +161,18 @@ public class Key implements Comparable<Key> {
            mVisualInsetsLeft = visualInsetsLeft;
            mVisualInsetsLeft = visualInsetsLeft;
            mVisualInsetsRight = visualInsetsRight;
            mVisualInsetsRight = visualInsetsRight;
        }
        }

        public static OptionalAttributes newInstance(final String outputText, final int altCode,
                final int disabledIconId, final int previewIconId,
                final int visualInsetsLeft, final int visualInsetsRight) {
            if (outputText == null && altCode == CODE_UNSPECIFIED
                    && disabledIconId == ICON_UNDEFINED && previewIconId == ICON_UNDEFINED
                    && visualInsetsLeft == 0 && visualInsetsRight == 0) {
                return null;
            }
            return new OptionalAttributes(outputText, altCode, disabledIconId, previewIconId,
                    visualInsetsLeft, visualInsetsRight);
        }
    }
    }


    private final int mHashCode;
    private final int mHashCode;
@@ -194,12 +206,9 @@ public class Key implements Comparable<Key> {
        mMoreKeys = null;
        mMoreKeys = null;
        mMoreKeysColumnAndFlags = 0;
        mMoreKeysColumnAndFlags = 0;
        mLabel = label;
        mLabel = label;
        if (outputText == null) {
        mOptionalAttributes = OptionalAttributes.newInstance(outputText, CODE_UNSPECIFIED,
            mOptionalAttributes = null;
                ICON_UNDEFINED, ICON_UNDEFINED,
        } else {
                0 /* visualInsetsLeft */, 0 /* visualInsetsRight */);
            mOptionalAttributes = new OptionalAttributes(outputText, CODE_UNSPECIFIED,
                    ICON_UNDEFINED, ICON_UNDEFINED, 0, 0);
        }
        mCode = code;
        mCode = code;
        mEnabled = (code != CODE_UNSPECIFIED);
        mEnabled = (code != CODE_UNSPECIFIED);
        mIconId = iconId;
        mIconId = iconId;
@@ -360,15 +369,8 @@ public class Key implements Comparable<Key> {
                KeySpecParser.parseCode(style.getString(keyAttr,
                KeySpecParser.parseCode(style.getString(keyAttr,
                R.styleable.Keyboard_Key_altCode), params.mCodesSet, CODE_UNSPECIFIED),
                R.styleable.Keyboard_Key_altCode), params.mCodesSet, CODE_UNSPECIFIED),
                needsToUpperCase, locale);
                needsToUpperCase, locale);
        if (outputText == null && altCode == CODE_UNSPECIFIED
        mOptionalAttributes = OptionalAttributes.newInstance(outputText, altCode,
                && disabledIconId == ICON_UNDEFINED && previewIconId == ICON_UNDEFINED
                disabledIconId, previewIconId, visualInsetsLeft, visualInsetsRight);
                && visualInsetsLeft == 0 && visualInsetsRight == 0) {
            mOptionalAttributes = null;
        } else {
            mOptionalAttributes = new OptionalAttributes(outputText, altCode,
                    disabledIconId, previewIconId,
                    visualInsetsLeft, visualInsetsRight);
        }
        mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr);
        mKeyVisualAttributes = KeyVisualAttributes.newInstance(keyAttr);
        keyAttr.recycle();
        keyAttr.recycle();
        mHashCode = computeHashCode(this);
        mHashCode = computeHashCode(this);
@@ -377,6 +379,35 @@ public class Key implements Comparable<Key> {
        }
        }
    }
    }


    /**
     * Copy constructor.
     *
     * @param key the original key.
     */
    protected Key(final Key key) {
        // Final attributes.
        mCode = key.mCode;
        mLabel = key.mLabel;
        mHintLabel = key.mHintLabel;
        mLabelFlags = key.mLabelFlags;
        mIconId = key.mIconId;
        mWidth = key.mWidth;
        mHeight = key.mHeight;
        mX = key.mX;
        mY = key.mY;
        mHitBox.set(key.mHitBox);
        mMoreKeys = key.mMoreKeys;
        mMoreKeysColumnAndFlags = key.mMoreKeysColumnAndFlags;
        mBackgroundType = key.mBackgroundType;
        mActionFlags = key.mActionFlags;
        mKeyVisualAttributes = key.mKeyVisualAttributes;
        mOptionalAttributes = key.mOptionalAttributes;
        mHashCode = key.mHashCode;
        // Key state.
        mPressed = key.mPressed;
        mEnabled = key.mEnabled;
    }

    private static boolean needsToUpperCase(final int labelFlags, final int keyboardElementId) {
    private static boolean needsToUpperCase(final int labelFlags, final int keyboardElementId) {
        if ((labelFlags & LABEL_FLAGS_PRESERVE_CASE) != 0) return false;
        if ((labelFlags & LABEL_FLAGS_PRESERVE_CASE) != 0) return false;
        switch (keyboardElementId) {
        switch (keyboardElementId) {
@@ -476,6 +507,22 @@ public class Key implements Comparable<Key> {
        }
        }
    }
    }


    public int getCode() {
        return mCode;
    }

    public String getLabel() {
        return mLabel;
    }

    public String getHintLabel() {
        return mHintLabel;
    }

    public MoreKeySpec[] getMoreKeys() {
        return mMoreKeys;
    }

    public void markAsLeftEdge(final KeyboardParams params) {
    public void markAsLeftEdge(final KeyboardParams params) {
        mHitBox.left = params.mLeftPadding;
        mHitBox.left = params.mLeftPadding;
    }
    }
@@ -522,6 +569,10 @@ public class Key implements Comparable<Key> {
                && (mLabelFlags & LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED) == 0;
                && (mLabelFlags & LABEL_FLAGS_SHIFTED_LETTER_ACTIVATED) == 0;
    }
    }


    public KeyVisualAttributes getVisualAttributes() {
        return mKeyVisualAttributes;
    }

    public final Typeface selectTypeface(final KeyDrawParams params) {
    public final Typeface selectTypeface(final KeyDrawParams params) {
        // TODO: Handle "bold" here too?
        // TODO: Handle "bold" here too?
        if ((mLabelFlags & LABEL_FLAGS_FONT_NORMAL) != 0) {
        if ((mLabelFlags & LABEL_FLAGS_FONT_NORMAL) != 0) {
@@ -696,6 +747,22 @@ public class Key implements Comparable<Key> {
                ? iconSet.getIconDrawable(previewIconId) : iconSet.getIconDrawable(mIconId);
                ? iconSet.getIconDrawable(previewIconId) : iconSet.getIconDrawable(mIconId);
    }
    }


    public int getWidth() {
        return mWidth;
    }

    public int getHeight() {
        return mHeight;
    }

    public int getX() {
        return mX;
    }

    public int getY() {
        return mY;
    }

    public final int getDrawX() {
    public final int getDrawX() {
        final OptionalAttributes attrs = mOptionalAttributes;
        final OptionalAttributes attrs = mOptionalAttributes;
        return (attrs == null) ? mX : mX + attrs.mVisualInsetsLeft;
        return (attrs == null) ? mX : mX + attrs.mVisualInsetsLeft;
@@ -733,6 +800,10 @@ public class Key implements Comparable<Key> {
        mEnabled = enabled;
        mEnabled = enabled;
    }
    }


    public Rect getHitBox() {
        return mHitBox;
    }

    /**
    /**
     * Detects if a point falls on this key.
     * Detects if a point falls on this key.
     * @param x the x-coordinate of the point
     * @param x the x-coordinate of the point
+4 −3
Original line number Original line Diff line number Diff line
@@ -108,8 +108,9 @@ public class KeyDetector {
            if (distance > minDistance) {
            if (distance > minDistance) {
                continue;
                continue;
            }
            }
            // To take care of hitbox overlaps, we compare mCode here too.
            // To take care of hitbox overlaps, we compare key's code here too.
            if (primaryKey == null || distance < minDistance || key.mCode > primaryKey.mCode) {
            if (primaryKey == null || distance < minDistance
                    || key.getCode() > primaryKey.getCode()) {
                minDistance = distance;
                minDistance = distance;
                primaryKey = key;
                primaryKey = key;
            }
            }
@@ -118,7 +119,7 @@ public class KeyDetector {
    }
    }


    public static String printableCode(Key key) {
    public static String printableCode(Key key) {
        return key != null ? Constants.printableCode(key.mCode) : "none";
        return key != null ? Constants.printableCode(key.getCode()) : "none";
    }
    }


    public static String printableCodes(int[] codes) {
    public static String printableCodes(int[] codes) {
+2 −2
Original line number Original line Diff line number Diff line
@@ -131,7 +131,7 @@ public class Keyboard {
            }
            }


            for (final Key key : mKeys) {
            for (final Key key : mKeys) {
                if (key.mCode == code) {
                if (key.getCode() == code) {
                    mKeyCache.put(code, key);
                    mKeyCache.put(code, key);
                    return key;
                    return key;
                }
                }
@@ -148,7 +148,7 @@ public class Keyboard {


        for (final Key key : mKeys) {
        for (final Key key : mKeys) {
            if (key == aKey) {
            if (key == aKey) {
                mKeyCache.put(key.mCode, key);
                mKeyCache.put(key.getCode(), key);
                return true;
                return true;
            }
            }
        }
        }
Loading