Skip to content

Commit

Permalink
Allow stopping location updates on status "285 Updates Not Required"
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Aug 23, 2018
1 parent bd4f6c8 commit 3639d79
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ public void onReceive(Context context, Intent intent) {
mDelegate.onLocationChanged(location);
return;
}

case LocationService.MSG_ON_STATIONARY: {
logger.debug("Received MSG_ON_STATIONARY");
bundle.setClassLoader(LocationService.class.getClassLoader());
Expand All @@ -162,13 +163,15 @@ public void onReceive(Context context, Intent intent) {
mDelegate.onStationaryChanged(location);
return;
}

case LocationService.MSG_ON_ACTIVITY: {
logger.debug("Received MSG_ON_ACTIVITY");
bundle.setClassLoader(LocationService.class.getClassLoader());
BackgroundActivity activity = (BackgroundActivity) bundle.getParcelable("payload");
mDelegate.onActitivyChanged(activity);
return;
}

case LocationService.MSG_ON_ERROR: {
logger.debug("Received MSG_ON_ERROR");
Bundle errorBundle = bundle.getBundle("payload");
Expand All @@ -177,16 +180,34 @@ public void onReceive(Context context, Intent intent) {
mDelegate.onError(new PluginException(errorMessage, errorCode));
return;
}

case LocationService.MSG_ON_SERVICE_STARTED: {
logger.debug("Received MSG_ON_SERVICE_STARTED");
mDelegate.onServiceStatusChanged(LocationService.SERVICE_STARTED);
return;
}

case LocationService.MSG_ON_SERVICE_STOPPED: {
logger.debug("Received MSG_ON_SERVICE_STOPPED");
mDelegate.onServiceStatusChanged(LocationService.SERVICE_STOPPED);
return;
}

case LocationService.MSG_ON_ABORT_REQUESTED: {
logger.debug("Received MSG_ON_ABORT_REQUESTED");

if (mDelegate != null) {
// We have a delegate, tell it that there's a request.
// It will decide whether to stop or not.
mDelegate.onAbortRequested();
} else {
// No delegate, we may be running in the background.
// Let's just stop.
stop();
}

return;
}
}
}
};
Expand Down
36 changes: 33 additions & 3 deletions src/main/java/com/marianhello/bgloc/LocationService.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public class LocationService extends Service implements ProviderDelegate {

public static final int MSG_ON_SERVICE_STARTED = 105;

public static final int MSG_ON_ABORT_REQUESTED = 106;

public static final int SERVICE_STOPPED = 0;
public static final int SERVICE_STARTED = 1;

Expand Down Expand Up @@ -189,7 +191,12 @@ public void onCreate() {

mResolver = ResourceResolver.newInstance(this);
mLocationDAO = (DAOFactory.createLocationDAO(this));
mPostLocationTask = new PostLocationTask(mLocationDAO);
mPostLocationTask = new PostLocationTask(mLocationDAO, new PostLocationTaskListener() {
@Override
public void onRequestedAbortUpdates() {
handleRequestedAbortUpdates();
}
});
mSyncAccount = AccountHelper.CreateSyncAccount(this, SyncService.ACCOUNT_NAME,
mResolver.getString(SyncService.ACCOUNT_TYPE_RESOURCE));

Expand Down Expand Up @@ -538,10 +545,12 @@ LocationService getService() {
private class PostLocationTask {
private final ExecutorService mExecutor;
private final LocationDAO mLocationDAO;
private final PostLocationTaskListener mListener;

public PostLocationTask(LocationDAO dao) {
public PostLocationTask(LocationDAO dao, PostLocationTaskListener listener) {
mLocationDAO = dao;
mExecutor = Executors.newSingleThreadExecutor();
mListener = listener;
}

public void shutdown() {
Expand Down Expand Up @@ -612,7 +621,17 @@ private boolean postLocation(BackgroundLocation location) {
return false;
}

if (responseCode != HttpURLConnection.HTTP_OK && responseCode != HttpURLConnection.HTTP_CREATED) {
if (responseCode == 285) {
// Okay, but we don't need to continue sending these

logger.debug("Location was sent to the server, and received an \"HTTP 285 Updates Not Required\"");

if (mListener != null)
mListener.onRequestedAbortUpdates();
}

// All 2xx statuses are okay
if (responseCode >= 200 && responseCode < 300) {
logger.warn("Server error while posting locations responseCode: {}", responseCode);
return false;
}
Expand All @@ -621,6 +640,12 @@ private boolean postLocation(BackgroundLocation location) {
}
}

public void handleRequestedAbortUpdates() {
Bundle bundle = new Bundle();
bundle.putInt("action", MSG_ON_ABORT_REQUESTED);
broadcastMessage(bundle);
}

/**
* Broadcast receiver which detects connectivity change condition
*/
Expand Down Expand Up @@ -674,4 +699,9 @@ public interface ILocationTransform

@Nullable BackgroundLocation transformLocationBeforeCommit(@NonNull Context context, @NonNull BackgroundLocation location);
}

public interface PostLocationTaskListener
{
void onRequestedAbortUpdates();
}
}
1 change: 1 addition & 0 deletions src/main/java/com/marianhello/bgloc/PluginDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ public interface PluginDelegate {
void onStationaryChanged(BackgroundLocation location);
void onActitivyChanged(BackgroundActivity activity);
void onServiceStatusChanged(int status);
void onAbortRequested();
void onError(PluginException error);
}
28 changes: 25 additions & 3 deletions src/oreo/java/com/marianhello/bgloc/sync/SyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SyncResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;

import com.marianhello.bgloc.Config;
import com.marianhello.bgloc.HttpPostService;
import com.marianhello.bgloc.LocationService;
import com.marianhello.bgloc.NotificationHelper;
import com.marianhello.bgloc.UploadingCallback;
import com.marianhello.bgloc.data.ConfigurationDAO;
Expand All @@ -24,7 +27,6 @@

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;

/**
Expand Down Expand Up @@ -146,13 +148,27 @@ private boolean uploadLocations(File file, String url, HashMap httpHeaders) {

try {
int responseCode = HttpPostService.postFile(url, file, httpHeaders, this);
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {

// All 2xx statuses are okay
boolean isStatusOkay = responseCode >= 200 && responseCode < 300;

if (responseCode == 285) {
// Okay, but we don't need to continue sending these

logger.debug("Location was sent to the server, and received an \"HTTP 285 Updates Not Required\"");

Bundle bundle = new Bundle();
bundle.putInt("action", LocationService.MSG_ON_ABORT_REQUESTED);
broadcastMessage(bundle);
}

if (isStatusOkay) {
builder.setContentText("Sync completed");
} else {
builder.setContentText("Sync failed due server error");
}

return responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED;
return isStatusOkay;
} catch (IOException e) {
logger.warn("Error uploading locations: {}", e.getMessage());
builder.setContentText("Sync failed: " + e.getMessage());
Expand Down Expand Up @@ -187,4 +203,10 @@ public void uploadListener(int progress) {
builder.setProgress(100, progress, false);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}

private void broadcastMessage(Bundle bundle) {
Intent intent = new Intent(LocationService.ACTION_BROADCAST);
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(getContext().getApplicationContext()).sendBroadcast(intent);
}
}
27 changes: 25 additions & 2 deletions src/preoreo/java/com/marianhello/bgloc/sync/SyncAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SyncResult;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;

import com.marianhello.bgloc.Config;
import com.marianhello.bgloc.HttpPostService;
import com.marianhello.bgloc.LocationService;
import com.marianhello.bgloc.NotificationHelper;
import com.marianhello.bgloc.UploadingCallback;
import com.marianhello.bgloc.data.ConfigurationDAO;
Expand Down Expand Up @@ -146,13 +149,27 @@ private boolean uploadLocations(File file, String url, HashMap httpHeaders) {

try {
int responseCode = HttpPostService.postFile(url, file, httpHeaders, this);
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {

// All 2xx statuses are okay
boolean isStatusOkay = responseCode >= 200 && responseCode < 300;

if (responseCode == 285) {
// Okay, but we don't need to continue sending these

logger.debug("Location was sent to the server, and received an \"HTTP 285 Updates Not Required\"");

Bundle bundle = new Bundle();
bundle.putInt("action", LocationService.MSG_ON_ABORT_REQUESTED);
broadcastMessage(bundle);
}

if (isStatusOkay) {
builder.setContentText("Sync completed");
} else {
builder.setContentText("Sync failed due server error");
}

return responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED;
return isStatusOkay;
} catch (IOException e) {
logger.warn("Error uploading locations: {}", e.getMessage());
builder.setContentText("Sync failed: " + e.getMessage());
Expand Down Expand Up @@ -187,4 +204,10 @@ public void uploadListener(int progress) {
builder.setProgress(100, progress, false);
notificationManager.notify(NOTIFICATION_ID, builder.build());
}

private void broadcastMessage(Bundle bundle) {
Intent intent = new Intent(LocationService.ACTION_BROADCAST);
intent.putExtras(bundle);
LocalBroadcastManager.getInstance(getContext().getApplicationContext()).sendBroadcast(intent);
}
}

0 comments on commit 3639d79

Please sign in to comment.