Skip to content

Commit

Permalink
add public api check, fix review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pstorch committed Jul 24, 2024
1 parent aa31db8 commit eed2ff5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.time.Instant;
Expand All @@ -18,6 +19,7 @@
import de.dennisguse.opentracks.data.models.TrackPoint;
import de.dennisguse.opentracks.services.TrackRecordingService;
import de.dennisguse.opentracks.services.TrackRecordingServiceConnection;
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.ui.markers.MarkerEditActivity;
import de.dennisguse.opentracks.util.IntentUtils;

Expand All @@ -35,22 +37,15 @@ public class CreateMarkerActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// TODO: do we want to check if public API is enabled?

Track.Id trackId;
if (getIntent().hasExtra(EXTRA_TRACK_ID)) {
trackId = new Track.Id(getIntent().getLongExtra(EXTRA_TRACK_ID, 0L));
} else {
trackId = null;
}
TrackPoint trackPoint;
if (getIntent().hasExtra(EXTRA_LOCATION)) {
Location location = getIntent().getParcelableExtra(EXTRA_LOCATION);
trackPoint = new TrackPoint(location, Instant.now());
} else {
trackPoint = null;
if (!PreferencesUtils.isPublicAPIenabled()) {
Toast.makeText(this, R.string.publicapi_disabled, Toast.LENGTH_LONG).show();
finish();
return;
}

Track.Id trackId = new Track.Id(getIntent().getLongExtra(EXTRA_TRACK_ID, 0L));
TrackPoint trackPoint = new TrackPoint(getIntent().<Location>getParcelableExtra(EXTRA_LOCATION), Instant.now());

TrackRecordingServiceConnection.execute(this, (service, self) -> {
Marker.Id marker = createNewMarker(trackId, trackPoint, service);
if (marker == null) {
Expand All @@ -67,16 +62,8 @@ protected void onCreate(Bundle savedInstanceState) {
});
}

private Marker.Id createNewMarker(Track.Id trackId, TrackPoint trackPoint, TrackRecordingService trackRecordingService) {
try {
if (trackPoint == null || trackId == null) {
throw new IllegalStateException("TrackPoint or Track.Id is null");
}
return trackRecordingService.insertMarker("", "", "", null, trackId, trackPoint);
} catch (IllegalStateException e) {
Log.e(TAG, "Unable to add marker.", e);
}
return null;
private Marker.Id createNewMarker(@NonNull Track.Id trackId, @NonNull TrackPoint trackPoint, TrackRecordingService trackRecordingService) {
return trackRecordingService.insertMarker("", "", "", null, trackId, trackPoint);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,10 @@ public class ShowMarkerActivity extends AppCompatActivity {
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);

Marker.Id markerId = null;
if (getIntent().hasExtra(EXTRA_MARKER_ID)) {
markerId = new Marker.Id(getIntent().getLongExtra(EXTRA_MARKER_ID, 0));
}
if (markerId == null) {
Log.d(TAG, "invalid marker id");
} else {
Intent intent = IntentUtils.newIntent(this, MarkerDetailActivity.class)
.putExtra(MarkerDetailActivity.EXTRA_MARKER_ID, markerId);
startActivity(intent);
}
Marker.Id markerId = new Marker.Id(getIntent().getLongExtra(EXTRA_MARKER_ID, 0));
Intent intent = IntentUtils.newIntent(this, MarkerDetailActivity.class)
.putExtra(MarkerDetailActivity.EXTRA_MARKER_ID, markerId);
startActivity(intent);
finish();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import android.util.Log;
import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.app.ServiceCompat;
Expand Down Expand Up @@ -273,11 +274,7 @@ public void newGpsStatus(GpsStatusValue gpsStatusValue) {
gpsStatusObservable.postValue(gpsStatusValue);
}

public Marker.Id insertMarker(String name, String category, String description, String photoUrl, Track.Id trackId, TrackPoint trackPoint) {
if (trackId == null || trackPoint == null) {
return null;
}

public Marker.Id insertMarker(String name, String category, String description, String photoUrl, @NonNull Track.Id trackId, @NonNull TrackPoint trackPoint) {
return trackRecordingManager.insertMarker(name, category, description, photoUrl, trackId, trackPoint);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,5 @@ limitations under the License.
<string name="settings_announcements_lap_power">Lap power</string>
<string name="power_x_watt">Power {x, plural, =1 {1 watt} other {# watts} }</string>
<string name="power">Power</string>
<string name="publicapi_disabled">Public API is disabled</string>
</resources>

0 comments on commit eed2ff5

Please sign in to comment.