From 4b0d91a3adc0172d608b1cbf50b020d2601b9f2a Mon Sep 17 00:00:00 2001 From: Dmitry Donskoy Date: Wed, 19 Dec 2018 18:12:31 +0300 Subject: [PATCH] [android] Fixed review notes --- android/AndroidManifest.xml | 17 +++++++------- .../com/mapswithme/maps/MwmApplication.java | 6 ++--- .../GeofenceLocation.java | 2 +- .../GeofenceReceiver.java | 2 +- .../maps/geofence/GeofenceRegistry.java | 11 +++++++++ .../GeofenceRegistryImpl.java | 23 ++++++++----------- .../GeofenceTransitionsIntentService.java | 22 ++++++++++++++---- .../maps/location/GeofenceRegistry.java | 9 -------- ...LocationPermissionNotGrantedException.java | 11 +++++++++ .../mapswithme/maps/scheduling/JobIdMap.java | 2 +- .../com/mapswithme/util/PermissionsUtils.java | 2 +- 11 files changed, 64 insertions(+), 43 deletions(-) rename android/src/com/mapswithme/maps/{location => geofence}/GeofenceLocation.java (96%) rename android/src/com/mapswithme/maps/{location => geofence}/GeofenceReceiver.java (89%) create mode 100644 android/src/com/mapswithme/maps/geofence/GeofenceRegistry.java rename android/src/com/mapswithme/maps/{location => geofence}/GeofenceRegistryImpl.java (85%) rename android/src/com/mapswithme/maps/{location => geofence}/GeofenceTransitionsIntentService.java (75%) delete mode 100644 android/src/com/mapswithme/maps/location/GeofenceRegistry.java create mode 100644 android/src/com/mapswithme/maps/location/LocationPermissionNotGrantedException.java diff --git a/android/AndroidManifest.xml b/android/AndroidManifest.xml index 1720d3823e..1e8e66a673 100644 --- a/android/AndroidManifest.xml +++ b/android/AndroidManifest.xml @@ -559,22 +559,21 @@ - - + android:name="com.pushwoosh.GCMRegistrationService" + android:exported="false"/> + android:name="com.pushwoosh.location.GeoLocationService"/> - + diff --git a/android/src/com/mapswithme/maps/MwmApplication.java b/android/src/com/mapswithme/maps/MwmApplication.java index 004614df8c..1f1ab384e2 100644 --- a/android/src/com/mapswithme/maps/MwmApplication.java +++ b/android/src/com/mapswithme/maps/MwmApplication.java @@ -3,7 +3,6 @@ package com.mapswithme.maps; import android.app.Application; import android.content.Context; import android.content.SharedPreferences; -import android.location.Location; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; @@ -20,9 +19,8 @@ import com.mapswithme.maps.bookmarks.data.BookmarkManager; import com.mapswithme.maps.downloader.CountryItem; import com.mapswithme.maps.downloader.MapManager; import com.mapswithme.maps.editor.Editor; -import com.mapswithme.maps.location.GeofenceLocation; -import com.mapswithme.maps.location.GeofenceRegistry; -import com.mapswithme.maps.location.GeofenceRegistryImpl; +import com.mapswithme.maps.geofence.GeofenceRegistry; +import com.mapswithme.maps.geofence.GeofenceRegistryImpl; import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.maps.location.TrackRecorder; import com.mapswithme.maps.maplayer.subway.SubwayManager; diff --git a/android/src/com/mapswithme/maps/location/GeofenceLocation.java b/android/src/com/mapswithme/maps/geofence/GeofenceLocation.java similarity index 96% rename from android/src/com/mapswithme/maps/location/GeofenceLocation.java rename to android/src/com/mapswithme/maps/geofence/GeofenceLocation.java index bebff0b68d..17ee4b32c9 100644 --- a/android/src/com/mapswithme/maps/location/GeofenceLocation.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceLocation.java @@ -1,4 +1,4 @@ -package com.mapswithme.maps.location; +package com.mapswithme.maps.geofence; import android.location.Location; import android.support.annotation.NonNull; diff --git a/android/src/com/mapswithme/maps/location/GeofenceReceiver.java b/android/src/com/mapswithme/maps/geofence/GeofenceReceiver.java similarity index 89% rename from android/src/com/mapswithme/maps/location/GeofenceReceiver.java rename to android/src/com/mapswithme/maps/geofence/GeofenceReceiver.java index 57fc0d8a77..e5ca4d1e4f 100644 --- a/android/src/com/mapswithme/maps/location/GeofenceReceiver.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceReceiver.java @@ -1,4 +1,4 @@ -package com.mapswithme.maps.location; +package com.mapswithme.maps.geofence; import android.content.BroadcastReceiver; import android.content.Context; diff --git a/android/src/com/mapswithme/maps/geofence/GeofenceRegistry.java b/android/src/com/mapswithme/maps/geofence/GeofenceRegistry.java new file mode 100644 index 0000000000..1e0f1f3f37 --- /dev/null +++ b/android/src/com/mapswithme/maps/geofence/GeofenceRegistry.java @@ -0,0 +1,11 @@ +package com.mapswithme.maps.geofence; + +import android.support.annotation.NonNull; + +import com.mapswithme.maps.location.LocationPermissionNotGrantedException; + +public interface GeofenceRegistry +{ + void registerGeofences(@NonNull GeofenceLocation location) throws LocationPermissionNotGrantedException; + void unregisterGeofences() throws LocationPermissionNotGrantedException; +} diff --git a/android/src/com/mapswithme/maps/location/GeofenceRegistryImpl.java b/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java similarity index 85% rename from android/src/com/mapswithme/maps/location/GeofenceRegistryImpl.java rename to android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java index 803904901d..6c02822345 100644 --- a/android/src/com/mapswithme/maps/location/GeofenceRegistryImpl.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceRegistryImpl.java @@ -1,4 +1,4 @@ -package com.mapswithme.maps.location; +package com.mapswithme.maps.geofence; import android.app.Application; import android.app.PendingIntent; @@ -11,8 +11,9 @@ 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.geofence.GeoFenceFeature; +import com.mapswithme.maps.location.LocationPermissionNotGrantedException; import com.mapswithme.util.PermissionsUtils; +import com.mapswithme.util.concurrency.UiThread; import java.util.ArrayList; import java.util.Iterator; @@ -21,7 +22,7 @@ import java.util.List; public class GeofenceRegistryImpl implements GeofenceRegistry { private static final int GEOFENCE_MAX_COUNT = 100; - private static final float PREFERRED_GEOFENCE_RADIUS = 125.0f; + private static final float PREFERRED_GEOFENCE_RADIUS = 100.0f; @NonNull private final Application mApplication; @@ -38,7 +39,7 @@ public class GeofenceRegistryImpl implements GeofenceRegistry } @Override - public void registryGeofences(@NonNull GeofenceLocation location) + public void registerGeofences(@NonNull GeofenceLocation location) throws LocationPermissionNotGrantedException { checkThread(); checkPermission(); @@ -67,18 +68,16 @@ public class GeofenceRegistryImpl implements GeofenceRegistry } @Override - public void invalidateGeofences() + public void unregisterGeofences() throws LocationPermissionNotGrantedException { checkThread(); checkPermission(); if (mGeofences.isEmpty()) return; - Iterator iterator = mGeofences.iterator(); List expiredGeofences = new ArrayList<>(); - while (iterator.hasNext()) + for (GeofenceAndFeature current: mGeofences) { - GeofenceAndFeature current = iterator.next(); String requestId = current.getGeofence().getRequestId(); expiredGeofences.add(requestId); } @@ -107,17 +106,15 @@ public class GeofenceRegistryImpl implements GeofenceRegistry } - private void checkPermission() + private void checkPermission() throws LocationPermissionNotGrantedException { if (!PermissionsUtils.isLocationGranted(mApplication)) - throw new UnsupportedOperationException("Geofence registry required android.Manifest" + - ".permission\n" + - " .ACCESS_FINE_LOCATION"); + throw new LocationPermissionNotGrantedException(); } private static void checkThread() { - if (!com.mapswithme.util.concurrency.UiThread.isUiThread()) + if (!UiThread.isUiThread()) throw new IllegalStateException("Must be call from Ui thread"); } diff --git a/android/src/com/mapswithme/maps/location/GeofenceTransitionsIntentService.java b/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java similarity index 75% rename from android/src/com/mapswithme/maps/location/GeofenceTransitionsIntentService.java rename to android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java index 7d803cbb4a..206dfa0435 100644 --- a/android/src/com/mapswithme/maps/location/GeofenceTransitionsIntentService.java +++ b/android/src/com/mapswithme/maps/geofence/GeofenceTransitionsIntentService.java @@ -1,4 +1,4 @@ -package com.mapswithme.maps.location; +package com.mapswithme.maps.geofence; import android.app.Application; import android.content.Context; @@ -11,10 +11,18 @@ import android.support.v4.app.JobIntentService; import com.google.android.gms.location.Geofence; import com.google.android.gms.location.GeofencingEvent; +import com.mapswithme.maps.location.LocationHelper; +import com.mapswithme.maps.location.LocationPermissionNotGrantedException; import com.mapswithme.maps.scheduling.JobIdMap; +import com.mapswithme.util.log.Logger; +import com.mapswithme.util.log.LoggerFactory; public class GeofenceTransitionsIntentService extends JobIntentService { + private static final Logger LOG = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC); + + private static final String TAG = GeofenceTransitionsIntentService.class.getSimpleName(); + @NonNull private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper()); @@ -61,9 +69,15 @@ public class GeofenceTransitionsIntentService extends JobIntentService GeofenceRegistry geofenceRegistry = GeofenceRegistryImpl.from(mApplication); if (mGeofencingEvent.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) { - geofenceRegistry.invalidateGeofences(); - geofenceRegistry.registryGeofences(GeofenceLocation.from(currentLocation)); - + try + { + geofenceRegistry.unregisterGeofences(); + geofenceRegistry.registerGeofences(GeofenceLocation.from(currentLocation)); + } + catch (LocationPermissionNotGrantedException e) + { + LOG.d(TAG, "Location permission not granted!", e); + } } } } diff --git a/android/src/com/mapswithme/maps/location/GeofenceRegistry.java b/android/src/com/mapswithme/maps/location/GeofenceRegistry.java deleted file mode 100644 index 52d2fee7df..0000000000 --- a/android/src/com/mapswithme/maps/location/GeofenceRegistry.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.mapswithme.maps.location; - -import android.support.annotation.NonNull; - -public interface GeofenceRegistry -{ - void registryGeofences(@NonNull GeofenceLocation location); - void invalidateGeofences(); -} diff --git a/android/src/com/mapswithme/maps/location/LocationPermissionNotGrantedException.java b/android/src/com/mapswithme/maps/location/LocationPermissionNotGrantedException.java new file mode 100644 index 0000000000..7e644d53e9 --- /dev/null +++ b/android/src/com/mapswithme/maps/location/LocationPermissionNotGrantedException.java @@ -0,0 +1,11 @@ +package com.mapswithme.maps.location; + +public class LocationPermissionNotGrantedException extends Exception +{ + private static final long serialVersionUID = -1053815743102694164L; + + public LocationPermissionNotGrantedException() + { + super("Location permission not granted!"); + } +} diff --git a/android/src/com/mapswithme/maps/scheduling/JobIdMap.java b/android/src/com/mapswithme/maps/scheduling/JobIdMap.java index b511c73860..eb8f44b314 100644 --- a/android/src/com/mapswithme/maps/scheduling/JobIdMap.java +++ b/android/src/com/mapswithme/maps/scheduling/JobIdMap.java @@ -3,7 +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.location.GeofenceTransitionsIntentService; +import com.mapswithme.maps.geofence.GeofenceTransitionsIntentService; import com.mapswithme.maps.location.TrackRecorderWakeService; import com.mapswithme.util.Utils; diff --git a/android/src/com/mapswithme/util/PermissionsUtils.java b/android/src/com/mapswithme/util/PermissionsUtils.java index 476202e28a..b9119714a6 100644 --- a/android/src/com/mapswithme/util/PermissionsUtils.java +++ b/android/src/com/mapswithme/util/PermissionsUtils.java @@ -50,7 +50,7 @@ public final class PermissionsUtils public static boolean isLocationGranted(@NonNull Context context) { - return checkPermissions(context).isExternalStorageGranted(); + return checkPermissions(context).isLocationGranted(); } /**