Android Open Source - yousense-android-tracker Sensor Scheduler






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.schedule;
/*from   w w w .  j  av a  2 s  . c om*/
import android.content.Context;
import android.location.Location;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

import com.linnap.locationtracker.ExpectedState.TrackerState;
import com.linnap.locationtracker.LocationTrackerService;
import com.linnap.locationtracker.SensorConfig;
import com.linnap.locationtracker.StateChange;
import com.linnap.locationtracker.gps.DistanceCycledGps;
import com.linnap.locationtracker.gps.DistanceCycledGps.GpsMovementListener;
import com.linnap.locationtracker.gps.DistanceCycledGps.GpsState;
import com.linnap.locationtracker.gps.LocationFix;
import com.linnap.locationtracker.movement.SmallMovement;
import com.linnap.locationtracker.movement.SmallMovement.SmallMovementDistanceListener;

public class SensorScheduler implements SmallMovementDistanceListener, GpsMovementListener {

  LocationTrackerService service;
  TrackerState state;
  
  WakeLock wakeLock;
  SmallMovement smallMovement;
  DistanceCycledGps distanceCycledGps;
  boolean gpsTracking;
  
  public SensorScheduler(LocationTrackerService service) {
    this.service = service;
    this.state = TrackerState.OFF;
    
    this.wakeLock = ((PowerManager)service.getSystemService(Context.POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, SensorConfig.TAG);
    this.wakeLock.setReferenceCounted(false);
    this.smallMovement = new SmallMovement(service, this);
    this.distanceCycledGps = new DistanceCycledGps(service, this);
    this.gpsTracking = false;
  }
  
  public synchronized void switchToState(TrackerState newState) {
    if (state != newState) {
      service.event("schedule_state", new StateChange(state, newState));
    
      if (state == TrackerState.OFF && newState != TrackerState.OFF) {
        wakeLock.acquire();
        service.startForegroundWithNotification();
      } else if (state != TrackerState.OFF && newState == TrackerState.OFF){
        wakeLock.release();
        service.stopForeground(true);
      }
    
      smallMovement.stop();
      if (newState == TrackerState.OFF) {        
        distanceCycledGps.switchToState(GpsState.OFF);
        gpsTracking = false;
      } else if (newState == TrackerState.BACKGROUND) {
        distanceCycledGps.switchToState(GpsState.HIGH);
        gpsTracking = true;
      } else {
        distanceCycledGps.switchToState(GpsState.LOCK_HIGH);
        gpsTracking = true;
      }
      state = newState;
    }
    
  }

  /// Callbacks from sensors

  public synchronized void maybeSmallMovement() {
    if (state == TrackerState.BACKGROUND) {
      if (!gpsTracking) {
        smallMovement.stop();
        distanceCycledGps.switchToState(GpsState.HIGH);
        gpsTracking = true;
      }
    }
  }

  public synchronized void noGpsMovement() {
    if (state == TrackerState.BACKGROUND) {
      if (gpsTracking) {
        distanceCycledGps.switchToState(GpsState.OFF);
        smallMovement.start();        
        gpsTracking = false;
      }
    }
  }
  
  public synchronized void locationChanged(Location location) {
    service.gpsFix(new LocationFix(location));
  }
  
}




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