package org.mozilla.focus.autocomplete;

import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;

import org.mozilla.focus.R;
import org.mozilla.focus.widget.InlineAutocompleteEditText;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class UrlAutoCompleteFilter implements InlineAutocompleteEditText.OnFilterListener {
    private List<String> domains;

    public UrlAutoCompleteFilter(Context context) {
        loadUrls(context);
    }

    @Override
    public void onFilter(String searchText, InlineAutocompleteEditText view) {
        if (domains == null || view == null) {
            return;
        }

        for (final String domain : domains) {
            final String wwwDomain = "www." + domain;
            if (wwwDomain.startsWith(searchText)) {
                view.onAutocomplete(wwwDomain);
                return;
            }

            if (domain.startsWith(searchText)) {
                view.onAutocomplete(domain);
                return;
            }
        }
    }

    private void loadUrls(Context context) {
        new AsyncTask<Resources, Void, List<String>>() {
            @Override
            protected List<String> doInBackground(Resources... resources) {
                try {
                    final BufferedReader reader = new BufferedReader(new InputStreamReader(
                            resources[0].openRawResource(R.raw.topdomains)));

                    final List<String> domains = new ArrayList<String>(460);

                    String line;
                    while ((line = reader.readLine()) != null) {
                        domains.add(line);
                    }

                    return domains;
                } catch (IOException e) {
                    // No autocomplete for you!
                    return null;
                }
            }

            @Override
            protected void onPostExecute(List<String> domains) {
                UrlAutoCompleteFilter.this.domains = domains;
            }
        }.execute(context.getResources());
    }
}

@@ -366,16 +366,41 @@
 il.List;
+%0Aimport java.util.Locale;
 %0A%0Apublic
@@ -602,24 +602,617 @@
 xt);%0A    %7D%0A%0A
+    /**%0A     * Our autocomplete list is all lower case, however the search text might be mixed case.%0A     * Our autocomplete EditText code does more string comparison, which fails if the suggestion%0A     * doesn't exactly match searchText (ie. if casing differs). It's simplest to just build a suggestion%0A     * that exactly matches the search text - which is what this method is for:%0A     */%0A    private static String prepareAutocompleteResult(final String rawSearchText, final String lowerCaseResult) %7B%0A        return rawSearchText + lowerCaseResult.substring(rawSearchText.length());%0A    %7D%0A%0A
     @Overrid
@@ -1242,16 +1242,25 @@
 ter(
+final 
 String 
-s
+rawS
 earc
@@ -1371,32 +1371,198 @@
 urn;%0A        %7D%0A%0A
+        // Search terms are all lowercase already, we just need to lowercase the search text%0A        final String searchText = rawSearchText.toLowerCase(Locale.US);%0A%0A
         for (fin
@@ -1733,16 +1733,57 @@
 omplete(
+prepareAutocompleteResult(rawSearchText, 
 wwwDomai
@@ -1780,24 +1780,25 @@
 , wwwDomain)
+)
 ;%0A          
@@ -1915,15 +1915,57 @@
 ete(
+prepareAutocompleteResult(rawSearchText, 
 domain)
+)
 ;%0A  

Make domain autocomplete case insensitive (#321)