Bug 1349797 - Make domain autocompletion case-insensitive. r?janH draft
authorMehdi Soleimannejad <mehdisolamannejad@gmail.com>
Wed, 26 Jul 2017 18:41:36 +0430
changeset 615870 09eb0f43126a76fd5d3b5fdf666d6e513390f384
parent 615861 48fe5c3399830664e657af5abf20c24792cb373a
child 639310 7d6fe68874d8671262e716697fbb9f00a07c84b1
push id70513
push userbmo:mehdisolamannejad@gmail.com
push dateWed, 26 Jul 2017 14:15:20 +0000
reviewersjanH
bugs1349797
milestone56.0a1
Bug 1349797 - Make domain autocompletion case-insensitive. r?janH MozReview-Commit-ID: Jxp7NksOjIH
mobile/android/base/java/org/mozilla/gecko/home/BrowserSearch.java
mobile/android/base/java/org/mozilla/gecko/toolbar/ToolbarEditText.java
mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/BrowserSearch.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/BrowserSearch.java
@@ -525,27 +525,28 @@ public class BrowserSearch extends HomeF
         mAutocompleteHandler.onAutocomplete(autocompletion);
         mAutocompleteHandler = null;
     }
 
     /**
      * Returns the substring of a provided URI, starting at the given offset,
      * and extending up to the end of the path segment in which the provided
      * index is found.
-     * <p>
+     *
      * For example, given
-     * <p>
-     * "www.reddit.com/r/boop/abcdef", 0, ?
-     * <p>
+     *
+     *   "www.reddit.com/r/boop/abcdef", 0, ?
+     *
      * this method returns
-     * <p>
-     * ?=2:  "www.reddit.com/"
-     * ?=17: "www.reddit.com/r/boop/"
-     * ?=21: "www.reddit.com/r/boop/"
-     * ?=22: "www.reddit.com/r/boop/abcdef"
+     *
+     *   ?=2:  "www.reddit.com/"
+     *   ?=17: "www.reddit.com/r/boop/"
+     *   ?=21: "www.reddit.com/r/boop/"
+     *   ?=22: "www.reddit.com/r/boop/abcdef"
+     *
      */
     private static String uriSubstringUpToMatchedPath(final String url, final int offset, final int begin) {
         final int afterEnd = url.length();
 
         // We want to include the trailing slash, but not other characters.
         int chop = url.indexOf('/', begin);
         if (chop != -1) {
             ++chop;
@@ -562,17 +563,16 @@ public class BrowserSearch extends HomeF
                 chop = afterEnd;
             }
         }
 
         return url.substring(offset, chop);
     }
 
     LinkedHashSet<String> domains = null;
-
     private LinkedHashSet<String> getDomains() {
         if (domains == null) {
             domains = new LinkedHashSet<String>(500);
             BufferedReader buf = null;
             try {
                 buf = new BufferedReader(new InputStreamReader(
                         getResources().openRawResource(R.raw.topdomains), StringUtils.UTF_8));
                 String res = null;
@@ -584,18 +584,17 @@ public class BrowserSearch extends HomeF
                     }
                 } while (res != null);
             } catch (IOException e) {
                 Log.e(LOGTAG, "Error reading domains", e);
             } finally {
                 if (buf != null) {
                     try {
                         buf.close();
-                    } catch (IOException e) {
-                    }
+                    } catch (IOException e) { }
                 }
             }
         }
         return domains;
     }
 
     private String searchDomains(String search) {
         for (String domain : getDomains()) {
@@ -931,18 +930,17 @@ public class BrowserSearch extends HomeF
             public void onAnimationStart(Animation a) {
                 // Increase the height of the view so a gap isn't shown during animation
                 mView.getLayoutParams().height = mView.getHeight() +
                         mSuggestionsOptInPrompt.getHeight();
                 mView.requestLayout();
             }
 
             @Override
-            public void onAnimationRepeat(Animation a) {
-            }
+            public void onAnimationRepeat(Animation a) {}
 
             @Override
             public void onAnimationEnd(Animation a) {
                 // Removing the view immediately results in a NPE in
                 // dispatchDraw(), possibly because this callback executes
                 // before drawing is finished. Posting this as a Runnable fixes
                 // the issue.
                 mView.post(new Runnable() {
@@ -1072,27 +1070,27 @@ public class BrowserSearch extends HomeF
         public SearchHistorySuggestionAsyncLoader(Context context, String searchTerm) {
             super(context, searchTerm);
         }
 
         @Override
         public ArrayList<String> loadInBackground() {
             final ContentResolver cr = getContext().getContentResolver();
 
-            String[] columns = new String[]{BrowserContract.SearchHistory.QUERY};
+            String[] columns = new String[] { BrowserContract.SearchHistory.QUERY };
             String actualQuery = BrowserContract.SearchHistory.QUERY + " LIKE ?";
-            String[] queryArgs = new String[]{'%' + mSearchTerm + '%'};
+            String[] queryArgs = new String[] { '%' + mSearchTerm + '%' };
 
             // For deduplication, the worst case is that all the first NETWORK_SUGGESTION_MAX history suggestions are duplicates
             // of search engine suggestions, and the there is a duplicate for the search term itself. A duplicate of the
             // search term  can occur if the user has previously searched for the same thing.
             final int maxSavedSuggestions = NETWORK_SUGGESTION_MAX + 1 + getContext().getResources().getInteger(R.integer.max_saved_suggestions);
 
             final String sortOrderAndLimit = BrowserContract.SearchHistory.DATE + " DESC LIMIT " + maxSavedSuggestions;
-            final Cursor result = cr.query(BrowserContract.SearchHistory.CONTENT_URI, columns, actualQuery, queryArgs, sortOrderAndLimit);
+            final Cursor result =  cr.query(BrowserContract.SearchHistory.CONTENT_URI, columns, actualQuery, queryArgs, sortOrderAndLimit);
 
             if (result == null) {
                 return new ArrayList<>();
             }
 
             final ArrayList<String> savedSuggestions = new ArrayList<>();
             try {
                 if (result.moveToFirst()) {
@@ -1111,22 +1109,22 @@ public class BrowserSearch extends HomeF
     }
 
     private class SearchAdapter extends MultiTypeCursorAdapter {
         private static final int ROW_SEARCH = 0;
         private static final int ROW_STANDARD = 1;
         private static final int ROW_SUGGEST = 2;
 
         public SearchAdapter(Context context) {
-            super(context, null, new int[]{ROW_STANDARD,
+            super(context, null, new int[] { ROW_STANDARD,
                             ROW_SEARCH,
-                            ROW_SUGGEST},
-                    new int[]{R.layout.home_item_row,
+                            ROW_SUGGEST },
+                    new int[] { R.layout.home_item_row,
                             R.layout.home_search_item_row,
-                            R.layout.home_search_item_row});
+                            R.layout.home_search_item_row });
         }
 
         @Override
         public int getItemViewType(int position) {
             if (position < getPrimaryEngineCount()) {
                 if (mSuggestionsEnabled && mSearchEngines.get(position).hasSuggestions()) {
                     // Give suggestion views their own type to prevent them from
                     // sharing other recycled search result views. Using other
@@ -1383,9 +1381,9 @@ public class BrowserSearch extends HomeF
 
             super.setPrivateMode(isPrivate);
         }
 
         private void setSelector(boolean isPrivate) {
             setSelector(isPrivate ? R.drawable.search_list_selector_private : R.drawable.search_list_selector);
         }
     }
-}
+}
\ No newline at end of file
--- a/mobile/android/base/java/org/mozilla/gecko/toolbar/ToolbarEditText.java
+++ b/mobile/android/base/java/org/mozilla/gecko/toolbar/ToolbarEditText.java
@@ -179,17 +179,17 @@ public class ToolbarEditText extends Cus
         mSettingAutoComplete = false;
         endBatchEdit();
     }
 
     /**
      * Reset autocomplete states to their initial values
      */
     private void resetAutocompleteState() {
-        mAutoCompleteSpans = new Object[]{
+        mAutoCompleteSpans = new Object[] {
                 // Span to mark the autocomplete text
                 AUTOCOMPLETE_SPAN,
                 // Span to change the autocomplete text color
                 new BackgroundColorSpan(getHighlightColor())
         };
 
         mAutoCompleteResult = "";
 
@@ -309,17 +309,17 @@ public class ToolbarEditText extends Cus
         mAutoCompleteResult = result;
 
         if (autoCompleteStart > -1) {
             // Autocomplete text already exists; we should replace existing autocomplete text.
 
             // If the result and the current text don't have the same prefixes,
             // the result is stale and we should wait for the another result to come in.
             if(!StringUtils.caseInsensitiveStartsWith(result.substring(0, autoCompleteStart),
-                                                      text.toString().substring(0, autoCompleteStart))){
+                    text.toString().substring(0, autoCompleteStart))) {
                 return;
             }
 
             beginSettingAutocomplete();
 
             // Replace the existing autocomplete text with new one.
             // replace() preserves the autocomplete spans that we set before.
             text.replace(autoCompleteStart, textLength, result, autoCompleteStart, resultLength);
@@ -412,17 +412,17 @@ public class ToolbarEditText extends Cus
         return false;
     }
 
     /**
      * Code to handle deleting autocomplete first when backspacing.
      * If there is no autocomplete text, both removeAutocomplete() and commitAutocomplete()
      * are no-ops and return false. Therefore we can use them here without checking explicitly
      * if we have autocomplete text or not.
-     * <p>
+     *
      * Also turns off text prediction for private mode tabs.
      */
     @Override
     public InputConnection onCreateInputConnection(final EditorInfo outAttrs) {
         final InputConnection ic = super.onCreateInputConnection(outAttrs);
         if (ic == null) {
             return null;
         }
@@ -628,9 +628,9 @@ public class ToolbarEditText extends Cus
                     removeAutocomplete(getText())) {
                 // Delete autocomplete text when backspacing or forward deleting.
                 return true;
             }
 
             return false;
         }
     }
-}
+}
\ No newline at end of file
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
@@ -278,12 +278,15 @@ public class StringUtils {
 
     /**
      * Returns true if 'text' starts with 'prefix', with no case sensitivity
      */
     public static boolean caseInsensitiveStartsWith(String text, String prefix) {
         return caseInsensitiveStartsWith(text, prefix, 0);
     }
 
+    /**
+     * Returns true if 'text' from index 'start' starts with 'prefix', with no case sensitivity
+     */
     public static boolean caseInsensitiveStartsWith(String text, String prefix, int start) {
         return text.regionMatches(true, start, prefix, 0, prefix.length());
     }
 }
\ No newline at end of file