Skip to content

Commit

Permalink
clean (AndroidFileDownloader.java): tryEnsureHighConnectionPriority()…
Browse files Browse the repository at this point in the history
… outside ensureFilesystemManagerIsInitializedExactlyOnce()

[skip ci]
  • Loading branch information
ksidirop-laerdal committed Oct 31, 2024
1 parent a9a9920 commit 0971c23
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public void beginReset()

_manager.reset(new McuMgrCallback<McuMgrOsResponse>()
{

@Override
public void onResponse(@NotNull final McuMgrOsResponse response)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,15 @@ public EAndroidFileDownloaderVerdict beginDownload(

try
{
setLoggingEnabled(false);
resetDownloadState(); //order must be called before ensureTransportIsInitializedExactlyOnce() because the environment might try to set the device via trySetBluetoothDevice()!!!
ensureTransportIsInitializedExactlyOnce(initialMtuSize); //order
setLoggingEnabledOnConnection(false); //order

final EAndroidFileDownloaderVerdict verdict = ensureFilesystemManagerIsInitializedExactlyOnce(); //order
if (verdict != EAndroidFileDownloaderVerdict.SUCCESS)
return verdict;

tryEnsureHighConnectionPriority(_transport); //order
tryEnsureConnectionPriorityOnTransport(); //order
ensureFileDownloaderCallbackProxyIsInitializedExactlyOnce(); //order

_downloadingController = _fileSystemManager.fileDownload(_remoteFilePathSanitized, _fileDownloaderCallbackProxy);
Expand All @@ -178,7 +179,7 @@ public void pause()
return;

setState(EAndroidFileDownloaderState.PAUSED);
setLoggingEnabled(true);
setLoggingEnabledOnConnection(true);
transferController.pause();
busyStateChangedAdvertisement(false);
}
Expand All @@ -194,7 +195,7 @@ public void resume()
busyStateChangedAdvertisement(true);
_initialBytes = 0;

setLoggingEnabled(false);
setLoggingEnabledOnConnection(false);
transferController.resume();
}

Expand Down Expand Up @@ -233,10 +234,11 @@ private void resetDownloadState()

private void ensureTransportIsInitializedExactlyOnce(int initialMtuSize)
{
if (_transport != null)
return;
if (_transport == null)
{
_transport = new McuMgrBleTransport(_context, _bluetoothDevice);
}

_transport = new McuMgrBleTransport(_context, _bluetoothDevice);
if (initialMtuSize > 0)
{
_transport.setInitialMtu(initialMtuSize);
Expand Down Expand Up @@ -270,9 +272,9 @@ private EAndroidFileDownloaderVerdict ensureFilesystemManagerIsInitializedExactl
return EAndroidFileDownloaderVerdict.SUCCESS;
}

private void tryEnsureHighConnectionPriority(final McuMgrBleTransport connection)
private void tryEnsureConnectionPriorityOnTransport()
{
connection.requestConnPriority(ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH);
_transport.requestConnPriority(ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH);
}

private void disposeTransport()
Expand Down Expand Up @@ -314,13 +316,9 @@ private void disposeCallbackProxy()
_fileDownloaderCallbackProxy = null;
}

private void setLoggingEnabled(final boolean enabled)
private void setLoggingEnabledOnConnection(final boolean enabled)
{
final McuMgrTransport mcuMgrTransporter = _fileSystemManager.getTransporter();
if (!(mcuMgrTransporter instanceof McuMgrBleTransport))
return;

((McuMgrBleTransport) mcuMgrTransporter).setLoggingEnabled(enabled);
_transport.setLoggingEnabled(enabled);
}

private void setState(final EAndroidFileDownloaderState newState)
Expand Down Expand Up @@ -481,7 +479,7 @@ public void onDownloadFailed(@NonNull final McuMgrException exception)
{
fileDownloadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
onError(exception.getMessage(), exception);
setLoggingEnabled(true);
setLoggingEnabledOnConnection(true);
busyStateChangedAdvertisement(false);

_downloadingController = null; //game over
Expand All @@ -493,7 +491,7 @@ public void onDownloadCanceled()
fileDownloadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
setState(EAndroidFileDownloaderState.CANCELLED);
cancelledAdvertisement();
setLoggingEnabled(true);
setLoggingEnabledOnConnection(true);
busyStateChangedAdvertisement(false);

_downloadingController = null; //game over
Expand All @@ -507,7 +505,7 @@ public void onDownloadCompleted(byte @NotNull [] data)
setState(EAndroidFileDownloaderState.COMPLETE); // order vital
downloadCompletedAdvertisement(_remoteFilePathSanitized, data); // order vital

setLoggingEnabled(true);
setLoggingEnabledOnConnection(true);
busyStateChangedAdvertisement(false);

_downloadingController = null; //game over
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ public EAndroidFileUploaderVerdict beginUpload(
return EAndroidFileUploaderVerdict.FAILED__INVALID_DATA;
}

resetUploadState(); //order must be called before ensureTransportIsInitializedExactlyOnce() because the environment might try to set the device via trySetBluetoothDevice()!!!
ensureTransportIsInitializedExactlyOnce(initialMtuSize); //order

final EAndroidFileUploaderVerdict verdict = ensureFilesystemManagerIsInitializedExactlyOnce(); //order
if (verdict != EAndroidFileUploaderVerdict.SUCCESS)
return verdict;
try
{
resetUploadState(); //order must be called before ensureTransportIsInitializedExactlyOnce() because the environment might try to set the device via trySetBluetoothDevice()!!!
ensureTransportIsInitializedExactlyOnce(initialMtuSize); //order
setLoggingEnabledOnTransport(false); //order

ensureFileUploaderCallbackProxyIsInitializedExactlyOnce(); //order
final EAndroidFileUploaderVerdict verdict = ensureFilesystemManagerIsInitializedExactlyOnce(); //order
if (verdict != EAndroidFileUploaderVerdict.SUCCESS)
return verdict;

setLoggingEnabled(false);
requestHighConnectionPriorityOnTransport(); //order
ensureFileUploaderCallbackProxyIsInitializedExactlyOnce(); //order

try
{
FileUploader fileUploader = new FileUploader( //00
_fileSystemManager,
_remoteFilePathSanitized,
Expand Down Expand Up @@ -205,12 +205,10 @@ private void resetUploadState()

private void ensureTransportIsInitializedExactlyOnce(int initialMtuSize)
{
if (_transport != null)
return;

logMessageAdvertisement("[AFU.ETIIEO.010] (Re)Initializing transport: initial-mtu-size=" + initialMtuSize, "FileUploader", "TRACE", _remoteFilePathSanitized);

_transport = new McuMgrBleTransport(_context, _bluetoothDevice);
if (_transport == null)
{
_transport = new McuMgrBleTransport(_context, _bluetoothDevice);
}

if (initialMtuSize > 0)
{
Expand All @@ -236,8 +234,6 @@ private EAndroidFileUploaderVerdict ensureFilesystemManagerIsInitializedExactlyO
try
{
_fileSystemManager = new FsManager(_transport); //order

requestHighConnectionPriority(_fileSystemManager); //order
}
catch (final Exception ex)
{
Expand All @@ -256,7 +252,7 @@ public void pause()
return;

setState(EAndroidFileUploaderState.PAUSED);
setLoggingEnabled(true);
setLoggingEnabledOnTransport(true);
transferController.pause();
busyStateChangedAdvertisement(false);
}
Expand All @@ -272,7 +268,7 @@ public void resume()
busyStateChangedAdvertisement(true);
_initialBytes = 0;

setLoggingEnabled(false);
setLoggingEnabledOnTransport(false);
transferController.resume();
}

Expand Down Expand Up @@ -303,14 +299,9 @@ public void cancel(final String reason)
transferController.cancel(); //order
}

static private void requestHighConnectionPriority(final FsManager fileSystemManager)
private void requestHighConnectionPriorityOnTransport()
{
final McuMgrTransport mcuMgrTransporter = fileSystemManager.getTransporter();
if (!(mcuMgrTransporter instanceof McuMgrBleTransport))
return;

final McuMgrBleTransport bleTransporter = (McuMgrBleTransport) mcuMgrTransporter;
bleTransporter.requestConnPriority(ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH);
_transport.requestConnPriority(ConnectionPriorityRequest.CONNECTION_PRIORITY_HIGH);
}

private void disposeTransport()
Expand Down Expand Up @@ -352,13 +343,9 @@ private void disposeCallbackProxy()
_fileUploaderCallbackProxy = null;
}

private void setLoggingEnabled(final boolean enabled)
private void setLoggingEnabledOnTransport(final boolean enabled)
{
final McuMgrTransport mcuMgrTransporter = _fileSystemManager.getTransporter();
if (!(mcuMgrTransporter instanceof McuMgrBleTransport))
return;

((McuMgrBleTransport) mcuMgrTransporter).setLoggingEnabled(enabled);
_transport.setLoggingEnabled(enabled);
}

private void setState(final EAndroidFileUploaderState newState)
Expand Down Expand Up @@ -503,7 +490,7 @@ public void onUploadFailed(@NonNull final McuMgrException error)
{
fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
onError(error.getMessage(), error);
setLoggingEnabled(true);
setLoggingEnabledOnTransport(true);
busyStateChangedAdvertisement(false);

_uploadController = null; //order
Expand All @@ -515,7 +502,7 @@ public void onUploadCanceled()
fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
setState(EAndroidFileUploaderState.CANCELLED);
cancelledAdvertisement(_cancellationReason);
setLoggingEnabled(true);
setLoggingEnabledOnTransport(true);
busyStateChangedAdvertisement(false);

_uploadController = null; //order
Expand All @@ -527,7 +514,7 @@ public void onUploadCompleted()
//fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(100, 0); //no need this is taken care of inside setState()
setState(EAndroidFileUploaderState.COMPLETE);
fileUploadedAdvertisement(_remoteFilePathSanitized);
setLoggingEnabled(true);
setLoggingEnabledOnTransport(true);
busyStateChangedAdvertisement(false);

_uploadController = null; //order
Expand Down

0 comments on commit 0971c23

Please sign in to comment.