From 532f0fedf250da2208cefd347e28e73593e1a301 Mon Sep 17 00:00:00 2001 From: Dmitry Donskoy Date: Fri, 21 Dec 2018 16:38:13 +0300 Subject: [PATCH] [android] Added "awesome" request id --- .../com/mapswithme/maps/LightFramework.java | 6 +- .../maps/bookmarks/data/FeatureId.java | 9 +++ .../maps/geofence/GeoFenceFeature.java | 6 +- .../maps/geofence/GeofenceRegistryImpl.java | 37 ++++-------- .../GeofenceTransitionsIntentService.java | 60 ++++++------------- 5 files changed, 45 insertions(+), 73 deletions(-) diff --git a/android/src/com/mapswithme/maps/LightFramework.java b/android/src/com/mapswithme/maps/LightFramework.java index 1c207c3c87..4905569658 100644 --- a/android/src/com/mapswithme/maps/LightFramework.java +++ b/android/src/com/mapswithme/maps/LightFramework.java @@ -4,6 +4,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import com.mapswithme.maps.background.NotificationCandidate; +import com.mapswithme.maps.bookmarks.data.FeatureId; import com.mapswithme.maps.geofence.GeoFenceFeature; import com.mapswithme.maps.geofence.GeofenceLocation; @@ -31,13 +32,12 @@ public class LightFramework } public static void logLocalAdsEvent(@NonNull GeofenceLocation location, - @NonNull GeoFenceFeature feature) + @NonNull FeatureId feature) { nativeLogLocalAdsEvent(Framework.LocalAdsEventType.LOCAL_ADS_EVENT_VISIT.ordinal(), location.getLat(), location.getLon(), - /* FIXME */ (int) location.getRadiusInMeters(), feature.getMwmVersion(), - feature.getCountryId(), feature.getFeatureIndex()); + feature.getMwmName(), feature.getFeatureIndex()); } private static native void nativeLogLocalAdsEvent(int type, double lat, double lon, diff --git a/android/src/com/mapswithme/maps/bookmarks/data/FeatureId.java b/android/src/com/mapswithme/maps/bookmarks/data/FeatureId.java index 5ff556b051..ad9ffe96de 100644 --- a/android/src/com/mapswithme/maps/bookmarks/data/FeatureId.java +++ b/android/src/com/mapswithme/maps/bookmarks/data/FeatureId.java @@ -5,6 +5,8 @@ import android.os.Parcelable; import android.support.annotation.NonNull; import android.text.TextUtils; +import com.google.android.gms.location.Geofence; + public class FeatureId implements Parcelable { public static final Creator CREATOR = new Creator() @@ -114,4 +116,11 @@ public class FeatureId implements Parcelable { return mMwmName + ":" + mMwmVersion + ":" + mFeatureIndex; } + + @NonNull + public static FeatureId from(@NonNull Geofence geofence) + { + String requestId = geofence.getRequestId(); + return fromString(requestId); + } } diff --git a/android/src/com/mapswithme/maps/geofence/GeoFenceFeature.java b/android/src/com/mapswithme/maps/geofence/GeoFenceFeature.java index 22c12f2488..0bb85d6754 100644 --- a/android/src/com/mapswithme/maps/geofence/GeoFenceFeature.java +++ b/android/src/com/mapswithme/maps/geofence/GeoFenceFeature.java @@ -4,6 +4,8 @@ import android.os.Parcel; import android.os.Parcelable; import android.support.annotation.NonNull; +import com.mapswithme.maps.bookmarks.data.FeatureId; + /** * Represents CampaignFeature from core. */ @@ -75,9 +77,9 @@ public class GeoFenceFeature implements Parcelable } @NonNull - public String getId() + public FeatureId getId() { - return String.valueOf(hashCode()); + return new FeatureId(countryId, mwmVersion, featureIndex); } @Override diff --git a/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java b/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java index e49d17ea0c..f7162656db 100644 --- a/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java @@ -9,7 +9,6 @@ import com.google.android.gms.location.Geofence; import com.google.android.gms.location.GeofencingClient; import com.google.android.gms.location.GeofencingRequest; import com.google.android.gms.location.LocationServices; -import com.mapswithme.maps.LightFramework; import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.location.LocationPermissionNotGrantedException; import com.mapswithme.util.PermissionsUtils; @@ -18,6 +17,7 @@ import com.mapswithme.util.log.Logger; import com.mapswithme.util.log.LoggerFactory; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; @@ -48,30 +48,30 @@ public class GeofenceRegistryImpl implements GeofenceRegistry checkThread(); checkPermission(); - List features = LightFramework.getLocalAdsFeatures( - location.getLat(), location.getLon(), location.getRadiusInMeters(), GEOFENCE_MAX_COUNT); + List features = Arrays.asList(new GeoFenceFeature(1, "a", 2, location.getLat(), location.getLon()));/*LightFramework.getLocalAdsFeatures( + location.getLat(), location.getLon(), location.getRadiusInMeters(), GEOFENCE_MAX_COUNT);*/ if (features.isEmpty()) return; - List geofences = new ArrayList<>(); for (GeoFenceFeature each : features) { Geofence geofence = new Geofence.Builder() - .setRequestId(each.getId()) + .setRequestId(each.getId().toString()) .setCircularRegion(each.getLatitude(), each.getLongitude(), PREFERRED_GEOFENCE_RADIUS) .setExpirationDuration(TimeUnit.DAYS.toMillis(GEOFENCE_TTL_IN_DAYS)) - .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER - | Geofence.GEOFENCE_TRANSITION_EXIT) + .setLoiteringDelay(1) + .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL) .build(); - geofences.add(geofence); + geofences.add(geofence); } GeofencingRequest geofencingRequest = makeGeofencingRequest(geofences); - PendingIntent intent = makeGeofencePendingIntent(features); + PendingIntent intent = makeGeofencePendingIntent(); mGeofencingClient.addGeofences(geofencingRequest, intent) .addOnSuccessListener(params -> onAddSucceeded()) .addOnFailureListener(params -> onAddFailed()); + } @Override @@ -79,18 +79,11 @@ public class GeofenceRegistryImpl implements GeofenceRegistry { checkThread(); checkPermission(); - mGeofencingClient.removeGeofences(makeGeofenceCleanUpPendingIntent()) + mGeofencingClient.removeGeofences(makeGeofencePendingIntent()) .addOnSuccessListener(params -> onRemoveFailed()) .addOnSuccessListener(params -> onRemoveSucceeded()); } - @NonNull - private PendingIntent makeGeofenceCleanUpPendingIntent() - { - Intent intent = new Intent(mApplication, GeofenceReceiver.class); - return makeGeofencePendingIntent(intent); - } - private void onAddSucceeded() { LOG.d(TAG, "onAddSucceeded"); @@ -124,15 +117,9 @@ public class GeofenceRegistryImpl implements GeofenceRegistry } @NonNull - private PendingIntent makeGeofencePendingIntent(@NonNull List features) + private PendingIntent makeGeofencePendingIntent() { 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); } @@ -140,7 +127,7 @@ public class GeofenceRegistryImpl implements GeofenceRegistry private GeofencingRequest makeGeofencingRequest(@NonNull List geofences) { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); - return builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER) + return builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL) .addGeofences(geofences) .build(); } diff --git a/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java b/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java index f55ff576d2..44c8e1ab73 100644 --- a/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java @@ -7,14 +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.bookmarks.data.FeatureId; import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.maps.location.LocationPermissionNotGrantedException; import com.mapswithme.maps.scheduling.JobIdMap; @@ -40,22 +39,19 @@ public class GeofenceTransitionsIntentService extends JobIntentService protected void onHandleWork(@NonNull Intent intent) { LOG.d(TAG, "onHandleWork"); - GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); - List features = Collections.unmodifiableList( - intent.getParcelableArrayListExtra(GeofenceRegistryImpl.GEOFENCE_FEATURES_EXTRA)); + GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);; if (geofencingEvent.hasError()) onError(geofencingEvent); else - onSuccess(geofencingEvent, features); + onSuccess(geofencingEvent); } - private void onSuccess(@NonNull GeofencingEvent geofencingEvent, - @NonNull List features) + private void onSuccess(@NonNull GeofencingEvent geofencingEvent) { int transitionType = geofencingEvent.getGeofenceTransition(); - if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) - onGeofenceEnter(geofencingEvent, features); + if (transitionType == Geofence.GEOFENCE_TRANSITION_DWELL) + onGeofenceEnter(geofencingEvent); else if (transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) onGeofenceExit(geofencingEvent); } @@ -66,18 +62,16 @@ public class GeofenceTransitionsIntentService extends JobIntentService mMainThreadHandler.post(new GeofencingEventExitTask(getApplication(), geofenceLocation)); } - private void onGeofenceEnter(@NonNull GeofencingEvent geofencingEvent, - @NonNull List features) + private void onGeofenceEnter(@NonNull GeofencingEvent geofencingEvent) { - makeLocationProbesBlockingSafely(geofencingEvent, features); + makeLocationProbesBlockingSafely(geofencingEvent); } - private void makeLocationProbesBlockingSafely(@NonNull GeofencingEvent geofencingEvent, - @NonNull List features) + private void makeLocationProbesBlockingSafely(@NonNull GeofencingEvent geofencingEvent) { try { - makeLocationProbesBlocking(geofencingEvent, features); + makeLocationProbesBlocking(geofencingEvent); } catch (InterruptedException e) { @@ -85,27 +79,25 @@ public class GeofenceTransitionsIntentService extends JobIntentService } } - private void makeLocationProbesBlocking(@NonNull GeofencingEvent event, - @NonNull List features) throws + private void makeLocationProbesBlocking(@NonNull GeofencingEvent event) throws InterruptedException { CountDownLatch latch = new CountDownLatch(LOCATION_PROBES_MAX_COUNT); for (int i = 0; i < LOCATION_PROBES_MAX_COUNT; i++) { - makeSingleLocationProbe(event, i, features); + makeSingleLocationProbe(event, i); } latch.await(LOCATION_PROBES_MAX_COUNT, TimeUnit.MINUTES); } - private void makeSingleLocationProbe(@NonNull GeofencingEvent event, int timeoutInMinutes, - @NonNull List features) + private void makeSingleLocationProbe(@NonNull GeofencingEvent event, int timeoutInMinutes) { GeofenceLocation geofenceLocation = GeofenceLocation.from(event.getTriggeringLocation()); List geofences = Collections.unmodifiableList(event.getTriggeringGeofences()); CheckLocationTask locationTask = new CheckLocationTask( getApplication(), geofences, - geofenceLocation, features); + geofenceLocation); mMainThreadHandler.postDelayed(locationTask, TimeUnit.MINUTES.toMillis(timeoutInMinutes)); } @@ -125,15 +117,12 @@ 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 List features) + @NonNull GeofenceLocation triggeringLocation) { super(application, triggeringLocation); mGeofences = geofences; - mFeatures = features; } @Override @@ -149,25 +138,10 @@ public class GeofenceTransitionsIntentService extends JobIntentService GeofenceLocation geofenceLocation = getGeofenceLocation(); for (Geofence each : mGeofences) { - GeoFenceFeature feature = getFeatureByGeofence(each); - LOG.d(TAG, "Feature " + feature + " for geofence = " + each); - if (feature != null) - LightFramework.logLocalAdsEvent(geofenceLocation, feature); + FeatureId feature = FeatureId.from(each); + 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