package org.wikipedia.page;

import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.SearchView;
import android.view.ActionProvider;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebView.FindListener;
import android.widget.TextView;

import org.wikipedia.R;
import org.wikipedia.analytics.FindInPageFunnel;
import org.wikipedia.util.DeviceUtil;

public class FindInPageActionProvider extends ActionProvider {
    @NonNull private final PageFragment fragment;
    @NonNull private final FindInPageFunnel funnel;

    private View findInPageNext;
    private View findInPagePrev;
    private TextView findInPageMatch;

    public FindInPageActionProvider(@NonNull PageFragment fragment,
                                    @NonNull FindInPageFunnel funnel) {
        super(fragment.getContext());
        this.fragment = fragment;
        this.funnel = funnel;
    }

    @Override
    public boolean overridesItemVisibility() {
        return true;
    }

    @Override
    public View onCreateActionView() {
        View view = View.inflate(fragment.getContext(), R.layout.group_find_in_page, null);
        findInPageNext = view.findViewById(R.id.find_in_page_next);
        findInPageNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DeviceUtil.hideSoftKeyboard(view);
                if (!pageFragmentValid()) {
                    return;
                }
                funnel.addFindNext();
                fragment.getWebView().findNext(true);
            }
        });

        findInPagePrev = view.findViewById(R.id.find_in_page_prev);
        findInPagePrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fragment.hideSoftKeyboard();
                if (!pageFragmentValid()) {
                    return;
                }
                funnel.addFindPrev();
                fragment.getWebView().findNext(false);
            }
        });

        findInPageMatch = (TextView) view.findViewById(R.id.find_in_page_match);

        SearchView searchView = (SearchView) view.findViewById(R.id.find_in_page_input);
        searchView.setQueryHint(fragment.getContext().getString(R.string.menu_page_find_in_page));
        searchView.setFocusable(true);
        searchView.requestFocusFromTouch();
        searchView.setOnQueryTextListener(searchQueryListener);
        searchView.setOnCloseListener(searchCloseListener);
        searchView.setIconified(false);
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchView.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        searchView.setSubmitButtonEnabled(false);
        // remove focus line from search plate
        View searchEditPlate = searchView
                .findViewById(android.support.v7.appcompat.R.id.search_plate);
        searchEditPlate.setBackgroundColor(Color.TRANSPARENT);
        return view;
    }

    private final SearchView.OnQueryTextListener searchQueryListener = new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            findInPageNext.setEnabled(s.length() > 0);
            findInPagePrev.setEnabled(s.length() > 0);
            if (!pageFragmentValid()) {
                return false;
            }
            funnel.setFindText(s);
            if (s.length() > 0) {
                findInPage(s);
            } else {
                fragment.getWebView().clearMatches();
                findInPageMatch.setVisibility(View.GONE);
            }
            return true;
        }
    };

    private final SearchView.OnCloseListener searchCloseListener = new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            fragment.closeFindInPage();
            return false;
        }
    };

    private boolean pageFragmentValid() {
        return fragment.getWebView() != null;
    }

    public void findInPage(String s) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            fragment.getWebView().setFindListener(new FindListener() {
                @Override
                public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) {
                    if (!isDoneCounting) {
                        return;
                    }
                    if (numberOfMatches > 0) {
                        findInPageMatch.setText(fragment.getString(R.string.find_in_page_result,
                                activeMatchOrdinal + 1, numberOfMatches));
                        findInPageNext.setEnabled(true);
                        findInPagePrev.setEnabled(true);
                    } else {
                        findInPageMatch.setText("0/0");
                        findInPageNext.setEnabled(false);
                        findInPagePrev.setEnabled(false);
                    }
                    findInPageMatch.setVisibility(View.VISIBLE);
                }
            });
            fragment.getWebView().findAllAsync(s);
        } else {
            //noinspection deprecation
            fragment.getWebView().findAll(s);
        }
    }
}

@@ -57,33 +57,8 @@
 or;%0A
-import android.os.Build;%0A
 impo
@@ -4205,83 +4205,8 @@
 ) %7B%0A
-        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN) %7B%0A    
     
@@ -4272,36 +4272,32 @@
 ) %7B%0A            
-    
 @Override%0A      
@@ -4282,36 +4282,32 @@
       @Override%0A
-    
             publ
@@ -4418,28 +4418,24 @@
             
-    
 if (!isDoneC
@@ -4437,36 +4437,32 @@
 DoneCounting) %7B%0A
-    
                 
@@ -4485,34 +4485,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -4546,36 +4546,32 @@
                 
-    
 findInPageMatch.
@@ -4655,20 +4655,16 @@
         
-    
 activeMa
@@ -4710,36 +4710,32 @@
                 
- 
-   
 findInPageNext.s
@@ -4743,36 +4743,32 @@
 tEnabled(true);%0A
-    
                 
@@ -4812,36 +4812,32 @@
                 
- 
-   
 %7D else %7B%0A       
@@ -4821,36 +4821,32 @@
        %7D else %7B%0A
-    
                 
@@ -4893,36 +4893,32 @@
                 
- 
-   
 findInPageNext.s
@@ -4927,36 +4927,32 @@
 Enabled(false);%0A
-    
                 
@@ -5001,34 +5001,26 @@
             
- 
-   %7D%0A    
+%7D%0A
             
@@ -5076,34 +5076,26 @@
             
-    %7D%0A    
+%7D%0A
         %7D);%0A
@@ -5094,36 +5094,32 @@
     %7D);%0A        
-  
-  
 fragment.getWebV
@@ -5145,120 +5145,8 @@
 s);%0A
-        %7D else %7B%0A            //noinspection deprecation%0A            fragment.getWebView().findAll(s);%0A        %7D%0A
     

Remove another unneeded API level check