Bug 769145 - Move search engine logic to AllPagesTab. r=mfinkle
authorBrian Nicholson <bnicholson@mozilla.com>
Fri, 06 Jul 2012 10:49:03 -0700
changeset 98547 e17e2363fb1bf3cedbd68efa2097c3f348289ba5
parent 98546 db774ca70b42c77b7af283be89831974b3ad8397
child 98548 adcf949f975e9ed67bf68cb86bc06ad985c3087d
push id11531
push userbnicholson@mozilla.com
push dateFri, 06 Jul 2012 17:50:25 +0000
treeherdermozilla-inbound@adcf949f975e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs769145
milestone16.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 769145 - Move search engine logic to AllPagesTab. r=mfinkle
mobile/android/base/AwesomeBar.java
mobile/android/base/AwesomeBarTabs.java
mobile/android/base/awesomebar/AllPagesTab.java
--- a/mobile/android/base/AwesomeBar.java
+++ b/mobile/android/base/AwesomeBar.java
@@ -48,37 +48,32 @@ import java.util.Map;
 
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 import org.mozilla.gecko.db.BrowserContract.Combined;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
 import org.mozilla.gecko.db.BrowserDB;
 
 import org.json.JSONObject;
 
-public class AwesomeBar extends GeckoActivity implements GeckoEventListener {
+public class AwesomeBar extends GeckoActivity {
     private static final String LOGTAG = "GeckoAwesomeBar";
 
-    private static final int SUGGESTION_TIMEOUT = 2000;
-    private static final int SUGGESTION_MAX = 3;
-
     static final String URL_KEY = "url";
     static final String CURRENT_URL_KEY = "currenturl";
     static final String TARGET_KEY = "target";
     static final String SEARCH_KEY = "search";
     static final String USER_ENTERED_KEY = "user_entered";
     static enum Target { NEW_TAB, CURRENT_TAB };
 
     private String mTarget;
     private AwesomeBarTabs mAwesomeTabs;
     private AwesomeBarEditText mText;
     private ImageButton mGoButton;
     private ContentResolver mResolver;
     private ContextMenuSubject mContextMenuSubject;
-    private SuggestClient mSuggestClient;
-    private AsyncTask<String, Void, ArrayList<String>> mSuggestTask;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
         Log.d(LOGTAG, "creating awesomebar");
 
         mResolver = Tabs.getInstance().getContentResolver();
@@ -170,34 +165,16 @@ public class AwesomeBar extends GeckoAct
                 String text = s.toString();
                 mAwesomeTabs.filter(text);
 
                 // If the AwesomeBar has a composition string, don't call updateGoButton().
                 // That method resets IME and composition state will be broken.
                 if (!hasCompositionString(s)) {
                     updateGoButton(text);
                 }
-
-                // cancel previous query
-                if (mSuggestTask != null) {
-                    mSuggestTask.cancel(true);
-                }
-
-                if (mSuggestClient != null) {
-                    mSuggestTask = new AsyncTask<String, Void, ArrayList<String>>() {
-                         protected ArrayList<String> doInBackground(String... query) {
-                             return mSuggestClient.query(query[0]);
-                         }
-
-                         protected void onPostExecute(ArrayList<String> suggestions) {
-                             mAwesomeTabs.setSuggestions(suggestions);
-                         }
-                    };
-                    mSuggestTask.execute(text);
-                }
             }
 
             public void beforeTextChanged(CharSequence s, int start, int count,
                                           int after) {
                 // do nothing
             }
 
             public void onTextChanged(CharSequence s, int start, int before,
@@ -223,34 +200,16 @@ public class AwesomeBar extends GeckoAct
         mText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
             public void onFocusChange(View v, boolean hasFocus) {
                 if (!hasFocus) {
                     InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                     imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                 }
             }
         });
-
-        GeckoAppShell.registerGeckoEventListener("SearchEngines:Data", this);
-        GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:Get", null));
-    }
-
-    public void handleMessage(String event, JSONObject message) {
-        try {
-            if (event.equals("SearchEngines:Data")) {
-                final String suggestEngine =  message.isNull("suggestEngine") ? null : message.getString("suggestEngine");
-                final String suggestTemplate = message.isNull("suggestTemplate") ? null : message.getString("suggestTemplate");
-                if (suggestTemplate != null)
-                    mSuggestClient = new SuggestClient(GeckoApp.mAppContext, suggestTemplate, SUGGESTION_TIMEOUT, SUGGESTION_MAX);
-                mAwesomeTabs.setSearchEngines(suggestEngine, message.getJSONArray("searchEngines"));
-            }
-        } catch (Exception e) {
-            // do nothing
-            Log.i(LOGTAG, "handleMessage throws " + e + " for message: " + event);
-        }
     }
 
     @Override
     public void onConfigurationChanged(Configuration newConfiguration) {
         super.onConfigurationChanged(newConfiguration);
     }
 
     @Override
@@ -419,17 +378,16 @@ public class AwesomeBar extends GeckoAct
         // not desktop bookmarks exist
         BrowserDB.invalidateCachedState();
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
         mAwesomeTabs.destroy();
-        GeckoAppShell.unregisterGeckoEventListener("SearchEngines:Data", this);
     }
 
     @Override
     public void onBackPressed() {
         // Let mAwesomeTabs try to handle the back press, since we may be in a
         // bookmarks sub-folder.
         if (mAwesomeTabs.onBackPressed())
             return;
--- a/mobile/android/base/AwesomeBarTabs.java
+++ b/mobile/android/base/AwesomeBarTabs.java
@@ -235,35 +235,12 @@ public class AwesomeBarTabs extends TabH
         // The tabs should only be visible if there's no on-going search
         int tabsVisibility = (searchTerm.length() == 0 ? View.VISIBLE : View.GONE);
         findViewById(R.id.tab_widget_container).setVisibility(tabsVisibility);
 
         // Perform the actual search
         allPages.filter(searchTerm);
     }
 
-    /**
-     * Sets suggestions associated with the current suggest engine.
-     * If there is no suggest engine, this does nothing.
-     */
-    public void setSuggestions(final ArrayList<String> suggestions) {
-        GeckoAppShell.getMainHandler().post(new Runnable() {
-            public void run() {
-                getAllPagesTab().setSuggestions(suggestions);
-            }
-        });
-    }
-
-    /**
-     * Sets search engines to be shown for user-entered queries.
-     */
-    public void setSearchEngines(final String suggestEngineName, final JSONArray engines) {
-        GeckoAppShell.getMainHandler().post(new Runnable() {
-            public void run() {
-                getAllPagesTab().setSearchEngines(suggestEngineName, engines);
-            }
-        });
-    }
-
     public boolean isInReadingList() {
         return getBookmarksTab().isInReadingList();
     }
 }
--- a/mobile/android/base/awesomebar/AllPagesTab.java
+++ b/mobile/android/base/awesomebar/AllPagesTab.java
@@ -27,16 +27,17 @@ import android.widget.SimpleCursorAdapte
 import android.widget.LinearLayout;
 import android.widget.TabHost.TabContentFactory;
 import android.view.ViewGroup;
 import android.graphics.drawable.Drawable;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.content.Intent;
 import android.widget.FilterQueryProvider;
+import android.os.AsyncTask;
 import android.os.SystemClock;
 import android.view.MenuInflater;
 import android.widget.TabHost;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -46,35 +47,44 @@ import org.json.JSONArray;
 import org.json.JSONObject;
 import org.json.JSONException;
 
 import org.mozilla.gecko.AwesomeBar.ContextMenuSubject;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
 import org.mozilla.gecko.db.BrowserContract.Combined;
 
-public class AllPagesTab extends AwesomeBarTab {
+public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
     public static final String LOGTAG = "ALL_PAGES";
     private static final String TAG = "allPages";
+
+    private static final int SUGGESTION_TIMEOUT = 3000;
+    private static final int SUGGESTION_MAX = 3;
+
     private String mSearchTerm;
     private SearchEngine mSuggestEngine;
     private ArrayList<SearchEngine> mSearchEngines;
+    private SuggestClient mSuggestClient;
+    private AsyncTask<String, Void, ArrayList<String>> mSuggestTask;
     private ListView mView = null;
     private AwesomeBarCursorAdapter mCursorAdapter = null;
 
     private class SearchEntryViewHolder {
         public FlowLayout suggestionView;
         public ImageView iconView;
         public LinearLayout userEnteredView;
         public TextView userEnteredTextView;
     }
 
     public AllPagesTab(Context context) {
         super(context);
         mSearchEngines = new ArrayList<SearchEngine>();
+
+        GeckoAppShell.registerGeckoEventListener("SearchEngines:Data", this);
+        GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("SearchEngines:Get", null));
     }
 
     public boolean onBackPressed() {
         return false;
     }
 
     public TabContentFactory getFactory() {
         return new TabContentFactory() {
@@ -107,28 +117,47 @@ public class AllPagesTab extends Awesome
             mView.setAdapter(adapter);
             mView.setOnTouchListener(mListListener);
         }
         return mView;
     }
 
     public void destroy() {
         AwesomeBarCursorAdapter adapter = getCursorAdapter();
+        GeckoAppShell.unregisterGeckoEventListener("SearchEngines:Data", this);
         if (adapter == null) {
             return;
         }
 
         Cursor cursor = adapter.getCursor();
         if (cursor != null)
             cursor.close();
     }
 
     public void filter(String searchTerm) {
         AwesomeBarCursorAdapter adapter = getCursorAdapter();
         adapter.filter(searchTerm);
+
+        // cancel previous query
+        if (mSuggestTask != null) {
+            mSuggestTask.cancel(true);
+        }
+
+        if (mSuggestClient != null) {
+            mSuggestTask = new AsyncTask<String, Void, ArrayList<String>>() {
+                 protected ArrayList<String> doInBackground(String... query) {
+                     return mSuggestClient.query(query[0]);
+                 }
+
+                 protected void onPostExecute(ArrayList<String> suggestions) {
+                     setSuggestions(suggestions);
+                 }
+            };
+            mSuggestTask.execute(searchTerm);
+        }
     }
 
     protected AwesomeBarCursorAdapter getCursorAdapter() {
         if (mCursorAdapter == null) {
             // Load the list using a custom adapter so we can create the bitmaps
             mCursorAdapter = new AwesomeBarCursorAdapter(mContext);
 
             mCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
@@ -478,16 +507,36 @@ public class AllPagesTab extends Awesome
             drawable = Drawable.createFromStream(stream, "src");
             stream.close();
         } catch (IllegalArgumentException e) {
             Log.i(LOGTAG, "exception while decoding drawable: " + base64, e);
         } catch (IOException e) { }
         return drawable;
     }
 
+    public void handleMessage(String event, JSONObject message) {
+        try {
+            if (event.equals("SearchEngines:Data")) {
+                final String suggestEngine =  message.isNull("suggestEngine") ? null : message.getString("suggestEngine");
+                final String suggestTemplate = message.isNull("suggestTemplate") ? null : message.getString("suggestTemplate");
+                final JSONArray engines = message.getJSONArray("searchEngines");
+                if (suggestTemplate != null)
+                    mSuggestClient = new SuggestClient(GeckoApp.mAppContext, suggestTemplate, SUGGESTION_TIMEOUT, SUGGESTION_MAX);
+                GeckoAppShell.getMainHandler().post(new Runnable() {
+                    public void run() {
+                        setSearchEngines(suggestEngine, engines);
+                    }
+                });
+            }
+        } catch (Exception e) {
+            // do nothing
+            Log.i(LOGTAG, "handleMessage throws " + e + " for message: " + event);
+        }
+    }
+
     public void handleItemClick(AdapterView<?> parent, View view, int position, long id) {
         ListView listview = getListView();
         if (listview == null)
             return;
 
         AwesomeBarItem item = (AwesomeBarItem)listview.getItemAtPosition(position);
         item.onClick();
     }