Add hex string utils
Bug: 9429906 Change-Id: I3267d1d86122ab471c3e63008c73e9f9b758fc8emain
parent
a94eb97413
commit
283cf9cfc9
|
@ -48,6 +48,8 @@ public abstract class PersonalizationDictionaryUpdateSession {
|
|||
|
||||
public abstract void onDictionaryReady();
|
||||
|
||||
public abstract void onDictionaryClosed();
|
||||
|
||||
public void setPredictionDictionary(String locale, DynamicPredictionDictionaryBase dictionary) {
|
||||
mPredictionDictionary = new WeakReference<DynamicPredictionDictionaryBase>(dictionary);
|
||||
mLocale = locale;
|
||||
|
@ -68,6 +70,7 @@ public abstract class PersonalizationDictionaryUpdateSession {
|
|||
|
||||
public void closeSession() {
|
||||
unsetPredictionDictionary();
|
||||
onDictionaryClosed();
|
||||
}
|
||||
|
||||
public void addBigramToPersonalizationDictionary(String word0, String word1, boolean isValid,
|
||||
|
|
|
@ -27,10 +27,10 @@ import com.android.inputmethod.latin.AudioAndHapticFeedbackManager;
|
|||
import com.android.inputmethod.latin.InputAttributes;
|
||||
import com.android.inputmethod.latin.R;
|
||||
import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
|
||||
import com.android.inputmethod.latin.utils.DebugLogUtils;
|
||||
import com.android.inputmethod.latin.utils.LocaleUtils;
|
||||
import com.android.inputmethod.latin.utils.ResourceUtils;
|
||||
import com.android.inputmethod.latin.utils.RunInLocale;
|
||||
import com.android.inputmethod.latin.utils.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
|
@ -90,6 +90,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
private static final String PREF_SUPPRESS_LANGUAGE_SWITCH_KEY =
|
||||
"pref_suppress_language_switch_key";
|
||||
|
||||
private static final String PREF_LAST_USED_PERSONALIZATION_TOKEN =
|
||||
"pref_last_used_personalization_token";
|
||||
public static final String PREF_SEND_FEEDBACK = "send_feedback";
|
||||
public static final String PREF_ABOUT_KEYBOARD = "about_keyboard";
|
||||
|
||||
|
@ -343,4 +345,14 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang
|
|||
return prefs.getBoolean(
|
||||
DebugSettings.PREF_USE_ONLY_PERSONALIZATION_DICTIONARY_FOR_DEBUG, false);
|
||||
}
|
||||
|
||||
public void writeLastUsedPersonalizationToken(byte[] token) {
|
||||
final String tokenStr = StringUtils.byteArrayToHexString(token);
|
||||
mPrefs.edit().putString(PREF_LAST_USED_PERSONALIZATION_TOKEN, tokenStr).apply();
|
||||
}
|
||||
|
||||
public byte[] readLastUsedPersonalizationToken() {
|
||||
final String tokenStr = mPrefs.getString(PREF_LAST_USED_PERSONALIZATION_TOKEN, null);
|
||||
return StringUtils.hexStringToByteArray(tokenStr);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -357,4 +357,30 @@ public final class StringUtils {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@UsedForTesting
|
||||
public static String byteArrayToHexString(byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) {
|
||||
return "";
|
||||
}
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b & 0xff));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@UsedForTesting
|
||||
public static byte[] hexStringToByteArray(String hexString) {
|
||||
if (TextUtils.isEmpty(hexString)) {
|
||||
return null;
|
||||
}
|
||||
final int N = hexString.length();
|
||||
final byte[] bytes = new byte[N / 2];
|
||||
for (int i = 0; i < N; i += 2) {
|
||||
bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
|
||||
+ Character.digit(hexString.charAt(i + 1), 16));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -256,4 +256,16 @@ public class StringUtilsTests extends AndroidTestCase {
|
|||
// code for now True is acceptable.
|
||||
assertTrue(StringUtils.lastPartLooksLikeURL(".abc/def"));
|
||||
}
|
||||
|
||||
public void testHexStringUtils() {
|
||||
final byte[] bytes = new byte[] { (byte)0x01, (byte)0x11, (byte)0x22, (byte)0x33,
|
||||
(byte)0x55, (byte)0x88, (byte)0xEE };
|
||||
final String bytesStr = StringUtils.byteArrayToHexString(bytes);
|
||||
final byte[] bytes2 = StringUtils.hexStringToByteArray(bytesStr);
|
||||
for (int i = 0; i < bytes.length; ++i) {
|
||||
assertTrue(bytes[i] == bytes2[i]);
|
||||
}
|
||||
final String bytesStr2 = StringUtils.byteArrayToHexString(bytes2);
|
||||
assertTrue(bytesStr.equals(bytesStr2));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue