Implement buffering logic
Этот коммит содержится в:
@@ -17,11 +17,17 @@ package org.traccar.client;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.os.Handler;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
|
|
||||||
public class TrackingController implements PositionProvider.PositionListener {
|
public class TrackingController implements PositionProvider.PositionListener {
|
||||||
|
|
||||||
|
private static final int RETRY_DELAY = 30 * 1000;
|
||||||
|
|
||||||
|
private boolean isWaiting;
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
|
private Handler handler;
|
||||||
private SharedPreferences preferences;
|
private SharedPreferences preferences;
|
||||||
|
|
||||||
private PositionProvider positionProvider;
|
private PositionProvider positionProvider;
|
||||||
@@ -29,6 +35,7 @@ public class TrackingController implements PositionProvider.PositionListener {
|
|||||||
|
|
||||||
public TrackingController(Context context) {
|
public TrackingController(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
handler = new Handler();
|
||||||
preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||||
positionProvider = new PositionProvider(context, this,
|
positionProvider = new PositionProvider(context, this,
|
||||||
preferences.getString(MainActivity.KEY_DEVICE, null),
|
preferences.getString(MainActivity.KEY_DEVICE, null),
|
||||||
@@ -43,15 +50,72 @@ public class TrackingController implements PositionProvider.PositionListener {
|
|||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
positionProvider.stopUpdates();
|
positionProvider.stopUpdates();
|
||||||
|
handler.removeCallbacksAndMessages(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPositionUpdate(Position position) {
|
public void onPositionUpdate(Position position) {
|
||||||
if (position != null) {
|
if (position != null) {
|
||||||
StatusActivity.addMessage(context.getString(R.string.status_location_update));
|
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(
|
String request = ProtocolFormatter.formatRequest(
|
||||||
preferences.getString(MainActivity.KEY_ADDRESS, null),
|
preferences.getString(MainActivity.KEY_ADDRESS, null),
|
||||||
Integer.parseInt(preferences.getString(MainActivity.KEY_PORT, 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() {
|
RequestManager.sendRequestAsync(request, new RequestManager.RequestHandler() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess() {
|
public void onSuccess() {
|
||||||
|
delete(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure() {
|
public void onFailure() {
|
||||||
StatusActivity.addMessage(context.getString(R.string.status_send_fail));
|
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