private void updateSolution(int state) {
   logger.info("Update Solution");
   currentState = state;
   if ((System.currentTimeMillis() - lastFixTimestamp) > 3000) {
     hasFix = false;
   }
   // locationUpdated(locationManager, locationManager.getLastKnownLocation(provider), true);
   if (state == LocationProvider.OUT_OF_SERVICE) {
     if (receiverList != null) {
       receiverList.receiveStatus(LocationMsgReceiver.STATUS_OFF, 0);
       receiverList.receiveMessage(
           Locale.get("androidlocationinput.ProviderStopped") /*provider stopped*/);
     }
     // some devices, e.g. Samsung Galaxy Note will claim LocationProvider.AVAILABLE
     // even when there's no fix, so use a timestamp to detect
   } else if (state == LocationProvider.TEMPORARILY_UNAVAILABLE || !hasFix) {
     if (receiverList != null) {
       receiverList.receiveStatus(LocationMsgReceiver.STATUS_NOFIX, numSatellites);
     }
   } else if (state == LocationProvider.AVAILABLE || hasFix) {
     if (receiverList != null) {
       receiverList.receiveStatus(LocationMsgReceiver.STATUS_ON, numSatellites);
     }
   }
 }
  public void locationUpdated(LocationManager manager, Location location, boolean lastKnown) {
    // #debug info
    logger.info("updateLocation: " + location);
    if (location == null) {
      return;
    }
    hasFix = true;
    if (currentState != LocationProvider.AVAILABLE) {
      updateSolution(LocationProvider.AVAILABLE);
    }
    lastFixTimestamp = System.currentTimeMillis();
    // #debug debug
    logger.debug("received Location: " + location);

    pos.latitude = (float) location.getLatitude();
    pos.longitude = (float) location.getLongitude();
    pos.altitude = (float) location.getAltitude();
    pos.course = location.getBearing();
    pos.speed = location.getSpeed();
    pos.timeMillis = location.getTime();
    pos.accuracy = location.getAccuracy();
    if (lastKnown) {
      pos.type = Position.TYPE_GPS_LASTKNOWN;
    } else {
      pos.type = Position.TYPE_GPS;
    }
    receiverList.receivePosition(pos);
    // logger.trace("exit locationUpdated(provider,location)");
  }
 public void onGpsStatusChanged(int state) {
   GpsStatus gpsStatus = locationManager.getGpsStatus(null);
   if (state == GpsStatus.GPS_EVENT_STOPPED) {
     hasFix = false;
     numSatellites = 0;
     updateSolution(LocationProvider.OUT_OF_SERVICE);
   } else if ((System.currentTimeMillis() - lastFixTimestamp) > 3000) {
     invalidateFix();
   }
   if (state == GpsStatus.GPS_EVENT_STARTED) {
     // FIXME do what's needed
     updateSolution(LocationProvider.AVAILABLE);
   }
   gpsState = state;
   if (state == GpsStatus.GPS_EVENT_SATELLITE_STATUS && gpsStatus != null) {
     for (int j = 0; j < 36; j++) {
       /** Resetting all the satellites to non locked */
       if ((satellites[j] != null)) {
         satellites[j].isLocked(false);
       }
     }
     Iterable<GpsSatellite> andSatellites = gpsStatus.getSatellites();
     Iterator<GpsSatellite> sat = andSatellites.iterator();
     int i = 0;
     while (sat.hasNext()) {
       GpsSatellite satellite = sat.next();
       if (satellites[i] == null) {
         satellites[i] = new Satellite();
       }
       if (i < 36) {
         satellites[i].isLocked(satellite.usedInFix());
         satellites[i].id = i;
         satellites[i].azimut = satellite.getAzimuth();
         satellites[i].elev = satellite.getElevation();
         satellites[i].snr = (int) satellite.getSnr();
         if (satellite.usedInFix()) {
           i++;
         }
       }
     }
     numSatellites = i;
     if (numSatellites > 0) {
       receiverList.receiveSatellites(satellites);
     }
   }
   // updateSolution(state);
 }
 public void close() {
   // #debug
   logger.trace("enter close()");
   // if (locationManager != null){
   // locationManager.setLocationListener(null, -1, -1, -1);
   // }
   if (locationManager != null) {
     locationManager.removeUpdates(this);
     locationManager.removeGpsStatusListener(this);
     locationManager.removeNmeaListener(this);
     if (looperThread != null) {
       locationLooper.quit();
     }
   }
   locationManager = null;
   receiverList.locationDecoderEnd();
   // #debug
   logger.trace("exit close()");
 }
 public boolean removeLocationMsgReceiver(LocationMsgReceiver receiver) {
   return receiverList.removeReceiver(receiver);
 }
 public void addLocationMsgReceiver(LocationMsgReceiver receiver) {
   receiverList.addReceiver(receiver);
 }
  /**
   * Initializes LocationProvider uses default criteria
   *
   * @throws Exception
   */
  void createLocationProvider() {
    logger.trace("enter createLocationProvider()");
    if (locationManager == null) {
      locationManager =
          (LocationManager) MidletBridge.instance.getSystemService(Context.LOCATION_SERVICE);
      // try out different locationprovider criteria combinations, the
      // ones with maximum features first
      for (int i = 0; i <= 3; i++) {
        try {
          Criteria criteria = new Criteria();
          switch (i) {
            case 0:
              criteria.setAccuracy(Criteria.ACCURACY_FINE);
              criteria.setAltitudeRequired(true);
              criteria.setBearingRequired(true);
              criteria.setSpeedRequired(true);
              break;
            case 1:
              criteria.setAccuracy(Criteria.ACCURACY_FINE);
              criteria.setBearingRequired(true);
              criteria.setSpeedRequired(true);
              break;
            case 2:
              criteria.setAccuracy(Criteria.ACCURACY_FINE);
              criteria.setAltitudeRequired(true);
          }
          provider = locationManager.getBestProvider(criteria, true);
          if (provider != null) {
            logger.info("Chosen location manager:" + locationManager);
            savedCriteria = criteria;
            break; // we are using this criteria
          }
        } catch (Exception e) {
          logger.exception(
              Locale.get(
                  "androidlocationinput.unexpectedExceptioninLocProv") /*unexpected exception while probing LocationManager criteria.*/,
              e);
        }
      }
      if (locationManager != null && provider != null) {
        try {
          locationManager.requestLocationUpdates(provider, 0, 0, this);
          locationManager.addGpsStatusListener(this);
          locationManager.addNmeaListener(this);
        } catch (Exception e) {
          logger.fatal("requestLocationUpdates fail: " + e.getMessage());

          receiverList.receiveStatus(LocationMsgReceiver.STATUS_SECEX, 0);
          locationManager = null;
        }
        if (locationManager != null) {
          // FIXME
          updateSolution(LocationProvider.TEMPORARILY_UNAVAILABLE);
        }

      } else {
        receiverList.locationDecoderEnd(
            Locale.get("androidlocationinput.nointprovider") /*no internal location provider*/);
        // #debug info
        logger.info("Cannot create LocationProvider for criteria.");
      }
    }
    // #debug
    logger.trace("exit createLocationProvider()");
  }