Skip to content
Permalink
15a9e802ea
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
200 lines (169 sloc) 6.63 KB
package com.boba.runassistant;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
PolylineOptions options;
Polyline line;
private TextView distanceTextView;
private TextView timeTextView;
private TextView paceTextView;
private GoogleMap mMap;
private LatLng ownLocation = new LatLng(52.405336, -1.499966);
private ArrayList<Location> route = new ArrayList<>();
private Location previousLocation;
private Float distanceTraveled = (float) 0;
private Integer timeElapsed = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
distanceTextView = findViewById(R.id.textViewDistance);
timeTextView = findViewById(R.id.textViewTime);
paceTextView = findViewById(R.id.textViewPace);
options = new PolylineOptions().width(16).color(0xFFFF0000).geodesic(true);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PackageManager.PERMISSION_GRANTED);
Button stopRunningButton;
Button centerMapButton;
stopRunningButton = findViewById(R.id.buttonFinishRunning);
stopRunningButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finishRunningActivityButton();
}
});
centerMapButton = findViewById(R.id.buttonCenterMap);
centerMapButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
centerMap();
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMinZoomPreference(14);
mMap.setMaxZoomPreference(18);
centerMap();
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (previousLocation != null) {
route.add(previousLocation);
options.add(new LatLng(previousLocation.getLatitude(), previousLocation.getLongitude()));
distanceTraveled += location.distanceTo(previousLocation);
timeElapsed = (int) (location.getTime() - route.get(0).getTime()) / 1000;
}
try {
mMap.clear();
ownLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(ownLocation).title("Current location"));
line = mMap.addPolyline(options);
} catch (SecurityException e) {
e.printStackTrace();
}
timeTextView.setText(getStringTimeFromSec(timeElapsed));
distanceTextView.setText(String.format(Locale.ENGLISH, "%.3f", distanceTraveled / 1000.0));
if (distanceTraveled > 0 && timeElapsed > 0) {
paceTextView.setText(String.format(Locale.ENGLISH, "%.2f", (double) timeElapsed / (0.06 * (double) distanceTraveled)));
}
previousLocation = location;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, locationListener);
} catch (SecurityException e) {
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
finishRunningActivityButton();
}
private String getStringTimeFromSec(Integer timeElapsed) {
Integer secondsElapsed = timeElapsed % 60;
Integer minutesElapsed = (timeElapsed / 60) % 60;
Integer hoursElapsed = timeElapsed / 3600;
if (hoursElapsed > 0) {
return String.format(Locale.ENGLISH, "%dh %dm %d", hoursElapsed, minutesElapsed, secondsElapsed);
} else if (minutesElapsed > 0) {
return String.format(Locale.ENGLISH, "%dm %d", minutesElapsed, secondsElapsed);
} else {
return String.format(Locale.ENGLISH, "%d", secondsElapsed);
}
}
private void writeDataToSharedPref() {
SharedPreferences sharedPreferences;
sharedPreferences = getSharedPreferences("previousRun", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("DIST_KEY", distanceTextView.getText().toString());
editor.putString("TIME_KEY", String.format(Locale.ENGLISH, "%s", timeTextView.getText().toString()));
editor.putString("PACE_KEY", paceTextView.getText().toString());
editor.putString("DATE_KEY", getStringDate());
editor.apply();
}
private String getStringDate() {
Calendar calendar = Calendar.getInstance();
return String.format(Locale.ENGLISH, "%d-%d-%d %dh %dm %ds",
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH) + 1,
calendar.get(Calendar.DAY_OF_MONTH),
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
calendar.get(Calendar.SECOND));
}
private void finishRunningActivityButton() {
new AlertDialog.Builder(this)
.setTitle("Confirmation")
.setMessage("Do you really want to stop and save the running exercise?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
writeDataToSharedPref();
finish(); // finish this activity
}
})
.setNegativeButton(android.R.string.no, null).show();
}
private void centerMap() {
mMap.moveCamera(CameraUpdateFactory.newLatLng(ownLocation));
}
}