Implement buffering logic
Этот коммит содержится в:
@@ -17,11 +17,17 @@ package org.traccar.client;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
public class TrackingController implements PositionProvider.PositionListener {
|
||||
|
||||
private static final int RETRY_DELAY = 30 * 1000;
|
||||
|
||||
private boolean isWaiting;
|
||||
|
||||
private Context context;
|
||||
private Handler handler;
|
||||
private SharedPreferences preferences;
|
||||
|
||||
private PositionProvider positionProvider;
|
||||
@@ -29,6 +35,7 @@ public class TrackingController implements PositionProvider.PositionListener {
|
||||
|
||||
public TrackingController(Context context) {
|
||||
this.context = context;
|
||||
handler = new Handler();
|
||||
preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
positionProvider = new PositionProvider(context, this,
|
||||
preferences.getString(MainActivity.KEY_DEVICE, null),
|
||||
@@ -43,15 +50,72 @@ public class TrackingController implements PositionProvider.PositionListener {
|
||||
|
||||
public void stop() {
|
||||
positionProvider.stopUpdates();
|
||||
handler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPositionUpdate(Position position) {
|
||||
if (position != null) {
|
||||
StatusActivity.addMessage(context.getString(R.string.status_location_update));
|
||||
write(position);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// write -> read -> send -> delete -> read
|
||||
//
|
||||
// read -> send -> retry -> read -> send
|
||||
//
|
||||
|
||||
private void read() {
|
||||
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
||||
@Override
|
||||
public void onSuccess(Position result) {
|
||||
if (result != null) {
|
||||
send(result);
|
||||
} else {
|
||||
isWaiting = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(RuntimeException error) {
|
||||
retry();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void write(Position position) {
|
||||
databaseHelper.insertPositionAsync(position, new DatabaseHelper.DatabaseHandler<Void>() {
|
||||
@Override
|
||||
public void onSuccess(Void result) {
|
||||
if (isWaiting) {
|
||||
read();
|
||||
isWaiting = false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(RuntimeException error) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void delete(Position position) {
|
||||
databaseHelper.deletePositionAsync(position.getId(), new DatabaseHelper.DatabaseHandler<Void>() {
|
||||
@Override
|
||||
public void onSuccess(Void result) {
|
||||
read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(RuntimeException error) {
|
||||
retry();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void send(final Position position) {
|
||||
String request = ProtocolFormatter.formatRequest(
|
||||
preferences.getString(MainActivity.KEY_ADDRESS, null),
|
||||
Integer.parseInt(preferences.getString(MainActivity.KEY_PORT, null)),
|
||||
@@ -60,14 +124,24 @@ public class TrackingController implements PositionProvider.PositionListener {
|
||||
RequestManager.sendRequestAsync(request, new RequestManager.RequestHandler() {
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
delete(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure() {
|
||||
StatusActivity.addMessage(context.getString(R.string.status_send_fail));
|
||||
retry();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void retry() {
|
||||
handler.postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
read();
|
||||
}
|
||||
}, RETRY_DELAY);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user