Merge "Move some methods from Utils to LocaleUtils class" into jb-mr1-dev

main
Tadashi G. Takaoka 2012-08-29 00:35:26 -07:00 committed by Android (Google) Code Review
commit 95c486cdf3
3 changed files with 40 additions and 39 deletions

View File

@ -32,6 +32,9 @@ import java.util.Locale;
* dictionary pack.
*/
public class LocaleUtils {
private static final HashMap<String, Long> EMPTY_LT_HASH_MAP = CollectionUtils.newHashMap();
private static final String LOCALE_AND_TIME_STR_SEPARATER = ",";
private LocaleUtils() {
// Intentional empty constructor for utility class.
}
@ -219,4 +222,38 @@ public class LocaleUtils {
return retval;
}
}
public static HashMap<String, Long> localeAndTimeStrToHashMap(String str) {
if (TextUtils.isEmpty(str)) {
return EMPTY_LT_HASH_MAP;
}
final String[] ss = str.split(LOCALE_AND_TIME_STR_SEPARATER);
final int N = ss.length;
if (N < 2 || N % 2 != 0) {
return EMPTY_LT_HASH_MAP;
}
final HashMap<String, Long> retval = CollectionUtils.newHashMap();
for (int i = 0; i < N / 2; ++i) {
final String localeStr = ss[i * 2];
final long time = Long.valueOf(ss[i * 2 + 1]);
retval.put(localeStr, time);
}
return retval;
}
public static String localeAndTimeHashMapToStr(HashMap<String, Long> map) {
if (map == null || map.isEmpty()) {
return "";
}
final StringBuilder builder = new StringBuilder();
for (String localeStr : map.keySet()) {
if (builder.length() > 0) {
builder.append(LOCALE_AND_TIME_STR_SEPARATER);
}
final Long time = map.get(localeStr);
builder.append(localeStr).append(LOCALE_AND_TIME_STR_SEPARATER);
builder.append(String.valueOf(time));
}
return builder.toString();
}
}

View File

@ -401,7 +401,7 @@ public class SettingsValues {
public static long getLastUserHistoryWriteTime(
final SharedPreferences prefs, final String locale) {
final String str = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, "");
final HashMap<String, Long> map = Utils.localeAndTimeStrToHashMap(str);
final HashMap<String, Long> map = LocaleUtils.localeAndTimeStrToHashMap(str);
if (map.containsKey(locale)) {
return map.get(locale);
}
@ -411,9 +411,9 @@ public class SettingsValues {
public static void setLastUserHistoryWriteTime(
final SharedPreferences prefs, final String locale) {
final String oldStr = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, "");
final HashMap<String, Long> map = Utils.localeAndTimeStrToHashMap(oldStr);
final HashMap<String, Long> map = LocaleUtils.localeAndTimeStrToHashMap(oldStr);
map.put(locale, System.currentTimeMillis());
final String newStr = Utils.localeAndTimeHashMapToStr(map);
final String newStr = LocaleUtils.localeAndTimeHashMapToStr(map);
prefs.edit().putString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, newStr).apply();
}

View File

@ -446,40 +446,4 @@ public class Utils {
}
return sDeviceOverrideValueMap.get(key);
}
private static final HashMap<String, Long> EMPTY_LT_HASH_MAP = CollectionUtils.newHashMap();
private static final String LOCALE_AND_TIME_STR_SEPARATER = ",";
public static HashMap<String, Long> localeAndTimeStrToHashMap(String str) {
if (TextUtils.isEmpty(str)) {
return EMPTY_LT_HASH_MAP;
}
final String[] ss = str.split(LOCALE_AND_TIME_STR_SEPARATER);
final int N = ss.length;
if (N < 2 || N % 2 != 0) {
return EMPTY_LT_HASH_MAP;
}
final HashMap<String, Long> retval = CollectionUtils.newHashMap();
for (int i = 0; i < N / 2; ++i) {
final String localeStr = ss[i * 2];
final long time = Long.valueOf(ss[i * 2 + 1]);
retval.put(localeStr, time);
}
return retval;
}
public static String localeAndTimeHashMapToStr(HashMap<String, Long> map) {
if (map == null || map.isEmpty()) {
return "";
}
final StringBuilder builder = new StringBuilder();
for (String localeStr : map.keySet()) {
if (builder.length() > 0) {
builder.append(LOCALE_AND_TIME_STR_SEPARATER);
}
final Long time = map.get(localeStr);
builder.append(localeStr).append(LOCALE_AND_TIME_STR_SEPARATER);
builder.append(String.valueOf(time));
}
return builder.toString();
}
}