Clean up LocationListenerPolicy.

This commit is contained in:
Jimmy Shih
2012-08-07 12:19:59 -07:00
parent 27c796cda0
commit a0b3e21b14
3 changed files with 53 additions and 70 deletions
@@ -19,41 +19,34 @@ package com.google.android.apps.mytracks.services;
/** /**
* This is a simple location listener policy that will always dictate the same * This is a simple location listener policy that will always dictate the same
* polling interval. * polling interval.
* *
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class AbsoluteLocationListenerPolicy implements LocationListenerPolicy { public class AbsoluteLocationListenerPolicy implements LocationListenerPolicy {
/**
* The interval to request for gps signal.
*/
private final long interval; private final long interval;
/** /**
* @param interval The interval to request for gps signal * Constructor.
*
* @param interval the interval to request for gps signal
*/ */
public AbsoluteLocationListenerPolicy(final long interval) { public AbsoluteLocationListenerPolicy(long interval) {
this.interval = interval; this.interval = interval;
} }
/** @Override
* @return The interval given in the constructor
*/
public long getDesiredPollingInterval() { public long getDesiredPollingInterval() {
return interval; return interval;
} }
/** @Override
* Discards the idle time.
*/
public void updateIdleTime(long idleTime) {
}
/**
* Returns the minimum distance between updates.
* Get all updates to properly measure moving time.
*/
public int getMinDistance() { public int getMinDistance() {
return 0; return 0;
} }
@Override
public void updateIdleTime(long idleTime) {
// Ignore
}
} }
@@ -16,67 +16,58 @@
package com.google.android.apps.mytracks.services; package com.google.android.apps.mytracks.services;
/** /**
* A LocationListenerPolicy that will change based on how long the user has been * A {@link LocationListenerPolicy} that will change based on how long the user
* stationary. * has been stationary. This policy will dictate a policy based on a min, max
* * and idle time. The policy will dictate an interval bounded by min and max,
* This policy will dictate a policy based on a min, max and idle time. * and is half of the idle time.
* The policy will dictate an interval bounded by min and max whic is half of *
* the idle time.
*
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class AdaptiveLocationListenerPolicy implements LocationListenerPolicy { public class AdaptiveLocationListenerPolicy implements LocationListenerPolicy {
/**
* Smallest interval this policy will dictate, in milliseconds.
*/
private final long minInterval; private final long minInterval;
/**
* Largest interval this policy will dictate, in milliseconds.
*/
private final long maxInterval; private final long maxInterval;
private final int minDistance; private final int minDistance;
/** // The time the user has been idle at the current location, in milliseconds.
* The time the user has been at the current location, in milliseconds.
*/
private long idleTime; private long idleTime;
/** /**
* Creates a policy that will be bounded by the given min and max. * Creates a policy that will be bounded by the given minInterval and
* * maxInterval.
* @param min Smallest interval this policy will dictate, in milliseconds *
* @param max Largest interval this policy will dictate, in milliseconds * @param minInterval the smallest interval this policy will dictate, in
* milliseconds
* @param maxInterval the largest interval this policy will dictate, in
* milliseconds
* @param minDistance the minimum distance in meters
*/ */
public AdaptiveLocationListenerPolicy(long min, long max, int minDistance) { public AdaptiveLocationListenerPolicy(long minInterval, long maxInterval, int minDistance) {
this.minInterval = min; this.minInterval = minInterval;
this.maxInterval = max; this.maxInterval = maxInterval;
this.minDistance = minDistance; this.minDistance = minDistance;
} }
/** /*
* @return An interval bounded by min and max which is half of the idle time * Returns an interval half of the idle time, but bounded by minInteval and
* maxInterval.
*/ */
@Override
public long getDesiredPollingInterval() { public long getDesiredPollingInterval() {
long desiredInterval = idleTime / 2; long desiredInterval = idleTime / 2;
// Round to avoid setting the interval too often. // Round to second to avoid setting the interval too often
desiredInterval = (desiredInterval / 1000) * 1000; desiredInterval = (desiredInterval / 1000) * 1000;
return Math.max(Math.min(maxInterval, desiredInterval), return Math.max(Math.min(maxInterval, desiredInterval), minInterval);
minInterval);
} }
public void updateIdleTime(long newIdleTime) { @Override
this.idleTime = newIdleTime;
}
/**
* Returns the minimum distance between updates.
*/
public int getMinDistance() { public int getMinDistance() {
return minDistance; return minDistance;
} }
@Override
public void updateIdleTime(long newIdleTime) {
idleTime = newIdleTime;
}
} }
@@ -17,19 +17,18 @@
package com.google.android.apps.mytracks.services; package com.google.android.apps.mytracks.services;
/** /**
* This is an interface for classes that will manage the location listener policy. * This is an interface for classes that will manage the location listener
* Different policy options are: * policy. Different policy options are: {@link AbsoluteLocationListenerPolicy}
* Absolute * and {@link AdaptiveLocationListenerPolicy}.
* Addaptive *
*
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public interface LocationListenerPolicy { public interface LocationListenerPolicy {
/** /**
* Returns the polling time this policy would like at this time. * Returns the polling interval this policy would like at this moment.
* *
* @return The polling that this policy dictates * @return the polling interval
*/ */
public long getDesiredPollingInterval(); public long getDesiredPollingInterval();
@@ -39,10 +38,10 @@ public interface LocationListenerPolicy {
public int getMinDistance(); public int getMinDistance();
/** /**
* Notifies the amount of time the user has been idle at their current * Notifies the amount of time the user has been idle at his current location.
* location. *
* * @param idleTime the time that the user has been idle at his current
* @param idleTime The time that the user has been idle at this spot * location
*/ */
public void updateIdleTime(long idleTime); public void updateIdleTime(long idleTime);
} }