package org.wikipedia.editing.summaries;

import android.app.*;
import android.content.*;
import android.database.*;
import android.os.*;
import android.text.*;
import android.view.*;
import android.widget.*;
import org.wikipedia.*;

import java.util.*;

public class EditSummaryHandler {
    private final Activity activity;
    private final View container;
    private final AutoCompleteTextView summaryEdit;
    private final EditSummaryAdapter adapter;

    public EditSummaryHandler(final Activity activity, PageTitle title) {
        this.activity = activity;

        container = activity.findViewById(R.id.group_edit_summary_container);
        summaryEdit = (AutoCompleteTextView)activity.findViewById(R.id.group_edit_summary_edit);

        container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                summaryEdit.requestFocus();
            }
        });

        adapter = new EditSummaryAdapter(activity, null, true);
        summaryEdit.setAdapter(adapter);

        Utils.setTextDirection(summaryEdit, title.getSite().getLanguage());

        adapter.setFilterQueryProvider(new FilterQueryProvider() {
            @Override
            public Cursor runQuery(CharSequence charSequence) {
                ContentProviderClient client = activity.getContentResolver().acquireContentProviderClient(EditSummary.PERSISTANCE_HELPER.getBaseContentURI());
                try {
                    return client.query(
                            EditSummary.PERSISTANCE_HELPER.getBaseContentURI(),
                            null,
                            "summary LIKE ?",
                            new String[] {charSequence + "%"},
                            "lastUsed DESC");
                } catch (RemoteException e) {
                    // This shouldn't really be happening
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public void show() {
        container.setVisibility(View.VISIBLE);
    }

    public String getSummary(String sectionHeader) {
        if (TextUtils.isEmpty(sectionHeader)) {
            return summaryEdit.getText().toString();
        } else {
            return "/* " + sectionHeader + " */ " + summaryEdit.getText().toString();
        }
    }

    public void persistSummary() {
        WikipediaApp app = (WikipediaApp)container.getContext().getApplicationContext();
        EditSummary summary = new EditSummary(summaryEdit.getText().toString(), new Date());
        app.getPersister(EditSummary.class).upsert(summary);
    }

    public boolean handleBackPressed() {
        if (container.getVisibility() == View.VISIBLE) {
            container.setVisibility(View.GONE);
            return true;
        }
        return false;
    }

    private  class EditSummaryAdapter extends android.support.v4.widget.CursorAdapter {
        public EditSummaryAdapter(Context context, Cursor c, boolean autoRequery) {
            super(context, c, autoRequery);
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            return activity.getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false);
        }

        @Override
        public void bindView(View convertView, Context context, Cursor cursor) {
            ((TextView)convertView).setText(convertToString(cursor));
        }

        @Override
        public CharSequence convertToString(Cursor cursor) {
            return EditSummary.PERSISTANCE_HELPER.fromCursor(cursor).getSummary();
        }
    }
}

@@ -953,183 +953,730 @@
     
-adapter = new EditSummaryAdapter(activity, null, true);%0A        summaryEdit.setAdapter(adapter);%0A%0A        Utils.setTextDirection(summaryEdit, title.getSite().getLanguage());%0A%0A
+if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.ICE_CREAM_SANDWICH) %7B%0A            // For some reason the autocomplete popup view crashes on%0A            // Gingerbread. This seems to be related to styles but I%0A            // can't quite figure out why.%0A            //%0A            // This call in AutoCompleteTextView.buildDropDown ends up failing:%0A            //%0A            //             mDropDownList.setSelector(mDropDownListHighlight);%0A            //%0A            // because mDropDownListHighlight seems to be null instead%0A            // of an expected drawable, and that ends up failing when used.%0A%0A            adapter = new EditSummaryAdapter(activity, null, true);%0A            summaryEdit.setAdapter(adapter);%0A%0A    
     
@@ -1742,32 +1742,36 @@
 ) %7B%0A            
+    
 @Override%0A      
@@ -1768,32 +1768,36 @@
 ide%0A            
+ 
+   
 public Cursor ru
@@ -1828,24 +1828,28 @@
 Sequence) %7B%0A
+    
             
@@ -2011,16 +2011,20 @@
         
+    
 try %7B%0A  
@@ -2037,24 +2037,28 @@
             
+   
+ 
 return clien
@@ -2066,16 +2066,20 @@
 .query(%0A
+    
         
@@ -2182,14 +2182,22 @@
     
+    
 null,%0A
+    
     
@@ -2266,16 +2266,20 @@
         
+    
 new Stri
@@ -2333,16 +2333,20 @@
         
+   
+ 
 %22lastUse
@@ -2355,16 +2355,20 @@
 DESC%22);%0A
+    
         
@@ -2417,32 +2417,36 @@
                 
+  
+  
 // This shouldn'
@@ -2467,16 +2467,20 @@
 ppening%0A
+    
         
@@ -2530,34 +2530,42 @@
                 
-%7D%0A
+    %7D%0A    
             %7D%0A  
@@ -2570,17 +2570,153 @@
         
-%7D
+    %7D);%0A        %7D else %7B%0A            adapter = null;%0A        %7D%0A%0A        Utils.setTextDirection(summaryEdit, title.getSite().getLanguage()
 );%0A    %7D

Disable crashy AutoCompleteTextView in edit summary