Bug 1057631 - Use real locale when pulling search engine out of the jar. r=rnewman
authorMargaret Leibovic <margaret.leibovic@gmail.com>
Fri, 05 Sep 2014 13:52:27 -0700
changeset 204036 f33e9c8cd993f209bc0ec13a252affd877febad5
parent 203894 c0748b8ad50fd069f624831c4c00945cbf9e0e3c
child 204037 46dfee14106aa8e64b5844093875d579d4f75dfa
push id27447
push usercbook@mozilla.com
push dateMon, 08 Sep 2014 13:27:37 +0000
treeherdermozilla-central@f7a27a866c47 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman
bugs1057631
milestone35.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1057631 - Use real locale when pulling search engine out of the jar. r=rnewman
mobile/android/search/java/org/mozilla/search/providers/SearchEngineManager.java
--- a/mobile/android/search/java/org/mozilla/search/providers/SearchEngineManager.java
+++ b/mobile/android/search/java/org/mozilla/search/providers/SearchEngineManager.java
@@ -6,28 +6,30 @@ package org.mozilla.search.providers;
 
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.os.AsyncTask;
 import android.text.TextUtils;
 import android.util.Log;
 
 import org.mozilla.gecko.AppConstants;
+import org.mozilla.gecko.BrowserLocaleManager;
 import org.mozilla.gecko.GeckoSharedPrefs;
 import org.mozilla.gecko.util.GeckoJarReader;
 import org.mozilla.search.R;
 import org.mozilla.search.SearchPreferenceActivity;
 import org.xmlpull.v1.XmlPullParserException;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
 
 public class SearchEngineManager implements SharedPreferences.OnSharedPreferenceChangeListener {
     private static final String LOG_TAG = "SearchEngineManager";
 
     private Context context;
     private SearchEngineCallback changeCallback;
     private SearchEngine engine;
 
@@ -113,18 +115,17 @@ public class SearchEngineManager impleme
     /**
      * Creates a list of SearchEngine instances from all available open search plugins.
      * This method does disk I/O, call it from a background thread.
      *
      * @return List of SearchEngine instances
      */
     public List<SearchEngine> getAllEngines() {
         // First try to read the engine list from the jar.
-        final String url = getSearchPluginsJarUrl("list.txt");
-        InputStream in = GeckoJarReader.getStream(url);
+        InputStream in = getInputStreamFromJar("list.txt");
 
         // Fallback for standalone search activity.
         if (in == null) {
             try {
                 in = context.getResources().getAssets().open("engines/list.txt");
             } catch (IOException e) {
                 throw new IllegalStateException("Error reading list.txt");
             }
@@ -162,17 +163,17 @@ public class SearchEngineManager impleme
     /**
      * Creates a SearchEngine instance from an open search plugin.
      * This method does disk I/O, call it from a background thread.
      *
      * @param identifier search engine identifier (e.g. "google")
      * @return SearchEngine instance for identifier
      */
     private SearchEngine createEngine(String identifier) {
-        InputStream in = getEngineFromJar(identifier);
+        InputStream in = getInputStreamFromJar(identifier + ".xml");
 
         // Fallback for standalone search activity.
         if (in == null) {
             in = getEngineFromAssets(identifier);
         }
 
         if (in == null) {
             throw new IllegalArgumentException("Couldn't find search engine for identifier: " + identifier);
@@ -205,33 +206,49 @@ public class SearchEngineManager impleme
             return context.getResources().getAssets().open("engines/" + identifier + ".xml");
         } catch (IOException e) {
             Log.e(LOG_TAG, "Exception getting search engine from assets", e);
             return null;
         }
     }
 
     /**
-     * Reads open search plugin XML file from the gecko jar. This will only work
+     * Reads a file from the searchplugins directory in the Gecko jar. This will only work
      * if the search activity is built as part of mozilla-central.
      *
-     * @param identifier search engine identifier (e.g. "google")
-     * @return InputStream for open search plugin XML
+     * @param fileName name of the file to read
+     * @return InputStream for file
      */
-    private InputStream getEngineFromJar(String identifier) {
-        final String url = getSearchPluginsJarUrl(identifier + ".xml");
+    private InputStream getInputStreamFromJar(String fileName) {
+        final Locale locale = Locale.getDefault();
+
+        // First, try a file path for the full locale.
+        final String languageTag = BrowserLocaleManager.getLanguageTag(locale);
+        String url = getSearchPluginsJarURL(languageTag, fileName);
+
+        final InputStream in = GeckoJarReader.getStream(url);
+        if (in != null) {
+            return in;
+        }
+
+        // If that doesn't work, try a file path for just the language.
+        final String language = BrowserLocaleManager.getLanguage(locale);
+        if (languageTag.equals(language)) {
+            // We already tried this, so just return null.
+            return null;
+        }
+
+        url = getSearchPluginsJarURL(language, fileName);
         return GeckoJarReader.getStream(url);
     }
 
     /**
      * Gets the jar URL for a file in the searchplugins directory
      *
-     * @param fileName
-     * @return
+     * @param locale String representing the Gecko locale (e.g. "en-US")
+     * @param fileName name of the file to read
+     * @return URL for jar file
      */
-    private String getSearchPluginsJarUrl(String fileName) {
-        // TODO: Get the real value for this
-        final String locale = "en-US";
-
+    private String getSearchPluginsJarURL(String locale, String fileName) {
         final String path = "!/chrome/" + locale + "/locale/" + locale + "/browser/searchplugins/" + fileName;
         return "jar:jar:file://" + context.getPackageResourcePath() + "!/" + AppConstants.OMNIJAR_NAME + path;
     }
 }