From e47e2f375c3309c897dae3efe903efa1fe3bcf7f Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 02:15:38 +0200 Subject: [PATCH 01/24] Handle native crash --- .../navigator/model/impl/sdk/MapBoxSdkImpl.java | 2 +- .../gps/navigator/mapboxsdk/MapSdkInstance.java | 15 +++++++++++++-- .../callback/MapSdkProviderListenerImpl.java | 15 +++++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/mapboxsdk/src/main/java/gps/map/navigator/model/impl/sdk/MapBoxSdkImpl.java b/mapboxsdk/src/main/java/gps/map/navigator/model/impl/sdk/MapBoxSdkImpl.java index fa4aa79..8893250 100644 --- a/mapboxsdk/src/main/java/gps/map/navigator/model/impl/sdk/MapBoxSdkImpl.java +++ b/mapboxsdk/src/main/java/gps/map/navigator/model/impl/sdk/MapBoxSdkImpl.java @@ -55,7 +55,7 @@ public void showMeOnMap(IPlaceListener placeListener) { public void setMapSettings(MapSetting mapSettings) { MapSdkInstance map = MapSdkProvider.getInstance().getMapSdkInstance(); if (map != null) { - map.setStyle(mapSettings); + map.setStyle(mapSettings, null); } } diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkInstance.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkInstance.java index 3a91b06..c87c021 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkInstance.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkInstance.java @@ -1,6 +1,10 @@ package gps.navigator.mapboxsdk; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.Style; import gps.map.navigator.model.InstanceCache; import gps.map.navigator.model.interfaces.MapSetting; @@ -11,10 +15,17 @@ public MapSdkInstance(MapboxMap instance) { super(instance); } - public void setStyle(MapSetting setting) { + public void setStyle(MapSetting setting, @Nullable final Style.OnStyleLoaded listener) { MapboxMap map = getInstance(); if (map != null) { - map.setStyle(new StyleProvider().getStyle(setting)); + map.setStyle(new StyleProvider().getStyle(setting), new Style.OnStyleLoaded() { + @Override + public void onStyleLoaded(@NonNull Style style) { + if (listener != null) { + listener.onStyleLoaded(style); + } + } + }); } } } diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSdkProviderListenerImpl.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSdkProviderListenerImpl.java index 85469bb..f84f23c 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSdkProviderListenerImpl.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSdkProviderListenerImpl.java @@ -1,6 +1,9 @@ package gps.navigator.mapboxsdk.callback; +import androidx.annotation.NonNull; + import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.Style; import gps.map.navigator.model.interfaces.Cache; import gps.map.navigator.model.interfaces.MapSdk; @@ -26,10 +29,14 @@ public void onMapSdkProvided(MapboxMap mapboxMap) { provider.setMapSdkInstance(instance); } if (cache != null) { - instance.setStyle(cache.getMapSettings()); - } - if (mapSdk != null) { - mapSdk.showMap(); + instance.setStyle(cache.getMapSettings(), new Style.OnStyleLoaded() { + @Override + public void onStyleLoaded(@NonNull Style style) { + if (mapSdk != null) { + mapSdk.showMap(); + } + } + }); } } } From 8263aa807ab594656e3557959a88a9e723687dc4 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 02:16:08 +0200 Subject: [PATCH 02/24] Version up --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index fe267bc..b21ce9b 100644 --- a/build.gradle +++ b/build.gradle @@ -32,8 +32,8 @@ ext { bundleId = "gps.map.navigator" //versions - versionCode = 3 - versionName = "1.0.3" + versionCode = 4 + versionName = "1.0.4" //proguard activateProguard = false From 6b6b023d843a60ef7b4d01976f901233af475543 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 02:57:25 +0200 Subject: [PATCH 03/24] Handle remove crash --- app/src/main/java/gps/map/navigator/model/DataCache.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/gps/map/navigator/model/DataCache.java b/app/src/main/java/gps/map/navigator/model/DataCache.java index 7ef31c6..55e9256 100644 --- a/app/src/main/java/gps/map/navigator/model/DataCache.java +++ b/app/src/main/java/gps/map/navigator/model/DataCache.java @@ -117,6 +117,9 @@ private void savePlace(@Nullable IMapPlace place, @Nullable String key) { } private boolean placeAlreadyExist(@NonNull List historyPlaces, @NonNull IMapPlace place) { + if (historyPlaces.isEmpty()) { + return false; + } for (int i = 0; i < historyPlaces.size(); i++) { if (place.getId().equals(historyPlaces.get(i).getId())) { return true; @@ -248,7 +251,7 @@ public void setRawData(@Nullable String key, @Nullable byte[] rawData) { public void removeHistoryPlace(@Nullable IMapPlace placeToRemove) { if (placeToRemove != null) { List places = getHistoryPlaces(); - if (places != null) { + if (places != null && !places.isEmpty()) { int position = getPosition(places, placeToRemove); places.remove(position); setHistoryPlaces(places); From f6b0c0bf2a0bd3bf516e76125f50795451e73584 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 02:57:42 +0200 Subject: [PATCH 04/24] handle start navigation crash --- .../callback/NavigationReadyCallback.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java index 1bc4d13..13d87ab 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java @@ -3,6 +3,7 @@ import android.app.Activity; import android.content.SharedPreferences; import android.location.Location; +import android.os.Handler; import android.preference.PreferenceManager; import androidx.appcompat.app.AppCompatDelegate; @@ -35,6 +36,7 @@ public class NavigationReadyCallback implements OnNavigationReadyCallback { private Activity activity; private NavigationView navigationView; private Cache cache; + private Handler handler; public NavigationReadyCallback(Activity activity, Cache cache, NavigationView navigationView) { this.activity = activity; @@ -44,6 +46,7 @@ public NavigationReadyCallback(Activity activity, Cache cache, NavigationView na @Override public void onNavigationReady(boolean isRunning) { + handler = new Handler(); updateNightMode(); fetchRoute(); } @@ -68,9 +71,14 @@ private void fetchRoute() { public void onResponse(Call call, Response response) { DirectionsResponse body = response.body(); if (body != null) { - List routes = body.routes(); - if (routes.size() > 0) { - startNavigation(routes.get(0)); + final List routes = body.routes(); + if (routes.size() > 0 && handler != null) { + handler.post(new Runnable() { + @Override + public void run() { + startNavigation(routes.get(0)); + } + }); } } } From 1dff3af0138a933939deeeaed3fae47954de715b Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 02:58:12 +0200 Subject: [PATCH 05/24] Version up --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index b21ce9b..a2e3754 100644 --- a/build.gradle +++ b/build.gradle @@ -32,8 +32,8 @@ ext { bundleId = "gps.map.navigator" //versions - versionCode = 4 - versionName = "1.0.4" + versionCode = 5 + versionName = "1.0.5" //proguard activateProguard = false From a05e46b4380e08a252f2685d19ca138d60edd7d5 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 17:56:45 +0200 Subject: [PATCH 06/24] Filter origin & destination match --- .../view/ui/fragment/BuildRouteFragment.java | 54 ++++++++++++------- .../view/ui/fragment/ShowRouteFragment.java | 14 +++-- .../listener/DestinationChangeListener.java | 13 ++++- .../listener/OriginChangeListener.java | 13 ++++- 4 files changed, 68 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/gps/map/navigator/view/ui/fragment/BuildRouteFragment.java b/app/src/main/java/gps/map/navigator/view/ui/fragment/BuildRouteFragment.java index aa506c8..5031254 100644 --- a/app/src/main/java/gps/map/navigator/view/ui/fragment/BuildRouteFragment.java +++ b/app/src/main/java/gps/map/navigator/view/ui/fragment/BuildRouteFragment.java @@ -63,10 +63,6 @@ public class BuildRouteFragment extends AbstractNaviFragment implements ISwipeRo @Named(Constants.DestinationClickListener) View.OnClickListener destinationClickListener; @Nullable - private IMapPlace originPlace; - @Nullable - private IMapPlace destinationPlace; - @Nullable private TextView originTitle; @Nullable private TextView destinationTitle; @@ -89,6 +85,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c setupDestination(view); setupSwipeOriginAndDestination(view); setupToolbarNavigation(view); + logger.debug("BuildRouteFragment onCreateView"); return view; } @@ -123,16 +120,17 @@ private IMapPlace getBestOrigin() { } private void setOriginPlace(@Nullable IMapPlace place) { - if (place != null) { + if (place != null + && (presenter.getLastOrigin() == null + || !placesAreTheSame(place, presenter.getLastDestination()))) { setOriginTitle(place.getTitle()); - originPlace = place; + presenter.setLastOrigin(place); logger.debug("Set origin as: " + place); } else { setOriginTitle(context.getResources().getString(R.string.choose_origin_default)); - originPlace = null; + presenter.setLastOrigin(null); logger.debug("Clean last origin"); } - presenter.setLastOrigin(originPlace); } private void setOriginTitle(@NonNull String title) { @@ -169,33 +167,47 @@ private void setupToolbarNavigation(@NonNull View view) { @Override public void swipeOriginAndDestination() { - IMapPlace lastOrigin = originPlace; - IMapPlace lastDestination = destinationPlace; + logger.debug("BuildRouteFragment swipeOriginAndDestination"); + IMapPlace lastOrigin = presenter.getLastOrigin(); + IMapPlace lastDestination = presenter.getLastDestination(); setOriginPlace(lastDestination); setDestinationPlace(lastOrigin); } @Override public void setOnlyOrigin(@NonNull IMapPlace origin) { - presenter.setLastOrigin(origin); + logger.debug("BuildRouteFragment setOnlyOrigin"); + if (!placesAreTheSame(origin, presenter.getLastDestination())) { + presenter.setLastOrigin(origin); + } } @Override public void setOnlyDestination(@NonNull IMapPlace destination) { - presenter.setLastDestination(destination); + logger.debug("BuildRouteFragment setOnlyDestination"); + if (!placesAreTheSame(destination, presenter.getLastOrigin())) { + presenter.setLastDestination(destination); + } } private void setDestinationPlace(@Nullable IMapPlace place) { - if (place != null) { + if (place != null + && (presenter.getLastDestination() == null + || !placesAreTheSame(place, presenter.getLastOrigin()))) { setDestinationTitle(place.getTitle()); - destinationPlace = place; + presenter.setLastDestination(place); logger.debug("Set destination as: " + place); } else { setDestinationTitle(context.getResources().getString(R.string.choose_destination_default)); - destinationPlace = null; + presenter.setLastDestination(null); logger.debug("Clean last destination"); } - presenter.setLastDestination(destinationPlace); + } + + private boolean placesAreTheSame(IMapPlace place, IMapPlace comparing) { + return place != null && comparing != null + && place.getLongitude() == comparing.getLongitude() + && place.getLatitude() == comparing.getLatitude(); } private void setDestinationTitle(@NonNull String title) { @@ -232,14 +244,16 @@ public void setHistoryPlaces(@NonNull List placeList) { @Override public void setNewPickedPlace(@NonNull IMapPlace mapPlace) { - if (originPlace == null) { + logger.debug("BuildRouteFragment setNewPickedPlace"); + if (presenter.getLastOrigin() == null && !placesAreTheSame(mapPlace, presenter.getLastDestination())) { setOriginPlace(mapPlace); - } else if (destinationPlace == null) { + openShowRouteFragmentIfRequied(); + } else if (presenter.getLastDestination() == null && !placesAreTheSame(mapPlace, presenter.getLastOrigin())) { setDestinationPlace(mapPlace); + openShowRouteFragmentIfRequied(); } else { logger.error("Can't use picked new place"); } - openShowRouteFragmentIfRequied(); } @Override @@ -286,7 +300,7 @@ private int getPosition(@NonNull List places, @NonNull IMapPlace item } private void openShowRouteFragmentIfRequied() { - if (originPlace != null && destinationPlace != null) { + if (presenter.getLastOrigin() != null && presenter.getLastDestination() != null) { IRoute route = buildNewRoute(); presenter.setLastRoute(route); presenter.setLastOrigin(null); diff --git a/app/src/main/java/gps/map/navigator/view/ui/fragment/ShowRouteFragment.java b/app/src/main/java/gps/map/navigator/view/ui/fragment/ShowRouteFragment.java index da80781..169773b 100644 --- a/app/src/main/java/gps/map/navigator/view/ui/fragment/ShowRouteFragment.java +++ b/app/src/main/java/gps/map/navigator/view/ui/fragment/ShowRouteFragment.java @@ -192,23 +192,29 @@ private void changeActiveRoute(@Nullable IMapPlace newOrigin, @Nullable IMapPlac if (route == null) { return; } - if (newOrigin != null) { + if (newOrigin != null && (newDestination != null || !placesAreTheSame(newOrigin, route.getDestination()))) { route.setOrigin(newOrigin); } - if (newDestination != null) { + if (newDestination != null && (newOrigin != null || !placesAreTheSame(newDestination, route.getOrigin()))) { route.setDestination(newDestination); } presenter.setLastRoute(route); - if (newOrigin != null) { + if (newOrigin != null && (newDestination != null || !placesAreTheSame(newOrigin, route.getDestination()))) { setOriginTitle(); } - if (newDestination != null) { + if (newDestination != null && (newOrigin != null || !placesAreTheSame(newDestination, route.getOrigin()))) { setDestinationTitle(); } showRouteOnMap(route); } + + private boolean placesAreTheSame(IMapPlace place, IMapPlace comparing) { + return place != null && comparing != null + && place.getLongitude() == comparing.getLongitude() + && place.getLatitude() == comparing.getLatitude(); + } } diff --git a/app/src/main/java/gps/map/navigator/view/ui/fragment/listener/DestinationChangeListener.java b/app/src/main/java/gps/map/navigator/view/ui/fragment/listener/DestinationChangeListener.java index fa59561..63c68c9 100644 --- a/app/src/main/java/gps/map/navigator/view/ui/fragment/listener/DestinationChangeListener.java +++ b/app/src/main/java/gps/map/navigator/view/ui/fragment/listener/DestinationChangeListener.java @@ -6,12 +6,15 @@ import javax.inject.Inject; +import gps.map.navigator.model.interfaces.Cache; import gps.map.navigator.model.interfaces.IMapPlace; import gps.map.navigator.model.interfaces.PlaceProxyListener; public class DestinationChangeListener implements PlaceProxyListener { @Inject ISwipeRoute swipeRoute; + @Inject + Cache cache; @Inject DestinationChangeListener() { @@ -19,11 +22,19 @@ public class DestinationChangeListener implements PlaceProxyListener { @Override public void onPlaceLocated(@NonNull IMapPlace mapPlace) { - swipeRoute.setOnlyDestination(mapPlace); + if (!placesAreTheSame(mapPlace, cache.getLastOrigin())) { + swipeRoute.setOnlyDestination(mapPlace); + } } @Override public void onPlacesLocated(@NonNull List mapPlaces) { } + + private boolean placesAreTheSame(IMapPlace place, IMapPlace comparing) { + return place != null && comparing != null + && place.getLongitude() == comparing.getLongitude() + && place.getLatitude() == comparing.getLatitude(); + } } diff --git a/app/src/main/java/gps/map/navigator/view/ui/fragment/listener/OriginChangeListener.java b/app/src/main/java/gps/map/navigator/view/ui/fragment/listener/OriginChangeListener.java index de28e79..a1d07cd 100644 --- a/app/src/main/java/gps/map/navigator/view/ui/fragment/listener/OriginChangeListener.java +++ b/app/src/main/java/gps/map/navigator/view/ui/fragment/listener/OriginChangeListener.java @@ -6,12 +6,15 @@ import javax.inject.Inject; +import gps.map.navigator.model.interfaces.Cache; import gps.map.navigator.model.interfaces.IMapPlace; import gps.map.navigator.model.interfaces.PlaceProxyListener; public class OriginChangeListener implements PlaceProxyListener { @Inject ISwipeRoute swipeRoute; + @Inject + Cache cache; @Inject OriginChangeListener() { @@ -19,11 +22,19 @@ public class OriginChangeListener implements PlaceProxyListener { @Override public void onPlaceLocated(@NonNull IMapPlace mapPlace) { - swipeRoute.setOnlyOrigin(mapPlace); + if (!placesAreTheSame(mapPlace, cache.getLastDestination())) { + swipeRoute.setOnlyOrigin(mapPlace); + } } @Override public void onPlacesLocated(@NonNull List mapPlaces) { } + + private boolean placesAreTheSame(IMapPlace place, IMapPlace comparing) { + return place != null && comparing != null + && place.getLongitude() == comparing.getLongitude() + && place.getLatitude() == comparing.getLatitude(); + } } From 9c8f9d2bb13e00d4a06799a431b37e9058c65920 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 17:59:26 +0200 Subject: [PATCH 07/24] Delete deprecated classes --- .../map/navigator/model/InstanceCache.java | 24 -------------- .../navigator/mapboxsdk/MapSdkInstance.java | 31 ------------------- .../navigator/mapboxsdk/MapSdkProvider.java | 25 --------------- .../navigator/mapboxsdk/MapViewInstance.java | 12 ------- .../navigator/mapboxsdk/MapViewProvider.java | 25 --------------- 5 files changed, 117 deletions(-) delete mode 100644 interfaces/src/main/java/gps/map/navigator/model/InstanceCache.java delete mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkInstance.java delete mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkProvider.java delete mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapViewInstance.java delete mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapViewProvider.java diff --git a/interfaces/src/main/java/gps/map/navigator/model/InstanceCache.java b/interfaces/src/main/java/gps/map/navigator/model/InstanceCache.java deleted file mode 100644 index 6916eaf..0000000 --- a/interfaces/src/main/java/gps/map/navigator/model/InstanceCache.java +++ /dev/null @@ -1,24 +0,0 @@ -package gps.map.navigator.model; - -import java.lang.ref.SoftReference; - -public class InstanceCache { - - private SoftReference reference; - - public InstanceCache(T instance) { - reference = new SoftReference<>(instance); - } - - public T getInstance() { - if (reference != null) { - return reference.get(); - } else { - return null; - } - } - - public void invalidate() { - reference = null; - } -} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkInstance.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkInstance.java deleted file mode 100644 index c87c021..0000000 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkInstance.java +++ /dev/null @@ -1,31 +0,0 @@ -package gps.navigator.mapboxsdk; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; - -import gps.map.navigator.model.InstanceCache; -import gps.map.navigator.model.interfaces.MapSetting; - -public class MapSdkInstance extends InstanceCache { - - public MapSdkInstance(MapboxMap instance) { - super(instance); - } - - public void setStyle(MapSetting setting, @Nullable final Style.OnStyleLoaded listener) { - MapboxMap map = getInstance(); - if (map != null) { - map.setStyle(new StyleProvider().getStyle(setting), new Style.OnStyleLoaded() { - @Override - public void onStyleLoaded(@NonNull Style style) { - if (listener != null) { - listener.onStyleLoaded(style); - } - } - }); - } - } -} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkProvider.java deleted file mode 100644 index 54c8b56..0000000 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapSdkProvider.java +++ /dev/null @@ -1,25 +0,0 @@ -package gps.navigator.mapboxsdk; - -public class MapSdkProvider { - - private static MapSdkProvider instance; - private MapSdkInstance mapSdkInstance; - - private MapSdkProvider() { - } - - public static MapSdkProvider getInstance() { - if (instance == null) { - instance = new MapSdkProvider(); - } - return instance; - } - - public MapSdkInstance getMapSdkInstance() { - return mapSdkInstance; - } - - public void setMapSdkInstance(MapSdkInstance mapSdkInstance) { - this.mapSdkInstance = mapSdkInstance; - } -} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapViewInstance.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapViewInstance.java deleted file mode 100644 index 948a521..0000000 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapViewInstance.java +++ /dev/null @@ -1,12 +0,0 @@ -package gps.navigator.mapboxsdk; - -import com.mapbox.mapboxsdk.maps.MapView; - -import gps.map.navigator.model.InstanceCache; - -public class MapViewInstance extends InstanceCache { - - public MapViewInstance(MapView instance) { - super(instance); - } -} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapViewProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapViewProvider.java deleted file mode 100644 index c3a25e9..0000000 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/MapViewProvider.java +++ /dev/null @@ -1,25 +0,0 @@ -package gps.navigator.mapboxsdk; - -public class MapViewProvider { - - private static MapViewProvider instance; - private MapViewInstance mapViewInstance; - - private MapViewProvider() { - } - - public static MapViewProvider getInstance() { - if (instance == null) { - instance = new MapViewProvider(); - } - return instance; - } - - public MapViewInstance getMapViewInstance() { - return mapViewInstance; - } - - public void setMapViewInstance(MapViewInstance mapViewInstance) { - this.mapViewInstance = mapViewInstance; - } -} From 8261f03fc8c4e6425e29d89803174c99a3a2e377 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:01:17 +0200 Subject: [PATCH 08/24] Handle navigation crash --- .../view/ui/fragment/FragmentNavigation.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentNavigation.java b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentNavigation.java index 9472e9c..0d2640e 100644 --- a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentNavigation.java +++ b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentNavigation.java @@ -1,5 +1,6 @@ package gps.map.navigator.view.ui.fragment; +import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -19,6 +20,7 @@ import gps.map.navigator.model.interfaces.IMapPlace; import gps.navigator.mapboxsdk.R; import gps.navigator.mapboxsdk.callback.NavigationReadyCallback; +import gps.navigator.mapboxsdk.interfaces.NavigationStatusListener; public class FragmentNavigation extends Fragment { @@ -39,7 +41,8 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat super.onViewCreated(view, savedInstanceState); navigationView = view.findViewById(R.id.navigation_view_fragment); navigationView.onCreate(savedInstanceState); - navigationView.initialize(new NavigationReadyCallback(getActivity(), cache, navigationView), getInitialPosition()); + navigationView.initialize(new NavigationReadyCallback(getActivity(), cache, navigationView, + new NavigationListener(getActivity())), getInitialPosition()); } private CameraPosition getInitialPosition() { @@ -120,4 +123,26 @@ public void onDestroyView() { navigationView.onDestroy(); } } + + private static class NavigationListener implements NavigationStatusListener { + + private Activity activity; + + private NavigationListener(Activity activity) { + this.activity = activity; + } + + @Override + public void onNavigationRunning() { + activity = null; + } + + @Override + public void onNavigationFailed() { + if (activity != null) { + activity.onBackPressed(); + } + activity = null; + } + } } From 7c215e0c462837e543d730abd093f8b226913fe1 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:01:49 +0200 Subject: [PATCH 09/24] Update fragments --- .../view/ui/fragment/FragmentMap.java | 80 +++++++++++++++++-- .../view/ui/fragment/FragmentRoute.java | 2 - .../view/ui/fragment/MapboxFragment.java | 23 ------ 3 files changed, 73 insertions(+), 32 deletions(-) diff --git a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentMap.java b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentMap.java index a940124..0603a52 100644 --- a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentMap.java +++ b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentMap.java @@ -1,5 +1,6 @@ package gps.map.navigator.view.ui.fragment; +import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -10,21 +11,28 @@ import com.mapbox.mapboxsdk.maps.MapView; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import javax.inject.Inject; import gps.map.navigator.model.interfaces.Cache; -import gps.map.navigator.model.interfaces.MapSdk; +import gps.map.navigator.model.interfaces.MapSetting; +import gps.map.navigator.view.interfaces.IPlaceListener; import gps.navigator.mapboxsdk.R; +import gps.navigator.mapboxsdk.callback.DeviceLocationProvider; import gps.navigator.mapboxsdk.callback.MapReadyCallback; -import gps.navigator.mapboxsdk.callback.MapSdkProviderListenerImpl; +import gps.navigator.mapboxsdk.callback.LastLocationProvider; +import gps.navigator.mapboxsdk.callback.MapSettingsProvider; +import gps.navigator.mapboxsdk.event.ChangeSettingsEvent; +import gps.navigator.mapboxsdk.event.MessageEvent; +import gps.navigator.mapboxsdk.event.RequestLocationEvent; public class FragmentMap extends MapboxFragment { @Nullable private MapView mapView; @Inject Cache cache; - @Inject - MapSdk mapSdk; @Nullable @Override @@ -32,11 +40,27 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c View root = inflater.inflate(R.layout.my_location_fragment, container, false); mapView = root.findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); - cacheMapViewInstance(); - mapView.getMapAsync(new MapReadyCallback(new MapSdkProviderListenerImpl(cache, mapSdk))); + showLastLocation(); return root; } + private void showLastLocation() { + try { + if (mapView != null) { + mapView.getMapAsync(new MapReadyCallback(new LastLocationProvider(getContext(), cache))); + } + } catch (Throwable t) { + closeActivity(); + } + } + + private void closeActivity() { + Activity activity = getActivity(); + if (activity != null) { + activity.onBackPressed(); + } + } + @Override protected MapView getMapView() { return mapView; @@ -44,6 +68,48 @@ protected MapView getMapView() { @Override protected void cleanReferences() { - mapView = null; + } + + @Override + public void onStart() { + EventBus.getDefault().register(this); + super.onStart(); + } + + @Override + public void onStop() { + EventBus.getDefault().unregister(this); + super.onStop(); + } + + @Subscribe + public void onMessageEvent(MessageEvent event) { + if (MessageEvent.TYPE_SHOW_LAST_LOCATION.equals(event.getMessageId())) { + showLastLocation(); + } else if (MessageEvent.TYPE_SHOW_DEVICE_LOCATION.equals(event.getMessageId())) { + showDeviceLocation(((RequestLocationEvent) event).getListener()); + } else if (MessageEvent.TYPE_CHANGE_MAP_SETTINGS.equals(event.getMessageId())) { + changeMapStyle(((ChangeSettingsEvent)event).getSetting()); + } + } + + private void showDeviceLocation(IPlaceListener listener) { + try { + if (mapView != null) { + mapView.getMapAsync(new MapReadyCallback(new DeviceLocationProvider(getContext(), cache, listener))); + } + } catch (Throwable t) { + closeActivity(); + } + } + + private void changeMapStyle(MapSetting setting) { + try { + if (mapView != null) { + mapView.getMapAsync(new MapReadyCallback(new MapSettingsProvider(setting))); + } + } catch (Throwable t) { + closeActivity(); + } } } diff --git a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentRoute.java b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentRoute.java index 26cffe7..7573adc 100644 --- a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentRoute.java +++ b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/FragmentRoute.java @@ -34,7 +34,6 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c View root = inflater.inflate(R.layout.route_fragment, container, false); mapView = root.findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); - cacheMapViewInstance(); mapView.getMapAsync(new MapRouteBuilderCallback(getContext(), cache, logger, mapView)); return root; } @@ -46,6 +45,5 @@ protected MapView getMapView() { @Override protected void cleanReferences() { - mapView = null; } } diff --git a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/MapboxFragment.java b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/MapboxFragment.java index 633bdf7..ce66bed 100644 --- a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/MapboxFragment.java +++ b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/MapboxFragment.java @@ -7,26 +7,12 @@ import com.mapbox.mapboxsdk.maps.MapView; -import gps.navigator.mapboxsdk.MapSdkProvider; -import gps.navigator.mapboxsdk.MapViewInstance; -import gps.navigator.mapboxsdk.MapViewProvider; - public abstract class MapboxFragment extends Fragment { - protected abstract MapView getMapView(); protected abstract void cleanReferences(); - protected void cacheMapViewInstance() { - MapViewProvider provider = MapViewProvider.getInstance(); - MapViewInstance instance = provider.getMapViewInstance(); - if (instance == null) { - instance = new MapViewInstance(getMapView()); - provider.setMapViewInstance(instance); - } - } - @Override public void onResume() { super.onResume(); @@ -79,15 +65,6 @@ public void onDestroyView() { if (mapView != null) { mapView.onDestroy(); } - cleanup(); - } - - - - private void cleanup() { - MapSdkProvider.getInstance().setMapSdkInstance(null); - MapViewProvider.getInstance().setMapViewInstance(null); - cleanReferences(); } @Override From f3bcf4eef16707d7dced8ac21ecff384e07d473d Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:02:00 +0200 Subject: [PATCH 10/24] Delete deprecated --- .../navigator/model/impl/FoundedPlace.java | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 mapboxsdk/src/main/java/gps/map/navigator/model/impl/FoundedPlace.java diff --git a/mapboxsdk/src/main/java/gps/map/navigator/model/impl/FoundedPlace.java b/mapboxsdk/src/main/java/gps/map/navigator/model/impl/FoundedPlace.java deleted file mode 100644 index 618b5f2..0000000 --- a/mapboxsdk/src/main/java/gps/map/navigator/model/impl/FoundedPlace.java +++ /dev/null @@ -1,22 +0,0 @@ -package gps.map.navigator.model.impl; - -import com.mapbox.api.geocoding.v5.models.CarmenFeature; -import com.mapbox.geojson.Point; - -import gps.map.navigator.model.impl.data.MapPlace; - -public class FoundedPlace extends MapPlace { - - public FoundedPlace(CarmenFeature item) { - setId(item.id()); - setLastUsedTime(System.currentTimeMillis()); - - setAddress(item.placeName()); - setTitle(item.text()); - - - Point point = item.center(); - setLatitude(point.latitude()); - setLongitude(point.longitude()); - } -} From c6028fc8eb52001387e401abd7d8ede2587b68eb Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:02:24 +0200 Subject: [PATCH 11/24] Use eventbus for mapsdk calls --- .../model/impl/sdk/MapBoxSdkImpl.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/mapboxsdk/src/main/java/gps/map/navigator/model/impl/sdk/MapBoxSdkImpl.java b/mapboxsdk/src/main/java/gps/map/navigator/model/impl/sdk/MapBoxSdkImpl.java index 8893250..af4e46c 100644 --- a/mapboxsdk/src/main/java/gps/map/navigator/model/impl/sdk/MapBoxSdkImpl.java +++ b/mapboxsdk/src/main/java/gps/map/navigator/model/impl/sdk/MapBoxSdkImpl.java @@ -5,6 +5,8 @@ import com.mapbox.mapboxsdk.Mapbox; +import org.greenrobot.eventbus.EventBus; + import javax.inject.Inject; import javax.inject.Named; @@ -19,12 +21,11 @@ import gps.map.navigator.view.interfaces.IRouteListener; import gps.map.navigator.view.interfaces.IRouteReadyListener; import gps.navigator.mapboxsdk.BuildConfig; -import gps.navigator.mapboxsdk.MapSdkInstance; -import gps.navigator.mapboxsdk.MapSdkProvider; +import gps.navigator.mapboxsdk.event.ChangeSettingsEvent; +import gps.navigator.mapboxsdk.event.MessageEvent; +import gps.navigator.mapboxsdk.event.RequestLocationEvent; import gps.navigator.mapboxsdk.geocode.GeocodeStrategy; import gps.navigator.mapboxsdk.geocode.impl.LocationIqGeocode; -import gps.navigator.mapboxsdk.map.MapProviderStategy; -import gps.navigator.mapboxsdk.map.MapboxMapProvider; public class MapBoxSdkImpl implements MapSdk { @Inject @@ -46,17 +47,14 @@ public void initialize(Context context, Bundle bundle) { @Override public void showMeOnMap(IPlaceListener placeListener) { - MapProviderStategy.getInstance() - .setStrategy(new MapboxMapProvider(context, cache)) - .showDeviceLocation(placeListener); + EventBus.getDefault() + .post(new RequestLocationEvent(MessageEvent.TYPE_SHOW_DEVICE_LOCATION, placeListener)); } @Override public void setMapSettings(MapSetting mapSettings) { - MapSdkInstance map = MapSdkProvider.getInstance().getMapSdkInstance(); - if (map != null) { - map.setStyle(mapSettings, null); - } + EventBus.getDefault() + .post(new ChangeSettingsEvent(MessageEvent.TYPE_CHANGE_MAP_SETTINGS, mapSettings)); } @Override @@ -93,8 +91,7 @@ public void navigate(IRoute route, IRouteListener routeListener) { @Override public void showMap() { - MapProviderStategy.getInstance() - .setStrategy(new MapboxMapProvider(context, cache)) - .showInitialLocation(null); + EventBus.getDefault() + .post(new RequestLocationEvent(MessageEvent.TYPE_SHOW_LAST_LOCATION, null)); } } From 70304bf9d5e4c125e3d9fc1d612a86b47057d950 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:02:53 +0200 Subject: [PATCH 12/24] Handle navigation crash --- .../callback/NavigationReadyCallback.java | 123 ++++++++++-------- 1 file changed, 71 insertions(+), 52 deletions(-) diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java index 13d87ab..f18852d 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java @@ -3,7 +3,6 @@ import android.app.Activity; import android.content.SharedPreferences; import android.location.Location; -import android.os.Handler; import android.preference.PreferenceManager; import androidx.appcompat.app.AppCompatDelegate; @@ -24,6 +23,7 @@ import gps.map.navigator.model.interfaces.Cache; import gps.map.navigator.model.interfaces.IMapPlace; import gps.navigator.mapboxsdk.BuildConfig; +import gps.navigator.mapboxsdk.interfaces.NavigationStatusListener; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -34,21 +34,24 @@ public class NavigationReadyCallback implements OnNavigationReadyCallback { private static final String WAS_NAVIGATION_STOPPED = "was_navigation_stopped"; private Activity activity; - private NavigationView navigationView; + private final NavigationView navigationView; private Cache cache; - private Handler handler; + private NavigationStatusListener listener; - public NavigationReadyCallback(Activity activity, Cache cache, NavigationView navigationView) { + public NavigationReadyCallback(Activity activity, Cache cache, NavigationView navigationView, + NavigationStatusListener listener) { this.activity = activity; this.navigationView = navigationView; this.cache = cache; + this.listener = listener; } @Override public void onNavigationReady(boolean isRunning) { - handler = new Handler(); - updateNightMode(); - fetchRoute(); + synchronized (navigationView) { + updateNightMode(); + fetchRoute(); + } } private void updateNightMode() { @@ -72,20 +75,25 @@ public void onResponse(Call call, Response routes = body.routes(); - if (routes.size() > 0 && handler != null) { - handler.post(new Runnable() { - @Override - public void run() { - startNavigation(routes.get(0)); - } - }); + if (routes.size() > 0) { + startNavigation(routes.get(0)); + } else { + if (listener != null) { + listener.onNavigationFailed(); + } + } + } else { + if (listener != null) { + listener.onNavigationFailed(); } } } @Override public void onFailure(Call call, Throwable t) { - + if (listener != null) { + listener.onNavigationFailed(); + } } }); } @@ -101,49 +109,60 @@ private Point getDestination() { } private void startNavigation(DirectionsRoute directionsRoute) { - if (directionsRoute == null) { - return; - } - NavigationViewOptions options = NavigationViewOptions.builder() - .directionsRoute(directionsRoute) - .shouldSimulateRoute(shouldSimulateRoute()) - .navigationListener(new NavigationListener() { - @Override - public void onCancelNavigation() { - navigationView.stopNavigation(); - stopNavigation(); - } + synchronized (navigationView) { + if (directionsRoute == null) { + return; + } + NavigationViewOptions options = NavigationViewOptions.builder() + .directionsRoute(directionsRoute) + .shouldSimulateRoute(shouldSimulateRoute()) + .navigationListener(new NavigationListener() { + @Override + public void onCancelNavigation() { + navigationView.stopNavigation(); + stopNavigation(); + } - @Override - public void onNavigationFinished() { + @Override + public void onNavigationFinished() { - } + } - @Override - public void onNavigationRunning() { + @Override + public void onNavigationRunning() { - } - }) - .progressChangeListener(new ProgressChangeListener() { - @Override - public void onProgressChange(Location location, RouteProgress routeProgress) { - boolean isInTunnel = routeProgress.inTunnel(); - boolean wasInTunnel = wasInTunnel(); - if (isInTunnel) { - if (!wasInTunnel) { - updateWasInTunnel(true); - updateCurrentNightMode(AppCompatDelegate.MODE_NIGHT_YES); - } - } else { - if (wasInTunnel) { - updateWasInTunnel(false); - updateCurrentNightMode(AppCompatDelegate.MODE_NIGHT_AUTO); + } + }) + .progressChangeListener(new ProgressChangeListener() { + @Override + public void onProgressChange(Location location, RouteProgress routeProgress) { + boolean isInTunnel = routeProgress.inTunnel(); + boolean wasInTunnel = wasInTunnel(); + if (isInTunnel) { + if (!wasInTunnel) { + updateWasInTunnel(true); + updateCurrentNightMode(AppCompatDelegate.MODE_NIGHT_YES); + } + } else { + if (wasInTunnel) { + updateWasInTunnel(false); + updateCurrentNightMode(AppCompatDelegate.MODE_NIGHT_AUTO); + } } } - } - }) - .build(); - navigationView.startNavigation(options); + }) + .build(); + try { + navigationView.startNavigation(options); + if (listener != null) { + listener.onNavigationRunning(); + } + } catch (Throwable t) { + if (listener != null) { + listener.onNavigationFailed(); + } + } + } } private boolean shouldSimulateRoute() { From 0c0c6a6dd3a3c114926dc18a983209864dce73e7 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:03:09 +0200 Subject: [PATCH 13/24] Add null check --- .../callback/MapRouteBuilderCallback.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapRouteBuilderCallback.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapRouteBuilderCallback.java index 45a35e6..d6bb2b7 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapRouteBuilderCallback.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapRouteBuilderCallback.java @@ -3,6 +3,7 @@ import android.content.Context; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.mapbox.api.directions.v5.models.DirectionsRoute; import com.mapbox.geojson.Point; @@ -22,13 +23,16 @@ import gps.navigator.mapboxsdk.navigation.NavigationRouteStrategy; public class MapRouteBuilderCallback implements OnMapReadyCallback { - + @Nullable private Cache cache; + @Nullable private Context context; + @Nullable private Logger logger; + @Nullable private MapView mapView; - public MapRouteBuilderCallback(Context context, Cache cache, Logger logger, MapView mapView) { + public MapRouteBuilderCallback(@Nullable Context context, @Nullable Cache cache, @Nullable Logger logger, @Nullable MapView mapView) { this.cache = cache; this.context = context; this.logger = logger; @@ -50,7 +54,9 @@ public void onRouteReady(DirectionsRoute route) { @Override public void onBuildFailed(Exception reason) { - logger.error(reason); + if (logger != null) { + logger.error(reason); + } } }); } @@ -58,11 +64,17 @@ public void onBuildFailed(Exception reason) { } private Point getOrigin() { + if (cache == null || cache.getLastRoute() == null) { + return null; + } IMapPlace place = cache.getLastRoute().getOrigin(); return Point.fromLngLat(place.getLongitude(), place.getLatitude()); } private Point getDestination() { + if (cache == null || cache.getLastRoute() == null) { + return null; + } IMapPlace place = cache.getLastRoute().getDestination(); return Point.fromLngLat(place.getLongitude(), place.getLatitude()); } From 11ab694fde9f52eb481f1879e3f4acbe31f62b0f Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:03:27 +0200 Subject: [PATCH 14/24] Add listener for map changes --- .../callback/DeviceLocationProvider.java | 41 ++++++++++++++++++ .../callback/LastLocationProvider.java | 37 ++++++++++++++++ .../callback/MapLocationProvider.java | 40 ++++++++++++++++++ .../callback/MapSdkProviderListenerImpl.java | 42 ------------------- .../callback/MapSettingsProvider.java | 21 ++++++++++ 5 files changed, 139 insertions(+), 42 deletions(-) create mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/DeviceLocationProvider.java create mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/LastLocationProvider.java create mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapLocationProvider.java delete mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSdkProviderListenerImpl.java create mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSettingsProvider.java diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/DeviceLocationProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/DeviceLocationProvider.java new file mode 100644 index 0000000..27e55d8 --- /dev/null +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/DeviceLocationProvider.java @@ -0,0 +1,41 @@ +package gps.navigator.mapboxsdk.callback; + +import android.content.Context; + +import androidx.annotation.Nullable; + +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.Style; + +import gps.map.navigator.model.interfaces.Cache; +import gps.map.navigator.view.interfaces.IPlaceListener; +import gps.navigator.mapboxsdk.StyleProvider; +import gps.navigator.mapboxsdk.map.MapProviderStategy; +import gps.navigator.mapboxsdk.map.MapboxMapProvider; + +public class DeviceLocationProvider extends MapLocationProvider { + @Nullable + private Cache cache; + @Nullable + private Context context; + @Nullable + private IPlaceListener listener; + + public DeviceLocationProvider(@Nullable Context context, @Nullable Cache cache, @Nullable IPlaceListener listener) { + this.cache = cache; + this.context = context; + this.listener = listener; + } + + @Override + public void openMap(MapboxMap mapboxMap) { + MapProviderStategy.getInstance() + .setStrategy(new MapboxMapProvider(context, cache, mapboxMap)) + .showDeviceLocation(listener); + } + + @Override + public String getMapStyle() { + return cache != null ? new StyleProvider().getStyle(cache.getMapSettings()) : Style.MAPBOX_STREETS; + } +} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/LastLocationProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/LastLocationProvider.java new file mode 100644 index 0000000..9b94dd5 --- /dev/null +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/LastLocationProvider.java @@ -0,0 +1,37 @@ +package gps.navigator.mapboxsdk.callback; + +import android.content.Context; + +import androidx.annotation.Nullable; + +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.Style; + +import gps.map.navigator.model.interfaces.Cache; +import gps.navigator.mapboxsdk.StyleProvider; +import gps.navigator.mapboxsdk.map.MapProviderStategy; +import gps.navigator.mapboxsdk.map.MapboxMapProvider; + +public class LastLocationProvider extends MapLocationProvider { + @Nullable + private Cache cache; + @Nullable + private Context context; + + public LastLocationProvider(@Nullable Context context, @Nullable Cache cache) { + this.cache = cache; + this.context = context; + } + + @Override + public void openMap(MapboxMap mapboxMap) { + MapProviderStategy.getInstance() + .setStrategy(new MapboxMapProvider(context, cache, mapboxMap)) + .showInitialLocation(null); + } + + @Override + public String getMapStyle() { + return cache != null ? new StyleProvider().getActiveStyle(cache) : Style.MAPBOX_STREETS; + } +} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapLocationProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapLocationProvider.java new file mode 100644 index 0000000..18477d6 --- /dev/null +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapLocationProvider.java @@ -0,0 +1,40 @@ +package gps.navigator.mapboxsdk.callback; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.Style; + +import gps.navigator.mapboxsdk.interfaces.MapSdkProviderListener; + +public abstract class MapLocationProvider implements MapSdkProviderListener { + + @Override + public void onMapSdkProvided(@Nullable MapboxMap mapboxMap) { + if (mapboxMap != null) { + mapboxMap.setStyle(getMapStyle(), new StyleLoader(mapboxMap, this)); + } + } + + private static class StyleLoader implements Style.OnStyleLoaded { + private MapboxMap mapboxMap; + private MapLocationProvider mapLocationProvider; + + private StyleLoader(MapboxMap mapboxMap, MapLocationProvider mapLocationProvider) { + this.mapboxMap = mapboxMap; + this.mapLocationProvider = mapLocationProvider; + } + + @Override + public void onStyleLoaded(@NonNull Style style) { + if (mapLocationProvider != null && mapboxMap != null) { + mapLocationProvider.openMap(mapboxMap); + } + } + } + + public abstract void openMap(MapboxMap mapboxMap); + + public abstract String getMapStyle(); +} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSdkProviderListenerImpl.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSdkProviderListenerImpl.java deleted file mode 100644 index f84f23c..0000000 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSdkProviderListenerImpl.java +++ /dev/null @@ -1,42 +0,0 @@ -package gps.navigator.mapboxsdk.callback; - -import androidx.annotation.NonNull; - -import com.mapbox.mapboxsdk.maps.MapboxMap; -import com.mapbox.mapboxsdk.maps.Style; - -import gps.map.navigator.model.interfaces.Cache; -import gps.map.navigator.model.interfaces.MapSdk; -import gps.navigator.mapboxsdk.MapSdkInstance; -import gps.navigator.mapboxsdk.MapSdkProvider; -import gps.navigator.mapboxsdk.interfaces.MapSdkProviderListener; - -public class MapSdkProviderListenerImpl implements MapSdkProviderListener { - private Cache cache; - private MapSdk mapSdk; - - public MapSdkProviderListenerImpl(Cache cache, MapSdk mapSdk) { - this.cache = cache; - this.mapSdk = mapSdk; - } - - @Override - public void onMapSdkProvided(MapboxMap mapboxMap) { - MapSdkProvider provider = MapSdkProvider.getInstance(); - MapSdkInstance instance = provider.getMapSdkInstance(); - if (instance == null) { - instance = new MapSdkInstance(mapboxMap); - provider.setMapSdkInstance(instance); - } - if (cache != null) { - instance.setStyle(cache.getMapSettings(), new Style.OnStyleLoaded() { - @Override - public void onStyleLoaded(@NonNull Style style) { - if (mapSdk != null) { - mapSdk.showMap(); - } - } - }); - } - } -} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSettingsProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSettingsProvider.java new file mode 100644 index 0000000..7380a94 --- /dev/null +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/MapSettingsProvider.java @@ -0,0 +1,21 @@ +package gps.navigator.mapboxsdk.callback; + +import com.mapbox.mapboxsdk.maps.MapboxMap; + +import gps.map.navigator.model.interfaces.MapSetting; +import gps.navigator.mapboxsdk.StyleProvider; +import gps.navigator.mapboxsdk.interfaces.MapSdkProviderListener; + +public class MapSettingsProvider implements MapSdkProviderListener { + + private MapSetting setting; + + public MapSettingsProvider(MapSetting setting) { + this.setting = setting; + } + + @Override + public void onMapSdkProvided(MapboxMap mapboxMap) { + mapboxMap.setStyle(new StyleProvider().getStyle(setting), null); + } +} From f8827fbbdc8818b34e47b02450b113ef0e07df58 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:03:39 +0200 Subject: [PATCH 15/24] Add eventbus messages --- .../mapboxsdk/event/ChangeSettingsEvent.java | 16 ++++++++++++++++ .../mapboxsdk/event/MessageEvent.java | 19 +++++++++++++++++++ .../mapboxsdk/event/RequestLocationEvent.java | 16 ++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/ChangeSettingsEvent.java create mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/MessageEvent.java create mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/RequestLocationEvent.java diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/ChangeSettingsEvent.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/ChangeSettingsEvent.java new file mode 100644 index 0000000..efbe7a0 --- /dev/null +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/ChangeSettingsEvent.java @@ -0,0 +1,16 @@ +package gps.navigator.mapboxsdk.event; + +import gps.map.navigator.model.interfaces.MapSetting; + +public class ChangeSettingsEvent extends MessageEvent { + private MapSetting setting; + + public ChangeSettingsEvent(String messageId, MapSetting setting) { + super(messageId); + this.setting = setting; + } + + public MapSetting getSetting() { + return setting; + } +} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/MessageEvent.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/MessageEvent.java new file mode 100644 index 0000000..e8f6f76 --- /dev/null +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/MessageEvent.java @@ -0,0 +1,19 @@ +package gps.navigator.mapboxsdk.event; + + +public class MessageEvent { + + public static final String TYPE_SHOW_DEVICE_LOCATION = "show_device_location"; + public static final String TYPE_SHOW_LAST_LOCATION = "show_last_location"; + public static final String TYPE_CHANGE_MAP_SETTINGS = "change_map_settings"; + + private String messageId; + + public MessageEvent(String messageId) { + this.messageId = messageId; + } + + public String getMessageId() { + return messageId; + } +} diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/RequestLocationEvent.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/RequestLocationEvent.java new file mode 100644 index 0000000..4785365 --- /dev/null +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/event/RequestLocationEvent.java @@ -0,0 +1,16 @@ +package gps.navigator.mapboxsdk.event; + +import gps.map.navigator.view.interfaces.IPlaceListener; + +public class RequestLocationEvent extends MessageEvent { + private IPlaceListener listener; + + public RequestLocationEvent(String messageId, IPlaceListener listener) { + super(messageId); + this.listener = listener; + } + + public IPlaceListener getListener() { + return listener; + } +} From 7980c9587f4f48c0b1dafd2e7faeae13eab3c93c Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:03:54 +0200 Subject: [PATCH 16/24] Add null check --- .../mapboxsdk/map/MapboxMapProvider.java | 76 +++++++++++-------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/map/MapboxMapProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/map/MapboxMapProvider.java index 762c269..9e4f900 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/map/MapboxMapProvider.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/map/MapboxMapProvider.java @@ -17,64 +17,67 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.Style; +import org.jetbrains.annotations.Nullable; + import gps.map.navigator.model.interfaces.Cache; import gps.map.navigator.model.interfaces.IMapPlace; import gps.map.navigator.view.interfaces.IPlaceListener; -import gps.navigator.mapboxsdk.MapSdkInstance; -import gps.navigator.mapboxsdk.MapSdkProvider; import gps.navigator.mapboxsdk.callback.StyleLoadedCallback; public class MapboxMapProvider implements IMapProvider { + @Nullable private Context context; + @Nullable private Cache cache; + @Nullable + private MapboxMap mapboxMap; - public MapboxMapProvider(Context context, Cache cache) { + public MapboxMapProvider(@Nullable Context context, @Nullable Cache cache, @Nullable MapboxMap mapboxMap) { this.context = context; this.cache = cache; + this.mapboxMap = mapboxMap; } @Override - public void showDeviceLocation(IPlaceListener placeListener) { - MapSdkProvider provider = MapSdkProvider.getInstance(); - MapSdkInstance mapboxMap = provider.getMapSdkInstance(); + public void showDeviceLocation(@Nullable IPlaceListener placeListener) { if (mapboxMap != null) { - MapboxMap map = mapboxMap.getInstance(); - if (map != null) { - map.getStyle(new StyleLoadedCallback(context, map, placeListener)); - } else { - placeListener.onPlaceLocationFailed(new Exception("No map")); - } - } else { + mapboxMap.getStyle(new StyleLoadedCallback(context, mapboxMap, placeListener)); + } else if (placeListener != null) { placeListener.onPlaceLocationFailed(new Exception("No map cache")); } } @Override - public void showInitialLocation(IPlaceListener placeListener) { - MapSdkProvider provider = MapSdkProvider.getInstance(); - MapSdkInstance mapboxMap = provider.getMapSdkInstance(); + public void showInitialLocation(@Nullable final IPlaceListener placeListener) { if (mapboxMap != null) { - final MapboxMap map = mapboxMap.getInstance(); final CameraPosition location = getPosition(); - if (map != null && location != null) { - map.setCameraPosition(location); - map.getStyle(new Style.OnStyleLoaded() { + if (location != null) { + mapboxMap.setCameraPosition(location); + mapboxMap.getStyle(new Style.OnStyleLoaded() { @SuppressLint("MissingPermission") @Override public void onStyleLoaded(@NonNull Style style) { - LocationComponent component = map.getLocationComponent(); - component.activateLocationComponent( - LocationComponentActivationOptions.builder(context, style) - .useDefaultLocationEngine(false) - .build()); - if (PermissionsManager.areLocationPermissionsGranted(context) && locationEnabled()) { - component.setLocationComponentEnabled(true); - } else { - component.setLocationComponentEnabled(false); + if (context != null && PermissionsManager.areLocationPermissionsGranted(context)) { + try { + LocationComponent component = mapboxMap.getLocationComponent(); + component.activateLocationComponent( + LocationComponentActivationOptions.builder(context, style) + .useDefaultLocationEngine(false) + .build()); + if (locationEnabled()) { + component.setLocationComponentEnabled(true); + component.setCameraMode(CameraMode.TRACKING); + component.setRenderMode(RenderMode.COMPASS); + component.forceLocationUpdate(buildLastLocation()); + } else { + component.setLocationComponentEnabled(false); + } + } catch (Throwable t) { + if (placeListener != null) { + placeListener.onPlaceLocationFailed(new Exception(t)); + } + } } - component.setCameraMode(CameraMode.TRACKING); - component.setRenderMode(RenderMode.COMPASS); - component.forceLocationUpdate(buildLastLocation()); } }); } @@ -82,6 +85,9 @@ public void onStyleLoaded(@NonNull Style style) { } private boolean locationEnabled() { + if (context == null) { + return false; + } LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); try { return lm != null && lm.isProviderEnabled(LocationManager.GPS_PROVIDER); @@ -91,6 +97,9 @@ private boolean locationEnabled() { } private Location buildLastLocation() { + if (cache == null) { + return null; + } Location location = new Location(""); IMapPlace place = cache.getLastPlace(); if (place == null) { @@ -106,6 +115,9 @@ private Location buildLastLocation() { } private CameraPosition getPosition() { + if (cache == null) { + return null; + } IMapPlace place = cache.getLastPlace(); if (place == null) { place = cache.getMyLocation(); From 021963370e785c3602e0ebf593cedb62de55ec3a Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:04:04 +0200 Subject: [PATCH 17/24] Add null check --- .../navigation/NavigationRouteProvider.java | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/navigation/NavigationRouteProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/navigation/NavigationRouteProvider.java index 3ca84d6..b439842 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/navigation/NavigationRouteProvider.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/navigation/NavigationRouteProvider.java @@ -29,15 +29,20 @@ import retrofit2.Response; public class NavigationRouteProvider implements INavigationProvider { + @Nullable private Context context; + @Nullable private MapboxMap mapboxMap; + @Nullable private NavigationMapRoute navigationMapRoute; - public NavigationRouteProvider(Context context, MapboxMap mapboxMap, MapView mapView, Style style) { + public NavigationRouteProvider(@Nullable Context context, @Nullable MapboxMap mapboxMap, @Nullable MapView mapView, @Nullable Style style) { this.context = context; this.mapboxMap = mapboxMap; initializeLocationComponent(mapboxMap, style); - navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap); + if (mapboxMap != null && mapView != null) { + navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap); + } } @Override @@ -66,12 +71,14 @@ public void onResponse(Call call, Response call, Throwable t) { } private Marker getMarker(LatLng position) { - return mapboxMap.addMarker(new MarkerOptions().position(position)); + return mapboxMap != null ? mapboxMap.addMarker(new MarkerOptions().position(position)) : null; } @Override @@ -93,14 +100,16 @@ public void drawRoute(@Nullable final DirectionsRoute route) { } @SuppressWarnings("MissingPermission") - private void initializeLocationComponent(MapboxMap mapboxMap, Style style) { - LocationComponent component = mapboxMap.getLocationComponent(); - component.activateLocationComponent(context, style); + private void initializeLocationComponent(@Nullable MapboxMap mapboxMap, @Nullable Style style) { + if (mapboxMap != null && style != null && context != null) { + LocationComponent component = mapboxMap.getLocationComponent(); + component.activateLocationComponent(context, style); - component.setLocationComponentEnabled(false); - component.setRenderMode(RenderMode.COMPASS); - component.setCameraMode(CameraMode.TRACKING); - component.zoomWhileTracking(10d); + component.setLocationComponentEnabled(false); + component.setRenderMode(RenderMode.COMPASS); + component.setCameraMode(CameraMode.TRACKING); + component.zoomWhileTracking(10d); + } } private LatLng getPlace(Point origin) { From 329453318082ca5cc6198e191e8c75132d2c5a50 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:04:19 +0200 Subject: [PATCH 18/24] Add eventbus --- mapboxsdk/build.gradle | 2 ++ .../mapboxsdk/interfaces/NavigationStatusListener.java | 8 ++++++++ 2 files changed, 10 insertions(+) create mode 100644 mapboxsdk/src/main/java/gps/navigator/mapboxsdk/interfaces/NavigationStatusListener.java diff --git a/mapboxsdk/build.gradle b/mapboxsdk/build.gradle index 190e33e..7428f42 100644 --- a/mapboxsdk/build.gradle +++ b/mapboxsdk/build.gradle @@ -49,4 +49,6 @@ dependencies { implementation "com.mapbox.mapboxsdk:mapbox-android-navigation:$rootProject.navigationLibraryVersion" implementation "com.mapbox.mapboxsdk:mapbox-android-navigation-ui:$rootProject.navigationLibraryVersion" + implementation "org.greenrobot:eventbus:$rootProject.eventbusVersion" + } diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/interfaces/NavigationStatusListener.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/interfaces/NavigationStatusListener.java new file mode 100644 index 0000000..fe77c21 --- /dev/null +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/interfaces/NavigationStatusListener.java @@ -0,0 +1,8 @@ +package gps.navigator.mapboxsdk.interfaces; + +public interface NavigationStatusListener { + + void onNavigationRunning(); + + void onNavigationFailed(); +} From 393b5294e6ba5315968f57936e2f0b42c35ae240 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:04:25 +0200 Subject: [PATCH 19/24] Add eventbus --- build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle b/build.gradle index a2e3754..d693429 100644 --- a/build.gradle +++ b/build.gradle @@ -48,6 +48,7 @@ ext { daggerVersion = '2.21' constraintlayoutLibraryVersion = '1.1.3' materialLibraryVersion = '1.0.0-alpha1' + eventbusVersion = '3.1.1' //map dependencies mapboxSdkLibraryVersion = '8.4.0' From 6e82bedc89b95762a5926286059e132d14385f78 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 18:05:05 +0200 Subject: [PATCH 20/24] Version up --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index d693429..7616d51 100644 --- a/build.gradle +++ b/build.gradle @@ -32,8 +32,8 @@ ext { bundleId = "gps.map.navigator" //versions - versionCode = 5 - versionName = "1.0.5" + versionCode = 6 + versionName = "1.0.6" //proguard activateProguard = false From e65f888892b22e0e9c0740447648ab84c61bed97 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 19:36:45 +0200 Subject: [PATCH 21/24] Fix tests --- .../gps/map/navigator/model/DataCache.java | 8 +++-- .../view/ui/fragment/BuildRouteFragment.java | 2 +- .../view/ui/fragment/FindPlaceFragment.java | 2 +- .../viewmodel/recyclerview/AdapterFilter.java | 11 +++--- .../recyclerview/MapPlaceAdapter.java | 6 ++-- .../ui/fragment/BuildRouteFragmentTest.java | 18 ++++++---- .../ui/fragment/ShowRouteFragmentTest.java | 36 +++++++++++++++++-- .../DestinationChangeListenerTest.java | 24 ++++++++++++- .../listener/OriginChangeListenerTest.java | 24 ++++++++++++- .../view/ui/fragment/MapboxFragment.java | 14 ++++---- .../callback/NavigationReadyCallback.java | 2 +- .../geocode/impl/LocationIqGeocode.java | 2 +- .../navigation/NavigationRouteProvider.java | 2 +- 13 files changed, 120 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/gps/map/navigator/model/DataCache.java b/app/src/main/java/gps/map/navigator/model/DataCache.java index 55e9256..7c9a115 100644 --- a/app/src/main/java/gps/map/navigator/model/DataCache.java +++ b/app/src/main/java/gps/map/navigator/model/DataCache.java @@ -46,7 +46,7 @@ public class DataCache implements Cache { @Override public List getHistoryPlaces() { List arrays = storage.getChunkedData(); - if (arrays != null) { + if (arrays != null && !arrays.isEmpty()) { int cacheSize = arrays.size(); List output = new ArrayList<>(cacheSize); for (int i = 0; i < cacheSize; i++) { @@ -69,7 +69,7 @@ private List appendMyLocation(@NonNull List output) { @Override public void setHistoryPlaces(@Nullable List historyPlaces) { - if (historyPlaces != null) { + if (historyPlaces != null && !historyPlaces.isEmpty()) { sortByLastUsedTime(historyPlaces); List arrays = new ArrayList<>(); for (int i = 0; i < historyPlaces.size(); i++) { @@ -80,7 +80,9 @@ public void setHistoryPlaces(@Nullable List historyPlaces) { } private void sortByLastUsedTime(@NonNull List places) { - Collections.sort(places, new DescendingTimeComparator()); + if (!places.isEmpty()) { + Collections.sort(places, new DescendingTimeComparator()); + } } @Nullable diff --git a/app/src/main/java/gps/map/navigator/view/ui/fragment/BuildRouteFragment.java b/app/src/main/java/gps/map/navigator/view/ui/fragment/BuildRouteFragment.java index 5031254..61cd396 100644 --- a/app/src/main/java/gps/map/navigator/view/ui/fragment/BuildRouteFragment.java +++ b/app/src/main/java/gps/map/navigator/view/ui/fragment/BuildRouteFragment.java @@ -269,7 +269,7 @@ public void markAsFavouritePlace(@NonNull IMapPlace mapPlace) { private void setFavouriteState(@NonNull IMapPlace mapPlace, boolean favourite) { List places = presenter.getHistoryPlaces(); - if (places != null) { + if (places != null && !places.isEmpty()) { int position = getPosition(places, mapPlace); mapPlace.setFavourite(favourite); diff --git a/app/src/main/java/gps/map/navigator/view/ui/fragment/FindPlaceFragment.java b/app/src/main/java/gps/map/navigator/view/ui/fragment/FindPlaceFragment.java index 6e99967..e803620 100644 --- a/app/src/main/java/gps/map/navigator/view/ui/fragment/FindPlaceFragment.java +++ b/app/src/main/java/gps/map/navigator/view/ui/fragment/FindPlaceFragment.java @@ -149,7 +149,7 @@ public void markAsFavouritePlace(@NonNull IMapPlace mapPlace) { private void setFavouriteState(@NonNull IMapPlace mapPlace, boolean favourite) { List places = presenter.getHistoryPlaces(); - if (places != null) { + if (places != null && !places.isEmpty()) { int position = getPosition(places, mapPlace); mapPlace.setFavourite(favourite); diff --git a/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/AdapterFilter.java b/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/AdapterFilter.java index 90461f9..6a30a63 100644 --- a/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/AdapterFilter.java +++ b/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/AdapterFilter.java @@ -21,9 +21,12 @@ public class AdapterFilter extends Filter { @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { - if (adapter != null) { - adapter.changePlacesList((List) results.values); - adapter.notifyDataSetChanged(); + if (adapter != null && results != null && results.values instanceof List) { + List list = (List) results.values; + if (!list.isEmpty()) { + adapter.changePlacesList(list); + adapter.notifyDataSetChanged(); + } } } @@ -45,7 +48,7 @@ protected FilterResults performFiltering(CharSequence constraint) { @NonNull private List getFilteredResults(@Nullable List originalPlacesList, @NonNull String constraint) { List results = new ArrayList<>(); - if (originalPlacesList != null) { + if (originalPlacesList != null && !originalPlacesList.isEmpty()) { for (IMapPlace item : originalPlacesList) { if (itemMatch(item, constraint)) { results.add(item); diff --git a/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/MapPlaceAdapter.java b/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/MapPlaceAdapter.java index 712172d..953e5e5 100644 --- a/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/MapPlaceAdapter.java +++ b/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/MapPlaceAdapter.java @@ -126,7 +126,7 @@ public void setInitialPlacesList(@NonNull List places) { public void showFoundedPlacesList(@NonNull List foundedPlaces) { if (places != null) { places.clear(); - } else { + } else { places = new ArrayList<>(); } places.addAll(foundedPlaces); @@ -144,6 +144,8 @@ private int getPosition(@NonNull List places, @NonNull IMapPlace item } private void sortByLastUsedTime(@NonNull List places) { - Collections.sort(places, new DescendingTimeComparator()); + if (!places.isEmpty()) { + Collections.sort(places, new DescendingTimeComparator()); + } } } diff --git a/app/src/test/java/gps/map/navigator/view/ui/fragment/BuildRouteFragmentTest.java b/app/src/test/java/gps/map/navigator/view/ui/fragment/BuildRouteFragmentTest.java index d4cf93f..ac7c064 100644 --- a/app/src/test/java/gps/map/navigator/view/ui/fragment/BuildRouteFragmentTest.java +++ b/app/src/test/java/gps/map/navigator/view/ui/fragment/BuildRouteFragmentTest.java @@ -188,7 +188,8 @@ public void make_onCreateView_no_history_places_verify() { @Test public void make_onCreateView_has_history_places_verify() { BuildRouteFragment fragment = createFragment(); - setOriginAndDestinationTitle(); + when(presenter.getLastOrigin()).thenReturn(originPlace); + when(presenter.getLastDestination()).thenReturn(null); View created = fragment.onCreateView(inflater, null, null); @@ -201,10 +202,10 @@ public void make_onCreateView_has_history_places_verify() { verify(destinationTitle).setOnClickListener(eq(destinationClickListener)); verify(originTitle).setText(eq("title1")); - verify(destinationTitle).setText(eq("title2")); + verify(destinationTitle).setText(eq("destination")); verify(presenter).setLastOrigin(eq(originPlace)); - verify(presenter).setLastDestination(eq(destinationPlace)); + verify(presenter).setLastDestination(nullable(IMapPlace.class)); verify(presenter).buildRoute(eq(buildRouteCallback)); } @@ -214,12 +215,15 @@ public void make_swipeOriginAndDestination_verify() { BuildRouteFragment fragment = createFragment(); setOriginAndDestinationTitle(); fragment.onCreateView(inflater, null, null); + when(presenter.getLastOrigin()).thenReturn(originPlace); + when(presenter.getLastDestination()).thenReturn(null); + fragment.swipeOriginAndDestination(); - verify(presenter).setLastOrigin(eq(destinationPlace)); + verify(presenter, times(2)).setLastOrigin(nullable(IMapPlace.class)); verify(presenter).setLastDestination(eq(originPlace)); - verify(originTitle).setText(eq("title2")); + verify(originTitle, times(2)).setText(eq("origin")); verify(destinationTitle).setText(eq("title1")); } @@ -284,9 +288,11 @@ public void make_setNewPickedPlace_verify_origin() { @Test public void make_setNewPickedPlace_verify_destination() { BuildRouteFragment fragment = createFragment(); - setInternalState(fragment, "originPlace", originPlace); + when(presenter.getLastOrigin()).thenReturn(originPlace); + when(presenter.getLastDestination()).thenReturn(null); IMapPlace place = getOriginPlace(); + when(place.getLongitude()).thenReturn(12d); fragment.setNewPickedPlace(place); verify(presenter).setLastDestination(eq(place)); diff --git a/app/src/test/java/gps/map/navigator/view/ui/fragment/ShowRouteFragmentTest.java b/app/src/test/java/gps/map/navigator/view/ui/fragment/ShowRouteFragmentTest.java index 410b0a8..5e0019f 100644 --- a/app/src/test/java/gps/map/navigator/view/ui/fragment/ShowRouteFragmentTest.java +++ b/app/src/test/java/gps/map/navigator/view/ui/fragment/ShowRouteFragmentTest.java @@ -189,10 +189,26 @@ private IRoute createRoute() { } @Test - public void make_setOnlyOrigin_verify() { + public void make_setOnlyOrigin_same_as_destination_verify() { ShowRouteFragment fragment = createFragment(); IRoute newRoute = createRoute(); when(presenter.getLastRoute()).thenReturn(newRoute); + when(newRoute.getDestination()).thenReturn(destinationPlace); + + fragment.setOnlyOrigin(destinationPlace); + + verify(newRoute, times(0)).setDestination(any(IMapPlace.class)); + verify(newRoute, times(0)).setOrigin(eq(destinationPlace)); + } + + @Test + public void make_setOnlyOrigin_not_same_as_destination_verify() { + ShowRouteFragment fragment = createFragment(); + IRoute newRoute = createRoute(); + when(presenter.getLastRoute()).thenReturn(newRoute); + IMapPlace newPlace = mock(IMapPlace.class); + when(newPlace.getLongitude()).thenReturn(12d); + when(newRoute.getDestination()).thenReturn(newPlace); fragment.setOnlyOrigin(destinationPlace); @@ -201,14 +217,30 @@ public void make_setOnlyOrigin_verify() { } @Test - public void make_setOnlyDestination_verify() { + public void make_setOnlyDestination_not_same_as_origin_verify() { ShowRouteFragment fragment = createFragment(); IRoute newRoute = createRoute(); when(presenter.getLastRoute()).thenReturn(newRoute); + IMapPlace newPlace = mock(IMapPlace.class); + when(newPlace.getLongitude()).thenReturn(12d); + when(newRoute.getOrigin()).thenReturn(newPlace); fragment.setOnlyDestination(originPlace); verify(newRoute, times(0)).setOrigin(any(IMapPlace.class)); verify(newRoute).setDestination(eq(originPlace)); } + + @Test + public void make_setOnlyDestination_same_as_origin_verify() { + ShowRouteFragment fragment = createFragment(); + IRoute newRoute = createRoute(); + when(presenter.getLastRoute()).thenReturn(newRoute); + when(newRoute.getOrigin()).thenReturn(originPlace); + + fragment.setOnlyDestination(originPlace); + + verify(newRoute, times(0)).setOrigin(any(IMapPlace.class)); + verify(newRoute, times(0)).setDestination(eq(originPlace)); + } } \ No newline at end of file diff --git a/app/src/test/java/gps/map/navigator/view/ui/fragment/listener/DestinationChangeListenerTest.java b/app/src/test/java/gps/map/navigator/view/ui/fragment/listener/DestinationChangeListenerTest.java index db91bd8..b1f2255 100644 --- a/app/src/test/java/gps/map/navigator/view/ui/fragment/listener/DestinationChangeListenerTest.java +++ b/app/src/test/java/gps/map/navigator/view/ui/fragment/listener/DestinationChangeListenerTest.java @@ -3,27 +3,49 @@ import org.junit.Before; import org.junit.Test; +import gps.map.navigator.model.interfaces.Cache; import gps.map.navigator.model.interfaces.IMapPlace; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.powermock.reflect.Whitebox.setInternalState; public class DestinationChangeListenerTest { private ISwipeRoute swipeRoute; private IMapPlace mapPlace; + private Cache cache; @Before public void setUp() throws Exception { swipeRoute = mock(ISwipeRoute.class); mapPlace = mock(IMapPlace.class); + cache = mock(Cache.class); } @Test - public void receive_onClick_verify() { + public void receive_onPlaceLocated_same_as_origin_verify() { DestinationChangeListener listener = new DestinationChangeListener(); setInternalState(listener, "swipeRoute", swipeRoute); + setInternalState(listener, "cache", cache); + when(cache.getLastOrigin()).thenReturn(mapPlace); + + listener.onPlaceLocated(mapPlace); + + verify(swipeRoute, times(0)).setOnlyDestination(eq(mapPlace)); + } + + @Test + public void receive_onPlaceLocated_not_same_as_origin_verify() { + DestinationChangeListener listener = new DestinationChangeListener(); + setInternalState(listener, "swipeRoute", swipeRoute); + setInternalState(listener, "cache", cache); + IMapPlace place = mock(IMapPlace.class); + when(place.getLatitude()).thenReturn(12d); + when(place.getLongitude()).thenReturn(12d); + when(cache.getLastOrigin()).thenReturn(place); listener.onPlaceLocated(mapPlace); diff --git a/app/src/test/java/gps/map/navigator/view/ui/fragment/listener/OriginChangeListenerTest.java b/app/src/test/java/gps/map/navigator/view/ui/fragment/listener/OriginChangeListenerTest.java index 8438744..b27cf9e 100644 --- a/app/src/test/java/gps/map/navigator/view/ui/fragment/listener/OriginChangeListenerTest.java +++ b/app/src/test/java/gps/map/navigator/view/ui/fragment/listener/OriginChangeListenerTest.java @@ -3,28 +3,50 @@ import org.junit.Before; import org.junit.Test; +import gps.map.navigator.model.interfaces.Cache; import gps.map.navigator.model.interfaces.IMapPlace; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import static org.powermock.reflect.Whitebox.setInternalState; public class OriginChangeListenerTest { private ISwipeRoute swipeRoute; private IMapPlace mapPlace; + private Cache cache; @Before public void setUp() throws Exception { swipeRoute = mock(ISwipeRoute.class); mapPlace = mock(IMapPlace.class); + cache = mock(Cache.class); } @Test - public void receive_onPlaceLocated_verify() { + public void receive_onPlaceLocated_same_as_destination_verify() { OriginChangeListener listener = new OriginChangeListener(); setInternalState(listener, "swipeRoute", swipeRoute); + setInternalState(listener, "cache", cache); + when(cache.getLastDestination()).thenReturn(mapPlace); + + listener.onPlaceLocated(mapPlace); + + verify(swipeRoute, times(0)).setOnlyOrigin(eq(mapPlace)); + } + + @Test + public void receive_onPlaceLocated_not_same_as_destination_verify() { + OriginChangeListener listener = new OriginChangeListener(); + setInternalState(listener, "swipeRoute", swipeRoute); + setInternalState(listener, "cache", cache); + IMapPlace place = mock(IMapPlace.class); + when(place.getLatitude()).thenReturn(12d); + when(place.getLongitude()).thenReturn(12d); + when(cache.getLastDestination()).thenReturn(place); listener.onPlaceLocated(mapPlace); diff --git a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/MapboxFragment.java b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/MapboxFragment.java index ce66bed..49fe16a 100644 --- a/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/MapboxFragment.java +++ b/mapboxsdk/src/main/java/gps/map/navigator/view/ui/fragment/MapboxFragment.java @@ -17,7 +17,7 @@ public abstract class MapboxFragment extends Fragment { public void onResume() { super.onResume(); MapView mapView = getMapView(); - if (mapView != null) { + if (mapView != null && !mapView.isDestroyed()) { mapView.onResume(); } } @@ -26,7 +26,7 @@ public void onResume() { public void onPause() { super.onPause(); MapView mapView = getMapView(); - if (mapView != null) { + if (mapView != null && !mapView.isDestroyed()) { mapView.onPause(); } } @@ -35,7 +35,7 @@ public void onPause() { public void onStart() { super.onStart(); MapView mapView = getMapView(); - if (mapView != null) { + if (mapView != null && !mapView.isDestroyed()) { mapView.onStart(); } } @@ -44,7 +44,7 @@ public void onStart() { public void onStop() { super.onStop(); MapView mapView = getMapView(); - if (mapView != null) { + if (mapView != null && !mapView.isDestroyed()) { mapView.onStop(); } } @@ -53,7 +53,7 @@ public void onStop() { public void onLowMemory() { super.onLowMemory(); MapView mapView = getMapView(); - if (mapView != null) { + if (mapView != null && !mapView.isDestroyed()) { mapView.onLowMemory(); } } @@ -62,7 +62,7 @@ public void onLowMemory() { public void onDestroyView() { super.onDestroyView(); MapView mapView = getMapView(); - if (mapView != null) { + if (mapView != null && !mapView.isDestroyed()) { mapView.onDestroy(); } } @@ -71,7 +71,7 @@ public void onDestroyView() { public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); MapView mapView = getMapView(); - if (mapView != null) { + if (mapView != null && !mapView.isDestroyed()) { mapView.onSaveInstanceState(outState); } } diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java index f18852d..8483958 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/callback/NavigationReadyCallback.java @@ -75,7 +75,7 @@ public void onResponse(Call call, Response routes = body.routes(); - if (routes.size() > 0) { + if (!routes.isEmpty()) { startNavigation(routes.get(0)); } else { if (listener != null) { diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/geocode/impl/LocationIqGeocode.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/geocode/impl/LocationIqGeocode.java index c17572b..77300e7 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/geocode/impl/LocationIqGeocode.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/geocode/impl/LocationIqGeocode.java @@ -32,7 +32,7 @@ public void searchForLocations(@Nullable String query, @Nullable final IPlaceLis @Override public void onResponse(Call> call, Response> response) { List body = response.body(); - if (body != null) { + if (body != null && !body.isEmpty()) { List tely = new ArrayList<>(); Place vely; for (int i = 0; i < body.size(); i++) { diff --git a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/navigation/NavigationRouteProvider.java b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/navigation/NavigationRouteProvider.java index b439842..71e50f3 100644 --- a/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/navigation/NavigationRouteProvider.java +++ b/mapboxsdk/src/main/java/gps/navigator/mapboxsdk/navigation/NavigationRouteProvider.java @@ -40,7 +40,7 @@ public NavigationRouteProvider(@Nullable Context context, @Nullable MapboxMap ma this.context = context; this.mapboxMap = mapboxMap; initializeLocationComponent(mapboxMap, style); - if (mapboxMap != null && mapView != null) { + if (mapboxMap != null && mapView != null && !mapView.isDestroyed()) { navigationMapRoute = new NavigationMapRoute(null, mapView, mapboxMap); } } From f1f23c8de5426c67f00d639b32df5f8e48a504e9 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 19:37:44 +0200 Subject: [PATCH 22/24] Version up --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 7616d51..86a401e 100644 --- a/build.gradle +++ b/build.gradle @@ -32,8 +32,8 @@ ext { bundleId = "gps.map.navigator" //versions - versionCode = 6 - versionName = "1.0.6" + versionCode = 7 + versionName = "1.0.7" //proguard activateProguard = false From cfce699be41bde2d4339110ee2528498a8661368 Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 21:56:59 +0200 Subject: [PATCH 23/24] Handle update issue --- .../recyclerview/MapPlaceAdapter.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/MapPlaceAdapter.java b/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/MapPlaceAdapter.java index 953e5e5..20887c5 100644 --- a/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/MapPlaceAdapter.java +++ b/app/src/main/java/gps/map/navigator/view/viewmodel/recyclerview/MapPlaceAdapter.java @@ -106,11 +106,19 @@ public void removePlace(int position, @NonNull IMapPlace place) { @Override public void updatePlace(@NonNull IMapPlace update) { if (places != null && originalPlacesList != null) { - int position = getPosition(places, update); - places.set(position, update); - int originalPosition = getPosition(originalPlacesList, update); - originalPlacesList.set(originalPosition, update); - notifyItemChanged(position); + if (places.isEmpty()) { + places.add(update); + } else { + int position = getPosition(places, update); + places.set(position, update); + if (originalPlacesList.isEmpty()) { + originalPlacesList.add(update); + } else { + int originalPosition = getPosition(originalPlacesList, update); + originalPlacesList.set(originalPosition, update); + } + notifyItemChanged(position); + } } } @@ -135,6 +143,9 @@ public void showFoundedPlacesList(@NonNull List foundedPlaces) { } private int getPosition(@NonNull List places, @NonNull IMapPlace item) { + if (places.isEmpty()) { + return 0; + } for (int i = 0; i < places.size(); i++) { if (item.getId().equals(places.get(i).getId())) { return i; From 1114a7b05e5f0f5be2120b44092fd13cbe0827df Mon Sep 17 00:00:00 2001 From: obolsh Date: Sun, 17 Nov 2019 21:57:54 +0200 Subject: [PATCH 24/24] Version up --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 86a401e..e65deba 100644 --- a/build.gradle +++ b/build.gradle @@ -32,8 +32,8 @@ ext { bundleId = "gps.map.navigator" //versions - versionCode = 7 - versionName = "1.0.7" + versionCode = 8 + versionName = "1.0.8" //proguard activateProguard = false