package br.com.intellectus.agenda;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.util.List;
import br.com.intellectus.agenda.dao.AlunoDAO;
import br.com.intellectus.agenda.modelo.Aluno;
import static br.com.intellectus.agenda.R.id.lista_alunos;
public class ListaAlunosActivity extends AppCompatActivity {
private ListView listaAlunos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_alunos);
listaAlunos = (ListView) findViewById(R.id.lista_alunos);
listaAlunos.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> lista, View item, int position, long id) {
Aluno aluno = (Aluno) listaAlunos.getItemAtPosition(position);
Intent intentVaiProFormuIario = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
intentVaiProFormuIario.putExtra("aluno", aluno);
startActivity(intentVaiProFormuIario);
}
});
Button novoAluno = (Button) findViewById(R.id.novo_aluno);
novoAluno.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
Intent intentVaiProFormulario = new Intent(ListaAlunosActivity.this, FormularioActivity.class);
startActivity(intentVaiProFormulario);
}
});
registerForContextMenu(listaAlunos);
}
private void carregaLista() {
AlunoDAO dao = new AlunoDAO(this);
List<Aluno> alunos = dao.buscaAlunos();
dao.close();
ArrayAdapter<Aluno> adapter = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos);
listaAlunos.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
carregaLista();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
final Aluno aluno = (Aluno) listaAlunos.getItemAtPosition(info.position);
MenuItem itemLigar = menu.add("Ligar");
itemLigar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (ActivityCompat.checkSelfPermission(ListaAlunosActivity.this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ListaAlunosActivity.this,String[][Manifest.permission.CALL_PHONE],123);
} else {
Intent intentLigar = new Intent(Intent.ACTION_CALL);
intentLigar.setData(Uri.parse("tel:" + aluno.getTelefone()));
startActivity(intentLigar);
}
return false;
}
});
MenuItem itemSMS = menu.add("Enviar SMS");
Intent intentSMS = new Intent(Intent.ACTION_VIEW) ;
intentSMS.setData(Uri.parse("sms:" + aluno.getTelefone()));
itemSMS.setIntent(intentSMS);
MenuItem itemMapa = menu.add("Visualizar no mapa");
Intent intentMapa = new Intent(Intent.ACTION_VIEW);
intentMapa.setData(Uri.parse("geo:0,0?q=" + aluno.getEndereco()));
itemMapa.setIntent(intentMapa);
MenuItem itemSite = menu.add("Visitar site");
Intent intentSite = new Intent(Intent.ACTION_VIEW);
String site = aluno.getSite();
if (!site.startsWith("http://")) {
site = "http://" + site;
}
intentSite.setData(Uri.parse(site));
itemSite.setIntent(intentSite);
MenuItem deletar = menu.add("Deletar");
deletar.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
AlunoDAO dao = new AlunoDAO(ListaAlunosActivity.this);
dao.deleta(aluno);
dao.close();
carregaLista();
return false;
}
});
}
}
/Users/administrador/AndroidStudioProjects/Agenda/app/src/main/java/br/com/intellectus/agenda/ListaAlunosActivity.java:88: error: ']' expected ActivityCompat.requestPermissions(ListaAlunosActivity.this,String[][Manifest.permission.CALL_PHONE],123); ^ /Users/administrador/AndroidStudioProjects/Agenda/app/src/main/java/br/com/intellectus/agenda/ListaAlunosActivity.java:88: error: illegal start of expression ActivityCompat.requestPermissions(ListaAlunosActivity.this,String[][Manifest.permission.CALL_PHONE],123); ```