From 06b64f585b7ef51e1f390d8631ef34225bdbcf9b Mon Sep 17 00:00:00 2001 From: fkloft Date: Thu, 22 Jan 2015 15:32:13 +0100 Subject: [PATCH] Sort members, add final modifiers, format code --- .../cradle/iitc_mobile/IITC_UserLocation.java | 265 +++++++++--------- 1 file changed, 125 insertions(+), 140 deletions(-) diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_UserLocation.java b/mobile/src/com/cradle/iitc_mobile/IITC_UserLocation.java index 498e8c15..f43f49fc 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_UserLocation.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_UserLocation.java @@ -13,21 +13,23 @@ import android.view.Surface; public class IITC_UserLocation implements LocationListener, SensorEventListener { private static final double SENSOR_DELAY_USER = 100 * 1e6; // 100 milliseconds - private int mMode = 0; - private boolean mRunning = false; - private boolean mLocationRegistered = false; - private boolean mOrientationRegistered = false; - private long mLastUpdate = 0; - private IITC_Mobile mIitc; + private static final int TWO_MINUTES = 1000 * 60 * 2; + + private boolean mFollowing = false; + private final IITC_Mobile mIitc; private Location mLastLocation = null; - private LocationManager mLocationManager; - private Sensor mSensorAccelerometer, mSensorMagnetometer; + private long mLastUpdate = 0; + 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 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; // 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); } + // 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) { // we have a transition defined for the rotation // changes to the orientation should always be less than 180° @@ -56,19 +64,19 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener } private void updateListeners() { - boolean useLocation = mRunning && mMode != 0; - boolean useOrientation = mRunning && mMode == 2; + final boolean useLocation = mRunning && mMode != 0; + final boolean useOrientation = mRunning && mMode == 2; if (useLocation && !mLocationRegistered) { try { mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); - } catch (IllegalArgumentException e) { + } catch (final IllegalArgumentException e) { // if the given provider doesn't exist Log.w(e); } try { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); - } catch (IllegalArgumentException e) { + } catch (final IllegalArgumentException e) { // if the given provider doesn't exist 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() { if (!mLocationRegistered) return false; return mLastLocation != null; @@ -104,7 +160,7 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener // do not touch the javascript while iitc boots if (mIitc.isLoading()) return; - Location location = mLastLocation; + final Location location = mLastLocation; if (location == null) return; mIitc.getWebView().loadJS("if(window.plugin && window.plugin.userLocation)" @@ -113,110 +169,12 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener + location.getAccuracy() + ", " + persistentZoom + ");"); } - 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 onAccuracyChanged(final Sensor sensor, final int accuracy) { } - 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; - } - - // ------------------------------------------------------------------------ - - // - @Override - public void onLocationChanged(Location location) { + public void onLocationChanged(final Location location) { if (!isBetterLocation(location, mLastLocation)) return; mLastLocation = location; @@ -230,30 +188,15 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener } @Override - public void onProviderDisabled(String provider) { + public void onProviderDisabled(final String provider) { } @Override - public void onProviderEnabled(String provider) { + public void onProviderEnabled(final String provider) { } @Override - public void onStatusChanged(String provider, int status, Bundle extras) { - - } - - // - - // ------------------------------------------------------------------------ - - // - - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) { - } - - @Override - public void onSensorChanged(SensorEvent event) { + public void onSensorChanged(final SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) mValuesGravity = event.values; 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 if (mValuesGravity == null || mValuesGeomagnetic == null) return; - float R[] = new float[9]; - float I[] = new float[9]; - float orientation[] = new float[3]; + final float R[] = new float[9]; + final float I[] = new float[9]; + final float orientation[] = new float[3]; if (!SensorManager.getRotationMatrix(R, I, mValuesGravity, mValuesGeomagnetic)) return; SensorManager.getOrientation(R, orientation); double direction = orientation[0] / Math.PI * 180; - int rotation = mIitc.getWindowManager().getDefaultDisplay().getRotation(); + final int rotation = mIitc.getWindowManager().getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_90: direction += 90; @@ -294,5 +237,47 @@ public class IITC_UserLocation implements LocationListener, SensorEventListener setOrientation(direction); } - // + 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; + } } \ No newline at end of file