Skip to content

Instantly share code, notes, and snippets.

@amadeu01
Last active August 4, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amadeu01/2a842f6932dbe88b48c96999c54f27eb to your computer and use it in GitHub Desktop.
Save amadeu01/2a842f6932dbe88b48c96999c54f27eb to your computer and use it in GitHub Desktop.
Provide location from route.json
import android.content.Context;
import android.content.res.AssetManager;
import android.location.Location;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.util.List;
public class MockPositionProvider implements PositionProvider {
private static Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
.excludeFieldsWithoutExposeAnnotation()
.create();
private CurrentPositionListener currentPositionListener;
private MockRoute mockRoute;
private Handler mHandler;
private HandlerThread mHandlerThread;
private Runnable positionMockRunnable = new Runnable() {
@Override
public void run() {
while (mockRoute.route.size() > 2) {
long start = System.currentTimeMillis();
MockPosition mockPosition = mockRoute.route.remove(0);
MockPosition nextMockPosition = mockRoute.route.get(0);
float[] distanceResult = new float[2];
Location.distanceBetween(
mockPosition.latitude,
mockPosition.longitude,
nextMockPosition.latitude,
nextMockPosition.longitude,
distanceResult
);
Location location = new Location("gps");
location.setAccuracy(mockPosition.accuracy);
location.setLatitude(mockPosition.latitude);
location.setLongitude(mockPosition.longitude);
location.setSpeed((float) mockPosition.speed);
location.setBearing(distanceResult[1]);
onLocationUpdate(location);
long sleepTime = Math.abs(
mockPosition.positionTimestamp - mockRoute.route.get(0).positionTimestamp
);
try {
Thread.sleep(sleepTime);
} catch (Exception e) {
App.logException(e);
}
long stop = System.currentTimeMillis();
Log.d("MockProvider",
"Start: " + String.valueOf(start) +
" Stop: " + String.valueOf(stop) +
" Difference: " + String.valueOf(stop - start) +
" Distance between two next points: "
+ String.valueOf(distanceResult[0]) +
" Bearing: " + String.valueOf(distanceResult[1])
);
}
}
};
public MockPositionProvider(Context context) {
this.mockRoute = getRouteFromAssets(context);
mHandlerThread = new HandlerThread("MockProvider");
mHandlerThread.start();
mHandler = new Handler(mHandlerThread.getLooper());
mHandler.post(this.positionMockRunnable);
}
@Override
public void requestLocationUpdates(
boolean shouldRequestGPSProvider,
boolean shouldRequestNetworkProvider,
boolean shouldRequestPassiveProvider) {
}
void onLocationUpdate(Location location) {
currentPositionListener.onCurrentPositionUpdate(location);
}
@Override
public void stopLocationUpdates() {
}
@Override
public void setCurrentPositionListener(CurrentPositionListener currentPositionListener) {
this.currentPositionListener = currentPositionListener;
}
public static MockRoute getRouteFromAssets(Context context) {
try {
String mockRouteJsonString = assetJSONFile("mock_routes/mock_route.json", context);
return gson.fromJson(mockRouteJsonString, MockRoute.class);
} catch (Exception exception) {
App.logException(exception);
}
return null;
}
private static String assetJSONFile (String filename, Context context) throws IOException {
AssetManager manager = context.getAssets();
InputStream file = manager.open(filename);
byte[] formArray = new byte[file.available()];
file.read(formArray);
file.close();
return new String(formArray);
}
public class MockRoute {
@Expose
@SerializedName("id_route")
int routeId;
@Expose
@SerializedName("id_vehicle")
int vehicleId;
@Expose
@SerializedName("route")
List<MockPosition> route;
}
public class MockPosition {
@Expose
@SerializedName("speed")
double speed;
@Expose
@SerializedName("accuracy")
int accuracy;
@Expose
@SerializedName("log_date")
long positionTimestamp;
@Expose
@SerializedName("latitude")
double latitude;
@Expose
@SerializedName("longitude")
double longitude;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment