Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Kunin <dkunin@mapswith.me>2013-09-19 17:45:37 +0400
committerAlex Zolotarev <alex@maps.me>2015-09-23 02:02:19 +0300
commit779f47924522c75ee04dfa02f1c2b0665fee5e7b (patch)
tree26e846108794277b53117de768ddd13f1bbe8aa9
parent0fb80668de892062d349b9048cf5bd7541f94bcf (diff)
[yota] Added more responsibility to LocRequester
-rw-r--r--android/YoPme/src/com/mapswithme/location/LocationRequester.java240
-rw-r--r--android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java13
2 files changed, 132 insertions, 121 deletions
diff --git a/android/YoPme/src/com/mapswithme/location/LocationRequester.java b/android/YoPme/src/com/mapswithme/location/LocationRequester.java
index 824efc5817..1d13d42d1b 100644
--- a/android/YoPme/src/com/mapswithme/location/LocationRequester.java
+++ b/android/YoPme/src/com/mapswithme/location/LocationRequester.java
@@ -26,101 +26,37 @@ public class LocationRequester implements Handler.Callback
protected LocationManager mLocationManager;
protected Handler mDelayedEventsHandler;
- // Location settings
- protected Set<String> mEnabledProviders = new HashSet<String>();
+ // Location
private long mMinDistance = 0;
private long mMinTime = 0;
- private boolean mIsListening = false;
- private final boolean mCancelGpsIfNotFound = true;
- private final Set<LocationListener> mListeners = new HashSet<LocationListener>();
- private final static long MAX_TIME_FOR_GPS_FIX = 15*60*1000;
+ private boolean mIsListening = false;
+ private boolean mCancelIfNotFound = true;
private BatteryLevel mBatteryLevel;
- // location settings
+
+ 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;
+ // location
// Handler messages
private final static int WHAT_LOCATION_REQUEST_CANCELATION = 0x01;
- private final static int WHAT_LOCATION_REQUEST_GPS_CANCELLATION = 0x02;
// handler massages
- public void addListener(LocationListener listener)
- {
- mListeners.add(listener);
- }
-
- public void removeListener(LocationListener listener)
- {
- mListeners.remove(listener);
- if (mListeners.isEmpty())
- {
- stopListening();
- Log.d(TAG, "No more listeners, stopping.");
- }
- }
-
- public void setMinDistance(long minDistance)
- {
- mMinDistance = minDistance;
- }
-
- public void setMinTime(long minTime)
- {
- mMinTime = minTime;
- }
-
- public void setUpProviders()
- {
- final float batteryLevel = BatteryHelper.getBatteryLevel(mContext);
-
- if (batteryLevel > BatteryHelper.BATTERY_LEVEL_LOW)
- {
- // GPS is expensive http://stackoverflow.com/a/4927117
- mEnabledProviders.add(LocationManager.GPS_PROVIDER);
- }
- if (batteryLevel > BatteryHelper.BATTERY_LEVEL_CRITICAL)
- mEnabledProviders.add(LocationManager.NETWORK_PROVIDER);
- // passive provider is free
- mEnabledProviders.add(LocationManager.PASSIVE_PROVIDER);
-
- Log.d(TAG, "Set up providers: " + mEnabledProviders + " at battery level: " + batteryLevel);
- }
-
+ // Intents
private PendingIntent mGpsLocationIntent;
private PendingIntent mNetworkLocationIntent;
private PendingIntent mPassiveLocationIntent;
- private void createPendingIntents()
- {
- mGpsLocationIntent = PendingIntent.getBroadcast(mContext, 0xF,
- new Intent(ACTION_LOCATION).putExtra(KEY_SOURCE, EXTRA_IS_GPS), PendingIntent.FLAG_CANCEL_CURRENT);
-
- mNetworkLocationIntent = PendingIntent.getBroadcast(mContext, 0xFF,
- new Intent(ACTION_LOCATION).putExtra(KEY_SOURCE, EXTRA_IS_NETWORK), PendingIntent.FLAG_CANCEL_CURRENT);
-
- mPassiveLocationIntent = PendingIntent.getBroadcast(mContext, 0xFFF,
- new Intent(ACTION_LOCATION).putExtra(KEY_SOURCE, EXTRA_IS_PASSAIVE), PendingIntent.FLAG_CANCEL_CURRENT);
- }
-
- public PendingIntent getIntentForProvider(String provider)
- {
- if (LocationManager.GPS_PROVIDER.equals(provider))
- return mGpsLocationIntent;
-
- if (LocationManager.NETWORK_PROVIDER.equals(provider))
- return mNetworkLocationIntent;
-
- if (LocationManager.PASSIVE_PROVIDER.equals(provider))
- return mPassiveLocationIntent;
-
- throw new IllegalArgumentException("WTF is " + provider + "?");
- }
-
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";
private final static String EXTRA_IS_PASSAIVE = ".is_passive";
+ // intents
+ // Receivers
private final BroadcastReceiver mLocationReciever = new BroadcastReceiver()
{
@Override
@@ -128,19 +64,25 @@ public class LocationRequester implements Handler.Callback
{
Log.d(TAG, "Got location update from : " + intent.getStringExtra(KEY_SOURCE));
-
for (final LocationListener listener: mListeners)
{
if (intent.hasExtra(LocationManager.KEY_LOCATION_CHANGED))
{
- listener.onLocationChanged((Location)intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED));
-
- // if location is from GPS - do not cancel
- if (EXTRA_IS_GPS.equals(intent.getStringExtra(KEY_SOURCE)))
- mDelayedEventsHandler.removeMessages(WHAT_LOCATION_REQUEST_GPS_CANCELLATION);
+ final Location l = (Location)intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
+ final boolean isLocationReallyChanged = isFirstOneBetterLocation(l, mLocation)
+ && areLocationsFarEnough(l, mLocation);
+ if (isLocationReallyChanged)
+ {
+ listener.onLocationChanged(l);
+ mLocation = l;
+ if (mCancelIfNotFound)
+ postRequestCancelation(MAX_TIME_FOR_SUBSCRIPTION_FIX);
+ }
}
- // TODO: add provider status event etc.
+
+ // TODO: add another events processing
}
+
}
};
@@ -156,15 +98,15 @@ public class LocationRequester implements Handler.Callback
if (newLevel != mBatteryLevel)
{
+ mBatteryLevel = newLevel;
setUpProviders();
stopListening();
startListening();
- mBatteryLevel = newLevel;
Log.d(TAG, "Changed providers list due to battery level update.");
}
}
}
- };
+ }; // receivers
public LocationRequester(Context context)
{
@@ -178,9 +120,52 @@ public class LocationRequester implements Handler.Callback
mBatteryLevel = BatteryHelper.getBatteryLevelRange(mContext);
}
+ private void createPendingIntents()
+ {
+ mGpsLocationIntent = PendingIntent.getBroadcast(mContext, 0xF,
+ new Intent(ACTION_LOCATION).putExtra(KEY_SOURCE, EXTRA_IS_GPS), PendingIntent.FLAG_CANCEL_CURRENT);
+
+ mNetworkLocationIntent = PendingIntent.getBroadcast(mContext, 0xFF,
+ new Intent(ACTION_LOCATION).putExtra(KEY_SOURCE, EXTRA_IS_NETWORK), PendingIntent.FLAG_CANCEL_CURRENT);
+
+ mPassiveLocationIntent = PendingIntent.getBroadcast(mContext, 0xFFF,
+ new Intent(ACTION_LOCATION).putExtra(KEY_SOURCE, EXTRA_IS_PASSAIVE), PendingIntent.FLAG_CANCEL_CURRENT);
+ }
+
+ public PendingIntent getIntentForProvider(String provider)
+ {
+ if (LocationManager.GPS_PROVIDER.equals(provider))
+ return mGpsLocationIntent;
+
+ if (LocationManager.NETWORK_PROVIDER.equals(provider))
+ return mNetworkLocationIntent;
+
+ if (LocationManager.PASSIVE_PROVIDER.equals(provider))
+ return mPassiveLocationIntent;
+
+ throw new IllegalArgumentException("WTF is " + provider + "?");
+ }
+
+ public void setUpProviders()
+ {
+ 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);
+
+ 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);
+ }
+
public void startListening()
{
- for (final String provider: mEnabledProviders)
+ for (final String provider: mProviders)
{
if (mLocationManager.isProviderEnabled(provider))
{
@@ -196,19 +181,13 @@ public class LocationRequester implements Handler.Callback
mContext.registerReceiver(mBatteryReciever, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
mIsListening = true;
- // GPS cancellation
- mDelayedEventsHandler.removeMessages(WHAT_LOCATION_REQUEST_GPS_CANCELLATION);
- if (mCancelGpsIfNotFound)
- {
- final Message msg = mDelayedEventsHandler.obtainMessage(WHAT_LOCATION_REQUEST_GPS_CANCELLATION);
- mDelayedEventsHandler.sendMessageDelayed(msg, MAX_TIME_FOR_GPS_FIX);
- Log.d(TAG, "Send GPS cancelation in ms:" + MAX_TIME_FOR_GPS_FIX);
- }
+ if (mCancelIfNotFound)
+ postRequestCancelation(MAX_TIME_FOR_SUBSCRIPTION_FIX);
}
public void stopListening()
{
- for (final String provider : mEnabledProviders)
+ for (final String provider : mProviders)
{
mLocationManager.removeUpdates(getIntentForProvider(provider));
Log.d(TAG, "Stopped listening to: " + provider);
@@ -283,10 +262,18 @@ public class LocationRequester implements Handler.Callback
return false;
}
- public Location getLastLocation()
+ private boolean areLocationsFarEnough(Location l1, Location l2)
+ {
+ if (l1 == null || l2 == null)
+ return true;
+
+ return l1.distanceTo(l2) > 5;
+ }
+
+ public Location getLastKnownLocation()
{
Location res = null;
- for (final String provider : mEnabledProviders)
+ for (final String provider : mProviders)
{
if (mLocationManager.isProviderEnabled(provider))
{
@@ -300,9 +287,9 @@ public class LocationRequester implements Handler.Callback
public void requestSingleUpdate(long delayMillis)
{
- if (mEnabledProviders.size() > 0)
+ if (mProviders.size() > 0)
{
- for (final String provider : mEnabledProviders)
+ for (final String provider : mProviders)
{
if (mLocationManager.isProviderEnabled(provider))
mLocationManager.requestSingleUpdate(provider, getIntentForProvider(provider));
@@ -314,16 +301,16 @@ public class LocationRequester implements Handler.Callback
Log.d(TAG, "Send single update request");
}
- public void removeUpdates(PendingIntent pi)
- {
- mLocationManager.removeUpdates(pi);
- }
-
private void postRequestCancelation(long delayMillis)
{
- final Message msg =
- mDelayedEventsHandler.obtainMessage(WHAT_LOCATION_REQUEST_CANCELATION);
+ // remove old message
+ mDelayedEventsHandler.removeMessages(WHAT_LOCATION_REQUEST_CANCELATION);
+
+ final Message msg =mDelayedEventsHandler.obtainMessage(WHAT_LOCATION_REQUEST_CANCELATION);
+ // send new
mDelayedEventsHandler.sendMessageDelayed(msg, delayMillis);
+
+ Log.d(TAG, "Postponed cancelation in: " + delayMillis + " ms");
}
@Override
@@ -335,13 +322,44 @@ public class LocationRequester implements Handler.Callback
Log.d(TAG, "Removed all updates due to timeout");
return true;
}
- else if (msg.what == WHAT_LOCATION_REQUEST_GPS_CANCELLATION)
+ return false;
+ }
+
+ public void addListener(LocationListener listener)
+ {
+ mListeners.add(listener);
+ }
+
+ public void removeListener(LocationListener listener)
+ {
+ mListeners.remove(listener);
+ if (mListeners.isEmpty())
{
- mLocationManager.removeUpdates(mGpsLocationIntent);
- Log.d(TAG, "Removed updates for GPS");
- return true;
+ stopListening();
+ Log.d(TAG, "No more listeners, stopping.");
}
+ }
- return false;
+ 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 25225bdbd8..3c4286d08a 100644
--- a/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java
+++ b/android/YoPme/src/com/mapswithme/yopme/BackscreenActivity.java
@@ -226,6 +226,8 @@ public class BackscreenActivity extends BSActivity implements LocationListener
mLocation = new Location("MapsWithMe");
mLocation.setLatitude(myLat);
mLocation.setLongitude(myLon);
+
+ mLocationRequester.setLocation(mLocation);
}
if (ACTION_LOCATION.equals(intent.getAction()))
@@ -309,7 +311,7 @@ public class BackscreenActivity extends BSActivity implements LocationListener
// before requesting updates try to get last known in the first try
if (mLocation == null)
- mLocation = mLocationRequester.getLastLocation();
+ mLocation = mLocationRequester.getLastKnownLocation();
// then listen to updates
if (updateInterval == -1)
@@ -324,14 +326,6 @@ public class BackscreenActivity extends BSActivity implements LocationListener
}
}
- private boolean areLocationsFarEnough(Location l1, Location l2)
- {
- if (l1 == null || l2 == null)
- return true;
-
- return l1.distanceTo(l2) > 5;
- }
-
private void showWaitMessage(CharSequence msg)
{
mWaitMessage.setText(msg);
@@ -494,7 +488,6 @@ public class BackscreenActivity extends BSActivity implements LocationListener
@Override
public void onLocationChanged(Location l)
{
- if (LocationRequester.isFirstOneBetterLocation(l, mLocation) && areLocationsFarEnough(l, mLocation))
mLocation = l;
}