From c899038eee5c01d520a2707cca01ee093a674d05 Mon Sep 17 00:00:00 2001
From: Jean Chalard <jchalard@google.com>
Date: Wed, 20 Apr 2011 16:52:07 +0900
Subject: [PATCH] Move language-specific keyboard setting to resources.

Some flag settings used to be stored in a keyboard layout extravalue setting. This
change:
- Introduces the capability of setting such specific flags in values/config.xml
- Retains the ability to use extravalues (for layout-specific settings), though
  there is no more any such setting at the moment.
- Fixes a bug where loading a dictionary from outside does not initialize the flags.
- Moves Flag to another class.

Note: this needs I705ec68c to avoid breaking the build

Change-Id: Ia7703aae3215b06c0b3cb792821649806e8998c1
---
 java/res/values-de/config.xml                 | 23 +++++++
 java/res/values/config.xml                    |  1 +
 java/res/xml/method.xml                       |  1 -
 .../inputmethod/latin/BinaryDictionary.java   | 34 +++-------
 .../com/android/inputmethod/latin/Flag.java   | 64 +++++++++++++++++++
 .../android/inputmethod/latin/Suggest.java    |  8 +--
 .../inputmethod/latin/SuggestHelper.java      |  2 +-
 7 files changed, 103 insertions(+), 30 deletions(-)
 create mode 100644 java/res/values-de/config.xml
 create mode 100644 java/src/com/android/inputmethod/latin/Flag.java

diff --git a/java/res/values-de/config.xml b/java/res/values-de/config.xml
new file mode 100644
index 0000000000..272ff326fc
--- /dev/null
+++ b/java/res/values-de/config.xml
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2011, 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.
+*/
+-->
+
+<resources>
+    <bool name="config_require_umlaut_processing">true</bool>
+</resources>
diff --git a/java/res/values/config.xml b/java/res/values/config.xml
index 64e2c67533..300aaa4230 100644
--- a/java/res/values/config.xml
+++ b/java/res/values/config.xml
@@ -78,4 +78,5 @@
     </string-array>
     <!--  Screen metrics for logging. 0 = "mdpi", 1 = "hdpi", 2 = "xlarge" -->
     <integer name="log_screen_metrics">0</integer>
+    <bool name="config_require_umlaut_processing">false</bool>
 </resources>
diff --git a/java/res/xml/method.xml b/java/res/xml/method.xml
index 1b122be5fe..513076733e 100644
--- a/java/res/xml/method.xml
+++ b/java/res/xml/method.xml
@@ -72,7 +72,6 @@
             android:label="@string/subtype_mode_de_keyboard"
             android:imeSubtypeLocale="de"
             android:imeSubtypeMode="keyboard"
-            android:imeSubtypeExtraValue="requiresGermanUmlautProcessing"
     />
 <!--     <subtype android:icon="@drawable/ic_subtype_mic" -->
 <!--             android:label="@string/subtype_mode_de_voice" -->
diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
index fa90fce673..58e9099a91 100644
--- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java
+++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java
@@ -63,18 +63,12 @@ public class BinaryDictionary extends Dictionary {
 
     private final KeyboardSwitcher mKeyboardSwitcher = KeyboardSwitcher.getInstance();
 
-    public static class Flag {
-        public final String mName;
-        public final int mValue;
-
-        public Flag(String name, int value) {
-            mName = name;
-            mValue = value;
-        }
-    }
-
     public static final Flag FLAG_REQUIRES_GERMAN_UMLAUT_PROCESSING =
-            new Flag("requiresGermanUmlautProcessing", 0x1);
+            new Flag(R.bool.config_require_umlaut_processing, 0x1);
+
+    // Can create a new flag from extravalue :
+    // public static final Flag FLAG_MYFLAG =
+    //         new Flag("my_flag", 0x02);
 
     private static final Flag[] ALL_FLAGS = {
         // Here should reside all flags that trigger some special processing
@@ -118,12 +112,12 @@ public class BinaryDictionary extends Dictionary {
                 return null;
             }
         }
-        sInstance.mFlags = initFlags(ALL_FLAGS, SubtypeSwitcher.getInstance());
+        sInstance.mFlags = Flag.initFlags(ALL_FLAGS, context, SubtypeSwitcher.getInstance());
         return sInstance;
     }
 
-    /* package for test */ static BinaryDictionary initDictionary(File dictionary, long startOffset,
-            long length, int dicTypeId, Flag[] flagArray) {
+    /* package for test */ static BinaryDictionary initDictionary(Context context, File dictionary,
+            long startOffset, long length, int dicTypeId, Flag[] flagArray) {
         synchronized (sInstance) {
             sInstance.closeInternal();
             if (dictionary.isFile()) {
@@ -134,19 +128,10 @@ public class BinaryDictionary extends Dictionary {
                 return null;
             }
         }
-        sInstance.mFlags = initFlags(flagArray, null);
+        sInstance.mFlags = Flag.initFlags(flagArray, context, null);
         return sInstance;
     }
 
-    private static int initFlags(Flag[] flagArray, SubtypeSwitcher switcher) {
-        int flags = 0;
-        for (Flag entry : flagArray) {
-            if (switcher == null || switcher.currentSubtypeContainsExtraValueKey(entry.mName))
-                flags |= entry.mValue;
-        }
-        return flags;
-    }
-
     static {
         Utils.loadNativeLibrary();
     }
@@ -179,6 +164,7 @@ public class BinaryDictionary extends Dictionary {
                 sInstance.mDicTypeId = dicTypeId;
             }
         }
+        sInstance.mFlags = Flag.initFlags(ALL_FLAGS, context, SubtypeSwitcher.getInstance());
         return sInstance;
     }
 
diff --git a/java/src/com/android/inputmethod/latin/Flag.java b/java/src/com/android/inputmethod/latin/Flag.java
new file mode 100644
index 0000000000..3cb8f7e177
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/Flag.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2011 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;
+
+import android.content.Context;
+import android.content.res.Resources;
+
+public class Flag {
+    public final String mName;
+    public final int mResource;
+    public final int mMask;
+    public final int mSource;
+
+    static private final int SOURCE_CONFIG = 1;
+    static private final int SOURCE_EXTRAVALUE = 2;
+
+    public Flag(int resourceId, int mask) {
+        mName = null;
+        mResource = resourceId;
+        mSource = SOURCE_CONFIG;
+        mMask = mask;
+    }
+
+    public Flag(String name, int mask) {
+        mName = name;
+        mResource = 0;
+        mSource = SOURCE_EXTRAVALUE;
+        mMask = mask;
+    }
+
+    // If context/switcher are null, set all related flags in flagArray to on.
+    public static int initFlags(Flag[] flagArray, Context context, SubtypeSwitcher switcher) {
+        int flags = 0;
+        final Resources res = null == context ? null : context.getResources();
+        for (Flag entry : flagArray) {
+            switch (entry.mSource) {
+                case Flag.SOURCE_CONFIG:
+                    if (res == null || res.getBoolean(entry.mResource))
+                        flags |= entry.mMask;
+                    break;
+                case Flag.SOURCE_EXTRAVALUE:
+                    if (switcher == null ||
+                            switcher.currentSubtypeContainsExtraValueKey(entry.mName))
+                        flags |= entry.mMask;
+                    break;
+            }
+        }
+        return flags;
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java
index 0cc9d4198f..cd7f71c2ae 100644
--- a/java/src/com/android/inputmethod/latin/Suggest.java
+++ b/java/src/com/android/inputmethod/latin/Suggest.java
@@ -112,10 +112,10 @@ public class Suggest implements Dictionary.WordCallback {
                 dictionaryResId));
     }
 
-    /* package for test */ Suggest(File dictionary, long startOffset, long length,
-            BinaryDictionary.Flag[] flagArray) {
-        init(null, BinaryDictionary.initDictionary(dictionary, startOffset, length, DIC_MAIN,
-                flagArray));
+    /* package for test */ Suggest(Context context, File dictionary, long startOffset, long length,
+            Flag[] flagArray) {
+        init(null, BinaryDictionary.initDictionary(context, dictionary, startOffset, length,
+                DIC_MAIN, flagArray));
     }
 
     private void init(Context context, BinaryDictionary mainDict) {
diff --git a/tests/src/com/android/inputmethod/latin/SuggestHelper.java b/tests/src/com/android/inputmethod/latin/SuggestHelper.java
index 4a357a6f22..8a11ff9554 100644
--- a/tests/src/com/android/inputmethod/latin/SuggestHelper.java
+++ b/tests/src/com/android/inputmethod/latin/SuggestHelper.java
@@ -43,7 +43,7 @@ public class SuggestHelper {
 
     protected SuggestHelper(Context context, File dictionaryPath, long startOffset, long length,
             KeyboardId keyboardId) {
-        mSuggest = new Suggest(dictionaryPath, startOffset, length, null);
+        mSuggest = new Suggest(context, dictionaryPath, startOffset, length, null);
         mKeyboard = new LatinKeyboard(context, keyboardId);
         mKeyDetector = new KeyDetector();
         init();
-- 
GitLab