diff --git a/android/src/com/mapswithme/maps/geofence/GeoFenceFeature.java b/android/src/com/mapswithme/maps/geofence/GeoFenceFeature.java index 5bec82aafb..22c12f2488 100644 --- a/android/src/com/mapswithme/maps/geofence/GeoFenceFeature.java +++ b/android/src/com/mapswithme/maps/geofence/GeoFenceFeature.java @@ -119,4 +119,17 @@ public class GeoFenceFeature implements Parcelable return new GeoFenceFeature[size]; } }; + + @Override + public String toString() + { + final StringBuilder sb = new StringBuilder("GeoFenceFeature{"); + sb.append("mwmVersion=").append(mwmVersion); + sb.append(", countryId='").append(countryId).append('\''); + sb.append(", featureIndex=").append(featureIndex); + sb.append(", latitude=").append(latitude); + sb.append(", longitude=").append(longitude); + sb.append('}'); + return sb.toString(); + } } diff --git a/android/src/com/mapswithme/maps/geofence/GeofenceRegistry.java b/android/src/com/mapswithme/maps/geofence/GeofenceRegistry.java index 6559864ece..1e0f1f3f37 100644 --- a/android/src/com/mapswithme/maps/geofence/GeofenceRegistry.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceRegistry.java @@ -2,14 +2,10 @@ package com.mapswithme.maps.geofence; import android.support.annotation.NonNull; -import com.google.android.gms.location.Geofence; import com.mapswithme.maps.location.LocationPermissionNotGrantedException; public interface GeofenceRegistry { void registerGeofences(@NonNull GeofenceLocation location) throws LocationPermissionNotGrantedException; void unregisterGeofences() throws LocationPermissionNotGrantedException; - - @NonNull - GeoFenceFeature getFeatureByGeofence(@NonNull Geofence geofence); } diff --git a/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java b/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java index eeae7d0d2d..e49d17ea0c 100644 --- a/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java @@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit; public class GeofenceRegistryImpl implements GeofenceRegistry { + public static final String GEOFENCE_FEATURES_EXTRA = "geofence_features"; private static final int GEOFENCE_MAX_COUNT = 100; private static final int GEOFENCE_TTL_IN_DAYS = 3; private static final float PREFERRED_GEOFENCE_RADIUS = 100.0f; @@ -33,13 +34,10 @@ public class GeofenceRegistryImpl implements GeofenceRegistry @NonNull private final Application mApplication; @NonNull - private final List mGeofences; - @NonNull private final GeofencingClient mGeofencingClient; public GeofenceRegistryImpl(@NonNull Application application) { - mGeofences = new ArrayList<>(); mApplication = application; mGeofencingClient = LocationServices.getGeofencingClient(mApplication); } @@ -55,6 +53,8 @@ public class GeofenceRegistryImpl implements GeofenceRegistry if (features.isEmpty()) return; + + List geofences = new ArrayList<>(); for (GeoFenceFeature each : features) { Geofence geofence = new Geofence.Builder() @@ -64,10 +64,11 @@ public class GeofenceRegistryImpl implements GeofenceRegistry .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) .build(); - mGeofences.add(new GeofenceAndFeature(geofence, each)); + geofences.add(geofence); } - GeofencingRequest geofencingRequest = makeGeofencingRequest(); - PendingIntent intent = makeGeofencePendingIntent(); + + GeofencingRequest geofencingRequest = makeGeofencingRequest(geofences); + PendingIntent intent = makeGeofencePendingIntent(features); mGeofencingClient.addGeofences(geofencingRequest, intent) .addOnSuccessListener(params -> onAddSucceeded()) .addOnFailureListener(params -> onAddFailed()); @@ -78,23 +79,16 @@ public class GeofenceRegistryImpl implements GeofenceRegistry { checkThread(); checkPermission(); - mGeofencingClient.removeGeofences(makeGeofencePendingIntent()) + mGeofencingClient.removeGeofences(makeGeofenceCleanUpPendingIntent()) .addOnSuccessListener(params -> onRemoveFailed()) .addOnSuccessListener(params -> onRemoveSucceeded()); } @NonNull - @Override - public GeoFenceFeature getFeatureByGeofence(@NonNull Geofence geofence) + private PendingIntent makeGeofenceCleanUpPendingIntent() { - checkThread(); - for (GeofenceAndFeature each : mGeofences) - { - if (each.getGeofence().getRequestId().equals(geofence.getRequestId())) - return each.getFeature(); - - } - throw new IllegalArgumentException("Geofence not found"); + Intent intent = new Intent(mApplication, GeofenceReceiver.class); + return makeGeofencePendingIntent(intent); } private void onAddSucceeded() @@ -130,62 +124,31 @@ public class GeofenceRegistryImpl implements GeofenceRegistry } @NonNull - private PendingIntent makeGeofencePendingIntent() + private PendingIntent makeGeofencePendingIntent(@NonNull List features) { Intent intent = new Intent(mApplication, GeofenceReceiver.class); + intent.putParcelableArrayListExtra(GEOFENCE_FEATURES_EXTRA, new ArrayList(features)); + return makeGeofencePendingIntent(intent); + } + + private PendingIntent makeGeofencePendingIntent(@NonNull Intent intent) + { return PendingIntent.getBroadcast(mApplication, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } @NonNull - private GeofencingRequest makeGeofencingRequest() + private GeofencingRequest makeGeofencingRequest(@NonNull List geofences) { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); return builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER) - .addGeofences(collectGeofences()) + .addGeofences(geofences) .build(); } - @NonNull - private List collectGeofences() - { - List geofences = new ArrayList<>(); - for (GeofenceAndFeature each : mGeofences) - { - geofences.add(each.getGeofence()); - } - return geofences; - } - @NonNull public static GeofenceRegistry from(@NonNull Application application) { MwmApplication app = (MwmApplication) application; return app.getGeofenceRegistry(); } - - private static class GeofenceAndFeature - { - @NonNull - private final Geofence mGeofence; - @NonNull - private final GeoFenceFeature mFeature; - - private GeofenceAndFeature(@NonNull Geofence geofence, @NonNull GeoFenceFeature feature) - { - mGeofence = geofence; - mFeature = feature; - } - - @NonNull - public Geofence getGeofence() - { - return mGeofence; - } - - @NonNull - public GeoFenceFeature getFeature() - { - return mFeature; - } - } } diff --git a/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java b/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java index 2a9c6b964f..f55ff576d2 100644 --- a/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java @@ -7,10 +7,13 @@ import android.location.Location; import android.os.Handler; import android.os.Looper; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v4.app.JobIntentService; +import android.text.TextUtils; import com.google.android.gms.location.Geofence; import com.google.android.gms.location.GeofencingEvent; +import com.mapswithme.maps.LightFramework; import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.maps.location.LocationPermissionNotGrantedException; @@ -38,18 +41,21 @@ public class GeofenceTransitionsIntentService extends JobIntentService { LOG.d(TAG, "onHandleWork"); GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); + List features = Collections.unmodifiableList( + intent.getParcelableArrayListExtra(GeofenceRegistryImpl.GEOFENCE_FEATURES_EXTRA)); if (geofencingEvent.hasError()) onError(geofencingEvent); else - onSuccess(geofencingEvent); + onSuccess(geofencingEvent, features); } - private void onSuccess(@NonNull GeofencingEvent geofencingEvent) + private void onSuccess(@NonNull GeofencingEvent geofencingEvent, + @NonNull List features) { int transitionType = geofencingEvent.getGeofenceTransition(); if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) - onGeofenceEnter(geofencingEvent); + onGeofenceEnter(geofencingEvent, features); else if (transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) onGeofenceExit(geofencingEvent); } @@ -60,16 +66,18 @@ public class GeofenceTransitionsIntentService extends JobIntentService mMainThreadHandler.post(new GeofencingEventExitTask(getApplication(), geofenceLocation)); } - private void onGeofenceEnter(@NonNull GeofencingEvent geofencingEvent) + private void onGeofenceEnter(@NonNull GeofencingEvent geofencingEvent, + @NonNull List features) { - makeLocationProbesBlockingSafely(geofencingEvent); + makeLocationProbesBlockingSafely(geofencingEvent, features); } - private void makeLocationProbesBlockingSafely(@NonNull GeofencingEvent geofencingEvent) + private void makeLocationProbesBlockingSafely(@NonNull GeofencingEvent geofencingEvent, + @NonNull List features) { try { - makeLocationProbesBlocking(geofencingEvent); + makeLocationProbesBlocking(geofencingEvent, features); } catch (InterruptedException e) { @@ -77,25 +85,27 @@ public class GeofenceTransitionsIntentService extends JobIntentService } } - private void makeLocationProbesBlocking(@NonNull GeofencingEvent event) throws - InterruptedException + private void makeLocationProbesBlocking(@NonNull GeofencingEvent event, + @NonNull List features) throws + InterruptedException { CountDownLatch latch = new CountDownLatch(LOCATION_PROBES_MAX_COUNT); for (int i = 0; i < LOCATION_PROBES_MAX_COUNT; i++) { - makeSingleLocationProbe(event, i); + makeSingleLocationProbe(event, i, features); } latch.await(LOCATION_PROBES_MAX_COUNT, TimeUnit.MINUTES); } - private void makeSingleLocationProbe(@NonNull GeofencingEvent event, int timeoutInMinutes) + private void makeSingleLocationProbe(@NonNull GeofencingEvent event, int timeoutInMinutes, + @NonNull List features) { GeofenceLocation geofenceLocation = GeofenceLocation.from(event.getTriggeringLocation()); List geofences = Collections.unmodifiableList(event.getTriggeringGeofences()); CheckLocationTask locationTask = new CheckLocationTask( getApplication(), geofences, - geofenceLocation); + geofenceLocation, features); mMainThreadHandler.postDelayed(locationTask, TimeUnit.MINUTES.toMillis(timeoutInMinutes)); } @@ -115,12 +125,15 @@ public class GeofenceTransitionsIntentService extends JobIntentService { @NonNull private final List mGeofences; + @NonNull + private final List mFeatures; CheckLocationTask(@NonNull Application application, @NonNull List geofences, - @NonNull GeofenceLocation triggeringLocation) + @NonNull GeofenceLocation triggeringLocation, @NonNull List features) { super(application, triggeringLocation); mGeofences = geofences; + mFeatures = features; } @Override @@ -134,13 +147,27 @@ public class GeofenceTransitionsIntentService extends JobIntentService LOG.d(TAG, "Geofences = " + Arrays.toString(mGeofences.toArray())); GeofenceLocation geofenceLocation = getGeofenceLocation(); - GeofenceRegistry registry = GeofenceRegistryImpl.from(getApplication()); for (Geofence each : mGeofences) { - //GeoFenceFeature geoFenceFeature = registry.getFeatureByGeofence(each); -// LightFramework.logLocalAdsEvent(geofenceLocation, geoFenceFeature); + GeoFenceFeature feature = getFeatureByGeofence(each); + LOG.d(TAG, "Feature " + feature + " for geofence = " + each); + if (feature != null) + LightFramework.logLocalAdsEvent(geofenceLocation, feature); } } + + @Nullable + private GeoFenceFeature getFeatureByGeofence(@NonNull Geofence geofence) + { + for (GeoFenceFeature each : mFeatures) + { + if (TextUtils.equals(String.valueOf(each.hashCode()), geofence.getRequestId())) + { + return each; + } + } + return null; + } } private class GeofencingEventExitTask extends AbstractGeofenceTask @@ -186,8 +213,10 @@ public class GeofenceTransitionsIntentService extends JobIntentService @Override public void run() { - if (!getApplication().arePlatformAndCoreInitialized()) - getApplication().initCore(); + + /* FIXME */ + /* if (!getApplication().arePlatformAndCoreInitialized()) + getApplication().initCore();*/ runInternal(); }