package com.mamewo.stacklr;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView;

public class StacklrActivity
    extends Activity
    implements AdapterView.OnItemClickListener,
    TextView.OnEditorActionListener
{
    private ListView stackListView_;
    private EditText targetEditText_;
    private StackAdapter stackAdapter_;
    static private
    final String TAG = "stacklr";
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        stackListView_ = (ListView) findViewById(R.id.stacklist);
        targetEditText_ = (EditText) findViewById(R.id.target_text_view);
        targetEditText_.setOnEditorActionListener(this);
        stackAdapter_ = new StackAdapter();
        stackListView_.setAdapter(stackAdapter_);
        stackListView_.setOnItemClickListener(this);
        Button pushButton = (Button) findViewById(R.id.push_button);
        pushButton.setOnClickListener(new PushButtonListener());
    }

    public class PushButtonListener
        implements View.OnClickListener
    {
        @Override
        public void onClick(View v) {
            String itemname = targetEditText_.getText().toString();
            //TODO: search
            if(itemname.length() == 0){
                return;
            }
            stackAdapter_.push(itemname);
            targetEditText_.setText("");
        }
    }

    public class StackAdapter
        extends BaseAdapter
    {
        List<String> stack_;

        public StackAdapter() {
            stack_ = new LinkedList<String>();
        }

        public void push(String item) {
            //TODO: select more efficient way
            for(int i = 0; i < stack_.size(); i++) {
                String existing = stack_.get(i);
                if(existing.equals(item)){
                    stack_.remove(i);
                    break;
                }
            }
            stack_.add(0, item);
            notifyDataSetChanged();
        }
        
        public void push(int pos){
            String itemtext = stack_.remove(pos);
            stack_.add(0, itemtext);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return stack_.size();
        }

        @Override
        public Object getItem(int position) {
            return stack_.get(position);
        }

        @Override
        public long getItemId(int position) {
            return stack_.get(position).hashCode();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = View.inflate(StacklrActivity.this, R.layout.stackitem, null);
            }
            String name = stack_.get(position);
            TextView labelview = (TextView) convertView.findViewById(R.id.itemname);
            labelview.setText(name);
            return convertView;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        stackAdapter_.push(position);
        targetEditText_.setText("");
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        Log.d(TAG, "onAction " + event + " " + Integer.toHexString(actionId));
        if(actionId == EditorInfo.IME_ACTION_DONE){
            String item = v.getText().toString();
            if(item.length() > 0) {
                stackAdapter_.push(item);
                v.setText("");
                return true;
            }
        }
        return false;
    }
}

@@ -123,17 +123,88 @@
 oid.
-os.Bundle
+content.Intent;%0Aimport android.os.Bundle;%0Aimport android.speech.RecognizerIntent
 ;%0Aim
@@ -252,24 +252,57 @@
 w.KeyEvent;%0A
+import android.view.MotionEvent;%0A
 import andro
@@ -891,16 +891,123 @@
 acklr%22;%0A
+    static private%0A    final int SPEECH_RECOGNITION_REQUEST_CODE = 2222;%0A    private Intent speechIntent_;%0A
     %0A   
@@ -1397,32 +1397,115 @@
 Listener(this);%0A
+        targetEditText_.setOnTouchListener(new MicClickListener(targetEditText_));%0A
         stackAda
@@ -1765,24 +1765,225 @@
 istener());%0A
+        %0A        speechIntent_ = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);%0A        speechIntent_.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);%0A
     %7D%0A%0A    p
@@ -2380,32 +2380,572 @@
        %7D%0A    %7D%0A%0A
+    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        if (requestCode == SPEECH_RECOGNITION_REQUEST_CODE) %7B%0A            if(resultCode != RESULT_OK) %7B%0A                return;%0A            %7D%0A            //TODO: select good one or display list dialog %0A            List%3CString%3E matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);%0A            if(matches.isEmpty())%7B%0A                return;%0A            %7D%0A            targetEditText_.setText(matches.get(0));%0A        %7D%0A    %7D%0A%0A
     public class
@@ -3156,54 +3156,8 @@
 ) %7B%0A
-            //TODO: select more efficient way%0A
     
@@ -5071,32 +5071,32 @@
     %7D%0A        %7D%0A
-
         return f
@@ -5106,11 +5106,420 @@
 e;%0A    %7D
+%0A%0A    private%0A    class MicClickListener%0A        extends RightDrawableOnTouchListener%0A    %7B%0A        public MicClickListener(TextView view) %7B%0A            super(view);%0A        %7D%0A%0A        @Override%0A        public boolean onDrawableTouch(MotionEvent event) %7B%0A            //enter by speech%0A            startActivityForResult(speechIntent_, SPEECH_RECOGNITION_REQUEST_CODE);%0A            return true;%0A        %7D%0A    %7D
 %0A%7D%0A

add simple speech recognition