package com.bgstation0.android.sample.webview_;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity implements OnEditorActionListener, OnQueryTextListener{

	// otB[hÌè`.
	Context mContext = null;	// mContextðnullÉú».
		
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;	// mContextÉthisðZbg.
        
        // WebViewÌúÝè.
        WebView webView = (WebView)findViewById(R.id.webview);	// webViewðæ¾.
        CustomWebViewClient customWVC = new CustomWebViewClient(this);	// CustomWebViewClientIuWFNgcustomWVCðì¬.
        webView.setWebViewClient(customWVC);	// webView.setWebViewClientÅcustomWVCðZbg.
        
    }
    
    // j[ªì¬³ê½.
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	
    	// j[Ìì¬
    	getMenuInflater().inflate(R.menu.main, menu);	// getMenuInflaterÅMenuItemðæ¾µ, »ÌÜÜinflateÅwè³ê½R.menu.mainð³Émenuðì¬.
    	ActionBar actionBar = getActionBar();	// getActionBarÅactionBarðæ¾.
    	actionBar.setDisplayShowTitleEnabled(false);	// actionBar.setDisplayShowTitleEnabledðfalseÉµÄ^Cgðñ\¦.
    	actionBar.setDisplayShowHomeEnabled(false);	// actionBar.setDisplayShowHomeEnabledðfalseÉµÄACRàñ\¦.
    	
    	// URLo[ÌúÝè.
    	MenuItem menuItemUrlBar = menu.findItem(R.id.menu_item_url_layout);	// menuItemUrlBarÌæ¾.
    	LinearLayout urlLayout = (LinearLayout)menuItemUrlBar.getActionView();	// urlLayoutæ¾.
        EditText urlbar = (EditText)urlLayout.findViewById(R.id.urlbar);	// urlbarðæ¾.
        urlbar.setOnEditorActionListener(this);	// thisðZbg.
        
        // SearchViewÌúÝè.
        SearchView searchview = (SearchView)urlLayout.findViewById(R.id.searchview);	// searchviewÌæ¾.
        searchview.setOnQueryTextListener(this);	// thisðZbg.
        
        // ButtonUpÌúÝè.
        Button buttonup = (Button)urlLayout.findViewById(R.id.button_up);	// buttonupÌæ¾.
        buttonup.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				WebView webView = (WebView)findViewById(R.id.webview);	// webViewðæ¾.
				webView.findNext(false);	// falseÅOðT·.
			}
			
		});
        
        // ButtonDownÌúÝè.
        Button buttonDown = (Button)urlLayout.findViewById(R.id.button_down);	// buttondownÌæ¾.
        buttonDown.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				WebView webView = (WebView)findViewById(R.id.webview);	// webViewðæ¾.
				webView.findNext(true);	// trueÅðT·.
			}
			
		});
        
    	return true;
    	
    }
    
    @Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		// TODO Auto-generated method stub
		
		// EnterL[(Done)ª³ê½.
		if (actionId == EditorInfo.IME_ACTION_DONE){	// IME_ACTION_DONEÌ.
			
			// [h.
			WebView webView = (WebView)findViewById(R.id.webview);	// webviewÌæ¾.
			webView.loadUrl(v.getText().toString());	// v.getTextÅæ¾µ½eLXgðgÁÄ, webView.loadUrlÅ[h.
			return true;	// trueðÔ·.
		
		}
		
		// false.
		return false;	// falseðÔ·.
		
	}
    
 // NGeLXgªÏX³ê½.
 	@Override
 	public boolean onQueryTextChange(String newText){
 		
 		return false;
 		
 	}
 	
 	// NGeLXgªèµ½.
 	@Override
 	public boolean onQueryTextSubmit(String query){
 		
 		// Sõ.
 		WebView webView = (WebView)findViewById(R.id.webview);	// webviewÌæ¾.
 		webView.findAll(query);	// webView.findAllÅSõ.
 		return false;
 		
 	}
    
}