quinta-feira, 9 de abril de 2015

BaseActivity

package br.org.curitiba.lapuinka.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;



import android.widget.TextView;
import android.widget.Toast;



abstract public class BaseActivity  extends Activity{
    protected ProgressDialog pleaseWaitDialog;
    protected GPSController gps;
    public Location getLocalizacao() {
        if(gps==null)
            gps = new GPSController(this);

        Location location = new Location("");
        location.setLatitude(0);
        location.setLongitude(0);

        if (AppConfig.GPS_SIMULADOR) {
            location.setLatitude(AppConfig.GPS_SIMULADOR_LATITUDE);
            location.setLongitude(AppConfig.GPS_SIMULADOR_LONGITUDE);
        }
        else {
            if (gps.canGetLocation()) {
                location.setLatitude(gps.getLatitude());
                location.setLongitude(gps.getLongitude());
            }
        }
        return location;
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
    @Override
    public void onStart() {
        super.onStart();
        ((ApplicacaoController) getApplicationContext()).listActivity.add(this);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        ((ApplicacaoController) getApplicationContext()).listActivity.remove(this);
    }
    public ApplicacaoController getApplicacao()
    {
        return ((ApplicacaoController)getApplicationContext());
    }
    public abstract  void onResponse(Response mensagem);

    public Object viewToObject(){
        return null;
    }
    public void objectToView(Object objeto){
    }
    public void UpdateObject(){
    }

    public static String TratamentoMensagem(Response mensagem)
    {
        String retorno;

        if(mensagem.Description!=null) {
            if (mensagem.Description.contains(Constants.ERROR_LOGIN_SENHA_INVALIDO)) {
                retorno= Constants.MSG_USUARIOSENHAINVALIDO;
            } else if (mensagem.Description.contains(Constants.ERROR_LOGIN_CARTAO_INATIVO)) {
                retorno= Constants.MSG_CARTAOBLOQUEADO;
            } else if (mensagem.Description.contains(Constants.ERROR_SENHA_NAO_CADASTRADA)) {
                retorno= Constants.MSG_CARTAONAOCADASTRADA;
            } else if (mensagem.Description.contains(Constants.ERROR_FALHA_CONEXAO)){
                retorno= Constants.MSG_FALHA_CONEXAO;
            }
            else if (mensagem.Description.contains(Constants.ERROR_CRIPTOGRAFIAINVALIDA)){
                retorno= Constants.MSG_CRIPTOGRAFIAINVALIDA;
            }
            else if (mensagem.Description.contains(Constants.ERROR_BASE64ERROR)){
                retorno= Constants.MSG_BASE64ERROR;
            }
            else if (mensagem.Description.contains(Constants.ERROR_TIMEOUT)){
                retorno= Constants.MSG_TIMEOUT;
            }
            else
                retorno= mensagem.Description;
        }
        else
        {
            retorno = Constants.STRING_VAZIA;
        }
        return retorno;
    }
    public void ToolTip(String Mensagem){
        Toast.makeText(this, Mensagem, Toast.LENGTH_LONG).show();
    }
    public static void MonstraMensagem(final Activity activity, String Mensagem, final String Category){
        if(!Mensagem.equals(Constants.STRING_VAZIA)) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity);
            alertDialogBuilder.setTitle(Constants.STRING_TITULO_APLICACAO);
            alertDialogBuilder
                    .setMessage(Mensagem)
                    .setCancelable(false)
                    .setPositiveButton(Constants.STRING_OK, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            try {
                                Response response = new Response(Category);
                                response.Description = Constants.STRING_OK;
                                ((IResponse) activity).onResponse(response);
                            } catch (Exception e) {
                                Log.e(Constants.STRING_OK, e.getMessage());
                            }
                        }
                    });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();
        }
    }

    public static void ConfirmaMensagem(final Activity activity, final Response Mensagem){
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage(Mensagem.Description);
        builder.setPositiveButton(Constants.STRING_SIM, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface arg0, int arg1)
            {
                Mensagem.Description = Constants.STRING_SIM;
                ((IResponse)activity).onResponse(Mensagem);
            }
        });
        builder.setNegativeButton(Constants.STRING_NAO, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Mensagem.Description = Constants.STRING_NAO;
                Mensagem.Error = true;
                ((IResponse)activity).onResponse(Mensagem);
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    public static void ConfirmaMensagem(final Activity activity, String Mensagem,final String Category){
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage(Mensagem);
        builder.setPositiveButton(Constants.STRING_SIM, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface arg0, int arg1)
            {
                Response mensagem = new Response(Category);
                mensagem.Description = Constants.STRING_SIM;
                ((IResponse)activity).onResponse(mensagem);
            }
        });
        builder.setNegativeButton(Constants.STRING_NAO, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                Response mensagem = new Response(Category);
                mensagem.Description = Constants.STRING_NAO;
                mensagem.Error = true;
                ((IResponse)activity).onResponse(mensagem);
            }
        });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();

    }

    public void MostrarCarregando(Boolean mostrar){
        try {
            if (mostrar) {
                if (this.pleaseWaitDialog == null)
                    this.pleaseWaitDialog =
                            ProgressDialog.show(this, Constants.STRING_AGUARDE, Constants.STRING_CARREGANDO, false);
            } else {
                if (this.pleaseWaitDialog != null) {
                    this.pleaseWaitDialog.dismiss();
                    this.pleaseWaitDialog = null;
                }
            }
        }
        catch(Exception e){
            Log.e("MostrarCarregando", e.getMessage());
        }
    }
  }
Postar um comentário