protected void onResume() {
   super.onResume();
   Log.d(TAG, Boolean.toString(animateToTaskPosition));
   registerReceiver(receiver, filter);
   if (animateToTaskPosition) {
     Task task = global_app.getDataStorage().getTaskById(taskId);
     GeoPoint tmp =
         new GeoPoint((int) (task.getLatitude() * 1E6), (int) (task.getLongitude() * 1E6));
     mapController.animateTo(tmp);
   } else {
     lastLocation = global_app.getLastLocation();
     int lat = (int) (lastLocation.getLatitude() * 1E6);
     int lng = (int) (lastLocation.getLongitude() * 1E6);
     geoPoint = new GeoPoint(lat, lng);
     mapController.animateTo(geoPoint);
   }
   paintCurrentPostion();
   paintTasks();
 }
 public void paintTasks() {
   allOverlays = mapView.getOverlays();
   allOverlays.remove(tasksPositionOverlay);
   tasksPositionOverlay = new TasksOverlay(doneMarker, this);
   tasksCursor = global_app.getDataStorage().getAllTasks();
   DecimalFormat km = new DecimalFormat();
   km.setMaximumFractionDigits(2);
   DecimalFormat m = new DecimalFormat();
   m.setMaximumFractionDigits(1);
   while (tasksCursor.moveToNext()) {
     double lat = tasksCursor.getDouble(tasksCursor.getColumnIndex(DataStorage.C_LAT));
     double lon = tasksCursor.getDouble(tasksCursor.getColumnIndex(DataStorage.C_LON));
     String taskTitle = (tasksCursor.getString(tasksCursor.getColumnIndex(DataStorage.C_NAME)));
     int taskState = (tasksCursor.getInt(tasksCursor.getColumnIndex(DataStorage.C_STATE)));
     int taskId = (tasksCursor.getInt(tasksCursor.getColumnIndex(DataStorage.C_ID)));
     GeoPoint taskLocation = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));
     Location tmp = new Location(provider);
     tmp.setLatitude(lat);
     tmp.setLongitude(lon);
     double distance = lastLocation.distanceTo(tmp);
     String prettyDistance;
     if (distance > 1000) prettyDistance = km.format(distance / 1000) + "km";
     else prettyDistance = m.format(distance) + "m";
     OverlayItem taskOverlayItem =
         new OverlayItem(
             taskLocation,
             taskTitle + " (id: " + Integer.toString(taskId) + ")",
             "oddalone o: " + prettyDistance);
     Drawable marker;
     switch (taskState) {
       case 0:
         marker = cancelMarker;
         break;
       case 1:
         marker = pendingMarker;
         break;
       case 2:
         marker = currentMarker;
         break;
       default:
         marker = doneMarker;
         break;
     }
     int w = marker.getIntrinsicWidth();
     int h = marker.getIntrinsicHeight();
     marker.setBounds(-w / 2, -h, w / 2, 0);
     taskOverlayItem.setMarker(marker);
     tasksPositionOverlay.addOverlay(taskOverlayItem);
   }
   allOverlays.add(tasksPositionOverlay);
   mapView.invalidate();
   tasksCursor.close();
 }
  public boolean onOptionsItemSelected(MenuItem item) {
    Log.d(TAG, "menu item selected" + item.toString());
    switch (item.getItemId()) {
      case R.id.synchronise:
        Log.d(TAG, "synchronizacja");
        if (global_app.isNetworkOn())
          startService(new Intent(DefaultMapActivity.this, SynchronisationService.class));
        else Toast.makeText(this, R.string.dialog_text_no_network, Toast.LENGTH_SHORT).show();
        break;

      case R.id.preferences:
        Log.d(TAG, "ustawienia");
        startActivity(new Intent(this, PreferencesActivity.class));
        break;
    }
    return true;
  }
  public void paintCurrentPostion() {
    int lat = (int) ((new Double(lastLocation.getLatitude())) * 1E6);
    int lng = (int) ((new Double(lastLocation.getLongitude())) * 1E6);
    geoPoint = new GeoPoint(lat, lng);

    allOverlays = mapView.getOverlays();
    allOverlays.remove(updatablePositionOverlay);
    updatablePositionOverlay = new TasksOverlay(userMarker, this);
    updatablePositionOverlay.addOverlay(
        new OverlayItem(
            geoPoint,
            global_app.getUsername(),
            Double.toString(Double.valueOf(lastLocation.getLatitude()))
                + " "
                + Double.toString(Double.valueOf(lastLocation.getLongitude()))));
    allOverlays.add(updatablePositionOverlay);
    mapView.invalidate();
  }
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    global_app = (SupervisorApplication) getApplication();
    decimalFormat = new DecimalFormat("#.########");

    setContentView(R.layout.layout_default_map);
    searchButton = (Button) findViewById(R.id.search);
    searchButton.setOnClickListener(this);
    searchButton.setOnTouchListener(this);
    logo = (Button) findViewById(R.id.logo);
    logo.setOnClickListener(this);
    logo.setOnTouchListener(this);

    lastLocation = global_app.getLastLocation();
    // Log.d(TAG, "lastLocation: LAT: " + Double.toString(lastLocation.getLatitude()) + " LON: " +
    // Double.toString(lastLocation.getLongitude()));

    userMarker = getResources().getDrawable(R.drawable.me);
    currentMarker = getResources().getDrawable(R.drawable.current_marker);
    pendingMarker = getResources().getDrawable(R.drawable.pending_marker);
    doneMarker = getResources().getDrawable(R.drawable.done_marker);
    cancelMarker = getResources().getDrawable(R.drawable.cancel_marker);

    try {
      taskId = getIntent().getExtras().getLong("taskId");
      Log.d(TAG + "task id", Long.toString(taskId));
      animateToTaskPosition = true;
    } catch (NullPointerException e) {
      animateToTaskPosition = false;
    }

    mapView = (MapView) findViewById(R.id.map);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(false);

    mapController = mapView.getController();
    mapController.setZoom(16);

    receiver = new TaskUpdateReceiver();
    filter = new IntentFilter(SupervisorApplication.UPDATE_VIEW_INTENT);
    registerReceiver(receiver, filter);
  }