Sort members, add final modifiers, format code

This commit is contained in:
fkloft 2015-01-22 15:32:13 +01:00
parent 712b954910
commit 06b64f585b

View File

@ -13,21 +13,23 @@ import android.view.Surface;
public class IITC_UserLocation implements LocationListener, SensorEventListener { public class IITC_UserLocation implements LocationListener, SensorEventListener {
private static final double SENSOR_DELAY_USER = 100 * 1e6; // 100 milliseconds private static final double SENSOR_DELAY_USER = 100 * 1e6; // 100 milliseconds
private int mMode = 0; private static final int TWO_MINUTES = 1000 * 60 * 2;
private boolean mRunning = false;
private boolean mLocationRegistered = false; private boolean mFollowing = false;
private boolean mOrientationRegistered = false; private final IITC_Mobile mIitc;
private long mLastUpdate = 0;
private IITC_Mobile mIitc;
private Location mLastLocation = null; private Location mLastLocation = null;
private LocationManager mLocationManager; private long mLastUpdate = 0;
private Sensor mSensorAccelerometer, mSensorMagnetometer; private final LocationManager mLocationManager;
private boolean mLocationRegistered = false;
private int mMode = 0;
private double mOrientation = 0;
private boolean mOrientationRegistered = false;
private boolean mRunning = false;
private final Sensor mSensorAccelerometer, mSensorMagnetometer;
private SensorManager mSensorManager = null; private SensorManager mSensorManager = null;
private float[] mValuesGravity = null, mValuesGeomagnetic = null; private float[] mValuesGravity = null, mValuesGeomagnetic = null;
private double mOrientation = 0;
private boolean mFollowing = false;
public IITC_UserLocation(IITC_Mobile iitc) { public IITC_UserLocation(final IITC_Mobile iitc) {
mIitc = iitc; mIitc = iitc;
// Acquire a reference to the Location Manager and Sensor Manager // Acquire a reference to the Location Manager and Sensor Manager
@ -37,6 +39,12 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
mSensorMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); mSensorMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
} }
// Checks whether two providers are the same
private boolean isSameProvider(final String provider1, final String provider2) {
if (provider1 == null) { return provider2 == null; }
return provider1.equals(provider2);
}
private void setOrientation(Double orientation) { private void setOrientation(Double orientation) {
// we have a transition defined for the rotation // we have a transition defined for the rotation
// changes to the orientation should always be less than 180° // changes to the orientation should always be less than 180°
@ -56,19 +64,19 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
} }
private void updateListeners() { private void updateListeners() {
boolean useLocation = mRunning && mMode != 0; final boolean useLocation = mRunning && mMode != 0;
boolean useOrientation = mRunning && mMode == 2; final boolean useOrientation = mRunning && mMode == 2;
if (useLocation && !mLocationRegistered) { if (useLocation && !mLocationRegistered) {
try { try {
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} catch (IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// if the given provider doesn't exist // if the given provider doesn't exist
Log.w(e); Log.w(e);
} }
try { try {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} catch (IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// if the given provider doesn't exist // if the given provider doesn't exist
Log.w(e); Log.w(e);
} }
@ -91,6 +99,54 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
} }
} }
/**
* Determines whether one Location reading is better than the current Location fix
*
* @param location
* The new Location that you want to evaluate
* @param currentBestLocation
* The current Location fix, to which you want to compare the new one
*
* code copied from http://developer.android.com/guide/topics/location/strategies.html#BestEstimate
*/
protected boolean isBetterLocation(final Location location, final Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
final long timeDelta = location.getTime() - currentBestLocation.getTime();
final boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
final boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
final boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) { return false; }
// Check whether the new location fix is more or less accurate
final int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
final boolean isLessAccurate = accuracyDelta > 0;
final boolean isMoreAccurate = accuracyDelta < 0;
final boolean isSignificantlyLessAccurate = accuracyDelta > 100;
// Check if the old and new location are from the same provider
final boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; }
return false;
}
public boolean hasCurrentLocation() { public boolean hasCurrentLocation() {
if (!mLocationRegistered) return false; if (!mLocationRegistered) return false;
return mLastLocation != null; return mLastLocation != null;
@ -104,7 +160,7 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
// do not touch the javascript while iitc boots // do not touch the javascript while iitc boots
if (mIitc.isLoading()) return; if (mIitc.isLoading()) return;
Location location = mLastLocation; final Location location = mLastLocation;
if (location == null) return; if (location == null) return;
mIitc.getWebView().loadJS("if(window.plugin && window.plugin.userLocation)" mIitc.getWebView().loadJS("if(window.plugin && window.plugin.userLocation)"
@ -113,110 +169,12 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
+ location.getAccuracy() + ", " + persistentZoom + ");"); + location.getAccuracy() + ", " + persistentZoom + ");");
} }
public void onStart() { @Override
mRunning = true; public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
updateListeners();
// in case we just switched from loc+sensor to loc-only, let javascript know
if (mMode == 1) {
setOrientation(null);
} }
}
public void onStop() {
mRunning = false;
updateListeners();
}
public void reset() {
setFollowMode(false);
}
public void setFollowMode(boolean follow) {
mFollowing = follow;
mIitc.invalidateOptionsMenu();
}
private static final int TWO_MINUTES = 1000 * 60 * 2;
/**
* Determines whether one Location reading is better than the current Location fix
* @param location The new Location that you want to evaluate
* @param currentBestLocation The current Location fix, to which you want to compare the new one
*
* code copied from http://developer.android.com/guide/topics/location/strategies.html#BestEstimate
*/
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}
// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
boolean isNewer = timeDelta > 0;
// If it's been more than two minutes since the current location, use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must be worse
} else if (isSignificantlyOlder) {
return false;
}
// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 100;
// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());
// Determine location quality using a combination of timeliness and accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
return true;
}
return false;
}
// Checks whether two providers are the same
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}
/**
* set the location mode to use. Available modes:
* 0: don't show user's position
* 1: show user's position
* 2: show user's position and orientation
*
* @return whether a reload is needed to reflect the changes made to the preferences
*/
public boolean setLocationMode(int mode) {
boolean needsReload = (mode == 0 && mMode != 0) || (mode != 0 && mMode == 0);
mMode = mode;
return needsReload;
}
// ------------------------------------------------------------------------
// <interface LocationListener>
@Override @Override
public void onLocationChanged(Location location) { public void onLocationChanged(final Location location) {
if (!isBetterLocation(location, mLastLocation)) return; if (!isBetterLocation(location, mLastLocation)) return;
mLastLocation = location; mLastLocation = location;
@ -230,30 +188,15 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
} }
@Override @Override
public void onProviderDisabled(String provider) { public void onProviderDisabled(final String provider) {
} }
@Override @Override
public void onProviderEnabled(String provider) { public void onProviderEnabled(final String provider) {
} }
@Override @Override
public void onStatusChanged(String provider, int status, Bundle extras) { public void onSensorChanged(final SensorEvent event) {
}
// </interface LocationListener>
// ------------------------------------------------------------------------
// <interface SensorEventListener>
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mValuesGravity = event.values; mValuesGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
@ -269,16 +212,16 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
// wait until both sensors have given us an event // wait until both sensors have given us an event
if (mValuesGravity == null || mValuesGeomagnetic == null) return; if (mValuesGravity == null || mValuesGeomagnetic == null) return;
float R[] = new float[9]; final float R[] = new float[9];
float I[] = new float[9]; final float I[] = new float[9];
float orientation[] = new float[3]; final float orientation[] = new float[3];
if (!SensorManager.getRotationMatrix(R, I, mValuesGravity, mValuesGeomagnetic)) return; if (!SensorManager.getRotationMatrix(R, I, mValuesGravity, mValuesGeomagnetic)) return;
SensorManager.getOrientation(R, orientation); SensorManager.getOrientation(R, orientation);
double direction = orientation[0] / Math.PI * 180; double direction = orientation[0] / Math.PI * 180;
int rotation = mIitc.getWindowManager().getDefaultDisplay().getRotation(); final int rotation = mIitc.getWindowManager().getDefaultDisplay().getRotation();
switch (rotation) { switch (rotation) {
case Surface.ROTATION_90: case Surface.ROTATION_90:
direction += 90; direction += 90;
@ -294,5 +237,47 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener
setOrientation(direction); setOrientation(direction);
} }
// </interface SensorEventListener> public void onStart() {
mRunning = true;
updateListeners();
// in case we just switched from loc+sensor to loc-only, let javascript know
if (mMode == 1) {
setOrientation(null);
}
}
@Override
public void onStatusChanged(final String provider, final int status, final Bundle extras) {
}
public void onStop() {
mRunning = false;
updateListeners();
}
public void reset() {
setFollowMode(false);
}
public void setFollowMode(final boolean follow) {
mFollowing = follow;
mIitc.invalidateOptionsMenu();
}
/**
* set the location mode to use. Available modes:
* 0: don't show user's position
* 1: show user's position
* 2: show user's position and orientation
*
* @return whether a reload is needed to reflect the changes made to the preferences
*/
public boolean setLocationMode(final int mode) {
final boolean needsReload = (mode == 0 && mMode != 0) || (mode != 0 && mMode == 0);
mMode = mode;
return needsReload;
}
} }