Skip to content

Commit

Permalink
Merge pull request #22 from snuhcs-course/debug/singlemode
Browse files Browse the repository at this point in the history
Debug: single mode immediate location update onResume
  • Loading branch information
tangamzaa authored Oct 22, 2023
2 parents ccd2417 + 7075ec6 commit 340ee46
Showing 1 changed file with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class SingleModeFragment extends Fragment {
private List<LatLng> pathPoints = new ArrayList<>();
Chronometer currentTimeText;
SimpleDateFormat dateFormat;
FusedLocationProviderClient fusedLocationClient;
LocationCallback locationCallback;
LocationRequest locationRequest;
MainActivity2 mainActivity;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
Expand All @@ -57,7 +61,7 @@ public View onCreateView(@NonNull LayoutInflater inflater,
binding = FragmentSingleModeBinding.inflate(inflater, container, false);
View root = binding.getRoot();

MainActivity2 mainActivity = (MainActivity2) getActivity();
mainActivity = (MainActivity2) getActivity();
Button showMissionButton = (Button) binding.showMissionButton;
Button quitButton = (Button) binding.quitButton;
TextView currentDistanceText = (TextView) binding.currentDistanceText;
Expand All @@ -84,14 +88,14 @@ public void onChronometerTick(Chronometer chronometer) {
});
currentTimeText.start();

LocationRequest locationRequest = LocationRequest.create();
locationRequest = LocationRequest.create();
locationRequest.setInterval(5000); // Update interval in milliseconds
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

//TODO: only draw lines if running is started
//TODO: doesn't update location when app is in background -> straight lines are drawn from the last location when app is opened again
//TODO: lines are ugly and noisy -> need to filter out some points or smoothed
LocationCallback locationCallback = new LocationCallback() {
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
if (locationResult != null) {
Expand All @@ -113,18 +117,8 @@ public void onLocationResult(LocationResult locationResult) {
}
};

FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(mainActivity);
fusedLocationClient = LocationServices.getFusedLocationProviderClient(mainActivity);

if (ActivityCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1000);
}
if (ActivityCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1000);
}

fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());

// Finding the visual component displaying the map
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
Expand Down Expand Up @@ -155,7 +149,26 @@ public void onDestroyView() {
currentTimeText.stop();
}

@Override
public void onResume() {
super.onResume();
fusedLocationClient.removeLocationUpdates(locationCallback);

if (ActivityCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1000);
}
if (ActivityCompat.checkSelfPermission(mainActivity, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1000);
}

fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());
}

@Override
public void onPause() {
super.onPause();
fusedLocationClient.removeLocationUpdates(locationCallback);
}
}

0 comments on commit 340ee46

Please sign in to comment.