Bug 1291751 - Use history or bookmarks search mode as appropriate in AS-search draft
authorAndrzej Hunt <ahunt@mozilla.com>
Wed, 03 Aug 2016 13:28:29 -0700
changeset 398861 29221a7b59cf81a5d8c1095195b6504a93898001
parent 398860 a70612489ec13f7dec804fafaf057dcd49a0f0ba
child 527775 ea6bb098bb36ff35022ad95f5933267d5d12eb5f
push id25659
push userahunt@mozilla.com
push dateTue, 09 Aug 2016 21:55:04 +0000
bugs1291751
milestone51.0a1
Bug 1291751 - Use history or bookmarks search mode as appropriate in AS-search MozReview-Commit-ID: FmyNGev01hC
mobile/android/base/java/org/mozilla/gecko/home/activitystream/ASDetailActivity.java
mobile/android/base/java/org/mozilla/gecko/home/activitystream/search/ASSearchActivity.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/ASDetailActivity.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/ASDetailActivity.java
@@ -34,17 +34,17 @@ public class ASDetailActivity
     public static final int ACTIVITY_REQUEST_SEARCH = 100;
 
     @Override
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
         setContentView(R.layout.as_detail_activity);
 
-        ViewPager pager = (ViewPager) findViewById(R.id.as_pager);
+        final ViewPager pager = (ViewPager) findViewById(R.id.as_pager);
 
         FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
             @Override
             public Fragment getItem(int position) {
                 switch (position) {
                     case 0:
                         return new ASCombinedHistoryPanel();
                     case 1:
@@ -92,16 +92,19 @@ public class ASDetailActivity
             }
         });
 
         final ImageView searchButton = (ImageView) findViewById(R.id.as_search_button);
         searchButton.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent i = new Intent(ASDetailActivity.this, ASSearchActivity.class);
+                if (pager.getCurrentItem() == 1) {
+                    i.putExtra(ASSearchActivity.ACTIVITY_ARG_BOOKMARKS_MODE, true);
+                }
                 startActivityForResult(i, ACTIVITY_REQUEST_SEARCH);
             }
         });
     }
 
     @Override
     public void onUrlOpen(String url, EnumSet<HomePager.OnUrlOpenListener.Flags> flags) {
         ASOpenURLDelegate.openURL(this, url, flags);
--- a/mobile/android/base/java/org/mozilla/gecko/home/activitystream/search/ASSearchActivity.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/activitystream/search/ASSearchActivity.java
@@ -26,59 +26,83 @@ import org.mozilla.gecko.home.HomePager;
 import org.mozilla.gecko.home.activitystream.ASOpenURLDelegate;
 import org.mozilla.gecko.widget.RecyclerViewClickSupport;
 
 import java.util.EnumSet;
 
 public class ASSearchActivity extends AppCompatActivity implements HomePager.OnUrlOpenListener {
     public static final String ACTIVITY_ARG_BOOKMARKS_MODE = "mode_bookmarks";
 
+    private enum SearchMode {
+        HISTORY,
+        BOOKMARKS
+    };
+
     private static final String LOADER_ARG_KEY_CONSTRAINT = "constraint";
 
+    private SearchMode searchMode;
+
     private CombinedHistoryRecyclerView resultsList;
 
     @Override
     public void onUrlOpen(String url, EnumSet<Flags> flags) {
         ASOpenURLDelegate.openURL(this, url, flags);
     }
 
     private static class SearchLoader extends CursorLoader {
         final String constraint;
+        final SearchMode searchMode;
 
-        public SearchLoader(Context context, String constraint) {
+        public SearchLoader(Context context, SearchMode searchMode, String constraint) {
             super(context);
 
+            this.searchMode = searchMode;
             this.constraint = constraint;
         }
 
         @Override
         public Cursor loadInBackground() {
             if (constraint == null || constraint.length() == 0) {
                 // We don't want to show any results when the query string is empty
                 return null;
             }
 
+            final EnumSet<BrowserDB.FilterFlags> flags;
+            switch (searchMode) {
+                case HISTORY:
+                    flags = EnumSet.of(BrowserDB.FilterFlags.EXCLUDE_BOOKMARKS);
+                    break;
+                case BOOKMARKS:
+                    flags = EnumSet.of(BrowserDB.FilterFlags.EXCLUDE_HISTORY);
+                    break;
+                default:
+                    // This should never happen unless another searchMode is added, however the compiler
+                    // will complain that flags might not be initialised despite the fact that we have
+                    // covered all enum cases above, hence we need this either way.
+                    throw new IllegalStateException();
+            }
+
             return GeckoProfile.get(getContext()).getDB().filter(getContext().getContentResolver(),
                     constraint,
                     50,
-                    EnumSet.noneOf(BrowserDB.FilterFlags.class));
+                    flags);
         }
     }
 
     private class CursorLoaderCallbacks implements LoaderManager.LoaderCallbacks<Cursor> {
         @Override
         public Loader<Cursor> onCreateLoader(int id, Bundle args) {
             final String constraint;
             if (args != null) {
                 constraint = args.getString(LOADER_ARG_KEY_CONSTRAINT);
             } else {
                 constraint = null;
             }
 
-            return new SearchLoader(getApplicationContext(), constraint);
+            return new SearchLoader(getApplicationContext(), searchMode, constraint);
         }
 
         @Override
         public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
             ((ASSearchResultsAdapter) resultsList.getAdapter()).swapCursor(data);
         }
 
         @Override
@@ -86,16 +110,22 @@ public class ASSearchActivity extends Ap
             ((ASSearchResultsAdapter) resultsList.getAdapter()).swapCursor(null);
         }
     }
 
 
     protected void onCreate(@Nullable Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        if (getIntent().getBooleanExtra(ACTIVITY_ARG_BOOKMARKS_MODE, false)) {
+            searchMode = SearchMode.BOOKMARKS;
+        } else {
+            searchMode = SearchMode.HISTORY;
+        }
+
         setContentView(R.layout.as_search_activity);
 
         findViewById(R.id.as_search_back).setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 finish();
             }
         });
@@ -111,20 +141,20 @@ public class ASSearchActivity extends Ap
         // for search results so we can just disable it completely for now.
         RecyclerViewClickSupport.addTo(resultsList).setOnItemLongClickListener(null);
 
         final CursorLoaderCallbacks callbacks = new CursorLoaderCallbacks();
         getSupportLoaderManager().initLoader(0, null, callbacks);
 
         final EditText searchEntry = (EditText) findViewById(R.id.as_search_entry);
 
-        if (getIntent().getBooleanExtra(ACTIVITY_ARG_BOOKMARKS_MODE, false)) {
+        if (searchMode == SearchMode.HISTORY) {
+            searchEntry.setHint(R.string.as_search_in_history);
+        } else if (searchMode == SearchMode.BOOKMARKS) {
             searchEntry.setHint(R.string.as_search_in_bookmarks);
-        } else {
-            searchEntry.setHint(R.string.as_search_in_history);
         }
 
         searchEntry.addTextChangedListener(new TextWatcher() {
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
 
             @Override
             public void onTextChanged(CharSequence s, int start, int before, int count) {