Fix wake lock release issue
Этот коммит содержится в:
@@ -41,6 +41,16 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
|
|
||||||
private PowerManager.WakeLock wakeLock;
|
private PowerManager.WakeLock wakeLock;
|
||||||
|
|
||||||
|
private void lock() {
|
||||||
|
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void unlock() {
|
||||||
|
if (wakeLock.isHeld()) {
|
||||||
|
wakeLock.release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public TrackingController(Context context) {
|
public TrackingController(Context context) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
handler = new Handler();
|
handler = new Handler();
|
||||||
@@ -105,7 +115,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);
|
lock();
|
||||||
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) {
|
||||||
@@ -113,19 +123,19 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
read();
|
read();
|
||||||
isWaiting = false;
|
isWaiting = false;
|
||||||
}
|
}
|
||||||
wakeLock.release();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(RuntimeException error) {
|
public void onFailure(RuntimeException error) {
|
||||||
wakeLock.release();
|
unlock();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void read() {
|
private void read() {
|
||||||
log("read", null);
|
log("read", null);
|
||||||
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
lock();
|
||||||
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
databaseHelper.selectPositionAsync(new DatabaseHelper.DatabaseHandler<Position>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(Position result) {
|
public void onSuccess(Position result) {
|
||||||
@@ -134,38 +144,38 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
} else {
|
} else {
|
||||||
isWaiting = true;
|
isWaiting = true;
|
||||||
}
|
}
|
||||||
wakeLock.release();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(RuntimeException error) {
|
public void onFailure(RuntimeException error) {
|
||||||
retry();
|
retry();
|
||||||
wakeLock.release();
|
unlock();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void delete(Position position) {
|
private void delete(Position position) {
|
||||||
log("delete", position);
|
log("delete", position);
|
||||||
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
lock();
|
||||||
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();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(RuntimeException error) {
|
public void onFailure(RuntimeException error) {
|
||||||
retry();
|
retry();
|
||||||
wakeLock.release();
|
unlock();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void send(final Position position) {
|
private void send(final Position position) {
|
||||||
log("send", position);
|
log("send", position);
|
||||||
wakeLock.acquire(WAKE_LOCK_TIMEOUT);
|
lock();
|
||||||
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)),
|
||||||
@@ -175,14 +185,14 @@ public class TrackingController implements PositionProvider.PositionListener, Ne
|
|||||||
@Override
|
@Override
|
||||||
public void onSuccess() {
|
public void onSuccess() {
|
||||||
delete(position);
|
delete(position);
|
||||||
wakeLock.release();
|
unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
@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();
|
unlock();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user