diff options
author | Dmitry Kunin <dkunin@mapswith.me> | 2013-09-23 17:11:37 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 02:02:25 +0300 |
commit | e9111ca5019b7b1be7bd0b1c31a655eb40d34e0f (patch) | |
tree | e41cfe4fe2d85294a5dda84ecdfce2cbbce6604b | |
parent | 34072f92b6c271b8ac7ab4356faabfdd43f3eb26 (diff) |
[yota] refactoring to simpler code.
3 files changed, 84 insertions, 168 deletions
diff --git a/android/YoPme/src/com/mapswithme/location/LocationRequester.java b/android/YoPme/src/com/mapswithme/location/LocationRequester.java index c6ded1c7c9..caf3cd0cb0 100644 --- a/android/YoPme/src/com/mapswithme/location/LocationRequester.java +++ b/android/YoPme/src/com/mapswithme/location/LocationRequester.java @@ -27,13 +27,8 @@ public class LocationRequester implements Handler.Callback protected Handler mDelayedEventsHandler; // Location - private long mMinDistance = 0; - private long mMinTime = 0; - private boolean mIsListening = false; - private boolean mCancelIfNotFound = true; + private boolean mIsRegistered = false; private BatteryLevel mBatteryLevel; - - private final Set<String> mProviders = new HashSet<String>(); private final Set<LocationListener> mListeners = new HashSet<LocationListener>(); private Location mLocation; private final static long MAX_TIME_FOR_SUBSCRIPTION_FIX = 15*60*1000; @@ -49,7 +44,6 @@ public class LocationRequester implements Handler.Callback private PendingIntent mPassiveLocationIntent; private final static String ACTION_LOCATION = ".location_request_action"; - private final static IntentFilter LOCATION_FILTER = new IntentFilter(ACTION_LOCATION); private final static String KEY_SOURCE = ".location_source"; private final static String EXTRA_IS_GPS = ".is_gps"; private final static String EXTRA_IS_NETWORK = ".is_network"; @@ -75,8 +69,7 @@ public class LocationRequester implements Handler.Callback { listener.onLocationChanged(l); mLocation = l; - if (mCancelIfNotFound) - postRequestCancelation(MAX_TIME_FOR_SUBSCRIPTION_FIX); + postRequestCancelation(MAX_TIME_FOR_SUBSCRIPTION_FIX); } } @@ -86,24 +79,23 @@ public class LocationRequester implements Handler.Callback } }; + private boolean canListenToLocation() + { + return mBatteryLevel != BatteryLevel.CRITICAL; + } + private final BroadcastReceiver mBatteryReciever = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (mIsListening) - { - final BatteryLevel newLevel = BatteryHelper.getBatteryLevelRange(mContext); - Log.d(TAG, "Got battery update"); + mBatteryLevel = BatteryHelper.getBatteryLevelRange(mContext); + Log.d(TAG, "Got battery update, level: " + mBatteryLevel); - if (newLevel != mBatteryLevel) - { - mBatteryLevel = newLevel; - stopListening(); - setUpProviders(); - startListening(); - Log.d(TAG, "Changed providers list due to battery level update."); - } + if (!canListenToLocation()) + { + stopListening(); + Log.d(TAG, "Stopped updated due to battery level update."); } } }; // receivers @@ -115,8 +107,6 @@ public class LocationRequester implements Handler.Callback mDelayedEventsHandler = new Handler(this); createPendingIntents(); - setUpProviders(); - mBatteryLevel = BatteryHelper.getBatteryLevelRange(mContext); } @@ -143,64 +133,70 @@ public class LocationRequester implements Handler.Callback if (LocationManager.PASSIVE_PROVIDER.equals(provider)) return mPassiveLocationIntent; - throw new IllegalArgumentException("WTF is " + provider + "?"); + return null; } - public void setUpProviders() + public void startListening(long time, boolean isSingle) { - final float batteryLevel = BatteryHelper.getBatteryLevel(mContext); - - // GPS is expensive http://stackoverflow.com/a/4927117 - if (batteryLevel > BatteryHelper.BATTERY_LEVEL_LOW) - mProviders.add(LocationManager.GPS_PROVIDER); + stopListening(); - if (batteryLevel > BatteryHelper.BATTERY_LEVEL_CRITICAL) - mProviders.add(LocationManager.NETWORK_PROVIDER); - - // passive provider is "free" - mProviders.add(LocationManager.PASSIVE_PROVIDER); - - Log.d(TAG, "Set up providers: " + mProviders + " at battery level: " + batteryLevel); - } + if (!canListenToLocation()) + return; - public void startListening() - { - for (final String provider: mProviders) + for (final String provider: mLocationManager.getProviders(true)) { - if (mLocationManager.isProviderEnabled(provider)) + final PendingIntent pi = getIntentForProvider(provider); + if (pi == null) + continue; + + if (isSingle) { - mLocationManager - .requestLocationUpdates(provider, mMinTime, mMinDistance, getIntentForProvider(provider)); - Log.d(TAG, "Registered provider: " + provider); + mLocationManager.requestSingleUpdate(provider, pi); + postRequestCancelation(time); } else - Log.d(TAG, "Provider disabled: " + provider); - } + { + mLocationManager.requestLocationUpdates(provider, time, 0, pi); + postRequestCancelation(MAX_TIME_FOR_SUBSCRIPTION_FIX); + } - mContext.registerReceiver(mLocationReciever, LOCATION_FILTER); - mContext.registerReceiver(mBatteryReciever, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); - mIsListening = true; + Log.d(TAG, "Registered provider: " + provider); + } - if (mCancelIfNotFound) - postRequestCancelation(MAX_TIME_FOR_SUBSCRIPTION_FIX); } public void stopListening() { - for (final String provider : mProviders) + for (final String provider : mLocationManager.getAllProviders()) { - mLocationManager.removeUpdates(getIntentForProvider(provider)); - Log.d(TAG, "Stopped listening to: " + provider); + final PendingIntent pi = getIntentForProvider(provider); + if (pi != null) + { + mLocationManager.removeUpdates(pi); + Log.d(TAG, "Stopped listening to: " + provider); + } } + } - if (mIsListening) + public void unregister() + { + if (mIsRegistered) { mContext.unregisterReceiver(mLocationReciever); mContext.unregisterReceiver(mBatteryReciever); - mIsListening = false; + + mIsRegistered = false; } } + public void register() + { + mContext.registerReceiver(mLocationReciever, new IntentFilter(ACTION_LOCATION)); + mContext.registerReceiver(mBatteryReciever, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); + + mIsRegistered = true; + } + private static final int TWO_MINUTES = 1000 * 60 * 2; /** Checks whether two providers are the same */ @@ -273,41 +269,19 @@ public class LocationRequester implements Handler.Callback public Location getLastKnownLocation() { Location res = null; - for (final String provider : mProviders) + for (final String provider : mLocationManager.getProviders(true)) { - if (mLocationManager.isProviderEnabled(provider)) - { - final Location l = mLocationManager.getLastKnownLocation(provider); - if (isFirstOneBetterLocation(l, res)) - res = l; - } + final Location l = mLocationManager.getLastKnownLocation(provider); + if (isFirstOneBetterLocation(l, res)) + res = l; } return res; } - public void requestSingleUpdate(long delayMillis) - { - if (mProviders.size() > 0) - { - for (final String provider : mProviders) - { - if (mLocationManager.isProviderEnabled(provider)) - mLocationManager.requestSingleUpdate(provider, getIntentForProvider(provider)); - } - - if (delayMillis > 0) - postRequestCancelation(delayMillis); - } - Log.d(TAG, "Send single update request"); - } - private void postRequestCancelation(long delayMillis) { - // remove old message mDelayedEventsHandler.removeMessages(WHAT_LOCATION_REQUEST_CANCELATION); - - final Message msg =mDelayedEventsHandler.obtainMessage(WHAT_LOCATION_REQUEST_CANCELATION); - // send new + final Message msg = mDelayedEventsHandler.obtainMessage(WHAT_LOCATION_REQUEST_CANCELATION); mDelayedEventsHandler.sendMessageDelayed(msg, delayMillis); Log.d(TAG, "Postponed cancelation in: " + delayMillis + " ms"); @@ -340,24 +314,6 @@ public class LocationRequester implements Handler.Callback } } - public void setMinDistance(long minDistance) - { - mMinDistance = minDistance; - } - - public void setMinTime(long minTime) - { - mMinTime = minTime; - } - - public void setCancelIfNotFound(boolean doCancel) - { - mCancelIfNotFound = doCancel; - - if (!doCancel) - mDelayedEventsHandler.removeMessages(WHAT_LOCATION_REQUEST_CANCELATION); - } - public void setLocation(Location location) { mLocation = location; diff --git a/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java b/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java index 2ac4fc2353..e88dbac3da 100644 --- a/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java +++ b/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java @@ -14,7 +14,6 @@ import android.location.LocationListener; import android.os.Build; import android.os.Bundle; import android.os.Environment; -import android.os.Handler; import android.preference.PreferenceManager; import android.text.TextUtils; import android.util.Log; @@ -76,27 +75,6 @@ public class BackscreenActivity extends BSActivity implements LocationListener protected MapDataProvider mMapDataProvider; private LocationRequester mLocationRequester; - private final Runnable mInvalidateDrawable = new Runnable() - { - @Override - public void run() - { - draw(); - } - }; - - private final Runnable mRequestLocation = new Runnable() - { - @Override - public void run() - { - requestLocationUpdateImpl(); - } - }; - - private final Handler mDeduplicateHandler = new Handler(); - private final static long REDRAW_MIN_INTERVAL = 333; - private static final String EXTRA_POINT = "point"; @Override @@ -145,16 +123,17 @@ public class BackscreenActivity extends BSActivity implements LocationListener protected void onBSPause() { super.onBSPause(); - mLocationRequester.stopListening(); - mDeduplicateHandler.removeCallbacks(mInvalidateDrawable); - mDeduplicateHandler.removeCallbacks(mRequestLocation); + mLocationRequester.stopListening(); + mLocationRequester.unregister(); } @Override protected void onBSResume() { super.onBSResume(); + + mLocationRequester.register(); requestLocationUpdate(); updateData(); @@ -268,8 +247,7 @@ public class BackscreenActivity extends BSActivity implements LocationListener public void invalidate() { - mDeduplicateHandler.removeCallbacks(mInvalidateDrawable); - mDeduplicateHandler.postDelayed(mInvalidateDrawable, REDRAW_MIN_INTERVAL); + draw(); } private boolean zoomIn() @@ -300,12 +278,6 @@ public class BackscreenActivity extends BSActivity implements LocationListener private void requestLocationUpdate() { - mDeduplicateHandler.removeCallbacks(mRequestLocation); - mDeduplicateHandler.postDelayed(mRequestLocation, 300); - } - - private void requestLocationUpdateImpl() - { final String updateIntervalStr = PreferenceManager.getDefaultSharedPreferences(this) .getString(getString(R.string.pref_loc_update), YopmePreference.LOCATION_UPDATE_DEFAULT); final long updateInterval = Long.parseLong(updateIntervalStr); @@ -316,15 +288,9 @@ public class BackscreenActivity extends BSActivity implements LocationListener // then listen to updates if (updateInterval == -1) - mLocationRequester.requestSingleUpdate(60*1000); + mLocationRequester.startListening(60*1000, true); else - { - // according to the manual, minDistance doesn't save battery life - mLocationRequester.setMinDistance(0); - mLocationRequester.setMinTime(updateInterval * 1000); - mLocationRequester.setUpProviders(); - mLocationRequester.startListening(); - } + mLocationRequester.startListening(updateInterval*1000, false); } private void showWaitMessage(CharSequence msg) @@ -343,29 +309,23 @@ public class BackscreenActivity extends BSActivity implements LocationListener { df.setRoundingMode(RoundingMode.DOWN); } - private void setDistance(double distance) + private void setDistance(float distance) { - if (distance < 0) - mPoiDist.setVisibility(View.GONE); - else - { - String suffix = "m"; - double div = 1; - df.setMaximumFractionDigits(0); + String suffix = "m"; + double div = 1; + df.setMaximumFractionDigits(0); - if (distance >= 1000) - { - suffix = "km"; - div = 1000; - - // set fraction digits only in [1..10) kilometers range - if (distance < 10000) - df.setMaximumFractionDigits(2); - } + if (distance >= 1000) + { + suffix = "km"; + div = 1000; - mPoiDist.setText(df.format(distance/div) + suffix); - mPoiDist.setVisibility(View.VISIBLE); + // set fraction digits only in [1..10) kilometers range + if (distance < 10000) + df.setMaximumFractionDigits(2); } + + mPoiDist.setText(df.format(distance/div) + suffix); } public void updateData() @@ -417,11 +377,13 @@ public class BackscreenActivity extends BSActivity implements LocationListener else hideWaitMessage(); - if (mMode == Mode.POI) + if (mPoint != null && mMode == Mode.POI) { mPoiText.setText(mPoint.getName()); mPoiInfo.setVisibility(TextUtils.isEmpty(mPoint.getName()) ? View.GONE : View.VISIBLE); } + else + mPoiInfo.setVisibility(View.GONE); } @@ -432,7 +394,8 @@ public class BackscreenActivity extends BSActivity implements LocationListener final Location poiLoc = new Location(""); poiLoc.setLatitude(mPoint.getLat()); poiLoc.setLongitude(mPoint.getLon()); - setDistance(poiLoc.distanceTo(mLocation)); + final float dist = poiLoc.distanceTo(mLocation); + setDistance(dist); } } diff --git a/android/src/com/mapswithme/maps/MWMActivity.java b/android/src/com/mapswithme/maps/MWMActivity.java index c61759f4c4..e542ebe126 100644 --- a/android/src/com/mapswithme/maps/MWMActivity.java +++ b/android/src/com/mapswithme/maps/MWMActivity.java @@ -618,10 +618,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService if (locState.hasPosition() && locState.isCentered()) Yota.showLocation(getApplicationContext(), zoom); else - { - final String coord = String.format(Locale.US, "%.4f, %.4f", latLon[0], latLon[1]); - Yota.showPoi(getApplicationContext(), latLon[0], latLon[1], zoom, coord, locState.hasPosition()); - } + Yota.showPoi(getApplicationContext(), latLon[0], latLon[1], zoom, "", locState.hasPosition()); } }); } |