Skip to content

Commit f8a9b32

Browse files
Merge pull request #1303 from smartdevicelink/add_Overwrite_Property
Add overwrite property
2 parents 1eab1db + b0fd97f commit f8a9b32

5 files changed

Lines changed: 148 additions & 19 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/file/FileManagerTests.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,4 +842,87 @@ public void testFileManagerConfig() {
842842
assertEquals(fileManagerConfig.getArtworkRetryCount(), 2);
843843
assertEquals(fileManagerConfig.getFileRetryCount(), 2);
844844
}
845+
846+
/**
847+
* Tests overwrite property for uploading a file.
848+
* Checks to make sure file does not overwrite itself if overwrite property is set to false
849+
*/
850+
public void testOverwriteFileProperty() {
851+
ISdl internalInterface = mock(ISdl.class);
852+
853+
doAnswer(onListFilesSuccess).when(internalInterface).sendRPC(any(ListFiles.class));
854+
doAnswer(onPutFileSuccess).when(internalInterface).sendRPC(any(PutFile.class));
855+
856+
FileManagerConfig fileManagerConfig = new FileManagerConfig();
857+
858+
final FileManager fileManager = new FileManager(internalInterface, mTestContext, fileManagerConfig);
859+
fileManager.start(new CompletionListener() {
860+
@Override
861+
public void onComplete(boolean success) {
862+
assertTrue(success);
863+
fileManager.uploadFile(validFile, new CompletionListener() {
864+
@Override
865+
public void onComplete(boolean success) {
866+
assertTrue(success);
867+
validFile.setOverwrite(false);
868+
fileManager.uploadFile(validFile, new CompletionListener() {
869+
@Override
870+
public void onComplete(boolean success) {
871+
assertTrue(success);
872+
}
873+
});
874+
875+
}
876+
});
877+
}
878+
});
879+
verify(internalInterface, times(2)).sendRPC(any(RPCMessage.class));
880+
}
881+
882+
/**
883+
* Tests overwrite property for uploading a list of files.
884+
* Checks to make sure files do not overwrite themselves if overwrite property is set to false.
885+
*/
886+
public void testOverWriteFilePropertyListFiles() {
887+
final ISdl internalInterface = mock(ISdl.class);
888+
889+
doAnswer(onListFilesSuccess).when(internalInterface).sendRPC(any(ListFiles.class));
890+
doAnswer(onListFileUploadSuccess).when(internalInterface).sendRequests(any(List.class), any(OnMultipleRequestListener.class));
891+
892+
final SdlFile validFile2 = new SdlFile();
893+
validFile2.setName(Test.GENERAL_STRING + "2");
894+
validFile2.setFileData(Test.GENERAL_BYTE_ARRAY);
895+
validFile2.setPersistent(false);
896+
validFile2.setType(FileType.GRAPHIC_JPEG);
897+
898+
final List<SdlFile> list = new ArrayList<>();
899+
list.add(validFile);
900+
list.add(validFile2);
901+
902+
FileManagerConfig fileManagerConfig = new FileManagerConfig();
903+
fileManagerConfig.setArtworkRetryCount(2);
904+
fileManagerConfig.setFileRetryCount(4);
905+
906+
final FileManager fileManager = new FileManager(internalInterface, mTestContext, fileManagerConfig);
907+
fileManager.start(new CompletionListener() {
908+
@Override
909+
public void onComplete(boolean success) {
910+
fileManager.uploadFiles(list, new MultipleFileCompletionListener() {
911+
@Override
912+
public void onComplete(Map<String, String> errors) {
913+
validFile.setOverwrite(false);
914+
validFile2.setOverwrite(false);
915+
fileManager.uploadFiles(list, new MultipleFileCompletionListener() {
916+
@Override
917+
public void onComplete(Map<String, String> errors) {
918+
assertNull(errors);
919+
}
920+
});
921+
}
922+
});
923+
924+
}
925+
});
926+
verify(internalInterface, times(1)).sendRequests(any(List.class), any(OnMultipleRequestListener.class));
927+
}
845928
}

android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlArtwork.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class SdlArtwork extends SdlFile implements Cloneable{
5151
/**
5252
* Creates a new instance of SdlArtwork
5353
*/
54-
public SdlArtwork(){}
54+
public SdlArtwork() {}
5555

5656
/**
5757
* Creates a new instance of SdlArtwork

android/sdl_android/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@
4242
* A class representing data to be uploaded to core
4343
*/
4444
public class SdlFile{
45-
private String fileName;
46-
private int id = -1;
47-
private Uri uri;
48-
private byte[] fileData;
49-
private FileType fileType;
50-
private boolean persistentFile;
51-
private boolean isStaticIcon;
45+
private String fileName;
46+
private int id = -1;
47+
private Uri uri;
48+
private byte[] fileData;
49+
private FileType fileType;
50+
private boolean persistentFile;
51+
private boolean isStaticIcon;
52+
// Overwrite property by default is set to true in SdlFile constructors indicating that a file can be overwritten
53+
private boolean overwrite = true;
5254

5355
/**
5456
* Creates a new instance of SdlFile
5557
*/
56-
public SdlFile(){}
58+
public SdlFile() { }
5759

5860
/**
5961
* Creates a new instance of SdlFile
@@ -219,4 +221,20 @@ public void setStaticIcon(boolean staticIcon) {
219221
public boolean isStaticIcon() {
220222
return isStaticIcon;
221223
}
224+
225+
/**
226+
* Gets the overwrite property for an SdlFile by default its set to true
227+
* @return a boolean value that indicates if a file can be overwritten.
228+
*/
229+
public boolean getOverwrite() {
230+
return overwrite;
231+
}
232+
233+
/**
234+
* Sets the overwrite property for an SdlFile by default its set to true
235+
* @param overwrite a boolean value that indicates if a file can be overwritten
236+
*/
237+
public void setOverwrite(boolean overwrite) {
238+
this.overwrite = overwrite;
239+
}
222240
}

base/src/main/java/com/smartdevicelink/managers/file/BaseFileManager.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ public void onResponse(int correlationId, RPCResponse response) {
325325
*/
326326
public void uploadFile(@NonNull final SdlFile file, final CompletionListener listener) {
327327
if (file.isStaticIcon()) {
328-
Log.w(TAG, "Static icons don't need to be uploaded");
328+
Log.w(TAG, String.format("%s is a static icon and doesn't need to be uploaded", file.getName()));
329+
listener.onComplete(true);
330+
return;
331+
}
332+
if (!file.getOverwrite() && hasUploadedFile(file)) {
333+
Log.w(TAG, String.format("%s has already been uploaded and the overwrite property is set to false. It will not be uploaded again", file.getName()));
329334
listener.onComplete(true);
330335
return;
331336
}
@@ -392,13 +397,18 @@ public void uploadFiles(@NonNull List<? extends SdlFile> files, final MultipleFi
392397
}
393398
final List<PutFile> putFileRequests = new ArrayList<>();
394399
for (SdlFile file : files) {
395-
if (!file.isStaticIcon()) {
396-
putFileRequests.add(createPutFile(file));
400+
if (file.isStaticIcon()) {
401+
Log.w(TAG, String.format("%s is a static icon and doesn't need to be uploaded", file.getName()));
402+
continue;
403+
}
404+
if (!file.getOverwrite() && hasUploadedFile(file)) {
405+
Log.w(TAG, String.format("%s has already been uploaded and the overwrite property is set to false. It will not be uploaded again", file.getName()));
406+
continue;
397407
}
408+
putFileRequests.add(createPutFile(file));
398409
}
399410
// if all files are static icons we complete listener with no errors
400411
if (putFileRequests.isEmpty()) {
401-
Log.w(TAG, "Static icons don't need to be uploaded");
402412
listener.onComplete(null);
403413
} else {
404414
final Map<String, String> errors = new HashMap<>();

javaSE/src/main/java/com/smartdevicelink/managers/file/filetypes/SdlFile.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@
3939
* A class representing data to be uploaded to core
4040
*/
4141
public class SdlFile{
42-
private String fileName;
43-
private String filePath;
44-
private byte[] fileData;
45-
private FileType fileType;
46-
private boolean persistentFile;
47-
private boolean isStaticIcon;
42+
private String fileName;
43+
private String filePath;
44+
private byte[] fileData;
45+
private FileType fileType;
46+
private boolean persistentFile;
47+
private boolean isStaticIcon;
48+
// Overwrite property by default is set to true in SdlFile constructors indicating that a file can be overwritten
49+
private boolean overwrite = true;
4850

4951
/**
5052
* Creates a new instance of SdlFile
@@ -185,4 +187,20 @@ public void setStaticIcon(boolean staticIcon) {
185187
public boolean isStaticIcon() {
186188
return isStaticIcon;
187189
}
190+
191+
/**
192+
* Gets the overwrite property for an SdlFile by default its set to true
193+
* @return a boolean value that indicates if a file can be overwritten.
194+
*/
195+
public boolean getOverwrite() {
196+
return overwrite;
197+
}
198+
199+
/**
200+
* Sets the overwrite property for an SdlFile by default its set to true
201+
* @param overwrite a boolean value that indicates if a file can be overwritten
202+
*/
203+
public void setOverwrite(boolean overwrite) {
204+
this.overwrite = overwrite;
205+
}
188206
}

0 commit comments

Comments
 (0)