load emoji keyboards only when used, and not on keyboard start

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
This commit is contained in:
Helium314 2023-12-11 10:03:21 +01:00
parent 89f8c44cfc
commit 3107144f38
4 changed files with 48 additions and 14 deletions

View file

@ -265,6 +265,34 @@ public class Key implements Comparable<Key> {
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

View file

@ -158,7 +158,7 @@ final class DynamicGridKeyboard extends Keyboard {
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()) {
@ -167,10 +167,12 @@ final class DynamicGridKeyboard extends Keyboard {
}
}
}
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()) {
@ -179,7 +181,9 @@ final class DynamicGridKeyboard extends Keyboard {
}
}
}
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) {

View file

@ -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;
}
}
@ -147,8 +151,7 @@ final class EmojiCategory {
}
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);
@ -167,9 +170,7 @@ final class EmojiCategory {
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);
}
@ -214,7 +215,7 @@ final class EmojiCategory {
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);
@ -258,7 +259,7 @@ final class EmojiCategory {
if (props.mCategoryId == categoryId) {
return sum + categoryPageId;
}
sum += props.mPageCount;
sum += props.getPageCount();
}
Log.w(TAG, "categoryId not found: " + categoryId);
return 0;

View file

@ -1,3 +1,4 @@
// SPDX-License-Identifier: GPL-3.0-only
package org.dslul.openboard.inputmethod.keyboard.internal.keyboard_parser
import android.content.Context