From 9b6b8b87ee743ff3b4011d3e3544415a34e06f79 Mon Sep 17 00:00:00 2001 From: mkrupczak3 Date: Wed, 26 Jun 2024 13:41:36 -0400 Subject: [PATCH] preserve GPS self location across DemManagmentActivity child classes --- .../java/com/openathena/AthenaActivity.java | 10 +++++++ .../com/openathena/DemManagementActivity.java | 28 ++++++++++++++++++- .../java/com/openathena/MainActivity.java | 6 ---- .../com/openathena/ManageDemsActivity.java | 18 ++++++------ .../openathena/NewElevationMapActivity.java | 18 ++++++------ 5 files changed, 55 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/openathena/AthenaActivity.java b/app/src/main/java/com/openathena/AthenaActivity.java index a6671e31..398ca417 100644 --- a/app/src/main/java/com/openathena/AthenaActivity.java +++ b/app/src/main/java/com/openathena/AthenaActivity.java @@ -13,6 +13,7 @@ import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.net.Uri; +import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.preference.PreferenceManager; import android.util.Log; @@ -99,6 +100,8 @@ public String getCurrentMeasurementUnitName() { protected Uri demUri = null; protected boolean isDEMLoaded; + protected static String lastSelfLocation = ""; + public AthenaActivity() { super(); isTargetCoordDisplayed = false; @@ -506,6 +509,13 @@ public boolean isCacheUri(Uri uri) { return uriPath.startsWith(cachePath); } + @Override + protected void onSaveInstanceState(Bundle saveInstanceState) { + Log.d(TAG,"onSaveInstanceState started"); + super.onSaveInstanceState(saveInstanceState); + saveStateToSingleton(); + } + protected abstract void saveStateToSingleton(); @Override diff --git a/app/src/main/java/com/openathena/DemManagementActivity.java b/app/src/main/java/com/openathena/DemManagementActivity.java index c6e90491..851bc74e 100644 --- a/app/src/main/java/com/openathena/DemManagementActivity.java +++ b/app/src/main/java/com/openathena/DemManagementActivity.java @@ -15,6 +15,8 @@ import androidx.appcompat.app.AppCompatActivity; +import java.util.Locale; + public abstract class DemManagementActivity extends AthenaActivity { protected LocationManager locationManager; protected LocationListener locationListener; @@ -24,11 +26,26 @@ public abstract class DemManagementActivity extends AthenaActivity { // semaphore value will be > 0 if a long process is currently running protected int showProgressBarSemaphore = 0; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationListener = createLocationListener(); + + athenaApp = (AthenaApp) getApplication(); + lastSelfLocation = athenaApp.getString("lastSelfLocation"); + } + + @Override + protected void onResume() { + super.onResume(); + lastSelfLocation = athenaApp.getString("lastSelfLocation"); + } + + @Override + protected void saveStateToSingleton() { + athenaApp.putString("lastSelfLocation", lastSelfLocation); } protected LocationListener createLocationListener() { @@ -65,7 +82,16 @@ public void onProviderDisabled(String provider) { }; } - protected abstract void updateLatLonText(Location location); + protected void updateLatLonText(Location location) { + if (location != null) { + double lat = location.getLatitude(); + double lon = location.getLongitude(); + String mgrs = CoordTranslator.toMGRS1m(lat,lon); + String latLonPair = String.format(Locale.US, "%f,%f", lat, lon); + lastSelfLocation = outputModeIsMGRS() ? mgrs : latLonPair; + athenaApp.putString("lastSelfLocation", lastSelfLocation); + } + } protected boolean requestPermissionGPS() { if (!hasAccessCoarseLocation() && !hasAccessFineLocation()) { diff --git a/app/src/main/java/com/openathena/MainActivity.java b/app/src/main/java/com/openathena/MainActivity.java index d6bbadd2..c1714b32 100644 --- a/app/src/main/java/com/openathena/MainActivity.java +++ b/app/src/main/java/com/openathena/MainActivity.java @@ -267,12 +267,6 @@ protected void onCreate(Bundle savedInstanceState) { } // onCreate - @Override - protected void onSaveInstanceState(Bundle saveInstanceState) { - Log.d(TAG,"onSaveInstanceState started"); - super.onSaveInstanceState(saveInstanceState); - saveStateToSingleton(); - } // save the current state of this activity to the athenaApp Singleton object @Override diff --git a/app/src/main/java/com/openathena/ManageDemsActivity.java b/app/src/main/java/com/openathena/ManageDemsActivity.java index 8e83a503..2aea775e 100644 --- a/app/src/main/java/com/openathena/ManageDemsActivity.java +++ b/app/src/main/java/com/openathena/ManageDemsActivity.java @@ -76,6 +76,12 @@ protected void onCreate(Bundle savedInstanceState) progressBar = (ProgressBar) findViewById(R.id.progressBar); progressBar.setVisibility(View.GONE); + // If user has previously obtained self GPS location in another DemManagementActivity, + // load the result into this activity to save them time + if (lastSelfLocation != null && !lastSelfLocation.isEmpty()) { + latLonText.setText(lastSelfLocation); + } + manageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -121,15 +127,9 @@ public void onClick(View v) { @Override protected 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)); - } + super.updateLatLonText(location); + if (lastSelfLocation != null && !lastSelfLocation.isEmpty()) { + latLonText.setText(lastSelfLocation); } } diff --git a/app/src/main/java/com/openathena/NewElevationMapActivity.java b/app/src/main/java/com/openathena/NewElevationMapActivity.java index 46a9cd0c..9e2ce08d 100644 --- a/app/src/main/java/com/openathena/NewElevationMapActivity.java +++ b/app/src/main/java/com/openathena/NewElevationMapActivity.java @@ -103,6 +103,12 @@ protected void onCreate(Bundle savedInstanceState) resultsLabel = (TextView)findViewById(R.id.new_dem_results); + // If user has previously obtained self GPS location in another DemManagementActivity, + // load the result into this activity to save them time + if (lastSelfLocation != null && !lastSelfLocation.isEmpty()) { + latLonText.setText(lastSelfLocation); + } + getPosGPSButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onClickGetPosGPS(); } @@ -138,15 +144,9 @@ public void onClick(View v) { @Override protected 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)); - } + super.updateLatLonText(location); + if (lastSelfLocation != null && !lastSelfLocation.isEmpty()) { + latLonText.setText(lastSelfLocation); } }