Skip to content

Commit

Permalink
load emoji keyboards only when used, and not on keyboard start
Browse files Browse the repository at this point in the history
ca 30% faster keyboard loading (on first start, or when theme/color change)
ca 5-10% reduced memory use
may result in short loading times when an emoji tab is opened for the first time
  • Loading branch information
Helium314 committed Dec 11, 2023
1 parent 89f8c44 commit 3107144
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,34 @@ protected Key(@NonNull final Key key, @Nullable final MoreKeySpec[] moreKeys,
mEnabled = key.mEnabled;
}

/** constructor for creating emoji recent keys when there is no keyboard to take keys from */
public Key(@NonNull final Key key, @Nullable final MoreKeySpec[] moreKeys,
@Nullable final String labelHint, final int backgroundType, final int code, @Nullable final String outputText) {
// Final attributes.
mCode = outputText == null ? code : CODE_OUTPUT_TEXT;
mLabel = outputText == null ? StringUtils.newSingleCodePointString(code) : outputText;
mHintLabel = labelHint;
mLabelFlags = key.mLabelFlags;
mIconId = key.mIconId;
mWidth = key.mWidth;
mHeight = key.mHeight;
mHorizontalGap = key.mHorizontalGap;
mVerticalGap = key.mVerticalGap;
mX = key.mX;
mY = key.mY;
mHitBox.set(key.mHitBox);
mMoreKeys = moreKeys;
mMoreKeysColumnAndFlags = key.mMoreKeysColumnAndFlags;
mBackgroundType = backgroundType;
mActionFlags = key.mActionFlags;
mKeyVisualAttributes = key.mKeyVisualAttributes;
mOptionalAttributes = outputText == null ? null : Key.OptionalAttributes.newInstance(outputText, CODE_UNSPECIFIED, ICON_UNDEFINED, 0, 0);
mHashCode = key.mHashCode;
// Key state.
mPressed = key.mPressed;
mEnabled = key.mEnabled;
}

/** constructor from KeyParams */
private Key(KeyParams keyParams) {
// stuff to copy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void saveRecentKeys() {
Settings.writeEmojiRecentKeys(mPrefs, jsonStr);
}

private static Key getKeyByCode(final Collection<DynamicGridKeyboard> keyboards,
private Key getKeyByCode(final Collection<DynamicGridKeyboard> keyboards,
final int code) {
for (final DynamicGridKeyboard keyboard : keyboards) {
for (final Key key : keyboard.getSortedKeys()) {
Expand All @@ -167,10 +167,12 @@ private static Key getKeyByCode(final Collection<DynamicGridKeyboard> keyboards,
}
}
}
return null;

// fall back to creating the key
return new Key(getTemplateKey(TEMPLATE_KEY_CODE_0), null, null, Key.BACKGROUND_TYPE_EMPTY, code, null);
}

private static Key getKeyByOutputText(final Collection<DynamicGridKeyboard> keyboards,
private Key getKeyByOutputText(final Collection<DynamicGridKeyboard> keyboards,
final String outputText) {
for (final DynamicGridKeyboard keyboard : keyboards) {
for (final Key key : keyboard.getSortedKeys()) {
Expand All @@ -179,7 +181,9 @@ private static Key getKeyByOutputText(final Collection<DynamicGridKeyboard> keyb
}
}
}
return null;

// fall back to creating the key
return new Key(getTemplateKey(TEMPLATE_KEY_CODE_0), null, null, Key.BACKGROUND_TYPE_EMPTY, 0, outputText);
}

public void loadRecentKeys(final Collection<DynamicGridKeyboard> keyboards) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ final class EmojiCategory {

public final class CategoryProperties {
public final int mCategoryId;
public final int mPageCount;
public CategoryProperties(final int categoryId, final int pageCount) {
private int mPageCount = -1;
public CategoryProperties(final int categoryId) {
mCategoryId = categoryId;
mPageCount = pageCount;
}
public int getPageCount() {
if (mPageCount < 0)
mPageCount = computeCategoryPageCount(mCategoryId);
return mPageCount;
}
}

Expand Down Expand Up @@ -147,8 +151,7 @@ public EmojiCategory(final SharedPreferences prefs, final Resources res,
}
addShownCategoryId(EmojiCategory.ID_EMOTICONS);

DynamicGridKeyboard recentsKbd =
getKeyboard(EmojiCategory.ID_RECENTS, 0 /* categoryPageId */);
DynamicGridKeyboard recentsKbd = getKeyboard(EmojiCategory.ID_RECENTS, 0);
recentsKbd.loadRecentKeys(mCategoryKeyboardMap.values());

mCurrentCategoryId = Settings.readLastShownEmojiCategoryId(mPrefs, defaultCategoryId);
Expand All @@ -167,9 +170,7 @@ public EmojiCategory(final SharedPreferences prefs, final Resources res,

private void addShownCategoryId(final int categoryId) {
// Load a keyboard of categoryId
getKeyboard(categoryId, 0 /* categoryPageId */);
final CategoryProperties properties =
new CategoryProperties(categoryId, computeCategoryPageCount(categoryId));
final CategoryProperties properties = new CategoryProperties(categoryId);
mShownCategories.add(properties);
}

Expand Down Expand Up @@ -214,7 +215,7 @@ public int getCurrentCategoryPageCount() {
public int getCategoryPageCount(final int categoryId) {
for (final CategoryProperties prop : mShownCategories) {
if (prop.mCategoryId == categoryId) {
return prop.mPageCount;
return prop.getPageCount();
}
}
Log.w(TAG, "Invalid category id: " + categoryId);
Expand Down Expand Up @@ -258,7 +259,7 @@ public int getPagerPageIdFromCategoryAndPageId(final int categoryId, final int c
if (props.mCategoryId == categoryId) {
return sum + categoryPageId;
}
sum += props.mPageCount;
sum += props.getPageCount();
}
Log.w(TAG, "categoryId not found: " + categoryId);
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: GPL-3.0-only
package org.dslul.openboard.inputmethod.keyboard.internal.keyboard_parser

import android.content.Context
Expand Down

0 comments on commit 3107144

Please sign in to comment.