FileDocCategorySizeDatePackage
LocationStatsAggregator.javaAPI DocAndroid 5.1 API8459Thu Mar 12 22:22:48 GMT 2015android.bordeaux.services

LocationStatsAggregator

public class LocationStatsAggregator extends Aggregator

Fields Summary
final String
TAG
public static final String
CURRENT_LOCATION
public static final String
CURRENT_SPEED
public static final String
UNKNOWN_LOCATION
private static final long
REPEAT_INTERVAL
private static final long
FRESH_THRESHOLD
private static final int
LOCATION_CHANGE
private long
mProviderSetTime
private android.os.Handler
mHandler
private android.os.HandlerThread
mHandlerThread
private android.app.AlarmManager
mAlarmManager
private android.location.LocationManager
mLocationManager
private ClusterManager
mClusterManager
private android.location.Criteria
mCriteria
private LocationUpdater
mLocationUpdater
private android.content.Context
mContext
private android.app.PendingIntent
mPendingIntent
private String
mFakeLocation
private final android.location.LocationListener
mLocationListener
Constructors Summary
public LocationStatsAggregator(android.content.Context context)


        
        mLocationManager =
            (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        mAlarmManager =
            (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        setClusteringThread(context);

        mCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
        mCriteria.setPowerRequirement(Criteria.POWER_LOW);
        /*
        mCriteria.setAltitudeRequired(false);
        mCriteria.setBearingRequired(false);
        mCriteria.setSpeedRequired(true);
        */
        mCriteria.setCostAllowed(true);


        IntentFilter filter = new IntentFilter(LocationUpdater.LOCATION_UPDATE);
        mLocationUpdater = new LocationUpdater();
        context.registerReceiver(mLocationUpdater, filter);

        Intent intent = new Intent(LocationUpdater.LOCATION_UPDATE);

        mContext = context;
        mPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);

        mAlarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                                          SystemClock.elapsedRealtime() + 30000, //
                                          REPEAT_INTERVAL,
                                          mPendingIntent);
    
Methods Summary
public java.util.ListgetClusterNames()

        return mClusterManager.getClusterNames();
    
public java.util.MapgetFeatureValue(java.lang.String featureName)

        HashMap<String,String> feature = new HashMap<String,String>();

        if (featureName.equals(CURRENT_LOCATION)) {
            // TODO: check last known location first before sending out location request.
            /*
              Location location =
              mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            */
            String location = mClusterManager.getSemanticLocation();
            if (!location.equals(UNKNOWN_LOCATION)) {
                if (mFakeLocation != null) {
                    feature.put(CURRENT_LOCATION, mFakeLocation);
                } else {
                    feature.put(CURRENT_LOCATION, location);
                }
            }
        }
        return (Map) feature;
    
private android.location.LocationgetLastKnownLocation()

        List<String> providers = mLocationManager.getAllProviders();
        Location bestResult = null;
        float bestAccuracy = Float.MAX_VALUE;
        long bestTime;

        // get the latest location data
        long currTime =  System.currentTimeMillis();
        for (String provider : providers) {
            Location location = mLocationManager.getLastKnownLocation(provider);

            if (location != null) {
                float accuracy = location.getAccuracy();
                long time = location.getTime();

                if (currTime - time < FRESH_THRESHOLD && accuracy < bestAccuracy) {
                    bestResult = location;
                    bestAccuracy = accuracy;
                    bestTime = time;
                }
            }
        }
        if (bestResult != null) {
            Log.i(TAG, "found location for free: " + bestResult);
        }
        return bestResult;
    
public java.lang.String[]getListOfFeatures()

        String[] list = { CURRENT_LOCATION } ;
        return list;
    
public voidrelease()

        mContext.unregisterReceiver(mLocationUpdater);
        mAlarmManager.cancel(mPendingIntent);
    
private voidsetClusteringThread(android.content.Context context)

        mClusterManager = new ClusterManager(context);

        mHandlerThread = new HandlerThread("Location Handler",
                Process.THREAD_PRIORITY_BACKGROUND);
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper()) {

            @Override
            public void handleMessage(Message msg) {
                if (!(msg.obj instanceof Location)) {
                    return;
                }
                Location location = (Location) msg.obj;
                switch(msg.what) {
                    case LOCATION_CHANGE:
                        mClusterManager.addSample(location);
                        break;
                    default:
                        super.handleMessage(msg);
                }
            }
        };
    
public voidsetFakeLocation(java.lang.String name)

        if (name != null && name.length() != 0)
            mFakeLocation = name;
        else mFakeLocation = null;