Bug 1018240 - Part 2: invalidate BrowserSearch engine list when locale has changed. r=bnicholson
authorRichard Newman <rnewman@mozilla.com>
Mon, 21 Jul 2014 10:54:58 -0700
changeset 195373 476a0455e0e816fe60cc1a6d0dfb893400873041
parent 195372 0234ad055a7b978dd0a9a43561ee9f1a701d1061
child 195374 70afb8fb9ae5766c32d223a4803f55c3f49ca123
push id46575
push userkwierso@gmail.com
push dateTue, 22 Jul 2014 00:35:21 +0000
treeherdermozilla-inbound@fee5c4bdd713 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbnicholson
bugs1018240
milestone33.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 1018240 - Part 2: invalidate BrowserSearch engine list when locale has changed. r=bnicholson
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) {