Refactored ExportServiceResultReceiver to make TrackRecordingService simpler.

This commit is contained in:
Dennis Guse
2022-01-28 19:23:45 +01:00
parent 29051ac978
commit 78519883c0
5 changed files with 48 additions and 50 deletions
@@ -310,37 +310,31 @@ public class ExportActivity extends FragmentActivity implements ExportServiceRes
viewBinding.exportProgressAlertIcon.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.ic_dialog_success_24dp));
viewBinding.exportProgressAlertMsg.setText(getString(R.string.generic_completed));
}
}
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
if (resultData == null) {
throw new RuntimeException(TAG + ": onReceiveResult resultData NULL");
}
Track.Id trackId = resultData.getParcelable(ExportServiceResultReceiver.RESULT_EXTRA_TRACK_ID);
public void onExportSuccess(Track.Id trackId) {
Track track = contentProviderUtils.getTrack(trackId);
switch (resultCode) {
case ExportServiceResultReceiver.RESULT_CODE_ERROR:
trackExportErrorCount++;
trackErrors.add(track.getName());
break;
case ExportServiceResultReceiver.RESULT_CODE_SUCCESS:
if (ExportUtils.isExportFileExists(track.getUuid(), trackFileFormat.getExtension(), directoryFiles)) {
trackExportOverwrittenCount++;
} else {
trackExportSuccessCount++;
}
break;
default:
throw new RuntimeException(TAG + ": export service result code invalid: " + resultCode);
if (ExportUtils.isExportFileExists(track.getUuid(), trackFileFormat.getExtension(), directoryFiles)) {
trackExportOverwrittenCount++;
} else {
trackExportSuccessCount++;
}
onExportCompleted(track);
}
@Override
public void onExportError(Track.Id trackId) {
Track track = contentProviderUtils.getTrack(trackId);
trackExportErrorCount++;
trackErrors.add(track.getName());
onExportCompleted(track);
}
private void conflict(Track track) {
PendingConflict newConflict = new PendingConflict(track);
conflictsQueue.add(newConflict);
@@ -6,6 +6,8 @@ import android.os.ResultReceiver;
import androidx.annotation.NonNull;
import de.dennisguse.opentracks.data.models.Track;
/**
* Create a new ResultReceive to receive results.
* Your {@link #onReceiveResult} method will be called from the thread running <var>handler</var> if given, or from an arbitrary thread if null.
@@ -26,10 +28,22 @@ public class ExportServiceResultReceiver extends ResultReceiver {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
receiver.onReceiveResult(resultCode, resultData);
Track.Id trackId = resultData.getParcelable(ExportServiceResultReceiver.RESULT_EXTRA_TRACK_ID);
switch (resultCode) {
case RESULT_CODE_SUCCESS:
receiver.onExportSuccess(trackId);
case RESULT_CODE_ERROR:
receiver.onExportError(trackId);
default:
throw new RuntimeException("Unknown resultCode.");
}
}
public interface Receiver {
void onReceiveResult(int resultCode, Bundle resultData);
default void onExportSuccess(Track.Id trackId) {
}
default void onExportError(Track.Id trackId) {
}
}
}
@@ -18,7 +18,6 @@ package de.dennisguse.opentracks.services;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager.WakeLock;
import android.util.Log;
@@ -31,28 +30,19 @@ import androidx.lifecycle.MutableLiveData;
import java.time.Duration;
import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.data.CustomContentProvider;
import de.dennisguse.opentracks.data.models.Distance;
import de.dennisguse.opentracks.data.models.Marker;
import de.dennisguse.opentracks.data.models.Track;
import de.dennisguse.opentracks.data.models.TrackPoint;
import de.dennisguse.opentracks.io.file.exporter.ExportServiceResultReceiver;
import de.dennisguse.opentracks.sensors.sensorData.SensorDataSet;
import de.dennisguse.opentracks.services.announcement.VoiceAnnouncementManager;
import de.dennisguse.opentracks.services.handlers.EGM2008CorrectionManager;
import de.dennisguse.opentracks.services.handlers.GpsStatusValue;
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
import de.dennisguse.opentracks.settings.SettingsActivity;
import de.dennisguse.opentracks.util.ExportUtils;
import de.dennisguse.opentracks.util.SystemUtils;
/**
* A background service that registers a location listener and records track points.
* Track points are saved to the {@link CustomContentProvider}.
*
* @author Leif Hendrik Wilden
*/
public class TrackRecordingService extends Service implements TrackPointCreator.Callback, ExportServiceResultReceiver.Receiver {
public class TrackRecordingService extends Service implements TrackPointCreator.Callback {
private static final String TAG = TrackRecordingService.class.getSimpleName();
@@ -265,7 +255,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
endRecording(true);
ExportUtils.postWorkoutExport(this, trackId, new ExportServiceResultReceiver(new Handler(), this));
ExportUtils.postWorkoutExport(this, trackId);
}
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
@@ -419,15 +409,4 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
recordingStatus = status;
recordingStatusObservable.postValue(recordingStatus);
}
@Override
public void onReceiveResult(final int resultCode, final Bundle resultData) {
Log.w(TAG, "onReceiveResult: " + resultCode);
if (resultCode != ExportServiceResultReceiver.RESULT_CODE_SUCCESS) {
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra(SettingsActivity.EXTRAS_CHECK_EXPORT_DIRECTORY, true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
@@ -157,9 +157,7 @@ public class TrackPointCreator implements BluetoothRemoteSensorManager.SensorDat
return new Pair<>(currentTrackPoint, sensorDataSet);
}
//TODO Limit visibility
@VisibleForTesting(otherwise = VisibleForTesting.PACKAGE_PRIVATE)
public Instant createNow() {
Instant createNow() {
return Instant.now(clock);
}
@@ -2,8 +2,10 @@ package de.dennisguse.opentracks.util;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.DocumentsContract;
import android.util.Log;
import android.widget.Toast;
@@ -25,12 +27,13 @@ import de.dennisguse.opentracks.io.file.exporter.ExportService;
import de.dennisguse.opentracks.io.file.exporter.ExportServiceResultReceiver;
import de.dennisguse.opentracks.io.file.exporter.TrackExporter;
import de.dennisguse.opentracks.settings.PreferencesUtils;
import de.dennisguse.opentracks.settings.SettingsActivity;
public class ExportUtils {
private static final String TAG = ExportUtils.class.getSimpleName();
public static void postWorkoutExport(Context context, Track.Id trackId, ExportServiceResultReceiver resultReceiver) {
public static void postWorkoutExport(Context context, Track.Id trackId) {
if (PreferencesUtils.shouldInstantExportAfterWorkout()) {
TrackFileFormat trackFileFormat = PreferencesUtils.getExportTrackFileFormat();
DocumentFile directory = IntentUtils.toDocumentFile(context, PreferencesUtils.getDefaultExportDirectoryUri());
@@ -40,6 +43,16 @@ public class ExportUtils {
return;
}
ExportServiceResultReceiver resultReceiver = new ExportServiceResultReceiver(new Handler(), new ExportServiceResultReceiver.Receiver() {
@Override
public void onExportError(Track.Id trackId) {
Intent intent = new Intent(context, SettingsActivity.class);
intent.putExtra(SettingsActivity.EXTRAS_CHECK_EXPORT_DIRECTORY, true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
ExportService.enqueue(context, resultReceiver, trackId, trackFileFormat, directory.getUri());
}
}