package br.com.caelum.cadastro;
import java.util.List;
import br.com.caelum.cadastro.adapter.ListaAlunosAdapter;
import br.com.caelum.cadastro.dao.AlunoDAO;
import br.com.caelum.cadastro.modelo.Aluno;
import br.com.caelum.cadastro.task.EnviarAlunosTask;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class ListaAlunos extends Activity {
private Aluno aluno;
private ListView lista;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listagem_alunos);
lista = (ListView) findViewById(R.id.lista);
registerForContextMenu(lista);
lista.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id) {
Aluno alunoClicado = (Aluno) adapter.getItemAtPosition(posicao);
Intent edicao = new Intent(ListaAlunos.this, Formulario.class);
Aluno alunoSelecionado = (Aluno) lista.getItemAtPosition(posicao);
edicao.putExtra(Extras.ALUNO_SELECIONADO, alunoSelecionado);
startActivity(edicao);
}
});
lista.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View view, int posicao, long id) {
aluno = (Aluno) adapter.getItemAtPosition(posicao);
return false;
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
MenuItem ligar = menu.add("Ligar");
ligar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent irParaTelaDeDiscagem = new Intent(Intent.ACTION_CALL);
Uri discarPara = Uri.parse("tel: " + aluno.getTelefone());
irParaTelaDeDiscagem.setData(discarPara);
startActivity(irParaTelaDeDiscagem);
return false;
}
});
menu.add("Enviar SMS");
MenuItem site = menu.add("Navegar no site");
site.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Intent irParaOSite = new Intent(Intent.ACTION_VIEW);
Uri localSite = Uri.parse("http://"+aluno.getSite());
irParaOSite.setData(localSite);
startActivity(irParaOSite);
return false;
}
});
MenuItem deletar = menu.add("Deletar");
deletar.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlunoDAO dao = new AlunoDAO(ListaAlunos.this);
dao.deletar(aluno);
dao.close();
carregaLista();
return false;
}
});
menu.add("Ver mapa");
menu.add("Enviar email");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
protected void onResume() {
super.onResume();
carregaLista();
}
private void carregaLista(){
AlunoDAO dao = new AlunoDAO(this); List alunos = dao.getLista();
dao.close();
ListaAlunosAdapter adapter = new ListaAlunosAdapter(alunos, this);
lista.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.lista_alunos, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemClicado = item.getItemId();
switch (itemClicado) {
case R.id.novo:
Intent irParaFormulario = new Intent(this, Formulario.class);
startActivity(irParaFormulario);
break;
case R.id.receber_provas:
Intent irParaProvas = new Intent(this, Provas.class); startActivity(irParaProvas);
break;
case R.id.enviar_alunos:
EnviarAlunosTask task = new EnviarAlunosTask(this);
task.execute();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}