2012-04-05 05:59:55 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2012-04-05 05:59:55 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-04-05 05:59:55 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2012-04-05 05:59:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard.internal;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
2014-01-31 06:08:49 +00:00
|
|
|
import android.text.TextUtils;
|
2012-04-05 05:59:55 +00:00
|
|
|
|
2012-10-03 08:36:45 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2014-01-31 06:58:14 +00:00
|
|
|
import com.android.inputmethod.latin.Constants;
|
2014-03-20 08:02:12 +00:00
|
|
|
import com.android.inputmethod.latin.utils.RunInLocale;
|
|
|
|
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
|
2012-04-05 05:59:55 +00:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
2014-02-06 06:43:00 +00:00
|
|
|
import java.util.Locale;
|
2012-04-05 05:59:55 +00:00
|
|
|
|
2012-04-19 23:39:11 +00:00
|
|
|
public final class KeyboardTextsSet {
|
2014-01-31 07:27:49 +00:00
|
|
|
public static final String PREFIX_TEXT = "!text/";
|
2014-03-27 02:08:11 +00:00
|
|
|
public static final String SWITCH_TO_ALPHA_KEY_LABEL = "keylabel_to_alpha";
|
2014-02-24 08:24:49 +00:00
|
|
|
|
2014-01-31 06:58:14 +00:00
|
|
|
private static final char BACKSLASH = Constants.CODE_BACKSLASH;
|
2014-01-31 06:08:49 +00:00
|
|
|
private static final int MAX_STRING_REFERENCE_INDIRECTION = 10;
|
|
|
|
|
2014-02-06 07:25:28 +00:00
|
|
|
private String[] mTextsTable;
|
2012-04-19 23:39:11 +00:00
|
|
|
// Resource name to text map.
|
2014-05-23 11:18:17 +00:00
|
|
|
private HashMap<String, String> mResourceNameToTextsMap = new HashMap<>();
|
2012-04-05 05:59:55 +00:00
|
|
|
|
2014-03-20 08:02:12 +00:00
|
|
|
public void setLocale(final Locale locale, final Context context) {
|
2014-04-08 08:01:13 +00:00
|
|
|
mTextsTable = KeyboardTextsTable.getTextsTable(locale);
|
2014-03-20 07:23:45 +00:00
|
|
|
final Resources res = context.getResources();
|
2013-03-22 04:52:52 +00:00
|
|
|
final int referenceId = context.getApplicationInfo().labelRes;
|
2014-03-20 07:23:45 +00:00
|
|
|
final String resourcePackageName = res.getResourcePackageName(referenceId);
|
2014-03-20 08:02:12 +00:00
|
|
|
final RunInLocale<Void> job = new RunInLocale<Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void job(final Resources resource) {
|
|
|
|
loadStringResourcesInternal(res, RESOURCE_NAMES, resourcePackageName);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Null means the current system locale.
|
|
|
|
job.runInLocale(res,
|
|
|
|
SubtypeLocaleUtils.NO_LANGUAGE.equals(locale.toString()) ? null : locale);
|
2012-04-05 05:59:55 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 08:36:45 +00:00
|
|
|
@UsedForTesting
|
2014-03-20 07:23:45 +00:00
|
|
|
void loadStringResourcesInternal(final Resources res, final String[] resourceNames,
|
|
|
|
final String resourcePackageName) {
|
2012-04-05 05:59:55 +00:00
|
|
|
for (final String resName : resourceNames) {
|
2014-03-20 07:23:45 +00:00
|
|
|
final int resId = res.getIdentifier(resName, "string", resourcePackageName);
|
2012-04-19 23:39:11 +00:00
|
|
|
mResourceNameToTextsMap.put(resName, res.getString(resId));
|
2012-04-05 05:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 23:39:11 +00:00
|
|
|
public String getText(final String name) {
|
2014-02-06 07:25:28 +00:00
|
|
|
final String text = mResourceNameToTextsMap.get(name);
|
|
|
|
return (text != null) ? text : KeyboardTextsTable.getText(name, mTextsTable);
|
2012-04-05 05:59:55 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 06:08:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-20 07:23:45 +00:00
|
|
|
// TODO: Resolve text reference when creating {@link KeyboardTextsTable} class.
|
2014-01-31 06:08:49 +00:00
|
|
|
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) {
|
2014-02-14 09:44:34 +00:00
|
|
|
throw new RuntimeException("Too many " + PREFIX_TEXT + "name indirection: " + text);
|
2014-01-31 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final int prefixLen = PREFIX_TEXT.length();
|
|
|
|
final int size = text.length();
|
|
|
|
if (size < prefixLen) {
|
2014-02-14 09:44:34 +00:00
|
|
|
break;
|
2014-01-31 06:08:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sb != null) {
|
|
|
|
text = sb.toString();
|
|
|
|
}
|
|
|
|
} while (sb != null);
|
|
|
|
return TextUtils.isEmpty(text) ? null : text;
|
|
|
|
}
|
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
// These texts' name should be aligned with the @string/<name> in
|
|
|
|
// values*/strings-action-keys.xml.
|
2014-03-20 08:02:12 +00:00
|
|
|
static final String[] RESOURCE_NAMES = {
|
2012-04-05 05:59:55 +00:00
|
|
|
// Labels for action.
|
|
|
|
"label_go_key",
|
|
|
|
"label_send_key",
|
|
|
|
"label_next_key",
|
|
|
|
"label_done_key",
|
2014-05-24 12:18:47 +00:00
|
|
|
"label_search_key",
|
2012-04-05 05:59:55 +00:00
|
|
|
"label_previous_key",
|
|
|
|
// Other labels.
|
|
|
|
"label_pause_key",
|
|
|
|
"label_wait_key",
|
|
|
|
};
|
|
|
|
}
|