Move wake lock to controller
Этот коммит содержится в:
@@ -18,6 +18,7 @@ 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.os.Handler;
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -25,6 +26,7 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
|
|
||||||
private static final String TAG = TrackingController.class.getSimpleName();
|
private static final String TAG = TrackingController.class.getSimpleName();
|
||||||
private static final int RETRY_DELAY = 30 * 1000;
|
private static final int RETRY_DELAY = 30 * 1000;
|
||||||
|
private static final int WAKE_LOCK_TIMEOUT = 60 * 1000;
|
||||||
|
|
||||||
private boolean isOnline;
|
private boolean isOnline;
|
||||||
private boolean isWaiting;
|
private boolean isWaiting;
|
||||||
@@ -37,6 +39,8 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
private DatabaseHelper databaseHelper;
|
private DatabaseHelper databaseHelper;
|
||||||
private NetworkManager networkManager;
|
private NetworkManager networkManager;
|
||||||
|
|
||||||
|
private PowerManager.WakeLock wakeLock;
|
||||||
|
|
||||||
public TrackingController(Context context) {
|
public TrackingController(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
handler = new Handler();
|
handler = new Handler();
|
||||||
@@ -48,6 +52,9 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
databaseHelper = new DatabaseHelper(context);
|
databaseHelper = new DatabaseHelper(context);
|
||||||
networkManager = new NetworkManager(context, this);
|
networkManager = new NetworkManager(context, this);
|
||||||
isOnline = networkManager.isOnline();
|
isOnline = networkManager.isOnline();
|
||||||
|
|
||||||
|
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||||
|
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() {
|
public void start() {
|
||||||
@@ -101,6 +108,7 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
|
|
||||||
private void write(Position position) {
|
private void write(Position position) {
|
||||||
log("write", position);
|
log("write", position);
|
||||||
|
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
||||||
databaseHelper.insertPositionAsync(position, new DatabaseHelper.DatabaseHandler<Void>() {
|
databaseHelper.insertPositionAsync(position, new DatabaseHelper.DatabaseHandler<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(Void result) {
|
public void onSuccess(Void result) {
|
||||||
@@ -108,16 +116,19 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
read();
|
read();
|
||||||
isWaiting = false;
|
isWaiting = false;
|
||||||
}
|
}
|
||||||
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(RuntimeException error) {
|
public void onFailure(RuntimeException error) {
|
||||||
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void read() {
|
private void read() {
|
||||||
log("read", null);
|
log("read", null);
|
||||||
|
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
||||||
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(Position result) {
|
public void onSuccess(Position result) {
|
||||||
@@ -126,32 +137,38 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
} else {
|
} else {
|
||||||
isWaiting = true;
|
isWaiting = true;
|
||||||
}
|
}
|
||||||
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(RuntimeException error) {
|
public void onFailure(RuntimeException error) {
|
||||||
retry();
|
retry();
|
||||||
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void delete(Position position) {
|
private void delete(Position position) {
|
||||||
log("delete", position);
|
log("delete", position);
|
||||||
|
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
||||||
databaseHelper.deletePositionAsync(position.getId(), new DatabaseHelper.DatabaseHandler<Void>() {
|
databaseHelper.deletePositionAsync(position.getId(), new DatabaseHelper.DatabaseHandler<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(Void result) {
|
public void onSuccess(Void result) {
|
||||||
read();
|
read();
|
||||||
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(RuntimeException error) {
|
public void onFailure(RuntimeException error) {
|
||||||
retry();
|
retry();
|
||||||
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void send(final Position position) {
|
private void send(final Position position) {
|
||||||
log("send", position);
|
log("send", position);
|
||||||
|
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
||||||
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)),
|
||||||
@@ -161,12 +178,14 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
@Override
|
@Override
|
||||||
public void onSuccess() {
|
public void onSuccess() {
|
||||||
delete(position);
|
delete(position);
|
||||||
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
@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();
|
retry();
|
||||||
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,12 +17,9 @@ package org.traccar.client;
|
|||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.content.Context;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.PowerManager;
|
|
||||||
import android.os.PowerManager.WakeLock;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
public class TrackingService extends Service {
|
public class TrackingService extends Service {
|
||||||
@@ -31,17 +28,11 @@ public class TrackingService extends Service {
|
|||||||
|
|
||||||
private TrackingController trackingController;
|
private TrackingController trackingController;
|
||||||
|
|
||||||
private WakeLock wakeLock;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
Log.i(TAG, "service create");
|
Log.i(TAG, "service create");
|
||||||
StatusActivity.addMessage(getString(R.string.status_service_create));
|
StatusActivity.addMessage(getString(R.string.status_service_create));
|
||||||
|
|
||||||
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
||||||
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
|
|
||||||
wakeLock.acquire();
|
|
||||||
|
|
||||||
trackingController = new TrackingController(this);
|
trackingController = new TrackingController(this);
|
||||||
trackingController.start();
|
trackingController.start();
|
||||||
}
|
}
|
||||||
@@ -72,8 +63,6 @@ public class TrackingService extends Service {
|
|||||||
if (trackingController != null) {
|
if (trackingController != null) {
|
||||||
trackingController.stop();
|
trackingController.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
wakeLock.release();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user