Bug 1018240 - Part 2: Invalidate BrowserSearch engine list when locale has changed. r=bnicholson, a=sledru
authorRichard Newman <rnewman@mozilla.com>
Mon, 21 Jul 2014 10:54:58 -0700
changeset 208177 d1e5cb0fbe70
parent 208176 bbfebb4ec504
child 208178 00f079c876f6
push id3754
push userryanvm@gmail.com
push date2014-07-28 15:24 +0000
treeherdermozilla-beta@61b30b605194 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson, sledru
bugs1018240
milestone32.0
Bug 1018240 - Part 2: Invalidate BrowserSearch engine list when locale has changed. r=bnicholson, a=sledru
mobile/android/base/home/BrowserSearch.java
--- a/mobile/android/base/home/BrowserSearch.java
+++ b/mobile/android/base/home/BrowserSearch.java
@@ -51,17 +51,20 @@ import android.view.animation.Accelerate
 import android.view.animation.Animation;
 import android.view.animation.TranslateAnimation;
 import android.widget.AdapterView;
 import android.widget.LinearLayout;
 import android.widget.ListView;
 import android.widget.TextView;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.EnumSet;
+import java.util.List;
+import java.util.Locale;
 
 /**
  * Fragment that displays frecency search results in a ListView.
  */
 public class BrowserSearch extends HomeFragment
                            implements GeckoEventListener {
     // Logging tag name
     private static final String LOGTAG = "GeckoBrowserSearch";
@@ -99,18 +102,24 @@ public class BrowserSearch extends HomeF
     private LinearLayout mView;
 
     // The list showing search results
     private HomeListView mList;
 
     // Client that performs search suggestion queries
     private volatile SuggestClient mSuggestClient;
 
-    // List of search engines from gecko
-    private ArrayList<SearchEngine> mSearchEngines;
+    // List of search engines from Gecko.
+    // Do not mutate this list.
+    // Access to this member must only occur from the UI thread.
+    private List<SearchEngine> mSearchEngines;
+
+    // Track the locale that was last in use when we filled mSearchEngines.
+    // Access to this member must only occur from the UI thread.
+    private Locale mLastLocale;
 
     // Whether search suggestions are enabled or not
     private boolean mSuggestionsEnabled;
 
     // Callbacks used for the search loader
     private CursorLoaderCallbacks mCursorLoaderCallbacks;
 
     // Callbacks used for the search suggestion loader
@@ -222,16 +231,21 @@ public class BrowserSearch extends HomeF
 
         getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
     }
 
     @Override
     public void onResume() {
         super.onResume();
 
+        // Fetch engines if we need to.
+        if (mSearchEngines.isEmpty() || !Locale.getDefault().equals(mLastLocale)) {
+            GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:GetVisible", null));
+        }
+
         Telemetry.startUISession(TelemetryContract.Session.FRECENCY);
     }
 
     @Override
     public void onPause() {
         super.onPause();
 
         Telemetry.stopUISession(TelemetryContract.Session.FRECENCY);
@@ -320,18 +334,16 @@ public class BrowserSearch extends HomeF
                 }
                 return false;
             }
         });
 
         registerForContextMenu(mList);
         EventDispatcher.getInstance().registerGeckoThreadListener(this,
             "SearchEngines:Data");
-
-        GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:GetVisible", null));
     }
 
     @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
 
         // Intialize the search adapter
         mAdapter = new SearchAdapter(getActivity());
@@ -501,21 +513,25 @@ public class BrowserSearch extends HomeF
         if (mSuggestionLoaderCallbacks == null) {
             mSuggestionLoaderCallbacks = new SuggestionLoaderCallbacks();
         }
 
         getLoaderManager().restartLoader(LOADER_ID_SUGGESTION, null, mSuggestionLoaderCallbacks);
     }
 
     private void setSuggestions(ArrayList<String> suggestions) {
+        ThreadUtils.assertOnUiThread();
+
         mSearchEngines.get(0).setSuggestions(suggestions);
         mAdapter.notifyDataSetChanged();
     }
 
     private void setSearchEngines(JSONObject data) {
+        ThreadUtils.assertOnUiThread();
+
         // This method is called via a Runnable posted from the Gecko thread, so
         // it's possible the fragment and/or its view has been destroyed by the
         // time we get here. If so, just abort.
         if (mView == null) {
             return;
         }
 
         try {
@@ -550,17 +566,18 @@ public class BrowserSearch extends HomeF
                         setSuggestClient(new SuggestClient(getActivity(), suggestTemplate,
                                     SUGGESTION_TIMEOUT, SUGGESTION_MAX));
                     }
                 } else {
                     searchEngines.add(engine);
                 }
             }
 
-            mSearchEngines = searchEngines;
+            mSearchEngines = Collections.unmodifiableList(searchEngines);
+            mLastLocale = Locale.getDefault();
 
             if (mAdapter != null) {
                 mAdapter.notifyDataSetChanged();
             }
 
             // Show suggestions opt-in prompt only if suggestions are not enabled yet,
             // user hasn't been prompted and we're not on a private browsing tab.
             if (!mSuggestionsEnabled && !suggestionsPrompted && mSuggestClient != null) {