From f5da1c8a83b056d067311e94fdd876a118fab3fa Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 6 Jan 2021 11:33:15 +0100 Subject: [PATCH] added displaying all locations upon stopping the route --- .../a1/nextlocation/data/RouteHandler.java | 27 +++++++ .../com/a1/nextlocation/data/StaticData.java | 8 --- .../nextlocation/fragments/HomeFragment.java | 68 +++++++++++------- .../main/res/drawable-anydpi/ic_stop_icon.xml | 12 ++++ .../main/res/drawable-hdpi/ic_stop_icon.png | Bin 0 -> 318 bytes .../main/res/drawable-mdpi/ic_stop_icon.png | Bin 0 -> 213 bytes .../main/res/drawable-xhdpi/ic_stop_icon.png | Bin 0 -> 416 bytes .../main/res/drawable-xxhdpi/ic_stop_icon.png | Bin 0 -> 552 bytes app/src/main/res/layout/fragment_home.xml | 11 +++ app/src/main/res/values/strings.xml | 1 + 10 files changed, 95 insertions(+), 32 deletions(-) create mode 100644 app/src/main/res/drawable-anydpi/ic_stop_icon.xml create mode 100644 app/src/main/res/drawable-hdpi/ic_stop_icon.png create mode 100644 app/src/main/res/drawable-mdpi/ic_stop_icon.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_stop_icon.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_stop_icon.png diff --git a/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java b/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java index 65fda9b..0075475 100644 --- a/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java +++ b/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java @@ -1,5 +1,7 @@ package com.a1.nextlocation.data; +import org.osmdroid.views.overlay.Polyline; + /** * singleton to track the current route that is being followed */ @@ -9,6 +11,21 @@ public enum RouteHandler { private boolean isFollowingRoute = false; private Route currentRoute; private int stepCount = 0; + private RouteFinishedListener routeFinishedListener; + + private Polyline currentRouteLine; + + public void setCurrentRouteLine(Polyline currentRouteLine) { + this.currentRouteLine = currentRouteLine; + } + + public Polyline getCurrentRouteLine() { + return currentRouteLine; + } + + public void setRouteFinishedListener(RouteFinishedListener routeFinishedListener) { + this.routeFinishedListener = routeFinishedListener; + } public int getStepCount() { return stepCount; @@ -22,6 +39,7 @@ public enum RouteHandler { stepCount = 0; isFollowingRoute = false; currentRoute = null; + currentRouteLine = null; } public void followRoute(Route route) { @@ -39,4 +57,13 @@ public enum RouteHandler { public boolean isFollowingRoute() { return isFollowingRoute; } + + public Route getCurrentRoute() { + return currentRoute; + } + + @FunctionalInterface + public interface RouteFinishedListener { + void onRouteFinish(); + } } diff --git a/app/src/main/java/com/a1/nextlocation/data/StaticData.java b/app/src/main/java/com/a1/nextlocation/data/StaticData.java index 0f0d4e3..d347d42 100644 --- a/app/src/main/java/com/a1/nextlocation/data/StaticData.java +++ b/app/src/main/java/com/a1/nextlocation/data/StaticData.java @@ -34,14 +34,6 @@ public enum StaticData { return locationsVisited; } - private Polyline currentRoute; - public void setCurrentRoute(Polyline currentRoute) { - this.currentRoute = currentRoute; - } - - public Polyline getCurrentRoute() { - return currentRoute; - } } diff --git a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java index 1786782..4a8ca4a 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -1,7 +1,6 @@ package com.a1.nextlocation.fragments; - import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; @@ -25,6 +24,7 @@ import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentActivity; import com.a1.nextlocation.R; +import com.a1.nextlocation.data.RouteHandler; import com.a1.nextlocation.data.StaticData; import com.a1.nextlocation.json.DirectionsResult; import com.a1.nextlocation.network.ApiHandler; @@ -46,17 +46,19 @@ import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay; import java.util.ArrayList; import java.util.List; -public class HomeFragment extends Fragment implements LocationListener{ +public class HomeFragment extends Fragment implements LocationListener { private final String userAgent = "com.ai.nextlocation.fragments"; public final static String MAPQUEST_API_KEY = "vuyXjqnAADpjeL9QwtgWGleIk95e36My"; private ImageButton imageButton; + private ImageButton stopButton; private MapView mapView; private final int REQUEST_PERMISSIONS_REQUEST_CODE = 1; private final String TAG = HomeFragment.class.getCanonicalName(); -// private RoadManager roadManager; + // private RoadManager roadManager; private Polyline roadOverlay; private int color; private Location currentLocation; + private Overlay allLocationsOverlay; @Override public void onCreate(Bundle savedInstanceState) { @@ -84,6 +86,23 @@ public class HomeFragment extends Fragment implements LocationListener{ ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, locationFragment).addToBackStack(null).commit(); }); + stopButton = view.findViewById(R.id.home_stop_route_button); + stopButton.setOnClickListener(v -> { + RouteHandler.INSTANCE.finishRoute(); + stopButton.setVisibility(View.GONE); + Toast.makeText(requireContext(),getResources().getString(R.string.route_stop_toast),Toast.LENGTH_SHORT).show(); + mapView.getOverlays().remove(roadOverlay); + mapView.getOverlays().remove(allLocationsOverlay); + addLocations(); + mapView.invalidate(); + roadOverlay = null; + }); + + if (RouteHandler.INSTANCE.isFollowingRoute()) { + stopButton.setVisibility(View.VISIBLE); + } else { + stopButton.setVisibility(View.GONE); + } ApiHandler.INSTANCE.addListener(this::onDirectionsAvailable); return view; } @@ -103,7 +122,7 @@ public class HomeFragment extends Fragment implements LocationListener{ roadOverlay.setColor(color); - StaticData.INSTANCE.setCurrentRoute(roadOverlay); + RouteHandler.INSTANCE.setCurrentRouteLine(roadOverlay); Log.d(TAG, "onDirectionsAvailable: successfully added road!"); } @@ -117,6 +136,7 @@ public class HomeFragment extends Fragment implements LocationListener{ /** * This method initializes the map and all the things it needs + * * @param view the view the map is on */ private void initMap(@NonNull View view) { @@ -133,7 +153,7 @@ public class HomeFragment extends Fragment implements LocationListener{ GpsMyLocationProvider gpsMyLocationProvider = new GpsMyLocationProvider(this.requireContext()); // add the compass overlay - CompassOverlay compassOverlay = new CompassOverlay(requireContext(),new InternalCompassOrientationProvider(requireContext()),mapView); + CompassOverlay compassOverlay = new CompassOverlay(requireContext(), new InternalCompassOrientationProvider(requireContext()), mapView); compassOverlay.enableCompass(); mapView.getOverlays().add(compassOverlay); @@ -153,19 +173,17 @@ public class HomeFragment extends Fragment implements LocationListener{ LocationManager locationManager = (LocationManager) requireActivity().getSystemService(Context.LOCATION_SERVICE); - - try { - locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this); - locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,this); + locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); + locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (currentLocation == null) { currentLocation = location; } - if( location != null ) { + if (location != null) { GeoPoint start = new GeoPoint(location.getLatitude(), location.getLongitude()); mapController.setCenter(start); } @@ -188,32 +206,34 @@ public class HomeFragment extends Fragment implements LocationListener{ private void displayRoute() { - if (roadOverlay == null) { - if (StaticData.INSTANCE.getCurrentRoute() != null) { - roadOverlay = StaticData.INSTANCE.getCurrentRoute(); + if (RouteHandler.INSTANCE.isFollowingRoute()) { + if (roadOverlay == null) { + if (RouteHandler.INSTANCE.getCurrentRouteLine() != null) { + roadOverlay = RouteHandler.INSTANCE.getCurrentRouteLine(); + mapView.getOverlays().add(roadOverlay); + mapView.invalidate(); + Log.d(TAG, "initMap: successfully added road!"); + } + } else { mapView.getOverlays().add(roadOverlay); mapView.invalidate(); Log.d(TAG, "initMap: successfully added road!"); } - } else { - mapView.getOverlays().add(roadOverlay); - mapView.invalidate(); - Log.d(TAG, "initMap: successfully added road!"); } } private void addLocations() { - List locations = LocationListManager.INSTANCE.getLocationList(); + List locations = RouteHandler.INSTANCE.isFollowingRoute() ? RouteHandler.INSTANCE.getCurrentRoute().getLocations() : LocationListManager.INSTANCE.getLocationList(); final ArrayList items = new ArrayList<>(locations.size()); - Drawable marker = ContextCompat.getDrawable(requireContext(),R.drawable.ic_baseline_location_on_24); + Drawable marker = ContextCompat.getDrawable(requireContext(), R.drawable.ic_baseline_location_on_24); marker.setAlpha(255); marker.setTint(getResources().getColor(R.color.primaryColour)); for (com.a1.nextlocation.data.Location location : locations) { - OverlayItem item = new OverlayItem(location.getName(),location.getDescription(),location.convertToGeoPoint()); + OverlayItem item = new OverlayItem(location.getName(), location.getDescription(), location.convertToGeoPoint()); item.setMarker(marker); items.add(item); } - Overlay allLocationsOverlay = new ItemizedIconOverlay(items, + allLocationsOverlay = new ItemizedIconOverlay(items, new ItemizedIconOverlay.OnItemGestureListener() { @Override public boolean onItemSingleTapUp(int index, OverlayItem item) { @@ -225,14 +245,14 @@ public class HomeFragment extends Fragment implements LocationListener{ @Override public boolean onItemLongPress(int index, OverlayItem item) { com.a1.nextlocation.data.Location clicked = locations.get(index); - Toast.makeText(requireContext(), clicked.getName(),Toast.LENGTH_SHORT).show(); + Toast.makeText(requireContext(), clicked.getName(), Toast.LENGTH_SHORT).show(); // Route route = new Route("Route to " + clicked.getName()); // route.addLocation(new com.a1.nextlocation.data.Location("Current location",currentLocation.getLatitude(),currentLocation.getLongitude(),"your location",null)); // route.addLocation(clicked); // ApiHandler.INSTANCE.getDirections(route); return true; } - },requireContext()); + }, requireContext()); mapView.getOverlays().add(allLocationsOverlay); Log.d(TAG, "addLocations: successfully added locations"); @@ -266,7 +286,7 @@ public class HomeFragment extends Fragment implements LocationListener{ //new thread because we don't want the main thread to hang Thread t = new Thread(() -> { for (com.a1.nextlocation.data.Location l : LocationListManager.INSTANCE.getLocationList()) { - if (com.a1.nextlocation.data.Location.getDistance(currentLocation.getLatitude(),currentLocation.getLongitude(),l.getLat(),l.getLong()) < 10) { + if (com.a1.nextlocation.data.Location.getDistance(currentLocation.getLatitude(), currentLocation.getLongitude(), l.getLat(), l.getLong()) < 10) { StaticData.INSTANCE.visitLocation(l); } } diff --git a/app/src/main/res/drawable-anydpi/ic_stop_icon.xml b/app/src/main/res/drawable-anydpi/ic_stop_icon.xml new file mode 100644 index 0000000..addfeb0 --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_stop_icon.xml @@ -0,0 +1,12 @@ + + + diff --git a/app/src/main/res/drawable-hdpi/ic_stop_icon.png b/app/src/main/res/drawable-hdpi/ic_stop_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..10f865ca7c0f856343e17b4e500a2ee08c580238 GIT binary patch literal 318 zcmV-E0m1%>P)|3_zKCht6G*@;OCWo3(dfC2Lo@a@*PxNHnVQBPK>Ak|jil{jiM_+%)I-hoO|Z zf;@x}J;)%XoKWx@@8KLiLkaT5LY&P z3t*n5U=~50)jjhAs!PrpDwq{i8~rtHN#K-3uO6zb&;j||rzABOLv<>&_!fGr!a$R^ zqQj)A0AzgO>h}ghn?4|00cglxOabK8$quMe$n0^Fn%!rQMT%8n+u8KIY}NDs2>_ QbpQYW07*qoM6N<$f{KuX}1CrErdaOKJst`6az z-rj59y121D{RIh;%qAwZz?TlD{x|4oV;M3!7RzX6i|3}x=j${i~4xj@%;ZB|w|1FT(P<+ja}GDI!33Bq(Fi-jW`UL!M;2rgDeg^Q1T)edfy)~c< zI(@myRW3TY0Kr-OqdXgw3V-kMn{{Q^(rb&*jDNJSBypp(48 zLD*sv-dap6O+Im;$tO-}@@-|YNOu(h$F<7{o~b!J+N?>EL?CbT($@~1Q?U>L0000< KMNUMnLSTX)D!CH? literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_stop_icon.png b/app/src/main/res/drawable-xxhdpi/ic_stop_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..b8448198dbc8314af7c75381bc4202b4ee7a3afc GIT binary patch literal 552 zcmV+@0@wYCP)SAq6L}2em~ToU;Nj zV{bZZ38|YlI6-c@iG`eYIHHiOz!8IF2~G%UtAu>91}8||mo0|!!Lmgw0Vsx*)H&=r zDM~()tdL_=+^f&6Qh^g#v8MNQsR5!s!F#swA{Vb$P9rv0TH)lP>l!21kSZV_YB|Sy zj;RBpK8hx110K|g{kR1{i`%%?=w$)WqS4I7=4mg0sIN-^wL1dTiX><&fEFd{ciX`U z1G-BkKPyRk03ZMYAYJW5lEyI48|G0cVyeNLYGAU8nzLsDq4kV*pc6;%ra{6X&I#ex?|Rgln2&lxaC)iJ_C4Sj zhUD+V%HJQ;n7B7f$a6{n4RsQ=LEVMAFytgFZq-*Z$8$Ga>YWHGrbqc_+XM3<^zg?5 qGd=4x^{*-X9$k?^5ClOGG(#`TUdl_oYHFGQ0000 + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c322028..cac68cf 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -17,4 +17,5 @@ Coupons gespaard: Coupons Start Route + Route stopped! \ No newline at end of file