Android Open Source - yousense-android-tracker Wifi Place Change






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.wifi;
//  w w  w  .ja  va  2 s  . c  om
import java.util.List;

import android.net.wifi.ScanResult;
import android.os.Handler;

import com.linnap.locationtracker.LocationTrackerService;
import com.linnap.locationtracker.SensorConfig;
import com.linnap.locationtracker.wifi.TimeoutScan.WifiScanFinished;

/**
 * Detects significant place changes based on WiFi fingerprint changes.
 * Does not schedule itself, has to be polled.
 * Note that due to the asynchronous nature of WiFi scans, it may take up to a few seconds for checkpoint or placeChanged to actually update state.
 */
public class WifiPlaceChange {

  LocationTrackerService service;
  Handler handler;
  MaybePlaceChangedListener listener;
  WifiFingerprint checkpoint;
  TimeoutScan checkpointScan;
  TimeoutScan placeChangedScan;
  
  public WifiPlaceChange(LocationTrackerService service, Handler handler, MaybePlaceChangedListener listener) {
    this.service = service;
    this.handler = handler;
    this.listener = listener;
    this.checkpoint = null;
    this.checkpointScan = null;
    this.placeChangedScan = null;
  }

  /**
   * Clears any checkpoint. Does not stop any checkpoint scans in progress.
   */
  public synchronized void clearCheckpoint() {
    checkpoint = null;
  }
  
  /**
   * Starts a new scan for a checkpoint. Checkpoint will be updated a few seconds later.
   */
  public synchronized void startCheckpoint() {    
    if (checkpointScan == null) {
      checkpointScan = new TimeoutScan(service, handler, new WifiScanFinished() {
        public void wifiScanFinished(List<ScanResult> results, boolean failed) {
          updateCheckpoint(results, failed);
        }
      }).start();
    } else {      
      // Checkpointing already in progress. Do not start another one, wait for it to finish, and use its results.
    }
  }
  
  /**
   * Starts a new scan for comparison to checkpoint. Listener will be called a few seconds later.
   */
  public synchronized void startComparison() {
    // TODO: Consider optimising for no checkpoint. Could fire maybePlaceChanged(true) immediately.
    if (placeChangedScan == null) {
      placeChangedScan = new TimeoutScan(service, handler, new WifiScanFinished() {
        public void wifiScanFinished(List<ScanResult> results, boolean failed) {
          updatePlaceChanged(results, failed);
        }
      }).start();
    } else {
      // Scan already in progress. Do not start another one, wait it to finish and use its results.
    }
  }
  
  public interface MaybePlaceChangedListener {
    public void maybePlaceChanged(boolean changed);
  }
  
  /// State updates
  
  private synchronized void updateCheckpoint(List<ScanResult> results, boolean failed) {
    checkpoint = failed ? null : new WifiFingerprint(results);
    checkpointScan = null;
  }
  
  private synchronized void updatePlaceChanged(List<ScanResult> results, boolean failed) {
    if (checkpoint == null) {
      // Nothing to compare against. May have moved.
      listener.maybePlaceChanged(true);
    } else if (failed) {
      // Nothing to compare against. May have moved.
      listener.maybePlaceChanged(true);
    } else {
      WifiFingerprint now = new WifiFingerprint(results);
      double similarity = checkpoint.similarity(now);
      boolean maybeMoved = similarity < SensorConfig.WIFI_SCAN_MAX_TANIMOTO_SIMILARITY_FOR_SAME_PLACE;
      listener.maybePlaceChanged(maybeMoved);
    }
    placeChangedScan = null;
  }
}




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