Skip to content
Snippets Groups Projects
Commit 95c486cd authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

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

parents 237f5e4f 0023a57e
No related branches found
No related tags found
No related merge requests found
...@@ -32,6 +32,9 @@ import java.util.Locale; ...@@ -32,6 +32,9 @@ import java.util.Locale;
* dictionary pack. * dictionary pack.
*/ */
public class LocaleUtils { 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() { private LocaleUtils() {
// Intentional empty constructor for utility class. // Intentional empty constructor for utility class.
} }
...@@ -219,4 +222,38 @@ public class LocaleUtils { ...@@ -219,4 +222,38 @@ public class LocaleUtils {
return retval; 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();
}
} }
...@@ -401,7 +401,7 @@ public class SettingsValues { ...@@ -401,7 +401,7 @@ public class SettingsValues {
public static long getLastUserHistoryWriteTime( public static long getLastUserHistoryWriteTime(
final SharedPreferences prefs, final String locale) { final SharedPreferences prefs, final String locale) {
final String str = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, ""); 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)) { if (map.containsKey(locale)) {
return map.get(locale); return map.get(locale);
} }
...@@ -411,9 +411,9 @@ public class SettingsValues { ...@@ -411,9 +411,9 @@ public class SettingsValues {
public static void setLastUserHistoryWriteTime( public static void setLastUserHistoryWriteTime(
final SharedPreferences prefs, final String locale) { final SharedPreferences prefs, final String locale) {
final String oldStr = prefs.getString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, ""); 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()); 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(); prefs.edit().putString(Settings.PREF_LAST_USER_DICTIONARY_WRITE_TIME, newStr).apply();
} }
......
...@@ -446,40 +446,4 @@ public class Utils { ...@@ -446,40 +446,4 @@ public class Utils {
} }
return sDeviceOverrideValueMap.get(key); 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();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment