From 1877a693d00dde90bff6332841a25e76603ddab7 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 6 Jan 2021 10:40:20 +0100 Subject: [PATCH 1/6] added boolean to singleton --- .../java/com/a1/nextlocation/data/StaticData.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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..fe861d1 100644 --- a/app/src/main/java/com/a1/nextlocation/data/StaticData.java +++ b/app/src/main/java/com/a1/nextlocation/data/StaticData.java @@ -11,6 +11,20 @@ public enum StaticData { INSTANCE; private double distanceTraveled = 0; private int locationsVisited = 0; + private boolean isFollowingRoute = false; + private String routeName = ""; + + public void followRoute(Route route) { + routeName = route.getName(); + setFollowingRoute(true); + } + public void setFollowingRoute(boolean followingRoute) { + isFollowingRoute = followingRoute; + } + + public boolean isFollowingRoute() { + return isFollowingRoute; + } private ArrayList visitedNames = new ArrayList<>(); From abc58d360650dfb42fcbdc9e88d3a153dcdf6fbb Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 6 Jan 2021 10:41:56 +0100 Subject: [PATCH 2/6] following route stuff --- app/src/main/java/com/a1/nextlocation/data/StaticData.java | 4 ++++ 1 file changed, 4 insertions(+) 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 fe861d1..0043e3d 100644 --- a/app/src/main/java/com/a1/nextlocation/data/StaticData.java +++ b/app/src/main/java/com/a1/nextlocation/data/StaticData.java @@ -18,6 +18,10 @@ public enum StaticData { routeName = route.getName(); setFollowingRoute(true); } + + public boolean isFollowingRoute(Route route) { + return isFollowingRoute && route.getName().equals(routeName); + } public void setFollowingRoute(boolean followingRoute) { isFollowingRoute = followingRoute; } From f6af7ae80b2ad5e69f90f6df3801b4a60939092b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 6 Jan 2021 10:48:03 +0100 Subject: [PATCH 3/6] added route singleton --- .../a1/nextlocation/data/RouteHandler.java | 42 +++++++++++++++++++ .../com/a1/nextlocation/data/StaticData.java | 18 -------- .../fragments/RouteDetailFragment.java | 3 ++ .../a1/nextlocation/network/ApiHandler.java | 1 + 4 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/com/a1/nextlocation/data/RouteHandler.java diff --git a/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java b/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java new file mode 100644 index 0000000..65fda9b --- /dev/null +++ b/app/src/main/java/com/a1/nextlocation/data/RouteHandler.java @@ -0,0 +1,42 @@ +package com.a1.nextlocation.data; + +/** + * singleton to track the current route that is being followed + */ +public enum RouteHandler { + INSTANCE; + + private boolean isFollowingRoute = false; + private Route currentRoute; + private int stepCount = 0; + + public int getStepCount() { + return stepCount; + } + + public void addStep() { + stepCount++; + } + + public void finishRoute() { + stepCount = 0; + isFollowingRoute = false; + currentRoute = null; + } + + public void followRoute(Route route) { + this.currentRoute = route; + setFollowingRoute(true); + } + + public boolean isFollowingRoute(Route route) { + return isFollowingRoute && route.equals(currentRoute); + } + public void setFollowingRoute(boolean followingRoute) { + isFollowingRoute = followingRoute; + } + + public boolean isFollowingRoute() { + return isFollowingRoute; + } +} 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 0043e3d..0f0d4e3 100644 --- a/app/src/main/java/com/a1/nextlocation/data/StaticData.java +++ b/app/src/main/java/com/a1/nextlocation/data/StaticData.java @@ -11,24 +11,6 @@ public enum StaticData { INSTANCE; private double distanceTraveled = 0; private int locationsVisited = 0; - private boolean isFollowingRoute = false; - private String routeName = ""; - - public void followRoute(Route route) { - routeName = route.getName(); - setFollowingRoute(true); - } - - public boolean isFollowingRoute(Route route) { - return isFollowingRoute && route.getName().equals(routeName); - } - public void setFollowingRoute(boolean followingRoute) { - isFollowingRoute = followingRoute; - } - - public boolean isFollowingRoute() { - return isFollowingRoute; - } private ArrayList visitedNames = new ArrayList<>(); diff --git a/app/src/main/java/com/a1/nextlocation/fragments/RouteDetailFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/RouteDetailFragment.java index 99e3445..5fded7c 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/RouteDetailFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/RouteDetailFragment.java @@ -14,6 +14,8 @@ import android.widget.Toast; import com.a1.nextlocation.R; import com.a1.nextlocation.data.Route; +import com.a1.nextlocation.data.RouteHandler; +import com.a1.nextlocation.data.StaticData; import com.a1.nextlocation.network.ApiHandler; public class RouteDetailFragment extends Fragment { @@ -45,6 +47,7 @@ public class RouteDetailFragment extends Fragment { public void startRoute(View view) { ApiHandler.INSTANCE.getDirections(route); + RouteHandler.INSTANCE.followRoute(route); Toast.makeText(requireContext(),"Route started!",Toast.LENGTH_SHORT).show(); ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new HomeFragment()).addToBackStack(null).commit(); diff --git a/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java b/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java index 570e683..46d3830 100644 --- a/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java +++ b/app/src/main/java/com/a1/nextlocation/network/ApiHandler.java @@ -127,6 +127,7 @@ public enum ApiHandler { } catch (IOException e) { Log.d(TAG, "getDirections: caught exception: " + e.getLocalizedMessage()); } + }); t.start(); 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 4/6] 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 From 926a984c716910b26ec7fbb595388d6f17159fd6 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 6 Jan 2021 11:33:35 +0100 Subject: [PATCH 5/6] stop icon --- app/src/main/res/drawable/stop_icon.png | Bin 0 -> 4171 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/src/main/res/drawable/stop_icon.png diff --git a/app/src/main/res/drawable/stop_icon.png b/app/src/main/res/drawable/stop_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..7d72917bcdbf5ee205ee3df3f7596c826e5d120a GIT binary patch literal 4171 zcmY*dc{tSD8~@H2F_MDdR)2KwHkwmun&Ard>kKgZk&hwo2ectmv=e*lFpZCxY5f7lm6vY4lK;hk8 zNdN#7kT5`WhhPdxY&Zb`QZjf~XCG?i*QuD?(Ec;fl4H)Xo{GUy=jKi~xrC56$*9hd z(!>G}6dq*i*8joGtH4EZU3GoEecZh3Z!U=Ja=Y(J9~Q4=db+&*8=S!E?-HfSKxLFZ z1#}3M|Cv3IkQVc-l+rq!*RT5BSwT9`oKk&)s^k+?(|Ac{vQv(E6V- zd_?C18Dx|T<-(SAA`oS^OLM{CJ33$0=FiS7Fb!%#r!SUgPeD65Sr9S2ceIbD7zzJ$ z`4_Ys(%{I`v>|vnW@!Ag8Br@k9?P{3f9)Cz>>Lj1iw=jMyn(5$YXM&Dxa_r=K2_Cw zDk%b#n#F|3e z1?T7z3`dM(`~I8mmTD?J_>bT0(@aTuIGUGG#giG69VUwi!-}QD3>jP0n%xU-xAs*H zY+$74>NUJ?mDo9Hos|j2mqKd&t~Kld&?x-TH$ty_uIRbk1_BO@q0?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+Ms7M0) zQKNdvf9inA*81cHx$sx}IeOeo3_ju2&-xzy_(TM-M4dIC+~<@r=DA^N`e1+c?9gN> z7$4c-A6?<7zWnQo=M4N{5MW1Zk%*y89TfL_Vv#0p93{DQ{k4dx;Fa#kQ&{i2d=Jwj z13-7!OwPsJxWX$IJ2$bdm=qUv{`W~o#Dmfv6sdUoqTC?u!xf>j=-sWVYN(t1w45=` zy0*^keNXJ+pn}*|r~rYOzumugCC+m$t2YTaY%eY$G&d(e7QYKY_dx#ORN=ACcLEcn zC_1e+0!paE47gJKGjw-f2KPAbMdC?FHfE%D1dc6B~PWfK#gF|s&G9(P8s3glDhij5WZR?|LD^#;e8WIFFAU?X>q%G3cvbJ;x@P~ z2^&2$W05sD&*oR(Dcts0zAX{OyhAZFek^DYRER%N9pjMN>?Bmvp9JYMkva4v(ZqC< zyt4wVe}-#u$mT@}dpxeEuK~Bt%Cw^3u8b;kyTXlr^>yG@sBp`CZ{#t^pBLrqf6SI* zZv1&n(oa9aWJ$4sUZW&vnamts`$u?y6~@vX=AkAfdZ@#XqXJZ)h(tWXH z6O^OY&b0gDYm(t!(;p>N(N3zG>>*OAOmUESlEroB1E5HD+{7ZFDl%}V>m*bTOdLff zaF@o}$GN8Kv(qVRL+{k!qn?|qi?Y}Z-4E4xjUHHZUgNGgq*ww?;ThOiOYEjvwM zKeEPMH54>)lFM{ALVVR)(hFA!bkiJGNBt3 Date: Wed, 6 Jan 2021 12:08:18 +0100 Subject: [PATCH 6/6] added comments --- .../nextlocation/fragments/HomeFragment.java | 83 +++++++++++++++---- 1 file changed, 69 insertions(+), 14 deletions(-) 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 4a8ca4a..1b8f2e6 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -68,8 +68,6 @@ public class HomeFragment extends Fragment implements LocationListener { Manifest.permission.ACCESS_FINE_LOCATION, // WRITE_EXTERNAL_STORAGE is required in order to show the map Manifest.permission.WRITE_EXTERNAL_STORAGE); -// roadManager = new MapQuestRoadManager(MAPQUEST_API_KEY); -// roadManager.addRequestOption("routeType=foot-walking"); color = requireContext().getColor(R.color.red); } @@ -80,12 +78,14 @@ public class HomeFragment extends Fragment implements LocationListener { View view = inflater.inflate(R.layout.fragment_home, container, false); + // set up the location list button this.imageButton = view.findViewById(R.id.location_list_button); this.imageButton.setOnClickListener(v -> { LocationFragment locationFragment = new LocationFragment(); ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, locationFragment).addToBackStack(null).commit(); }); + // set up the route stop button stopButton = view.findViewById(R.id.home_stop_route_button); stopButton.setOnClickListener(v -> { RouteHandler.INSTANCE.finishRoute(); @@ -107,18 +107,15 @@ public class HomeFragment extends Fragment implements LocationListener { return view; } + /** + * callback method that gets called when there are new directions available in the form of a {@link DirectionsResult} object. + * @param directionsResult the directions received from the api + */ private void onDirectionsAvailable(DirectionsResult directionsResult) { Log.d(TAG, "onDirectionsAvailable: got result! " + directionsResult); ArrayList geoPoints = directionsResult.getGeoPoints(); roadOverlay = new Polyline(); roadOverlay.setPoints(geoPoints); - - // this is for mapquest, but it gives a "no value for guidancelinkcollection" error and google has never heard of that -// GeoPoint[] gp = directionsResult.getStartAndEndPoint(); -// ArrayList arrayList = new ArrayList<>(Arrays.asList(gp)); -// Road road = roadManager.getRoad(arrayList); -// roadOverlay = RoadManager.buildRoadOverlay(road); - roadOverlay.setColor(color); @@ -175,13 +172,13 @@ public class HomeFragment extends Fragment implements LocationListener { try { + // request location updates for the distance checking locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); + // get the current location and set it as center Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); - if (currentLocation == null) { - currentLocation = location; - } + if (currentLocation == null) currentLocation = location; if (location != null) { GeoPoint start = new GeoPoint(location.getLatitude(), location.getLongitude()); @@ -204,6 +201,9 @@ public class HomeFragment extends Fragment implements LocationListener { } + /** + * displays the route that is currently being followed as a red line + */ private void displayRoute() { if (RouteHandler.INSTANCE.isFollowingRoute()) { @@ -222,30 +222,54 @@ public class HomeFragment extends Fragment implements LocationListener { } } + /** + * adds the locations of the current route to the map. If there is no current route, show all locations + */ private void addLocations() { + // get the locations of the current route or all locations List locations = RouteHandler.INSTANCE.isFollowingRoute() ? RouteHandler.INSTANCE.getCurrentRoute().getLocations() : LocationListManager.INSTANCE.getLocationList(); final ArrayList items = new ArrayList<>(locations.size()); + // marker icon Drawable marker = ContextCompat.getDrawable(requireContext(), R.drawable.ic_baseline_location_on_24); marker.setAlpha(255); marker.setTint(getResources().getColor(R.color.primaryColour)); + + // add all locations to the overlay itemss for (com.a1.nextlocation.data.Location location : locations) { OverlayItem item = new OverlayItem(location.getName(), location.getDescription(), location.convertToGeoPoint()); item.setMarker(marker); items.add(item); } + + // create the overlay that will hold all locations and add listeners allLocationsOverlay = new ItemizedIconOverlay(items, new ItemizedIconOverlay.OnItemGestureListener() { + /** + * on sinlge click, navigate to that location's detail fragment + * @param index the index in the location list + * @param item the item that was clicked + * @return true + */ @Override public boolean onItemSingleTapUp(int index, OverlayItem item) { com.a1.nextlocation.data.Location clicked = locations.get(index); requireActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, new LocationDetailFragment(clicked)).commit(); - return false; + return true; } + /** + * on item long press, show that location's name in a toast message + * @param index the index in the location list + * @param item the item that was clicked + * @return true + */ @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(); + + // create a route to the clicked location, didn't work and didn't have enough time to make it work ¯\_(ツ)_/¯ + // 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); @@ -254,11 +278,17 @@ public class HomeFragment extends Fragment implements LocationListener { } }, requireContext()); + // add the overlay to the map mapView.getOverlays().add(allLocationsOverlay); Log.d(TAG, "addLocations: successfully added locations"); } + /** + * @author Ricky + * request the permissions needed for location and network, made by Ricky + * @param permissions tbe permissions we want to ask + */ private void requestPermissionsIfNecessary(String... permissions) { ArrayList permissionsToRequest = new ArrayList<>(); if (this.getContext() != null) @@ -277,13 +307,18 @@ public class HomeFragment extends Fragment implements LocationListener { } } + /** + * location callback that gets called each time the location is updated. It is used for updating the distance walked and checking if there are locations you have visited + * @param location the new location + */ @Override public void onLocationChanged(@NonNull Location location) { + // calculate the distance walked double distance = currentLocation.distanceTo(location); // in meters StaticData.INSTANCE.addDistance(distance); currentLocation = location; - //new thread because we don't want the main thread to hang + //new thread because we don't want the main thread to hang, this method gets called a lot 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) { @@ -295,6 +330,8 @@ public class HomeFragment extends Fragment implements LocationListener { t.start(); } + // empty override methods for the LocationListener + @Override public void onStatusChanged(String provider, int status, Bundle extras) { @@ -309,4 +346,22 @@ public class HomeFragment extends Fragment implements LocationListener { public void onProviderDisabled(@NonNull String provider) { } + + /** + * method that gets called when the app gets paused + */ + @Override + public void onPause() { + super.onPause(); + mapView.onPause(); + } + + /** + * method that gets called when the app gets resumed + */ + @Override + public void onResume() { + super.onResume(); + mapView.onResume(); + } } \ No newline at end of file