Skip to content

Commit

Permalink
clean (AndroidFileUploader.java): trivial neutral cleanups and simpli…
Browse files Browse the repository at this point in the history
…fications in terms of error-logging
  • Loading branch information
ksidirop-laerdal committed Oct 22, 2024
1 parent 7ee6ad0 commit b2055ed
Showing 1 changed file with 20 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public boolean trySetBluetoothDevice(@NonNull final BluetoothDevice bluetoothDev

_bluetoothDevice = bluetoothDevice; //order

logMessageAdvertisement("[AFU.TSBD.010] Successfully set the android-bluetooth-device to the given value", "FileUploader", "TRACE", _remoteFilePathSanitized);
logMessageAdvertisement("[AFU.TSBD.030] Successfully set the android-bluetooth-device to the given value", "FileUploader", "TRACE", _remoteFilePathSanitized);

return true;
}
Expand Down Expand Up @@ -108,53 +108,46 @@ public EAndroidFileUploaderVerdict beginUpload(
final int memoryAlignment
)
{
if (!IsCold()) {
setState(EAndroidFileUploaderState.ERROR);
onError("N/A", "Another upload is already in progress", null);
if (remoteFilePath == null || remoteFilePath.isEmpty()) {
onError("N/A", "Provided target-path is empty", null);

return EAndroidFileUploaderVerdict.FAILED__OTHER_UPLOAD_ALREADY_IN_PROGRESS;
return EAndroidFileUploaderVerdict.FAILED__INVALID_SETTINGS;
}

if (_context == null) {
setState(EAndroidFileUploaderState.ERROR);
onError("N/A", "No context specified - call trySetContext() first", null);
_remoteFilePathSanitized = remoteFilePath.trim();
if (_remoteFilePathSanitized.endsWith("/")) //the path must point to a file not a directory
{
onError(_remoteFilePathSanitized, "Provided target-path points to a directory not a file", null);

return EAndroidFileUploaderVerdict.FAILED__INVALID_SETTINGS;
}

if (_bluetoothDevice == null) {
setState(EAndroidFileUploaderState.ERROR);
onError("N/A", "No bluetooth-device specified - call trySetBluetoothDevice() first", null);
if (!_remoteFilePathSanitized.startsWith("/"))
{
onError(_remoteFilePathSanitized, "Provided target-path is not an absolute path", null);

return EAndroidFileUploaderVerdict.FAILED__INVALID_SETTINGS;
}

if (remoteFilePath == null || remoteFilePath.isEmpty()) {
setState(EAndroidFileUploaderState.ERROR);
onError("N/A", "Provided target-path is empty", null);
if (!IsCold()) {
onError(_remoteFilePathSanitized, "Another upload is already in progress", null);

return EAndroidFileUploaderVerdict.FAILED__INVALID_SETTINGS;
return EAndroidFileUploaderVerdict.FAILED__OTHER_UPLOAD_ALREADY_IN_PROGRESS;
}

_remoteFilePathSanitized = remoteFilePath.trim();
if (_remoteFilePathSanitized.endsWith("/")) //the path must point to a file not a directory
{
setState(EAndroidFileUploaderState.ERROR);
onError(_remoteFilePathSanitized, "Provided target-path points to a directory not a file", null);
if (_context == null) {
onError(_remoteFilePathSanitized, "No context specified - call trySetContext() first", null);

return EAndroidFileUploaderVerdict.FAILED__INVALID_SETTINGS;
}

if (!_remoteFilePathSanitized.startsWith("/"))
{
setState(EAndroidFileUploaderState.ERROR);
onError(_remoteFilePathSanitized, "Provided target-path is not an absolute path", null);
if (_bluetoothDevice == null) {
onError(_remoteFilePathSanitized, "No bluetooth-device specified - call trySetBluetoothDevice() first", null);

return EAndroidFileUploaderVerdict.FAILED__INVALID_SETTINGS;
}

if (data == null) { // data being null is not ok but data.length==0 is perfectly ok because we might want to create empty files
setState(EAndroidFileUploaderState.ERROR);
onError(_remoteFilePathSanitized, "Provided data is null", null);

return EAndroidFileUploaderVerdict.FAILED__INVALID_DATA;
Expand Down Expand Up @@ -185,7 +178,6 @@ public EAndroidFileUploaderVerdict beginUpload(
}
catch (final Exception ex)
{
setState(EAndroidFileUploaderState.ERROR);
onError(_remoteFilePathSanitized, "Failed to initialize the upload", ex);

return EAndroidFileUploaderVerdict.FAILED__ERROR_UPON_COMMENCING;
Expand Down Expand Up @@ -242,7 +234,6 @@ private EAndroidFileUploaderVerdict ensureFilesystemManagerIsInitializedExactlyO
}
catch (final Exception ex)
{
setState(EAndroidFileUploaderState.ERROR);
onError(_remoteFilePathSanitized, ex.getMessage(), ex);

return EAndroidFileUploaderVerdict.FAILED__INVALID_SETTINGS;
Expand Down Expand Up @@ -401,6 +392,8 @@ public void onError(
final Exception exception
)
{
setState(EAndroidFileUploaderState.ERROR); //keep first

if (!(exception instanceof McuMgrErrorException))
{
fatalErrorOccurredAdvertisement(remoteFilePath, errorMessage, -1, -1); // -1 values get mapped to the 'generic' error code
Expand Down Expand Up @@ -502,7 +495,6 @@ public void onUploadProgressChanged(final int bytesSent, final int fileSize, fin
public void onUploadFailed(@NonNull final McuMgrException error)
{
fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
setState(EAndroidFileUploaderState.ERROR);
onError(_remoteFilePathSanitized, error.getMessage(), error);
setLoggingEnabled(true);
busyStateChangedAdvertisement(false);
Expand Down

0 comments on commit b2055ed

Please sign in to comment.