Skip to content

Commit 5f95808

Browse files
Generate name for SdlFile if not provided by dev (javaSE)
1 parent b05d810 commit 5f95808

3 files changed

Lines changed: 89 additions & 31 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,18 @@ public SdlFile(@NonNull StaticIconName staticIconName){
116116
* Sets the name of the file
117117
* @param fileName a String value representing the name that will be used to store the file in the head unit. You can pass null if you want the library to auto generate the name
118118
*/
119-
public void setName(String fileName){
119+
public void setName(String fileName) {
120120
if (fileName != null) {
121121
this.shouldAutoGenerateName = false;
122122
this.fileName = fileName;
123123
} else {
124124
this.shouldAutoGenerateName = true;
125125
if (this.getFileData() != null) {
126126
this.fileName = generateFileNameFromData(this.getFileData());
127-
} else if (this.getUri() != null){
128-
this.fileName = generateFileNameFromUri(uri);
129-
} else if (this.getResourceId() != 0){
130-
this.fileName = generateFileNameFromResourceId(this.id);
127+
} else if (this.getUri() != null) {
128+
this.fileName = generateFileNameFromUri(this.getUri());
129+
} else if (this.getResourceId() != 0) {
130+
this.fileName = generateFileNameFromResourceId(this.getResourceId());
131131
}
132132
}
133133
}
@@ -273,7 +273,7 @@ private String generateFileNameFromData(@NonNull byte[] data) {
273273
}
274274

275275
/**
276-
* Generates a file name from data by hashing the uri and returning the last 16 chars
276+
* Generates a file name from uri by hashing the uri string and returning the last 16 chars
277277
* @param uri a URI value representing a file's location
278278
* @return a String value representing the name that will be used to store the file in the head unit
279279
*/
@@ -282,7 +282,7 @@ private String generateFileNameFromUri(@NonNull Uri uri) {
282282
}
283283

284284
/**
285-
* Generates a file name from data by hashing the id and returning the last 16 chars
285+
* Generates a file name from resourceId by hashing the id and returning the last 16 chars
286286
* @param id an int value representing the android resource id of the file
287287
* @return a String value representing the name that will be used to store the file in the head unit
288288
*/

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,23 @@ public SdlArtwork(){}
5252

5353
/**
5454
* Creates a new instance of SdlArtwork
55-
* @param fileName a String value representing the name that will be used to store the file in the head unit
55+
* @param fileName a String value representing the name that will be used to store the file in the head unit. You can pass null if you want the library to auto generate the name
5656
* @param fileType a FileType enum value representing the type of the file
5757
* @param filePath a String value representing the the location of the file
5858
* @param persistentFile a boolean value that indicates if the file is meant to persist between sessions / ignition cycles
5959
*/
60-
public SdlArtwork(@NonNull String fileName, @NonNull FileType fileType, String filePath, boolean persistentFile) {
60+
public SdlArtwork(String fileName, @NonNull FileType fileType, String filePath, boolean persistentFile) {
6161
super(fileName, fileType, filePath, persistentFile);
6262
}
6363

6464
/**
6565
* Creates a new instance of SdlArtwork
66-
* @param fileName a String value representing the name that will be used to store the file in the head unit
66+
* @param fileName a String value representing the name that will be used to store the file in the head unit. You can pass null if you want the library to auto generate the name
6767
* @param fileType a FileType enum value representing the type of the file
6868
* @param data a byte array representing the data of the file
6969
* @param persistentFile a boolean value that indicates if the file is meant to persist between sessions / ignition cycles
7070
*/
71-
public SdlArtwork(@NonNull String fileName, @NonNull FileType fileType, byte[] data, boolean persistentFile) {
71+
public SdlArtwork(String fileName, @NonNull FileType fileType, byte[] data, boolean persistentFile) {
7272
super(fileName, fileType, data, persistentFile);
7373
}
7474

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

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,13 @@
3232
package com.smartdevicelink.managers.file.filetypes;
3333

3434
import android.support.annotation.NonNull;
35+
3536
import com.smartdevicelink.proxy.rpc.enums.FileType;
3637
import com.smartdevicelink.proxy.rpc.enums.StaticIconName;
3738

39+
import java.security.MessageDigest;
40+
import java.security.NoSuchAlgorithmException;
41+
3842
/**
3943
* A class representing data to be uploaded to core
4044
*/
@@ -45,6 +49,8 @@ public class SdlFile{
4549
private FileType fileType;
4650
private boolean persistentFile;
4751
private boolean isStaticIcon;
52+
private boolean shouldAutoGenerateName;
53+
4854

4955
/**
5056
* Creates a new instance of SdlFile
@@ -53,49 +59,59 @@ public SdlFile(){}
5359

5460
/**
5561
* Creates a new instance of SdlFile
56-
* @param fileName a String value representing the name that will be used to store the file in the head unit
62+
* @param fileName a String value representing the name that will be used to store the file in the head unit. You can pass null if you want the library to auto generate the name
5763
* @param fileType a FileType enum value representing the type of the file
5864
* @param filePath a String value representing the the location of the file
5965
* @param persistentFile a boolean value that indicates if the file is meant to persist between sessions / ignition cycles
6066
*/
61-
public SdlFile(@NonNull String fileName, @NonNull FileType fileType, String filePath, boolean persistentFile){
62-
this.fileName = fileName;
63-
this.fileType = fileType;
64-
this.filePath = filePath;
65-
this.persistentFile = persistentFile;
67+
public SdlFile(String fileName, @NonNull FileType fileType, String filePath, boolean persistentFile){
68+
setName(fileName);
69+
setType(fileType);
70+
setFilePath(filePath);
71+
setPersistent(persistentFile);
6672
}
6773

6874
/**
6975
* Creates a new instance of SdlFile
70-
* @param fileName a String value representing the name that will be used to store the file in the head unit
76+
* @param fileName a String value representing the name that will be used to store the file in the head unit. You can pass null if you want the library to auto generate the name
7177
* @param fileType a FileType enum value representing the type of the file
7278
* @param data a byte array representing the data of the file
7379
* @param persistentFile a boolean value that indicates if the file is meant to persist between sessions / ignition cycles
7480
*/
75-
public SdlFile(@NonNull String fileName, @NonNull FileType fileType, byte[] data, boolean persistentFile){
76-
this.fileName = fileName;
77-
this.fileType = fileType;
78-
this.fileData = data;
79-
this.persistentFile = persistentFile;
81+
public SdlFile(String fileName, @NonNull FileType fileType, byte[] data, boolean persistentFile){
82+
setName(fileName);
83+
setType(fileType);
84+
setFileData(data);
85+
setPersistent(persistentFile);
8086
}
8187

8288
/**
8389
* Creates a new instance of SdlFile
8490
* @param staticIconName a StaticIconName enum value representing the name of a static file that comes pre-shipped with the head unit
8591
*/
8692
public SdlFile(@NonNull StaticIconName staticIconName){
87-
this.fileName = staticIconName.toString();
88-
this.fileData = staticIconName.toString().getBytes();
89-
this.persistentFile = false;
90-
this.isStaticIcon = true;
93+
setName(staticIconName.toString());
94+
setFileData(staticIconName.toString().getBytes());
95+
setPersistent(false);
96+
setStaticIcon(true);
9197
}
9298

9399
/**
94100
* Sets the name of the file
95-
* @param fileName a String value representing the name that will be used to store the file in the head unit
96-
*/
97-
public void setName(@NonNull String fileName){
98-
this.fileName = fileName;
101+
* @param fileName a String value representing the name that will be used to store the file in the head unit. You can pass null if you want the library to auto generate the name
102+
*/
103+
public void setName(String fileName) {
104+
if (fileName != null) {
105+
this.shouldAutoGenerateName = false;
106+
this.fileName = fileName;
107+
} else {
108+
this.shouldAutoGenerateName = true;
109+
if (this.getFileData() != null) {
110+
this.fileName = generateFileNameFromData(this.getFileData());
111+
} else if (this.getFilePath() != null) {
112+
this.fileName = generateFileNameFromFilePath(this.getFilePath());
113+
}
114+
}
99115
}
100116

101117
/**
@@ -112,6 +128,9 @@ public String getName(){
112128
*/
113129
public void setFilePath(String filePath){
114130
this.filePath = filePath;
131+
if (shouldAutoGenerateName && filePath != null) {
132+
this.fileName = generateFileNameFromFilePath(filePath);
133+
}
115134
}
116135

117136
/**
@@ -128,6 +147,9 @@ public String getFilePath(){
128147
*/
129148
public void setFileData(byte[] data){
130149
this.fileData = data;
150+
if (shouldAutoGenerateName && data != null) {
151+
this.fileName = generateFileNameFromData(data);
152+
}
131153
}
132154

133155
/**
@@ -185,4 +207,40 @@ public void setStaticIcon(boolean staticIcon) {
185207
public boolean isStaticIcon() {
186208
return isStaticIcon;
187209
}
210+
211+
/**
212+
* Generates a file name from data by hashing the data and returning the last 16 chars
213+
* @param data a byte array representing the data of the file
214+
* @return a String value representing the name that will be used to store the file in the head unit
215+
*/
216+
private String generateFileNameFromData(@NonNull byte[] data) {
217+
String result;
218+
MessageDigest messageDigest;
219+
try {
220+
messageDigest = MessageDigest.getInstance("md5");
221+
} catch (NoSuchAlgorithmException e) {
222+
e.printStackTrace();
223+
return null;
224+
}
225+
byte[] hash = new byte[0];
226+
if (messageDigest != null) {
227+
hash = messageDigest.digest(data);
228+
}
229+
StringBuilder stringBuilder = new StringBuilder(2 * hash.length);
230+
for (byte b : hash) {
231+
stringBuilder.append(String.format("%02x", b & 0xff));
232+
}
233+
String hashString = stringBuilder.toString();
234+
result = hashString.substring(hashString.length() - 16);
235+
return result;
236+
}
237+
238+
/**
239+
* Generates a file name from filePath by hashing the filePath and returning the last 16 chars
240+
* @param filePath a String value representing the the location of the file
241+
* @return a String value representing the name that will be used to store the file in the head unit
242+
*/
243+
private String generateFileNameFromFilePath(String filePath) {
244+
return generateFileNameFromData(filePath.getBytes());
245+
}
188246
}

0 commit comments

Comments
 (0)