Skip to content

Commit

Permalink
preserve GPS self location across DemManagmentActivity child classes
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrupczak3 committed Jun 26, 2024
1 parent 7cb4b0b commit 9b6b8b8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/com/openathena/AthenaActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,6 +100,8 @@ public String getCurrentMeasurementUnitName() {
protected Uri demUri = null;
protected boolean isDEMLoaded;

protected static String lastSelfLocation = "";

public AthenaActivity() {
super();
isTargetCoordDisplayed = false;
Expand Down Expand Up @@ -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
Expand Down
28 changes: 27 additions & 1 deletion app/src/main/java/com/openathena/DemManagementActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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()) {
Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/com/openathena/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/com/openathena/ManageDemsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/com/openathena/NewElevationMapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 9b6b8b8

Please sign in to comment.