I think refresh time was fixed.
parent
f52a6e7a9a
commit
c2f7c563cb
|
@ -4,6 +4,7 @@ import android.app.ProgressDialog;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Looper;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentManager;
|
import android.support.v4.app.FragmentManager;
|
||||||
import android.support.v4.app.FragmentTransaction;
|
import android.support.v4.app.FragmentTransaction;
|
||||||
|
@ -106,7 +107,7 @@ public class MainActivity extends ActionBarActivity {
|
||||||
|
|
||||||
swipeHandler = new SwipeHandler(this);
|
swipeHandler = new SwipeHandler(this);
|
||||||
gestureDetector = new GestureDetectorCompat(this, swipeHandler);
|
gestureDetector = new GestureDetectorCompat(this, swipeHandler);
|
||||||
locationCallback = new StandardLocationListener();
|
locationCallback = new StandardLocationListener(this);
|
||||||
|
|
||||||
// init fragments
|
// init fragments
|
||||||
MainActivityState fragId = savedInstanceState != null
|
MainActivityState fragId = savedInstanceState != null
|
||||||
|
@ -300,14 +301,27 @@ public class MainActivity extends ActionBarActivity {
|
||||||
|
|
||||||
lg.debug("Selected location provider {} is available", locationProvider);
|
lg.debug("Selected location provider {} is available", locationProvider);
|
||||||
|
|
||||||
|
final Long updateTime = HelperUtils.fetchGpsUpdateFreq(this);
|
||||||
|
lg.debug("Update time: {}", updateTime);
|
||||||
if (locationProvider != null) {
|
if (locationProvider != null) {
|
||||||
lg.debug("Requesting location updates");
|
lg.debug("Requesting location updates");
|
||||||
LocationManager locationService = (LocationManager)getSystemService(LOCATION_SERVICE);
|
LocationManager locationService = (LocationManager)getSystemService(LOCATION_SERVICE);
|
||||||
locationService.requestLocationUpdates(locationProvider,
|
locationService.requestLocationUpdates(locationProvider,
|
||||||
HelperUtils.fetchGpsUpdateFreq(this),
|
updateTime,
|
||||||
HelperUtils.fetchGpsDistanceFreq(this),
|
HelperUtils.fetchGpsDistanceFreq(this),
|
||||||
locationCallback);
|
locationCallback);
|
||||||
locationServicesActivated = true;
|
locationServicesActivated = true;
|
||||||
|
|
||||||
|
final Long lastLocationUpdateTime = LocationUtils.getLastLocationUpdate(this);
|
||||||
|
lg.debug("Last location update time: {}", lastLocationUpdateTime);
|
||||||
|
final long now = System.currentTimeMillis();
|
||||||
|
lg.debug("Now: {}", now);
|
||||||
|
final long lastLocationUpdateTimeAgo = now - lastLocationUpdateTime;
|
||||||
|
lg.debug("Last location update was {} ms ago", lastLocationUpdateTimeAgo);
|
||||||
|
if (lastLocationUpdateTime < 0 || lastLocationUpdateTimeAgo >= updateTime) {
|
||||||
|
lg.info("Last location update time exceeded. Requesting single update...");
|
||||||
|
locationService.requestSingleUpdate(locationProvider, locationCallback, Looper.getMainLooper());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
savedConfiguration = false;
|
savedConfiguration = false;
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package pl.tpolgrabia.urbanexplorer.callbacks;
|
package pl.tpolgrabia.urbanexplorer.callbacks;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.location.LocationListener;
|
import android.location.LocationListener;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.widget.Toast;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import pl.tpolgrabia.panoramiobindings.callback.ProviderStatusCallback;
|
import pl.tpolgrabia.panoramiobindings.callback.ProviderStatusCallback;
|
||||||
import pl.tpolgrabia.urbanexplorerutils.callbacks.StandardLocationListenerCallback;
|
import pl.tpolgrabia.urbanexplorerutils.callbacks.StandardLocationListenerCallback;
|
||||||
|
import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -16,21 +19,29 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class StandardLocationListener implements LocationListener {
|
public class StandardLocationListener implements LocationListener {
|
||||||
private static final Logger lg = LoggerFactory.getLogger(StandardLocationListener.class);
|
private static final Logger lg = LoggerFactory.getLogger(StandardLocationListener.class);
|
||||||
|
private final Context ctx;
|
||||||
private List<StandardLocationListenerCallback> locationChangedCallbacks = new ArrayList<>();
|
private List<StandardLocationListenerCallback> locationChangedCallbacks = new ArrayList<>();
|
||||||
private List<ProviderStatusCallback>
|
private List<ProviderStatusCallback>
|
||||||
providerStatusCallbacks = new ArrayList<>();
|
providerStatusCallbacks = new ArrayList<>();
|
||||||
|
|
||||||
|
public StandardLocationListener(Context ctx) {
|
||||||
|
this.ctx = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLocationChanged(Location location) {
|
public void onLocationChanged(Location location) {
|
||||||
lg.info("Location provider changed: {}", location);
|
lg.info("Location provider changed: {}", location);
|
||||||
for (StandardLocationListenerCallback callback : locationChangedCallbacks) {
|
for (StandardLocationListenerCallback callback : locationChangedCallbacks) {
|
||||||
callback.callback(location);
|
callback.callback(location);
|
||||||
}
|
}
|
||||||
|
Toast.makeText(ctx, "Location changed " + location, Toast.LENGTH_LONG).show();
|
||||||
|
LocationUtils.updateLastLocationUPdate(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStatusChanged(String provider, int status, Bundle extras) {
|
public void onStatusChanged(String provider, int status, Bundle extras) {
|
||||||
lg.debug("Location provider {} status has changed to {} with {}", provider, status, extras);
|
lg.debug("Location provider {} status has changed to {} with {}", provider, status, extras);
|
||||||
|
Toast.makeText(ctx, "Location provider " + provider + " status changed to " + status, Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -6,12 +6,16 @@ import android.location.LocationManager;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by tpolgrabia on 28.08.16.
|
* Created by tpolgrabia on 28.08.16.
|
||||||
*/
|
*/
|
||||||
public class LocationUtils {
|
public class LocationUtils {
|
||||||
|
|
||||||
private static final Logger lg = LoggerFactory.getLogger(LocationUtils.class);
|
private static final Logger lg = LoggerFactory.getLogger(LocationUtils.class);
|
||||||
|
public static final String LOCATION_UPDATE_TIME_CACHE = "location-cache.dat";
|
||||||
|
|
||||||
public static String getDefaultLocation(Context ctx) {
|
public static String getDefaultLocation(Context ctx) {
|
||||||
|
|
||||||
if (ctx == null) {
|
if (ctx == null) {
|
||||||
|
@ -46,4 +50,55 @@ public class LocationUtils {
|
||||||
|
|
||||||
return NetUtils.getSystemService(ctx).getLastKnownLocation(locationProvider);
|
return NetUtils.getSystemService(ctx).getLastKnownLocation(locationProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void updateLastLocationUPdate(Context ctx) {
|
||||||
|
File f = new File(ctx.getCacheDir(), LOCATION_UPDATE_TIME_CACHE);
|
||||||
|
BufferedWriter bw = null;
|
||||||
|
try {
|
||||||
|
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, false)));
|
||||||
|
bw.write("" + System.currentTimeMillis());
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
lg.error("File cannot be found", e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
lg.error("Error during writing to file", e);
|
||||||
|
} finally {
|
||||||
|
if (bw != null) {
|
||||||
|
try {
|
||||||
|
bw.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
lg.error("Error during writer closing", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Long getLastLocationUpdate(Context ctx) {
|
||||||
|
File f = new File(ctx.getCacheDir(), LOCATION_UPDATE_TIME_CACHE);
|
||||||
|
BufferedReader br = null;
|
||||||
|
try {
|
||||||
|
br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = br.readLine()) != null) {
|
||||||
|
sb.append(line);
|
||||||
|
}
|
||||||
|
return Long.parseLong(sb.toString());
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
return Long.MIN_VALUE;
|
||||||
|
} catch (IOException e) {
|
||||||
|
lg.error("I/O error during reading last location update timestamp", e);
|
||||||
|
return Long.MIN_VALUE;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
lg.error("Wrong timestamp format", e);
|
||||||
|
return Long.MIN_VALUE;
|
||||||
|
} finally {
|
||||||
|
if (br != null) {
|
||||||
|
try {
|
||||||
|
br.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
lg.error("Cannot close reader", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue