package br.com.alura.agenda;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;

import br.com.alura.agenda.dao.AlunoDAO;
import br.com.alura.agenda.modelo.Aluno;

public class FormularioActivity extends AppCompatActivity {

    public static final int CODIGO_CAMERA = 567;
    private FormularioHelper helper;
    private String caminhoFoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_formulario);

        helper = new FormularioHelper(this);

        Intent intent = getIntent();
        Aluno aluno = (Aluno) intent.getSerializableExtra("aluno");
        if (aluno != null) {
            helper.preencheFormulario(aluno);
        }

        Button botaoFoto = (Button) findViewById(R.id.formulario_botao_foto);
        botaoFoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                caminhoFoto = getExternalFilesDir(null) + "/" + System.currentTimeMillis() + ".jpg";
                File arquivoFoto = new File(caminhoFoto);
                intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(arquivoFoto));
                startActivityForResult(intentCamera, CODIGO_CAMERA);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CODIGO_CAMERA) {
                helper.carregaImagem(caminhoFoto);
            }
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_formulario, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_formulario_ok:
                Aluno aluno = helper.pegaAluno();

                AlunoDAO dao = new AlunoDAO(this);
                if (aluno.getId() != null) {
                    dao.altera(aluno);
                } else {
                    dao.insere(aluno);
                }
                dao.close();

                Toast.makeText(FormularioActivity.this, "Aluno " + aluno.getNome() + " salvo!", Toast.LENGTH_SHORT).show();

                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -633,16 +633,66 @@
 o.Aluno;
+%0Aimport br.com.alura.agenda.tasks.InsereAlunoTask;
 %0A%0Apublic
@@ -1346,24 +1346,61 @@
 otao_foto);%0A
+        if (botaoFoto != null) %7B%0A    
         bota
@@ -1461,24 +1461,28 @@
             
+    
 @Override%0A  
@@ -1487,24 +1487,28 @@
             
+ 
+   
 public void 
@@ -1521,24 +1521,28 @@
 k(View v) %7B%0A
+    
             
@@ -1612,16 +1612,20 @@
 PTURE);%0A
+    
         
@@ -1733,16 +1733,20 @@
         
+    
 File arq
@@ -1771,32 +1771,36 @@
 e(caminhoFoto);%0A
+    
                 
@@ -1890,16 +1890,20 @@
         
+  
+  
 startAct
@@ -1951,34 +1951,42 @@
 A);%0A            
-%7D%0A
+    %7D%0A    
         %7D);%0A    
@@ -1981,16 +1981,26 @@
     %7D);%0A
+        %7D%0A
     %7D%0A%0A 
@@ -2914,32 +2914,32 @@
               %7D%0A
-
                 
@@ -2948,24 +2948,69 @@
 o.close();%0A%0A
+                new InsereAlunoTask(aluno);%0A%0A
             

Call insert student async task when the action to create a new student is selected