From 6e29d261cb480ef0f0d90867d4582fabce0b9113 Mon Sep 17 00:00:00 2001
From: Mohammadinamul Sheik <inamul@google.com>
Date: Tue, 3 Mar 2015 16:17:27 -0800
Subject: [PATCH] Introduce DictionaryHeaderUtils to get dictionary header
 values

Change-Id: I86cef6c7f2f68a75428bb2b9c4d5a1dc1876b541
---
 .../latin/utils/DictionaryHeaderUtils.java    | 31 +++++++++++++++++++
 .../latin/utils/DictionaryInfoUtils.java      | 27 +++++++---------
 2 files changed, 42 insertions(+), 16 deletions(-)
 create mode 100644 java-overridable/src/com/android/inputmethod/latin/utils/DictionaryHeaderUtils.java

diff --git a/java-overridable/src/com/android/inputmethod/latin/utils/DictionaryHeaderUtils.java b/java-overridable/src/com/android/inputmethod/latin/utils/DictionaryHeaderUtils.java
new file mode 100644
index 0000000000..5eb613c9b3
--- /dev/null
+++ b/java-overridable/src/com/android/inputmethod/latin/utils/DictionaryHeaderUtils.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2015 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.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * 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
+ */
+
+package com.android.inputmethod.latin.utils;
+
+import com.android.inputmethod.latin.AssetFileAddress;
+import com.android.inputmethod.latin.makedict.DictionaryHeader;
+
+import java.io.File;
+
+public class DictionaryHeaderUtils {
+
+    public static int getContentVersion(AssetFileAddress fileAddress) {
+        final DictionaryHeader header = DictionaryInfoUtils.getDictionaryFileHeaderOrNull(
+                new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength);
+        return Integer.parseInt(header.mVersionString);
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java b/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java
index 3ed6a021e9..fd567f49d3 100644
--- a/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/DictionaryInfoUtils.java
@@ -340,7 +340,7 @@ public class DictionaryInfoUtils {
         return getDictionaryFileHeaderOrNull(file, 0, file.length());
     }
 
-    private static DictionaryHeader getDictionaryFileHeaderOrNull(final File file,
+    public static DictionaryHeader getDictionaryFileHeaderOrNull(final File file,
             final long offset, final long length) {
         try {
             final DictionaryHeader header =
@@ -357,23 +357,17 @@ public class DictionaryInfoUtils {
      * Returns information of the dictionary.
      *
      * @param fileAddress the asset dictionary file address.
+     * @param locale Locale for this file.
      * @return information of the specified dictionary.
      */
     @Nullable
     private static DictionaryInfo createDictionaryInfoFromFileAddress(
-            final AssetFileAddress fileAddress) {
-        // TODO: Read the header and update the version number for the new dictionaries.
-        // This will make sure that the dictionary version is updated in the database.
-        final DictionaryHeader header = getDictionaryFileHeaderOrNull(
-                new File(fileAddress.mFilename), fileAddress.mOffset, fileAddress.mLength);
-        if (header == null) {
-            return null;
-        }
-        final String id = header.mIdString;
-        final Locale locale = LocaleUtils.constructLocaleFromString(header.mLocaleString);
-        final String description = header.getDescription();
-        final String version = header.mVersionString;
-        return new DictionaryInfo(id, locale, description, fileAddress, Integer.parseInt(version));
+            final AssetFileAddress fileAddress, Locale locale) {
+        final String id = getMainDictId(locale);
+        final int version = DictionaryHeaderUtils.getContentVersion(fileAddress);
+        final String description = SubtypeLocaleUtils
+                .getSubtypeLocaleDisplayName(locale.toString());
+        return new DictionaryInfo(id, locale, description, fileAddress, version);
     }
 
     private static void addOrUpdateDictInfo(final ArrayList<DictionaryInfo> dictList,
@@ -410,7 +404,7 @@ public class DictionaryInfoUtils {
                     final Locale locale = LocaleUtils.constructLocaleFromString(localeString);
                     final AssetFileAddress fileAddress = AssetFileAddress.makeFromFile(dict);
                     final DictionaryInfo dictionaryInfo =
-                            createDictionaryInfoFromFileAddress(fileAddress);
+                            createDictionaryInfoFromFileAddress(fileAddress, locale);
                     // Protect against cases of a less-specific dictionary being found, like an
                     // en dictionary being used for an en_US locale. In this case, the en dictionary
                     // should be used for en_US but discounted for listing purposes.
@@ -435,7 +429,8 @@ public class DictionaryInfoUtils {
             }
             final AssetFileAddress fileAddress =
                     BinaryDictionaryGetter.loadFallbackResource(context, resourceId);
-            final DictionaryInfo dictionaryInfo = createDictionaryInfoFromFileAddress(fileAddress);
+            final DictionaryInfo dictionaryInfo = createDictionaryInfoFromFileAddress(fileAddress,
+                    locale);
             // Protect against cases of a less-specific dictionary being found, like an
             // en dictionary being used for an en_US locale. In this case, the en dictionary
             // should be used for en_US but discounted for listing purposes.
-- 
GitLab