package org.wikipedia.history;

import android.support.v4.view.MenuItemCompat;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;

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

public abstract class SearchActionModeCallback implements ActionMode.Callback {
    public static final String ACTION_MODE_TAG = "searchActionMode";
    private SearchView searchView;

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        mode.setTag(ACTION_MODE_TAG);
        mode.getMenuInflater().inflate(R.menu.menu_action_mode_search, menu);
        searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search_view));
        searchView.setIconified(false);
        searchView.setQueryHint(getSearchHintString());
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                onQueryChange(s);
                return true;
            }
        });
        return true;
    }

    protected abstract String getSearchHintString();

    protected abstract void onQueryChange(String s);

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        searchView.requestFocus();
        DeviceUtil.showSoftKeyboard(searchView);
        return true;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem menuItem) {
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
    }
}

@@ -215,16 +215,75 @@
 enuItem;
+%0Aimport android.view.View;%0Aimport android.widget.ImageView;
 %0A%0Aimport
@@ -522,16 +522,53 @@
 rchView;
+%0A    private ImageView searchMagIcon;
 %0A%0A    @O
@@ -857,24 +857,182 @@
 rch_view));%0A
+        searchMagIcon = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_mag_icon);%0A        searchMagIcon.setImageDrawable(null);%0A
         sear
@@ -1050,16 +1050,25 @@
 conified
+ByDefault
 (false);
@@ -1751,16 +1751,64 @@
 ocus();%0A
+        searchMagIcon.setVisibility(View.GONE);%0A
         
@@ -2002,32 +2002,32 @@
 %0A%0A    @Override%0A
-
     public void 
@@ -2061,20 +2061,117 @@
 ode mode) %7B%0A
+        if (searchView != null) %7B%0A            searchView.setOnQueryTextListener(null);%0A        %7D%0A
     %7D%0A%7D%0A

Remove SearchView close button when no text is present