Android Open Source - yousense-android-tracker Timeout Scan






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;
/*from ww  w  .j  a va 2s. c o m*/
import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Handler;

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

/**
 * Wifi Scanner with timeouts.
 * This is a single-use class, create a new one each time.
 */
public class TimeoutScan {

  LocationTrackerService service;
  Handler handler;
  WifiManager wifiManager;  
  WifiScanFinished listener;
  boolean scanStarted;
  boolean listenerCalled;
  
  public TimeoutScan(LocationTrackerService service, Handler handler, WifiScanFinished listener) {
    this.service = service;
    this.handler = handler;
    this.wifiManager = (WifiManager)service.getSystemService(Context.WIFI_SERVICE);
    this.listener = listener;
    this.scanStarted = false;
    this.listenerCalled = false;
  }
  
  /**
   * Starts the scan. Returns itself, for create and start() in one line.
   */
  public synchronized TimeoutScan start() {
    if (scanStarted || listenerCalled) {
      throw new IllegalStateException("TimeoutScan is a single-use class.");
    } else {
      scanStarted = true;
      handler.postDelayed(timeout, SensorConfig.WIFI_SCAN_TIMEOUT_MILLIS);
      service.registerReceiver(scanResultsReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION), null, handler);      
      boolean success = wifiManager.startScan();  // If startScan() fails, still not calling listener right now. Perhaps easier to think about threads.
      service.event("wifi_scan_started", new ScanStartedData(!success));
      return this;
    }
  }
  
  public interface WifiScanFinished {
    public void wifiScanFinished(List<ScanResult> results, boolean failed);
  }
  
  /// Implementation
  
  private synchronized void reportResults(List<ScanResult> results, boolean failed) {    
    if (!listenerCalled) {
      listenerCalled = true;
      // Unregister BroadcastReceiver only once
      service.unregisterReceiver(scanResultsReceiver);      
      service.event("wifi_scan_results", new ScanResultsData(results, failed));
      listener.wifiScanFinished(results, failed);
      // Do not bother unregistering timeout handler.
    } else {
      // Listener already called. Ignoring double reports.
    }
  }
  
  Runnable timeout = new Runnable() {
    public void run() {
      reportResults(new ArrayList<ScanResult>(), true);
    }
  };
  
  BroadcastReceiver scanResultsReceiver = new BroadcastReceiver() {
    public void onReceive(Context _context, Intent intent) {
      List<ScanResult> results = wifiManager.getScanResults();
      if (results == null) {
        reportResults(new ArrayList<ScanResult>(), true);
      } else {
        reportResults(results, false);
      }
    }
  };
}




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