/**
 * 
 */
package frontend.android;

import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import backend.PassGenerator;
import backend.humanizer.IHumanizer;
import backend.humanizer.MarkovEnglishHumanizer;
import backend.seedgenerator.ISeedGenerator;
import backend.seedgenerator.SHASeedGenerator;

import com.p4553d.youshallpass.R;

/**
 * @author p4553d
 * 
 */
public class YouShallPassActivity extends Activity {
    private PassGenerator m_generator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	InputStream inputStream = getResources().openRawResource(R.raw.english);

	IHumanizer h = new MarkovEnglishHumanizer(inputStream);
	ISeedGenerator sg = null;
	try {
	    sg = new SHASeedGenerator(5);
	    m_generator = new PassGenerator(sg, h);
	} catch (Exception e) {
	    e.printStackTrace();
	    finish();
	}

	setContentView(R.layout.passview);
    }

    public void generate(View v) {
	generatePassword();
    }

    public void generateAndCopy(View v) {
	generatePassword();
	copyToBuffer();
    }

    private String getMaster() {
	EditText etmp = (EditText) findViewById(R.id.editTextMasterPassword);
	return etmp.getText().toString();
    }

    private String getMasterRpt() {
	EditText etmp = (EditText) findViewById(R.id.editTextMasterPasswordRepeat);
	return etmp.getText().toString();
    }

    private String getSiteName() {
	EditText etmp = (EditText) findViewById(R.id.editTextSiteName);
	return etmp.getText().toString();
    }

    private void setPassword(String value) {
	EditText etmp = (EditText) findViewById(R.id.editTextPassword);
	etmp.setText(value);
    }

    private void generatePassword() {

	boolean error = false; // not yet
	String output = "+ + +";

	// get input
	String master = getMaster();
	String masterRpt = getMasterRpt();

	String siteName = getSiteName();

	// check plausibility
	if (master.isEmpty() || siteName.isEmpty()) { // neither master, nor site should be empty
	    error = true;
	    output = "Provide both, master password and site name!";
	}

	if (!masterRpt.isEmpty() && !masterRpt.equals(master)) { // if master repeat provided, have to be equal
	    error = true;
	    output = "Master password and confirmation differ!";
	}

	// TODO: master length?

	// generate password
	if (!error) {
	    output = m_generator.generatePassword(master, siteName);
	}

	// output
	setPassword(output);
    }

    private void copyToBuffer() {

    }

}

@@ -116,16 +116,54 @@
 Bundle;%0A
+import android.text.ClipboardManager;%0A
 import a
@@ -1097,16 +1097,21 @@
 assword(
+false
 );%0A    %7D
@@ -1176,25 +1176,12 @@
 ord(
-);%0A%09copyToBuffer(
+true
 );%0A 
@@ -1802,16 +1802,39 @@
 assword(
+boolean copyToClipboard
 ) %7B%0A%0A%09bo
@@ -2522,35 +2522,129 @@
 e);%0A
-%09%7D%0A%0A%09// output%0A%09setPassword
+%0A%09    if (copyToClipboard) %7B%0A%09%09ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);%0A%09%09cm.setText
 (out
@@ -2653,55 +2653,56 @@
 t);%0A
+%09
     %7D%0A
-%0A    private void copyToBuffer() %7B%0A
+%09%7D%0A%0A%09// output%0A%09setPassword(output);
 %0A    %7D%0A
-%0A
 %7D%0A

Copy to clipboard added