[android] Fixed location state reset after device rotations.

This commit is contained in:
Dmitry Yunitsky 2015-09-04 21:14:56 +03:00 committed by Alex Zolotarev
parent 4f51e50a0d
commit 3d55d99e00
3 changed files with 57 additions and 40 deletions

View file

@ -196,33 +196,6 @@ public class MwmActivity extends BaseMwmFragmentActivity
.putExtra(DownloadResourcesActivity.EXTRA_UPDATE_COUNTRIES, true);
}
private void pauseLocation()
{
LocationHelper.INSTANCE.removeLocationListener(this);
// Enable automatic turning screen off while app is idle
Utils.keepScreenOn(false, getWindow());
mLocationPredictor.pause();
}
private void listenLocationUpdates()
{
LocationHelper.INSTANCE.addLocationListener(this);
// Do not turn off the screen while displaying position
Utils.keepScreenOn(true, getWindow());
mLocationPredictor.resume();
}
/**
* Invalidates location state in core.
* Updates location button accordingly.
*/
public void invalidateLocationState()
{
final int currentLocationMode = LocationState.INSTANCE.getLocationStateMode();
refreshLocationState(currentLocationMode);
LocationState.INSTANCE.invalidatePosition();
}
private void checkUserMarkActivation()
{
final Intent intent = getIntent();
@ -923,19 +896,19 @@ public class MwmActivity extends BaseMwmFragmentActivity
pauseLocation();
break;
case LocationState.PENDING_POSITION:
listenLocationUpdates();
resumeLocation();
break;
default:
break;
}
}
private void listenLocationStateModeUpdates()
private void listenLocationStateUpdates()
{
mLocationStateModeListenerId = LocationState.INSTANCE.addLocationStateModeListener(this);
}
private void stopWatchingCompassStatusUpdate()
private void stopLocationStateUpdates()
{
LocationState.INSTANCE.removeLocationStateModeListener(mLocationStateModeListenerId);
}
@ -945,7 +918,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
super.onResume();
listenLocationStateModeUpdates();
listenLocationStateUpdates();
invalidateLocationState();
startWatchingExternalStorage();
adjustZoomButtons(Framework.nativeIsRoutingActive());
@ -998,12 +971,39 @@ public class MwmActivity extends BaseMwmFragmentActivity
{
pauseLocation();
stopWatchingExternalStorage();
stopWatchingCompassStatusUpdate();
stopLocationStateUpdates();
TtsPlayer.INSTANCE.stop();
LikesManager.INSTANCE.cancelDialogs();
super.onPause();
}
private void resumeLocation()
{
LocationHelper.INSTANCE.addLocationListener(this);
// Do not turn off the screen while displaying position
Utils.keepScreenOn(true, getWindow());
mLocationPredictor.resume();
}
private void pauseLocation()
{
LocationHelper.INSTANCE.removeLocationListener(this);
// Enable automatic turning screen off while app is idle
Utils.keepScreenOn(false, getWindow());
mLocationPredictor.pause();
}
/**
* Invalidates location state in core.
* Updates location button accordingly.
*/
public void invalidateLocationState()
{
final int currentLocationMode = LocationState.INSTANCE.getLocationStateMode();
refreshLocationState(currentLocationMode);
LocationState.INSTANCE.invalidatePosition();
}
private void updateExternalStorageState()
{
boolean available = false, writable = false;

View file

@ -17,6 +17,7 @@ import java.util.List;
public class AndroidNativeProvider extends BaseLocationProvider implements android.location.LocationListener
{
private LocationManager mLocationManager;
private boolean mIsActive;
public AndroidNativeProvider()
{
@ -26,18 +27,21 @@ public class AndroidNativeProvider extends BaseLocationProvider implements andro
@Override
protected void startUpdates()
{
if (mIsActive)
return;
final List<String> providers = getFilteredProviders();
if (providers.size() == 0)
LocationHelper.INSTANCE.notifyLocationError(LocationHelper.ERROR_DENIED);
else
{
mIsActive = true;
for (final String provider : providers)
mLocationManager.requestLocationUpdates(provider, LOCATION_UPDATE_INTERVAL, 0, this);
LocationHelper.INSTANCE.registerSensorListeners();
// Choose best location from available
final Location newLocation = findBestNotExpiredLocation(providers);
if (isLocationBetterThanLast(newLocation))
LocationHelper.INSTANCE.setLastLocation(newLocation);
@ -55,6 +59,7 @@ public class AndroidNativeProvider extends BaseLocationProvider implements andro
protected void stopUpdates()
{
mLocationManager.removeUpdates(this);
mIsActive = false;
}
private Location findBestNotExpiredLocation(List<String> providers)

View file

@ -13,12 +13,14 @@ import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.util.LocationUtils;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.SimpleLogger;
@ -36,6 +38,7 @@ public enum LocationHelper implements SensorEventListener
public static final int ERROR_GPS_OFF = 3;
public static final String LOCATION_PREDICTOR_PROVIDER = "LocationPredictorProvider";
private static final long STOP_DELAY = 5000;
public interface LocationListener
{
@ -63,6 +66,17 @@ public enum LocationHelper implements SensorEventListener
private final float[] mI = new float[9];
private final float[] mOrientation = new float[3];
private Runnable mStopLocationTask = new Runnable() {
@Override
public void run()
{
mLocationProvider.stopUpdates();
mMagneticField = null;
if (mSensorManager != null)
mSensorManager.unregisterListener(LocationHelper.this);
}
};
LocationHelper()
{
mLogger = SimpleLogger.get(LocationHelper.class.getName());
@ -153,22 +167,20 @@ public enum LocationHelper implements SensorEventListener
public void addLocationListener(LocationListener listener)
{
UiThread.cancelDelayedTasks(mStopLocationTask);
if (mListeners.isEmpty())
mLocationProvider.startUpdates();
mListeners.add(listener);
notifyLocationUpdated();
}
public void removeLocationListener(LocationListener listener)
{
mListeners.remove(listener);
if (mListeners.isEmpty())
{
mLocationProvider.stopUpdates();
// Reset current parameters to force initialize in the next addLocationListener
mMagneticField = null;
if (mSensorManager != null)
mSensorManager.unregisterListener(LocationHelper.this);
}
// Make a delay with disconnection from location providers, so that orientation changes and short app sleeps
// doesn't take long time to connect again.
UiThread.runLater(mStopLocationTask, STOP_DELAY);
}
void registerSensorListeners()