Jakizu.java :  » App » jakizu » com » jakizu » Android Open Source

Android Open Source » App » jakizu 
jakizu » com » jakizu » Jakizu.java
package com.jakizu;

import java.util.ArrayList;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.xml.Centro;
import com.xml.RequestCentros;
//import com.google.android.maps.MyLocationOverlay;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ZoomControls;
import android.widget.AdapterView.OnItemClickListener;

public class Jakizu extends MapActivity {
    /** Called when the activity is first created. */
  static final int PROGRESS_DIALOG = 0;
  private List<Overlay> mapOverlays;
  private HelloItemizedOverlay itemizedOverlay;
  private MyLocationOverlay myLocationOverlay;
  private LocationManager locationManager;
  private String locationProvider;
  private LinearLayout zoomLayout;
  private MapController mapController;
  private ZoomControls mZoom;
  public static String last_url;
  public static ArrayList<Centro> m_centros = null;
  public static ArrayList<Centro> m_centros_view = null;
  public static int centroIndex = 0;
  public static Double lat;
  public static Double lng;
    private IconListViewAdapter m_adapter;
    private MapView gMapView;
    private ListView listView;
    private Intent centroView;
    private ProgressDialog progressDialog;
    private RequestCentros requestCentros;
    private boolean farmacias = true, centros = true, consultorios = true, ambulatorios = true, hospitales = true;
    private boolean first = true;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); 
        
    setupLocation();

    createList();

    createMap();

    progressDialog.dismiss();
    }
    
    @Override
    public void onResume() {
      super.onResume();
      
      loadList();
    }
    
    private void loadList() {
      m_centros_view.clear();
    for (int i = 0; i < m_centros.size(); i++) {
      Centro centro = m_centros.get(i);
      if(centro.getPatrocinado() != null) {
        if(centro.getTipo().equals("1") && farmacias) {
          m_centros_view.add(centro);
        } else if(centro.getTipo().equals("2") && centros) {
          m_centros_view.add(centro);
        } else if(centro.getTipo().equals("3") && consultorios) {
          m_centros_view.add(centro);
        } else if(centro.getTipo().equals("4") && ambulatorios) {
          m_centros_view.add(centro);
        } else if(centro.getTipo().equals("5") && hospitales) {
          m_centros_view.add(centro);
        }
      }
    }
    for (int i = 0; i < m_centros.size(); i++) {
      Centro centro = m_centros.get(i);
      if(centro.getPatrocinado() == null) {
        if(centro.getTipo().equals("1") && farmacias) {
          m_centros_view.add(centro);
        } else if(centro.getTipo().equals("2") && centros) {
          m_centros_view.add(centro);
        } else if(centro.getTipo().equals("3") && consultorios) {
          m_centros_view.add(centro);
        } else if(centro.getTipo().equals("4") && ambulatorios) {
          m_centros_view.add(centro);
        } else if(centro.getTipo().equals("5") && hospitales) {
          m_centros_view.add(centro);
        }
      }
    }
      m_adapter.clear();
      if(m_centros_view.isEmpty()) {
        listView.setEnabled(false);
      } else {
        listView.setEnabled(true);
        
        for (int i = 0; i < m_centros_view.size(); i++) {
        m_adapter.add(m_centros_view.get(i));
      }
        m_adapter.notifyDataSetChanged();
      itemizedOverlay = new HelloItemizedOverlay(this.getResources().getDrawable(R.drawable.map_pin));
      if(!m_centros_view.isEmpty()) {
            for(int i = 0;i < m_centros_view.size(); i++) {
              Centro centro = m_centros_view.get(i);
              GeoPoint point = new GeoPoint(Integer.valueOf(centro.getLat().replace(".", "")),Integer.valueOf(centro.getLon().replace(".", "")));
              OverlayItem overlayitem = new OverlayItem(point, "", "");
              itemizedOverlay.addOverlay(overlayitem);
            }
      }
          mapOverlays.clear();
          mapOverlays.add(myLocationOverlay);
          mapOverlays.add(itemizedOverlay);
          
          listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View v, int position,
            long id) {
          mapController.setCenter(itemizedOverlay.getItem(position).getPoint());
          centroView = new Intent(Jakizu.this, CentroView.class);
          centroIndex = position;
          startActivity(centroView);
        }});
          
          if(first) {
            GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
          mapController.animateTo(point);
            first = false;
          } else {
            mapController.setCenter(itemizedOverlay.getItem(centroIndex).getPoint());
          }
      }
    }
    
    private void createList() {
      listView = (ListView) findViewById(R.id.list);
      m_centros = new ArrayList<Centro>();
      m_centros_view = new ArrayList<Centro>();
        m_adapter = new IconListViewAdapter(this, R.layout.iconrow, m_centros);
        listView.setAdapter(this.m_adapter);
        
        progressDialog = ProgressDialog.show(Jakizu.this, "", 
                "Cargando. Espere por favor...", true);
        last_url = "http://medical.jakizu.es/nagusia/index/"+String.valueOf(lat.intValue()/1E6)+"/"+String.valueOf(lng.intValue()/1E6)+"/";
        requestCentros = new RequestCentros("http://medical.jakizu.es/nagusia/index/"+String.valueOf(lat.intValue()/1E6)+"/"+String.valueOf(lng.intValue()/1E6)+"/0.xml");
        requestCentros.search();
    }
    
    private void createMap() {
      gMapView = (MapView) findViewById(R.id.myGMap);
      zoomLayout = (LinearLayout) findViewById(R.id.zoomview);
      
        mZoom = (ZoomControls) gMapView.getZoomControls();
        zoomLayout.addView(mZoom);
        mapOverlays = gMapView.getOverlays();
        
        myLocationOverlay = new MyLocationOverlay(this, gMapView);
      mapOverlays.add(myLocationOverlay);
      myLocationOverlay.enableMyLocation();
        
        mapController = gMapView.getController();
        mapController.setZoom(15);
    }
    
    private void setupLocation() {

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

      Criteria criteria = new Criteria();
      criteria.setAccuracy(Criteria.ACCURACY_FINE);
      criteria.setAltitudeRequired(false);
      criteria.setBearingRequired(false);
      criteria.setCostAllowed(true);
      criteria.setPowerRequirement(Criteria.POWER_LOW);

      locationProvider = locationManager.getBestProvider(criteria, true);
      if (!locationManager.isProviderEnabled(locationProvider))
        this.finish();
      Location location = locationManager
          .getLastKnownLocation(locationProvider);
      updateWithNewLocation(location);

      locationManager.requestLocationUpdates(locationProvider, 2000, 10,
          locationListener);
    }    
    
    private void updateWithNewLocation(Location location) {
      if (location != null) {
        lat = location.getLatitude()*1E6;
        lng = location.getLongitude()*1E6;
      }
    }
    
  private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider) {
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
  };
    
  @Override
  protected boolean isRouteDisplayed() {
    // Auto-generated method stub
    return false;
  }
  
  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    
    MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main_menu, menu);
        
        return true;
  }
  
  public boolean onOptionsItemSelected(MenuItem item) {
        // Handle all of the possible menu actions.
        switch (item.getItemId()) {
        case R.id.filtrar:
          item.getSubMenu().setGroupCheckable(R.id.filter_menu, true, true);
          item.getSubMenu().setGroupVisible(R.id.filter_menu, true);
            break;
        case R.id.todos:
        case R.id.hospitales:
        case R.id.farmacias:
        case R.id.centros:
        case R.id.consultorios:
        case R.id.ambulatorios:
          filtrar(item);
          break;
        case R.id.buscar:
          onSearchRequested();
            break;
        case R.id.ubicacion:
          lat = locationManager.getLastKnownLocation(locationProvider).getLatitude()*1E6;
        lng = locationManager.getLastKnownLocation(locationProvider).getLongitude()*1E6;
        GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
        mapController.animateTo(point);
          break;
        case R.id.about:
          createAbout();
            break;
        case R.id.quit:
          createAreYouSure();
          break;
        }
        return super.onOptionsItemSelected(item);
    }
  
  private void filtrar(MenuItem item) {
    String tipo = "0";
    
    farmacias = false;
    hospitales = false;
    ambulatorios = false;
    centros = false;
    consultorios = false;
    
    item.setChecked(true);
    switch(item.getItemId()) {
    case R.id.todos: 
      farmacias = true;
      hospitales = true;
      ambulatorios = true;
      centros = true;
      consultorios = true;
      break;
    case R.id.farmacias: farmacias = item.isChecked(); tipo = "1"; break;
    case R.id.hospitales: hospitales = item.isChecked(); tipo = "5"; break;
    case R.id.ambulatorios: ambulatorios = item.isChecked(); tipo = "4"; break;
    case R.id.centros: centros = item.isChecked(); tipo = "2"; break;
    case R.id.consultorios: consultorios = item.isChecked(); tipo = "3"; break;
    }
    requestCentros = new RequestCentros(last_url+tipo+".xml");
        requestCentros.search();
    loadList();
  }
  
  private void createAreYouSure() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(getString(R.string.areyousure))
           .setCancelable(false)
           .setPositiveButton(getString(R.string.areyousure_yes), new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            Jakizu.this.finish();
          }
        }).setNegativeButton(getString(R.string.areyousure_no),
        new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
          }
        });

    AlertDialog alert = builder.create();
    alert.show();
  }

  private void createAbout() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = (LayoutInflater) getSystemService("layout_inflater");
    View layout = inflater.inflate(R.layout.acerca_de,
                                   (ViewGroup) findViewById(R.id.layout_root));
    builder.setTitle(getString(R.string.acercade_title));
    builder.setIcon(R.drawable.jakizu_alpha48x48);
    builder.setCancelable(false)
           .setNegativeButton(getString(R.string.acercade_close), new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    builder.setView(layout);
    AlertDialog alert = builder.create();
    alert.show();

  }
  
  public class IconListViewAdapter extends ArrayAdapter<Centro> {

    public ArrayList<Centro> items;

    public IconListViewAdapter(Context context, int textViewResourceId,
        ArrayList<Centro> items) {
      super(context, textViewResourceId, items);
      this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View v = convertView;
      
      Centro centro = items.get(position);
      if (centro != null) {
        if(centro.getPatrocinado() != null) {
          LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = vi.inflate(R.layout.iconrowpatr, null);
        } else if(centro.getHorario() != null) {
          LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = vi.inflate(R.layout.iconrowguard, null);
        } else {
          LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          v = vi.inflate(R.layout.iconrow, null);
        }
        // poblamos la lista de elementos

        TextView tt = (TextView) v.findViewById(R.id.row_toptext);
        TextView bt = (TextView) v.findViewById(R.id.row_bottomtext);
        ImageView im = (ImageView) v.findViewById(R.id.icon);
        
        if (im != null) {
          switch(Integer.valueOf(centro.getTipo())) {
            case 1: im.setImageResource(R.drawable.icono_farmacia);
                break;
            case 2: im.setImageResource(R.drawable.centro);
                break;
            case 3: im.setImageResource(R.drawable.consultorio);
                break;
            case 4: im.setImageResource(R.drawable.ambulatorio);
                break;
            case 5: im.setImageResource(R.drawable.hospital);
                break;
          }
        }
        if (tt != null) {
          switch(Integer.valueOf(centro.getTipo())) {
          case 1: if(centro.getHorario() == null) centro.setNombre(getString(R.string.tipo_farmacia));
              else centro.setNombre(getString(R.string.tipo_farmaciadeguardia)); break;
          case 2: centro.setNombre(getString(R.string.tipo_centrodesalud)); break;
          case 3: centro.setNombre(getString(R.string.tipo_consultorio)); break;
          case 4: centro.setNombre(getString(R.string.tipo_ambulatorio)); break;
          case 5: centro.setNombre(getString(R.string.tipo_hospital)); break;
          }
          tt.setText(centro.getNombre()+" - "+centro.getDireccion());
        }
        if (bt != null) {
          if((centro.getVal_pos() == null) && (centro.getVal_neg() == null) ) {
            bt.setText(getString(R.string.votos_null));
          } else {  
            bt.setText(getString(R.string.votos_positivos)+centro.getVal_pos()+" "+getString(R.string.votos_negativos)+centro.getVal_neg());
          }
        }
      }
      return v;
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.