@Override
  public void onLocationChanged(Location location) {
    Logcat.d(
        "Geolocation.onLocationChanged(): "
            + location.getProvider()
            + " / "
            + location.getLatitude()
            + " / "
            + location.getLongitude()
            + " / "
            + new Date(location.getTime()).toString());

    // check location age
    long timeDelta = System.currentTimeMillis() - location.getTime();
    if (timeDelta > LOCATION_AGE) {
      Logcat.d("Geolocation.onLocationChanged(): gotten location is too old");
      // gotten location is too old
      return;
    }

    // return location
    mCurrentLocation = new Location(location);
    stop();
    GeolocationListener listener = mListener.get();
    if (listener != null && location != null)
      listener.onGeolocationRespond(Geolocation.this, mCurrentLocation);
  }
 @Override
 public void onStatusChanged(String provider, int status, Bundle extras) {
   Logcat.d("Geolocation.onStatusChanged(): " + provider);
   switch (status) {
     case LocationProvider.OUT_OF_SERVICE:
       Logcat.d("Geolocation.onStatusChanged(): status OUT_OF_SERVICE");
       break;
     case LocationProvider.TEMPORARILY_UNAVAILABLE:
       Logcat.d("Geolocation.onStatusChanged(): status TEMPORARILY_UNAVAILABLE");
       break;
     case LocationProvider.AVAILABLE:
       Logcat.d("Geolocation.onStatusChanged(): status AVAILABLE");
       break;
   }
 }
 public void stop() {
   Logcat.d("Geolocation.stop()");
   if (mTimer != null) mTimer.cancel();
   if (mLocationManager != null) {
     mLocationManager.removeUpdates(this);
     mLocationManager = null;
   }
 }
  // should be called in Activity.onStop()
  public void stopAll() {
    Logcat.d("SoundManager.stopAll(): " + mMediaMap.size());

    Collection<MediaPlayer> collection = mMediaMap.values();
    Iterator<MediaPlayer> iterator = collection.iterator();

    while (iterator.hasNext()) {
      MediaPlayer mediaPlayer = iterator.next();
      if (mediaPlayer != null) {
        Logcat.d("SoundManager.stopAll(): release");
        if (mediaPlayer.isPlaying()) mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null;
      }
    }

    mMediaMap.clear();
    printMediaList();
  }
  @Override
  protected void onHandleIntent(Intent intent) {
    // intent may be null if the service is being restarted
    if (intent == null) return;

    Logcat.d("IntentService.onHandleIntent(): " + intent.getIntExtra("arg", -1));

    long endTime = System.currentTimeMillis() + 5l * 1000l;
    while (System.currentTimeMillis() < endTime) {
      synchronized (this) {
        try {
          // TODO: do something
          wait(endTime - System.currentTimeMillis());
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
  // returns last known freshest location from network or GPS
  private Location getLastKnownLocation(LocationManager locationManager) {
    Logcat.d("Geolocation.getLastKnownLocation()");

    Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    Location locationGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    long timeNet = 0l;
    long timeGps = 0l;

    if (locationNet != null) {
      timeNet = locationNet.getTime();
    }

    if (locationGps != null) {
      timeGps = locationGps.getTime();
    }

    if (timeNet > timeGps) return locationNet;
    else return locationGps;
  }
  private void playSound(final String path, AssetFileDescriptor assetFileDescriptor) {
    // stop all currently playing sounds
    if (mMode.equals(Mode.PLAY_SINGLE)) {
      stopAll();
    }

    // sound already playing
    if (mMediaMap.containsKey(path)) {
      Logcat.d("SoundManager.play(): sound is already playing");
      printMediaList();
      return;
    }

    // stop all currently playing sounds
    if (mMode.equals(Mode.PLAY_SINGLE_CONTINUE)) {
      stopAll();
    }

    // init media player
    MediaPlayer mediaPlayer;
    try {
      Logcat.d("SoundManager.prepareAsync(): " + mMediaMap.size());
      mediaPlayer = new MediaPlayer();
      mMediaMap.put(path, mediaPlayer);

      // data source
      if (assetFileDescriptor != null) {
        mediaPlayer.setDataSource(
            assetFileDescriptor.getFileDescriptor(),
            assetFileDescriptor.getStartOffset(),
            assetFileDescriptor.getLength());
      } else {
        mediaPlayer.setDataSource(path);
      }

      mediaPlayer.prepareAsync();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
      return;
    } catch (IllegalStateException e) {
      e.printStackTrace();
      return;
    } catch (IOException e) {
      e.printStackTrace();
      return;
    }

    // play sound
    mediaPlayer.setOnPreparedListener(
        new MediaPlayer.OnPreparedListener() {
          @Override
          public void onPrepared(MediaPlayer mediaPlayer) {
            Logcat.d("SoundManager.onPrepared(): " + mMediaMap.size());
            mediaPlayer.start();
          }
        });

    // release media player
    mediaPlayer.setOnCompletionListener(
        new MediaPlayer.OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mediaPlayer) {
            Logcat.d("SoundManager.onCompletion(): " + mMediaMap.size());
            mMediaMap.remove(path);
            if (mediaPlayer != null) {
              mediaPlayer.release();
              mediaPlayer = null;
            }
            printMediaList();
          }
        });
  }
 public void printMediaList() {
   Logcat.d("SoundManager.printMediaList(): " + mMediaMap.size());
 }
  public ExampleIntentService() {
    super("ExampleIntentService");
    setIntentRedelivery(false); // TODO: START_NOT_STICKY if false, START_REDELIVER_INTENT if true

    Logcat.d("IntentService.IntentService()");
  }
  @Override
  public void onDestroy() {
    Logcat.d("IntentService.onDestroy()");

    super.onDestroy();
  }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Logcat.d("IntentService.onStartCommand()");

    return super.onStartCommand(intent, flags, startId);
  }
  @Override
  public void onCreate() {
    Logcat.d("IntentService.onCreate()");

    super.onCreate();
  }
 @Override
 public void onProviderEnabled(String provider) {
   Logcat.d("Geolocation.onProviderEnabled(): " + provider);
 }