Skip to content

Commit

Permalink
ManageDemsActivity fix getPosGPSButton
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrupczak3 committed May 3, 2024
1 parent bccf3d8 commit 0d22e5c
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion app/src/main/java/com/openathena/ManageDemsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@
import android.widget.TextView;
import android.widget.Toast;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.Settings;

import java.util.Locale;

public class ManageDemsActivity extends AthenaActivity
{
public static String TAG = ManageDemsActivity.class.getSimpleName();
Expand All @@ -48,6 +55,9 @@ public class ManageDemsActivity extends AthenaActivity
private Button lookupButton;
private Button resultsButton;

private LocationManager locationManager;
private LocationListener locationListener;

@Override
protected void onCreate(Bundle savedInstanceState)
{
Expand Down Expand Up @@ -93,15 +103,59 @@ public void onClick(View v) {

resultsButton.setEnabled(false);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
// Update the EditText with the latest location
updateLatLonText(location);
// Remove updates to save battery after location is obtained
locationManager.removeUpdates(this);
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}

@Override
public void onProviderEnabled(String provider) {}

@Override
public void onProviderDisabled(String provider) {
// Prompt user to enable GPS if disabled
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};

} // onCreate()

private void onClickGetPosGPS() {
boolean hasGPSAccess = requestPermissionGPS();
if (!hasGPSAccess) {
if (hasGPSAccess) {
try {
// Request location updates; you might want to customize the request parameters
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
} catch (SecurityException se) {
Toast.makeText(this, "Need GPS permission to fetch location", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, getString(R.string.permissions_toast_error_msg), Toast.LENGTH_SHORT).show();
}
}

private void updateLatLonText(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lon = location.getLongitude();
String mgrs = CoordTranslator.toMGRS1m(lat,lon);
if (outputModeIsMGRS() ) {
latLonText.setText(mgrs);
} else {
latLonText.setText(String.format(Locale.getDefault(), "%f,%f", lat, lon));
}
}
}

private boolean requestPermissionGPS() {
if (!hasAccessCoarseLocation() && !hasAccessFineLocation()) {
requestPermissions(new String[] {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, requestNo);
Expand Down

0 comments on commit 0d22e5c

Please sign in to comment.