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

Commit e5320c1d authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Generate KeyboardTextsTable.java instead of KeyboardTextsSet.java

Change-Id: Ic48b093dc64b514822cc9daa1ac3d79deb7af089
parent 4f72923f
Loading
Loading
Loading
Loading
+4 −3521

File changed.

Preview size limit exceeded, changes collapsed.

+3546 −0

File added.

Preview size limit exceeded, changes collapsed.

+94 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 The Android Open Source Project
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -16,22 +16,15 @@

package com.android.inputmethod.keyboard.internal;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;

import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.utils.CollectionUtils;

import java.util.HashMap;
import java.util.Locale;

/**
 * !!!!! DO NOT EDIT THIS FILE !!!!!
 *
 * This file is generated by tools/make-keyboard-text. The base template file is
 *   tools/make-keyboard-text/res/com/android/inputmethod/keyboard/internal/KeyboardTextsSet.tmpl
 *   tools/make-keyboard-text/res/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl
 *
 * This file must be updated when any text resources in keyboard layout files have been changed.
 * These text resources are referred as "!text/<resource_name>" in keyboard XML definitions,
@@ -45,131 +38,29 @@ import java.util.Locale;
 *
 * The updated source file will be generated to the following path (this file).
 *   packages/inputmethods/LatinIME/java/src/com/android/inputmethod/keyboard/internal/
 *   KeyboardTextsSet.java
 *   KeyboardTextsTable.java
 */
public final class KeyboardTextsSet {
    public static final String PREFIX_TEXT = "!text/";
    private static final char BACKSLASH = Constants.CODE_BACKSLASH;
    private static final int MAX_STRING_REFERENCE_INDIRECTION = 10;

public final class KeyboardTextsTable {
    // Name to index map.
    private static final HashMap<String, Integer> sNameToIndexesMap = CollectionUtils.newHashMap();
    // Language to texts map.
    private static final HashMap<String, String[]> sLanguageToTextsMap =
            CollectionUtils.newHashMap();
    private static final HashMap<String, Integer> sNameToIdsMap = CollectionUtils.newHashMap();

    private String[] mTexts;
    // Resource name to text map.
    private HashMap<String, String> mResourceNameToTextsMap = CollectionUtils.newHashMap();

    public void setLocale(final Locale locale) {
        final String language = locale.getLanguage();
        mTexts = sLanguageToTextsMap.get(language);
        if (mTexts == null) {
            mTexts = LANGUAGE_DEFAULT;
        }
    }

    public void loadStringResources(final Context context) {
        final int referenceId = context.getApplicationInfo().labelRes;
        loadStringResourcesInternal(context, RESOURCE_NAMES, referenceId);
    }

    @UsedForTesting
    void loadStringResourcesInternal(final Context context, final String[] resourceNames,
            final int referenceId) {
        final Resources res = context.getResources();
        final String packageName = res.getResourcePackageName(referenceId);
        for (final String resName : resourceNames) {
            final int resId = res.getIdentifier(resName, "string", packageName);
            mResourceNameToTextsMap.put(resName, res.getString(resId));
        }
    }

    public String getText(final String name) {
        String text = mResourceNameToTextsMap.get(name);
        if (text != null) {
            return text;
        }
        final Integer id = sNameToIdsMap.get(name);
        if (id == null) throw new RuntimeException("Unknown label: " + name);
        text = (id < mTexts.length) ? mTexts[id] : null;
        return (text == null) ? LANGUAGE_DEFAULT[id] : text;
    }

    private static int searchTextNameEnd(final String text, final int start) {
        final int size = text.length();
        for (int pos = start; pos < size; pos++) {
            final char c = text.charAt(pos);
            // Label name should be consisted of [a-zA-Z_0-9].
            if ((c >= 'a' && c <= 'z') || c == '_' || (c >= '0' && c <= '9')) {
                continue;
            }
            return pos;
        }
        return size;
    }

    public String resolveTextReference(final String rawText) {
        if (TextUtils.isEmpty(rawText)) {
            return null;
        }
        int level = 0;
        String text = rawText;
        StringBuilder sb;
        do {
            level++;
            if (level >= MAX_STRING_REFERENCE_INDIRECTION) {
                throw new RuntimeException("too many @string/resource indirection: " + text);
            }

            final int prefixLen = PREFIX_TEXT.length();
            final int size = text.length();
            if (size < prefixLen) {
                return TextUtils.isEmpty(text) ? null : text;
            }

            sb = null;
            for (int pos = 0; pos < size; pos++) {
                final char c = text.charAt(pos);
                if (text.startsWith(PREFIX_TEXT, pos)) {
                    if (sb == null) {
                        sb = new StringBuilder(text.substring(0, pos));
                    }
                    final int end = searchTextNameEnd(text, pos + prefixLen);
                    final String name = text.substring(pos + prefixLen, end);
                    sb.append(getText(name));
                    pos = end - 1;
                } else if (c == BACKSLASH) {
                    if (sb != null) {
                        // Append both escape character and escaped character.
                        sb.append(text.substring(pos, Math.min(pos + 2, size)));
                    }
                    pos++;
                } else if (sb != null) {
                    sb.append(c);
    public static String getText(final String name, final String[] textsTable) {
        final Integer indexObj = sNameToIndexesMap.get(name);
        if (indexObj == null) {
            throw new RuntimeException("Unknown text name: " + name);
        }
        final int index = indexObj;
        final String text = (index < textsTable.length) ? textsTable[index] : null;
        return (text != null) ? text : LANGUAGE_DEFAULT[index];
    }

            if (sb != null) {
                text = sb.toString();
    public static String[] getTextsTable(final String language) {
        final String[] textsTable = sLanguageToTextsMap.get(language);
        return textsTable != null ? textsTable : LANGUAGE_DEFAULT;
    }
        } while (sb != null);
        return TextUtils.isEmpty(text) ? null : text;
    }

    // These texts' name should be aligned with the @string/<name> in
    // values*/strings-action-keys.xml.
    private static final String[] RESOURCE_NAMES = {
        // Labels for action.
        "label_go_key",
        "label_send_key",
        "label_next_key",
        "label_done_key",
        "label_previous_key",
        // Other labels.
        "label_pause_key",
        "label_wait_key",
    };

    private static final String[] NAMES = {
        /* @NAMES@ */
@@ -190,9 +81,8 @@ public final class KeyboardTextsSet {
    };

    static {
        int id = 0;
        for (final String name : NAMES) {
            sNameToIdsMap.put(name, id++);
        for (int index = 0; index < NAMES.length; index++) {
            sNameToIndexesMap.put(NAMES[index], index);
        }

        for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) {
+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ import java.util.jar.JarFile;
public class MoreKeysResources {
    private static final String TEXT_RESOURCE_NAME = "donottranslate-more-keys.xml";

    private static final String JAVA_TEMPLATE = "KeyboardTextsSet.tmpl";
    private static final String JAVA_TEMPLATE = "KeyboardTextsTable.tmpl";
    private static final String MARK_NAMES = "@NAMES@";
    private static final String MARK_DEFAULT_TEXTS = "@DEFAULT_TEXTS@";
    private static final String MARK_TEXTS = "@TEXTS@";