package jp.ito.camera;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MailSetting extends Activity {
	private static final String TAG = MailSetting.class.getSimpleName();
	static final String ADDRESS = "Mail.Address";
	static final String PASSWORD = "Mail.PassWord";
	static final String HOST = "Mail.Host";
	static final String PORT = "Mail.Port";
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.mailsetting);
        setTitle("見守り君 - メール設定");

        SharedPreferences pref = getSharedPreferences("jp.ito.mimamori",MODE_PRIVATE);
		String strAddress = pref.getString(ADDRESS, "boo@hoo.uu");
		EditText editAddress = (EditText)findViewById( R.id.editMailAddress );
		editAddress.setText(strAddress);
        
		String strPassWord = pref.getString(PASSWORD, "");
		EditText editPassWord = (EditText)findViewById( R.id.editMailPassWord );
		editPassWord.setText(strPassWord);
        
		String strHost = pref.getString(HOST, "smtp.gmail.com");
		EditText editHost = (EditText)findViewById( R.id.editMailHost );
		editHost.setText(strHost);
        
		String strPort = pref.getString(PORT, "587");
		EditText editPort = (EditText)findViewById( R.id.editMailPort );
		editPort.setText(strPort);

		Button btn  = (Button) findViewById(R.id.button);
        btn.setOnClickListener(btnListener);	//リスナの登録
    }
    private OnClickListener btnListener = new OnClickListener() {
        public void onClick(View v) {
            switch(v.getId()){
            case R.id.button:
                SharedPreferences pref = getSharedPreferences("jp.ito.mimamori",MODE_PRIVATE);
        		Editor e = pref.edit();
        		e.putString(ADDRESS, ((EditText)findViewById( R.id.editMailAddress )).getText().toString());
        		e.putString(PASSWORD, ((EditText)findViewById( R.id.editMailPassWord )).getText().toString());
        		e.putString(HOST, ((EditText)findViewById( R.id.editMailHost )).getText().toString());
        		e.putString(PORT, ((EditText)findViewById( R.id.editMailPort )).getText().toString());
        		e.commit();
        		Intent i = new Intent();
        		setResult(RESULT_OK, i);
        		finish();
                break;
            }
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
		Log.d(TAG, "onStart()");
    }
    //アプリの開始
    @Override
    protected void onResume() {
        super.onResume();
		Log.d(TAG, "onResume()");
    }

    @Override
    protected void onPause(){
		super.onPause();
		Log.d(TAG, "onPause()");
	}

    //アプリの停止
    @Override
    protected void onStop() {
        super.onStop();
		Log.d(TAG, "onStop()");
    }    

    @Override
    protected void onDestroy(){
		super.onDestroy();
		Log.d(TAG, "onDestroy()");
	}
}
