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

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

Merge "Add null analysis annotations to keyboard package"

parents 949045b6 9d5d01a5
Loading
Loading
Loading
Loading
+33 −13
Original line number Diff line number Diff line
@@ -43,6 +43,9 @@ import com.android.inputmethod.latin.common.StringUtils;
import java.util.Arrays;
import java.util.Locale;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Class for describing the position and characteristics of a single key in the keyboard.
 */
@@ -113,9 +116,11 @@ public class Key implements Comparable<Key> {
    /** Y coordinate of the top-left corner of the key in the keyboard layout, excluding the gap. */
    private final int mY;
    /** Hit bounding box of the key */
    @Nonnull
    private final Rect mHitBox = new Rect();

    /** More keys. It is guaranteed that this is null or an array of one or more elements */
    @Nullable
    private final MoreKeySpec[] mMoreKeys;
    /** More keys column number and flags */
    private final int mMoreKeysColumnAndFlags;
@@ -158,8 +163,9 @@ public class Key implements Comparable<Key> {
    private static final int ACTION_FLAGS_ALT_CODE_WHILE_TYPING = 0x04;
    private static final int ACTION_FLAGS_ENABLE_LONG_PRESS = 0x08;

    @Nullable
    private final KeyVisualAttributes mKeyVisualAttributes;

    @Nullable
    private final OptionalAttributes mOptionalAttributes;

    private static final class OptionalAttributes {
@@ -181,6 +187,7 @@ public class Key implements Comparable<Key> {
            mVisualInsetsRight = visualInsetsRight;
        }

        @Nullable
        public static OptionalAttributes newInstance(final String outputText, final int altCode,
                final int disabledIconId, final int visualInsetsLeft, final int visualInsetsRight) {
            if (outputText == null && altCode == CODE_UNSPECIFIED
@@ -204,10 +211,10 @@ public class Key implements Comparable<Key> {
     * Constructor for a key on <code>MoreKeyKeyboard</code>, on <code>MoreSuggestions</code>,
     * and in a <GridRows/>.
     */
    public Key(final String label, final int iconId, final int code, final String outputText,
            final String hintLabel, final int labelFlags, final int backgroundType, final int x,
            final int y, final int width, final int height, final int horizontalGap,
            final int verticalGap) {
    public Key(@Nullable final String label, final int iconId, final int code,
            @Nullable final String outputText, @Nullable final String hintLabel,
            final int labelFlags, final int backgroundType, final int x, final int y,
            final int width, final int height, final int horizontalGap, final int verticalGap) {
        mWidth = width - horizontalGap;
        mHeight = height - verticalGap;
        mHorizontalGap = horizontalGap;
@@ -245,8 +252,9 @@ public class Key implements Comparable<Key> {
     * @param row the row that this key belongs to. row's x-coordinate will be the right edge of
     *        this key.
     */
    public Key(final String keySpec, final TypedArray keyAttr, final KeyStyle style,
            final KeyboardParams params, final KeyboardRow row) {
    public Key(@Nullable final String keySpec, @Nonnull final TypedArray keyAttr,
            @Nonnull final KeyStyle style, @Nonnull final KeyboardParams params,
            @Nonnull final KeyboardRow row) {
        mHorizontalGap = isSpacer() ? 0 : params.mHorizontalGap;
        mVerticalGap = params.mVerticalGap;

@@ -403,11 +411,11 @@ public class Key implements Comparable<Key> {
     *
     * @param key the original key.
     */
    protected Key(final Key key) {
    protected Key(@Nonnull final Key key) {
        this(key, key.mMoreKeys);
    }

    private Key(final Key key, final MoreKeySpec[] moreKeys) {
    private Key(@Nonnull final Key key, @Nullable final MoreKeySpec[] moreKeys) {
        // Final attributes.
        mCode = key.mCode;
        mLabel = key.mLabel;
@@ -433,8 +441,9 @@ public class Key implements Comparable<Key> {
        mEnabled = key.mEnabled;
    }

    public static Key removeRedundantMoreKeys(final Key key,
            final MoreKeySpec.LettersOnBaseLayout lettersOnBaseLayout) {
    @Nonnull
    public static Key removeRedundantMoreKeys(@Nonnull final Key key,
            @Nonnull final MoreKeySpec.LettersOnBaseLayout lettersOnBaseLayout) {
        final MoreKeySpec[] moreKeys = key.getMoreKeys();
        final MoreKeySpec[] filteredMoreKeys = MoreKeySpec.removeRedundantMoreKeys(
                moreKeys, lettersOnBaseLayout);
@@ -554,14 +563,17 @@ public class Key implements Comparable<Key> {
        return mCode;
    }

    @Nullable
    public String getLabel() {
        return mLabel;
    }

    @Nullable
    public String getHintLabel() {
        return mHintLabel;
    }

    @Nullable
    public MoreKeySpec[] getMoreKeys() {
        return mMoreKeys;
    }
@@ -620,6 +632,7 @@ public class Key implements Comparable<Key> {
        return mKeyVisualAttributes;
    }

    @Nonnull
    public final Typeface selectTypeface(final KeyDrawParams params) {
        switch (mLabelFlags & LABEL_FLAGS_FONT_MASK) {
        case LABEL_FLAGS_FONT_NORMAL:
@@ -696,6 +709,7 @@ public class Key implements Comparable<Key> {
        return params.mLetterSize;
    }

    @Nonnull
    public Typeface selectPreviewTypeface(final KeyDrawParams params) {
        if (previewHasLetterSize()) {
            return selectTypeface(params);
@@ -780,6 +794,7 @@ public class Key implements Comparable<Key> {
        return (mMoreKeysColumnAndFlags & MORE_KEYS_FLAGS_NO_PANEL_AUTO_MORE_KEY) != 0;
    }

    @Nullable
    public final String getOutputText() {
        final OptionalAttributes attrs = mOptionalAttributes;
        return (attrs != null) ? attrs.mOutputText : null;
@@ -794,6 +809,7 @@ public class Key implements Comparable<Key> {
        return mIconId;
    }

    @Nullable
    public Drawable getIcon(final KeyboardIconsSet iconSet, final int alpha) {
        final OptionalAttributes attrs = mOptionalAttributes;
        final int disabledIconId = (attrs != null) ? attrs.mDisabledIconId : ICON_UNDEFINED;
@@ -805,6 +821,7 @@ public class Key implements Comparable<Key> {
        return icon;
    }

    @Nullable
    public Drawable getPreviewIcon(final KeyboardIconsSet iconSet) {
        return iconSet.getIconDrawable(getIconId());
    }
@@ -897,6 +914,7 @@ public class Key implements Comparable<Key> {
        mEnabled = enabled;
    }

    @Nonnull
    public Rect getHitBox() {
        return mHitBox;
    }
@@ -968,8 +986,10 @@ public class Key implements Comparable<Key> {
     * @return the background drawable of the key.
     * @see android.graphics.drawable.StateListDrawable#setState(int[])
     */
    public final Drawable selectBackgroundDrawable(final Drawable keyBackground,
            final Drawable functionalKeyBackground, final Drawable spacebarBackground) {
    @Nonnull
    public final Drawable selectBackgroundDrawable(@Nonnull final Drawable keyBackground,
            @Nonnull final Drawable functionalKeyBackground,
            @Nonnull final Drawable spacebarBackground) {
        final Drawable background;
        if (mBackgroundType == BACKGROUND_TYPE_FUNCTIONAL) {
            background = functionalKeyBackground;
+18 −4
Original line number Diff line number Diff line
@@ -28,6 +28,9 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Loads an XML description of a keyboard and stores the attributes of the keys. A keyboard
 * consists of rows of keys.
@@ -47,6 +50,7 @@ import java.util.List;
 * </pre>
 */
public class Keyboard {
    @Nonnull
    public final KeyboardId mId;
    public final int mThemeId;

@@ -78,17 +82,22 @@ public class Keyboard {
    public final int mMaxMoreKeysKeyboardColumn;

    /** List of keys in this keyboard */
    @Nonnull
    private final List<Key> mSortedKeys;
    @Nonnull
    public final List<Key> mShiftKeys;
    @Nonnull
    public final List<Key> mAltCodeKeysWhileTyping;
    @Nonnull
    public final KeyboardIconsSet mIconsSet;

    private final SparseArray<Key> mKeyCache = new SparseArray<>();

    @Nonnull
    private final ProximityInfo mProximityInfo;
    private final boolean mProximityCharsCorrectionEnabled;

    public Keyboard(final KeyboardParams params) {
    public Keyboard(@Nonnull final KeyboardParams params) {
        mId = params.mId;
        mThemeId = params.mThemeId;
        mOccupiedHeight = params.mOccupiedHeight;
@@ -114,7 +123,7 @@ public class Keyboard {
        mProximityCharsCorrectionEnabled = params.mProximityCharsCorrectionEnabled;
    }

    protected Keyboard(final Keyboard keyboard) {
    protected Keyboard(@Nonnull final Keyboard keyboard) {
        mId = keyboard.mId;
        mThemeId = keyboard.mThemeId;
        mOccupiedHeight = keyboard.mOccupiedHeight;
@@ -150,6 +159,7 @@ public class Keyboard {
        return canAssumeNativeHasProximityCharsInfoOfAllKeys || Character.isLetter(code);
    }

    @Nonnull
    public ProximityInfo getProximityInfo() {
        return mProximityInfo;
    }
@@ -160,10 +170,12 @@ public class Keyboard {
     * The list may contain {@link Key.Spacer} object as well.
     * @return the sorted unmodifiable list of {@link Key}s of this keyboard.
     */
    @Nonnull
    public List<Key> getSortedKeys() {
        return mSortedKeys;
    }

    @Nullable
    public Key getKey(final int code) {
        if (code == Constants.CODE_UNSPECIFIED) {
            return null;
@@ -185,7 +197,7 @@ public class Keyboard {
        }
    }

    public boolean hasKey(final Key aKey) {
    public boolean hasKey(@Nonnull final Key aKey) {
        if (mKeyCache.indexOfValue(aKey) >= 0) {
            return true;
        }
@@ -211,6 +223,7 @@ public class Keyboard {
     * @return the list of the nearest keys to the given point. If the given
     * point is out of range, then an array of size zero is returned.
     */
    @Nonnull
    public List<Key> getNearestKeys(final int x, final int y) {
        // Avoid dead pixels at edges of the keyboard
        final int adjustedX = Math.max(0, Math.min(x, mOccupiedWidth - 1));
@@ -218,7 +231,8 @@ public class Keyboard {
        return mProximityInfo.getNearestKeys(adjustedX, adjustedY);
    }

    public int[] getCoordinates(final int[] codePoints) {
    @Nonnull
    public int[] getCoordinates(@Nonnull final int[] codePoints) {
        final int length = codePoints.length;
        final int[] coordinates = CoordinateUtils.newCoordinateArray(length);
        for (int i = 0; i < length; ++i) {
+7 −2
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ public final class KeyboardLayoutSet {
    private static final String KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX = "keyboard_layout_set_";

    private final Context mContext;
    @Nonnull
    private final Params mParams;

    // How many layouts we forcibly keep in cache. This only includes ALPHABET (default) and
@@ -84,6 +85,7 @@ public final class KeyboardLayoutSet {
    private static final Keyboard[] sForcibleKeyboardCache = new Keyboard[FORCIBLE_CACHE_SIZE];
    private static final HashMap<KeyboardId, SoftReference<Keyboard>> sKeyboardCache =
            new HashMap<>();
    @Nonnull
    private static final KeysCache sKeysCache = new KeysCache();
    private final static HashMap<InputMethodSubtype, Integer> sScriptIdsForSubtypes =
            new HashMap<>();
@@ -145,7 +147,8 @@ public final class KeyboardLayoutSet {
        sKeysCache.clear();
    }

    public static int getScriptId(final Resources resources, final InputMethodSubtype subtype) {
    public static int getScriptId(final Resources resources,
            @Nonnull final InputMethodSubtype subtype) {
        final Integer value = sScriptIdsForSubtypes.get(subtype);
        if (null == value) {
            final int scriptId = Builder.readScriptId(resources, subtype);
@@ -155,11 +158,12 @@ public final class KeyboardLayoutSet {
        return value;
    }

    KeyboardLayoutSet(final Context context, final Params params) {
    KeyboardLayoutSet(final Context context, @Nonnull final Params params) {
        mContext = context;
        mParams = params;
    }

    @Nonnull
    public Keyboard getKeyboard(final int baseKeyboardLayoutSetElementId) {
        final int keyboardLayoutSetElementId;
        switch (mParams.mMode) {
@@ -203,6 +207,7 @@ public final class KeyboardLayoutSet {
        }
    }

    @Nonnull
    private Keyboard getKeyboard(final ElementParams elementParams, final KeyboardId id) {
        final SoftReference<Keyboard> ref = sKeyboardCache.get(id);
        final Keyboard cachedKeyboard = (ref == null) ? null : ref.get();
+8 −1
Original line number Diff line number Diff line
@@ -103,7 +103,8 @@ public class KeyboardView extends View {
    // TODO: Consider having a dummy keyboard object to make this @Nonnull
    @Nullable
    private Keyboard mKeyboard;
    protected final KeyDrawParams mKeyDrawParams = new KeyDrawParams();
    @Nonnull
    private final KeyDrawParams mKeyDrawParams = new KeyDrawParams();

    // Drawing
    /** True if all keys should be drawn */
@@ -120,6 +121,7 @@ public class KeyboardView extends View {
    @Nonnull
    private final Paint mPaint = new Paint();
    private final Paint.FontMetrics mFontMetrics = new Paint.FontMetrics();

    public KeyboardView(final Context context, final AttributeSet attrs) {
        this(context, attrs, R.attr.keyboardViewStyle);
    }
@@ -210,6 +212,11 @@ public class KeyboardView extends View {
        return mVerticalCorrection;
    }

    @Nonnull
    protected KeyDrawParams getKeyDrawParams() {
        return mKeyDrawParams;
    }

    protected void updateKeyDrawParams(final int keyHeight) {
        mKeyDrawParams.updateParams(keyHeight, mKeyVisualAttributes);
    }
+1 −1
Original line number Diff line number Diff line
@@ -484,7 +484,7 @@ public final class MainKeyboardView extends KeyboardView implements DrawingProxy

        locatePreviewPlacerView();
        getLocationInWindow(mOriginCoords);
        mKeyPreviewChoreographer.placeAndShowKeyPreview(key, keyboard.mIconsSet, mKeyDrawParams,
        mKeyPreviewChoreographer.placeAndShowKeyPreview(key, keyboard.mIconsSet, getKeyDrawParams(),
                getWidth(), mOriginCoords, mDrawingPreviewPlacerView, isHardwareAccelerated());
    }

Loading