|
|
|
@ -14,11 +14,14 @@ import com.google.common.base.Function;
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.tasks.R;
|
|
|
|
|
import org.tasks.injection.ForApplication;
|
|
|
|
|
import org.tasks.preferences.Preferences;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
|
|
import static com.google.android.gms.location.Geofence.NEVER_EXPIRE;
|
|
|
|
@ -31,14 +34,16 @@ public class GeofenceApi {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(GeofenceApi.class);
|
|
|
|
|
|
|
|
|
|
private Context context;
|
|
|
|
|
private Preferences preferences;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
public GeofenceApi(@ForApplication Context context) {
|
|
|
|
|
public GeofenceApi(@ForApplication Context context, Preferences preferences) {
|
|
|
|
|
this.context = context;
|
|
|
|
|
this.preferences = preferences;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void register(final List<Geofence> geofences) {
|
|
|
|
|
if (geofences.isEmpty()) {
|
|
|
|
|
if (geofences.isEmpty() || !preferences.geofencesEnabled()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -66,19 +71,32 @@ public class GeofenceApi {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cancel(final Geofence geofence) {
|
|
|
|
|
cancel(singletonList(geofence));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cancel(final List<Geofence> geofences) {
|
|
|
|
|
if (geofences.isEmpty()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final List<String> ids = newArrayList(transform(geofences, new Function<Geofence, String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public String apply(Geofence geofence) {
|
|
|
|
|
return Long.toString(geofence.getMetadataId());
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
newClient(new GoogleApi.GoogleApiClientConnectionHandler() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onConnect(final GoogleApiClient client) {
|
|
|
|
|
LocationServices.GeofencingApi.removeGeofences(
|
|
|
|
|
client,
|
|
|
|
|
singletonList(Long.toString(geofence.getMetadataId())))
|
|
|
|
|
LocationServices.GeofencingApi.removeGeofences(client, ids)
|
|
|
|
|
.setResultCallback(new ResultCallback<Status>() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onResult(Status status) {
|
|
|
|
|
if (status.isSuccess()) {
|
|
|
|
|
log.info("Removed {}", geofence);
|
|
|
|
|
log.info("Removed {}", geofences);
|
|
|
|
|
} else {
|
|
|
|
|
log.error("Failed to remove {}", geofence);
|
|
|
|
|
log.error("Failed to remove {}", geofences);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.disconnect();
|
|
|
|
@ -102,9 +120,11 @@ public class GeofenceApi {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private com.google.android.gms.location.Geofence toGoogleGeofence(Geofence geofence) {
|
|
|
|
|
int radius = preferences.getIntegerFromString(R.string.p_geofence_radius, 250);
|
|
|
|
|
int responsiveness = (int) TimeUnit.SECONDS.toMillis(preferences.getIntegerFromString(R.string.p_geofence_responsiveness, 60));
|
|
|
|
|
return new com.google.android.gms.location.Geofence.Builder()
|
|
|
|
|
.setCircularRegion(geofence.getLatitude(), geofence.getLongitude(), geofence.getRadius())
|
|
|
|
|
.setNotificationResponsiveness((int) TimeUnit.SECONDS.toMillis(30))
|
|
|
|
|
.setCircularRegion(geofence.getLatitude(), geofence.getLongitude(), radius)
|
|
|
|
|
.setNotificationResponsiveness(responsiveness)
|
|
|
|
|
.setRequestId(Long.toString(geofence.getMetadataId()))
|
|
|
|
|
.setTransitionTypes(GeofencingRequest.INITIAL_TRIGGER_ENTER)
|
|
|
|
|
.setExpirationDuration(NEVER_EXPIRE)
|
|
|
|
|