Android Open Source - yousense-android-tracker Distance Cycled Gps






From Project

Back to project page yousense-android-tracker.

License

The source code is released under:

Energy-efficent motion and location tracker for Android. Based on Mattias's power and tracking work. I plan to release it as GPL, once I have a paper published that goes with it. Might also release i...

If you think the Android project yousense-android-tracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.linnap.locationtracker.gps;
//  w ww  .  j a  v a  2 s  .  c om
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;

import com.linnap.locationtracker.LocationTrackerService;
import com.linnap.locationtracker.SensorConfig;
import com.linnap.locationtracker.StateChange;

public class DistanceCycledGps implements LocationListener {

  public enum GpsState { OFF, LOW, HIGH, LOCK_HIGH };
  
  LocationTrackerService service;
  Handler handler;
  GpsMovementListener listener;
  LocationManager locationManager;
  GpsState state;  
  long lastGpsFixMillis;
  GpsHistory history;
  
  public DistanceCycledGps(LocationTrackerService service, GpsMovementListener listener) {
    this.service = service;
    this.handler = new Handler();
    this.listener = listener;
    this.locationManager = (LocationManager)service.getSystemService(Context.LOCATION_SERVICE);
    this.state = GpsState.OFF;
    this.lastGpsFixMillis = 0;
    this.history = new GpsHistory(service);
  }
  
  public interface GpsMovementListener {
    public void noGpsMovement();
    public void locationChanged(Location location);
  }
  
  public synchronized void switchToState(GpsState newState) {
    if (state != newState) {
      service.event("gps_state", new StateChange(state, newState));
      
      history.clear();  // Must clear history, to make sure the new history is based on expected fix frequency.
      advanceGiveupTimer();
      handler.removeCallbacks(giveupTimer);
            
      // OFF and LOCK_HIGH do not have giveup timers.    
      if (stateHasGiveup(newState)) {
        handler.postDelayed(giveupTimer, SensorConfig.GPS_GIVEUP_CHECK_DELAY);
      }
      
      if (newState == GpsState.OFF) {
        locationManager.removeUpdates(this);
      } else {
        long interval = stateGpsInterval(state);
        long newInterval = stateGpsInterval(newState);
        if (interval != newInterval) {
          locationManager.removeUpdates(this);
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, newInterval, 0.0f, this);
        }
      }
      
      state = newState;
    }
  }
  
  // LocationListener Methods  
  
  public synchronized void onLocationChanged(Location location) {
    if (state != GpsState.OFF && location != null) {
      listener.locationChanged(location);
      
      // Delay give-up timers
      if (stateHasGiveup(state))
        advanceGiveupTimer();
      
      // Check for state changes.
      if (stateHasHistory(state)) {
        history.addFix(location);
        if (state == GpsState.HIGH) {
          if (history.shouldSwitchToLowSpeed())
            switchToState(GpsState.LOW);
        } else if (state == GpsState.LOW) { 
          if (history.shouldSwitchToHighSpeed())
            switchToState(GpsState.HIGH);
          else if (history.shouldReportStationary()) {
            service.log("GPS is actually stationary.");
            listener.noGpsMovement();
          }
        }
      }
    }
  }

  public synchronized void onProviderDisabled(String provider) {
    service.log("GPS onProviderDisabled()");
    if (stateHasGiveup(state))
      listener.noGpsMovement();
  }

  public synchronized void onProviderEnabled(String provider) {
    service.log("GPS onProviderEnabled()");
  }

  public synchronized void onStatusChanged(String provider, int status, Bundle extras) {
    service.log("GPS onStatusChanged to " + status);
    if (stateHasGiveup(state)) {
      if (status != LocationProvider.AVAILABLE) {      
        listener.noGpsMovement();
      }
    }
  }
  
  // Give-up Timer
  
  Runnable giveupTimer = new Runnable() {
    public void run() {
      synchronized (DistanceCycledGps.this) {
        if (stateHasGiveup(state)) {
          if (shouldGiveup()) {
            service.log("GPS giveup timeout reached");
            listener.noGpsMovement();
          }
          
          handler.postDelayed(this, SensorConfig.GPS_GIVEUP_CHECK_DELAY);
        }
      }
    }
  };
  
  private void advanceGiveupTimer() { 
    lastGpsFixMillis = SystemClock.elapsedRealtime();
  }
  
  private boolean shouldGiveup() {
    return SystemClock.elapsedRealtime() - lastGpsFixMillis > SensorConfig.GPS_GIVEUP_AFTER_NO_FIX_FOR_MILLIS;
  }
  
  private static final boolean stateHasGiveup(GpsState state) {
    return state == GpsState.LOW || state == GpsState.HIGH;
  }
  
  private static final boolean stateHasHistory(GpsState state) {
    return state == GpsState.LOW || state == GpsState.HIGH;
  }
  
  private static final long stateGpsInterval(GpsState state) {
    switch (state) {
      case OFF: return -1;
      case LOW: return SensorConfig.GPS_LOWSPEED_DELAY_MILLIS;
      case HIGH: return SensorConfig.GPS_HIGHSPEED_DELAY_MILLIS;
      case LOCK_HIGH: return SensorConfig.GPS_HIGHSPEED_DELAY_MILLIS;
      default: return -1;
    }
  }
}




Java Source Code List

com.linnap.locationtracker.EventBindings.java
com.linnap.locationtracker.ExpectedState.java
com.linnap.locationtracker.LocationTrackerService.java
com.linnap.locationtracker.SensorConfig.java
com.linnap.locationtracker.Sets.java
com.linnap.locationtracker.StateChange.java
com.linnap.locationtracker.gps.DistanceCycledGps.java
com.linnap.locationtracker.gps.GpsHistory.java
com.linnap.locationtracker.gps.LocationFix.java
com.linnap.locationtracker.movement.AccelerometerData.java
com.linnap.locationtracker.movement.BooleanState.java
com.linnap.locationtracker.movement.DutyCycledAccelerometer.java
com.linnap.locationtracker.movement.SmallMovement.java
com.linnap.locationtracker.movement.StatsAccumulator.java
com.linnap.locationtracker.schedule.SensorScheduler.java
com.linnap.locationtracker.wifi.ScanResultsData.java
com.linnap.locationtracker.wifi.ScanStartedData.java
com.linnap.locationtracker.wifi.TimeoutScan.java
com.linnap.locationtracker.wifi.WifiFingerprint.java
com.linnap.locationtracker.wifi.WifiPlaceChange.java