From 0aa659b42377e37573557da417dc627b8d854327 Mon Sep 17 00:00:00 2001 From: Dmitry Donskoy Date: Fri, 21 Dec 2018 11:45:40 +0300 Subject: [PATCH] [android] Added scheduling for tests --- android/AndroidManifest.xml | 3 + .../geofence/CheckGeofenceEnterService.java | 73 +++++++++++++++++++ .../maps/geofence/GeofenceRegistryImpl.java | 22 +++--- .../GeofenceTransitionsIntentService.java | 57 ++++++++++----- .../mapswithme/maps/scheduling/JobIdMap.java | 2 + 5 files changed, 130 insertions(+), 27 deletions(-) create mode 100644 android/src/com/mapswithme/maps/geofence/CheckGeofenceEnterService.java diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index 1e8e66a673..9c035f8a76 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -570,6 +570,9 @@ android:name="com.mapswithme.maps.geofence.GeofenceTransitionsIntentService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/> + geofences = Collections.unmodifiableList(geofencingEvent.getTriggeringGeofences()); + CheckLocationTask locationTask = new CheckLocationTask( + getApplication(), + geofences, + geofenceLocation); + UiThread.runLater(locationTask); + } + + public static void enqueueWork(@NonNull Context context, @NonNull Intent intent) + { + int id = JobIdMap.getId(CheckGeofenceEnterService.class); + enqueueWork(context, CheckGeofenceEnterService.class, id, intent); + } + + private static class CheckLocationTask extends GeofenceTransitionsIntentService.AbstractGeofenceTask + { + @NonNull + private final List mGeofences; + + CheckLocationTask(@NonNull Application application, @NonNull List geofences, + @NonNull GeofenceLocation triggeringLocation) + { + super(application, triggeringLocation); + mGeofences = geofences; + } + + @Override + public void run() + { + requestLocationCheck(); + } + + private void requestLocationCheck() + { +// LOG.d(TAG, "requestLocationCheck"); + GeofenceLocation geofenceLocation = getGeofenceLocation(); + if (!getApplication().arePlatformAndCoreInitialized()) + getApplication().initCore(); + + GeofenceRegistry registry = GeofenceRegistryImpl.from(getApplication()); + for (Geofence each : mGeofences) + { + GeoFenceFeature geoFenceFeature = registry.getFeatureByGeofence(each); + LightFramework.logLocalAdsEvent(geofenceLocation, geoFenceFeature); + } + } + } +} diff --git a/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java b/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java index eeae7d0d2d..58dd2d4c1d 100644 --- a/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java @@ -52,20 +52,22 @@ public class GeofenceRegistryImpl implements GeofenceRegistry List features = LightFramework.getLocalAdsFeatures( location.getLat(), location.getLon(), location.getRadiusInMeters(), GEOFENCE_MAX_COUNT); +/* if (features.isEmpty()) - return; + return;*/ for (GeoFenceFeature each : features) { - Geofence geofence = new Geofence.Builder() - .setRequestId(each.getId()) - .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) - .build(); - mGeofences.add(new GeofenceAndFeature(geofence, each)); + } + Geofence geofence = new Geofence.Builder() + .setRequestId(String.valueOf(System.currentTimeMillis())) + .setCircularRegion(location.getLat(), location.getLon(), PREFERRED_GEOFENCE_RADIUS) + .setExpirationDuration(TimeUnit.DAYS.toMillis(GEOFENCE_TTL_IN_DAYS)) + .setLoiteringDelay(1000) + .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_DWELL) + .build(); + mGeofences.add(new GeofenceAndFeature(geofence, null)); GeofencingRequest geofencingRequest = makeGeofencingRequest(); PendingIntent intent = makeGeofencePendingIntent(); mGeofencingClient.addGeofences(geofencingRequest, intent) @@ -140,7 +142,7 @@ public class GeofenceRegistryImpl implements GeofenceRegistry private GeofencingRequest makeGeofencingRequest() { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); - return builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER) + return builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL) .addGeofences(collectGeofences()) .build(); } diff --git a/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java b/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java index 80fd8b818f..417e610f94 100644 --- a/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java @@ -6,6 +6,7 @@ import android.content.Intent; import android.location.Location; import android.os.Handler; import android.os.Looper; +import android.os.Parcelable; import android.support.annotation.NonNull; import android.support.v4.app.JobIntentService; @@ -16,9 +17,11 @@ import com.mapswithme.maps.MwmApplication; import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.maps.location.LocationPermissionNotGrantedException; import com.mapswithme.maps.scheduling.JobIdMap; +import com.mapswithme.util.concurrency.UiThread; import com.mapswithme.util.log.Logger; import com.mapswithme.util.log.LoggerFactory; +import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.concurrent.Callable; @@ -33,7 +36,7 @@ public class GeofenceTransitionsIntentService extends JobIntentService private static final Logger LOG = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC); private static final String TAG = GeofenceTransitionsIntentService.class.getSimpleName(); public static final int LOCATION_PROBES_MAX_COUNT = 10; - public static final int TIMEOUT_IN_MINUTS = 1; + public static final int TIMEOUT_IN_MINUTS = 10; @NonNull private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); @@ -52,7 +55,7 @@ public class GeofenceTransitionsIntentService extends JobIntentService { int transitionType = geofencingEvent.getGeofenceTransition(); - if (transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) + if (transitionType == Geofence.GEOFENCE_TRANSITION_DWELL) onGeofenceEnter(geofencingEvent); else if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) onGeofenceExit(geofencingEvent); @@ -71,7 +74,7 @@ public class GeofenceTransitionsIntentService extends JobIntentService private void makeLocationProbesBlocking(@NonNull GeofencingEvent geofencingEvent) { - for (int i = 0; i < LOCATION_PROBES_MAX_COUNT; i++) + for (int i = 0; i < 1; i++) { try { @@ -94,14 +97,20 @@ public class GeofenceTransitionsIntentService extends JobIntentService private void makeSingleLocationProbOrThrow(GeofencingEvent geofencingEvent) throws InterruptedException, ExecutionException, TimeoutException { - getExecutor().submit(new InfinityTask()).get(TIMEOUT_IN_MINUTS, TimeUnit.MINUTES); - GeofenceLocation geofenceLocation = GeofenceLocation.from(geofencingEvent.getTriggeringLocation()); - List geofences = Collections.unmodifiableList(geofencingEvent.getTriggeringGeofences()); - CheckLocationTask locationTask = new CheckLocationTask( - getApplication(), - geofences, - geofenceLocation); - mMainThreadHandler.post(locationTask); +// getExecutor().submit(new InfinityTask()).get(TIMEOUT_IN_MINUTS, TimeUnit.MINUTES); + CountDownLatch countDownLatch = new CountDownLatch(10); + InfinityTask infinityTask = new InfinityTask(countDownLatch); + for (int i = 0; i < 10; i++) + { + GeofenceLocation geofenceLocation = GeofenceLocation.from(geofencingEvent.getTriggeringLocation()); + List geofences = Collections.unmodifiableList(geofencingEvent.getTriggeringGeofences()); + CheckLocationTask locationTask = new CheckLocationTask( + getApplication(), + geofences, + geofenceLocation, infinityTask); + mMainThreadHandler.postDelayed(locationTask, i * 1000 * 20); + } + countDownLatch.await(); } private void onError(@NonNull GeofencingEvent geofencingEvent) @@ -119,12 +128,18 @@ public class GeofenceTransitionsIntentService extends JobIntentService private static class InfinityTask implements Callable { private static final int LATCH_COUNT = 1; + private final CountDownLatch mCountDownLatch; + + public InfinityTask(CountDownLatch countDownLatch) + { + + mCountDownLatch = countDownLatch; + } @Override public Object call() throws Exception { - CountDownLatch latch = new CountDownLatch(LATCH_COUNT); - latch.await(); + mCountDownLatch.countDown(); return null; } } @@ -133,12 +148,14 @@ public class GeofenceTransitionsIntentService extends JobIntentService { @NonNull private final List mGeofences; + private final InfinityTask mInfinityTask; CheckLocationTask(@NonNull Application application, @NonNull List geofences, - @NonNull GeofenceLocation triggeringLocation) + @NonNull GeofenceLocation triggeringLocation, InfinityTask infinityTask) { super(application, triggeringLocation); mGeofences = geofences; + mInfinityTask = infinityTask; } @Override @@ -149,6 +166,10 @@ public class GeofenceTransitionsIntentService extends JobIntentService private void requestLocationCheck() { + + String errorMessage = "Geo = " + Arrays.toString(mGeofences.toArray()); + LOG.e(TAG, errorMessage); + GeofenceLocation geofenceLocation = getGeofenceLocation(); if (!getApplication().arePlatformAndCoreInitialized()) getApplication().initCore(); @@ -156,9 +177,11 @@ public class GeofenceTransitionsIntentService extends JobIntentService GeofenceRegistry registry = GeofenceRegistryImpl.from(getApplication()); for (Geofence each : mGeofences) { - GeoFenceFeature geoFenceFeature = registry.getFeatureByGeofence(each); - LightFramework.logLocalAdsEvent(geofenceLocation, geoFenceFeature); + //GeoFenceFeature geoFenceFeature = registry.getFeatureByGeofence(each); +// LightFramework.logLocalAdsEvent(geofenceLocation, geoFenceFeature); } + + getApplication().getGeofenceProbesExecutor().submit(mInfinityTask); } } @@ -188,7 +211,7 @@ public class GeofenceTransitionsIntentService extends JobIntentService } } - private static abstract class AbstractGeofenceTask implements Runnable + public static abstract class AbstractGeofenceTask implements Runnable { @NonNull private final MwmApplication mApplication; diff --git a/android/src/com/mapswithme/maps/scheduling/JobIdMap.java b/android/src/com/mapswithme/maps/scheduling/JobIdMap.java index eb8f44b314..d03979ec0c 100644 --- a/android/src/com/mapswithme/maps/scheduling/JobIdMap.java +++ b/android/src/com/mapswithme/maps/scheduling/JobIdMap.java @@ -3,6 +3,7 @@ package com.mapswithme.maps.scheduling; import com.mapswithme.maps.background.NotificationService; import com.mapswithme.maps.background.WorkerService; import com.mapswithme.maps.bookmarks.SystemDownloadCompletedService; +import com.mapswithme.maps.geofence.CheckGeofenceEnterService; import com.mapswithme.maps.geofence.GeofenceTransitionsIntentService; import com.mapswithme.maps.location.TrackRecorderWakeService; import com.mapswithme.util.Utils; @@ -21,6 +22,7 @@ public class JobIdMap MAP.put(SystemDownloadCompletedService.class, calcIdentifier(MAP.size())); MAP.put(WorkerService.class, calcIdentifier(MAP.size())); MAP.put(GeofenceTransitionsIntentService.class, calcIdentifier(MAP.size())); + MAP.put(CheckGeofenceEnterService.class, calcIdentifier(MAP.size())); } private static final int ID_BASIC = 1070;