Skip to content

Commit 35e14c2

Browse files
author
Robert Henigan
authored
Merge pull request #1470 from smartdevicelink/feature/issue_1469_sdl_file_uri
JavaSE - Add SdlFile constructor that takes URI
2 parents 56771c0 + 2b85be5 commit 35e14c2

3 files changed

Lines changed: 102 additions & 5 deletions

File tree

javaSE/src/main/java/com/smartdevicelink/managers/file/FileManager.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@
3636
import com.smartdevicelink.managers.file.filetypes.SdlFile;
3737
import com.smartdevicelink.proxy.interfaces.ISdl;
3838
import com.smartdevicelink.proxy.rpc.PutFile;
39+
import com.smartdevicelink.util.DebugTool;
3940
import com.smartdevicelink.util.FileUtls;
4041

42+
import java.io.IOException;
43+
import java.io.InputStream;
44+
import java.net.URI;
45+
4146
/**
4247
* <strong>FileManager</strong> <br>
4348
*
@@ -94,6 +99,14 @@ PutFile createPutFile(@NonNull final SdlFile file){
9499
}else{
95100
throw new IllegalArgumentException("File at path was empty");
96101
}
102+
}else if(file.getURI() != null){
103+
// Use URI to upload file
104+
byte[] data = contentsOfUri(file.getURI());
105+
if(data != null){
106+
putFile.setFileData(data);
107+
}else{
108+
throw new IllegalArgumentException("Uri was empty");
109+
}
97110
}else if(file.getFileData() != null){
98111
// Use file data (raw bytes) to upload file
99112
putFile.setFileData(file.getFileData());
@@ -110,4 +123,28 @@ PutFile createPutFile(@NonNull final SdlFile file){
110123
return putFile;
111124
}
112125

126+
127+
/**
128+
* Helper method to take Uri and turn it into byte array
129+
* @param uri Uri for desired file
130+
* @return Resulting byte array
131+
*/
132+
private byte[] contentsOfUri(URI uri){
133+
InputStream is = null;
134+
try{
135+
is = uri.toURL().openStream();
136+
return contentsOfInputStream(is);
137+
} catch (IOException e){
138+
DebugTool.logError(TAG, "Can't read from URI", e);
139+
return null;
140+
} finally {
141+
if (is != null) {
142+
try {
143+
is.close();
144+
} catch (IOException e) {
145+
e.printStackTrace();
146+
}
147+
}
148+
}
149+
}
113150
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import com.smartdevicelink.proxy.rpc.enums.StaticIconName;
3939
import com.smartdevicelink.util.DebugTool;
4040

41+
import java.net.URI;
42+
4143
/**
4244
* A class that extends SdlFile, representing artwork (JPEG, PNG, or BMP) to be uploaded to core
4345
*/
@@ -61,6 +63,17 @@ public SdlArtwork(String fileName, @NonNull FileType fileType, String filePath,
6163
super(fileName, fileType, filePath, persistentFile);
6264
}
6365

66+
/**
67+
* Creates a new instance of SdlArtwork
68+
* @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
69+
* @param fileType a FileType enum value representing the type of the file
70+
* @param uri a URI value representing a file's location. Currently, it only supports local files
71+
* @param persistentFile a boolean value that indicates if the file is meant to persist between sessions / ignition cycles
72+
*/
73+
public SdlArtwork(String fileName, @NonNull FileType fileType, URI uri, boolean persistentFile) {
74+
super(fileName, fileType, uri, persistentFile);
75+
}
76+
6477
/**
6578
* Creates a new instance of SdlArtwork
6679
* @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

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

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.smartdevicelink.proxy.rpc.enums.FileType;
3737
import com.smartdevicelink.proxy.rpc.enums.StaticIconName;
3838

39+
import java.net.URI;
3940
import java.security.MessageDigest;
4041
import java.security.NoSuchAlgorithmException;
4142

@@ -45,6 +46,7 @@
4546
public class SdlFile{
4647
private String fileName;
4748
private String filePath;
49+
private URI uri;
4850
private byte[] fileData;
4951
private FileType fileType;
5052
private boolean persistentFile;
@@ -72,6 +74,20 @@ public SdlFile(String fileName, @NonNull FileType fileType, String filePath, boo
7274
setPersistent(persistentFile);
7375
}
7476

77+
/**
78+
* Creates a new instance of SdlFile
79+
* @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
80+
* @param fileType a FileType enum value representing the type of the file
81+
* @param uri a URI value representing a file's location. Currently, it only supports local files
82+
* @param persistentFile a boolean value that indicates if the file is meant to persist between sessions / ignition cycles
83+
*/
84+
public SdlFile(String fileName, @NonNull FileType fileType, URI uri, boolean persistentFile){
85+
setName(fileName);
86+
setType(fileType);
87+
setURI(uri);
88+
setPersistent(persistentFile);
89+
}
90+
7591
/**
7692
* Creates a new instance of SdlFile
7793
* @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
@@ -109,6 +125,8 @@ public void setName(String fileName) {
109125
this.shouldAutoGenerateName = true;
110126
if (this.getFileData() != null) {
111127
this.fileName = generateFileNameFromData(this.getFileData());
128+
} else if (this.getURI() != null) {
129+
this.fileName = generateFileNameFromUri(this.getURI());
112130
} else if (this.getFilePath() != null) {
113131
this.fileName = generateFileNameFromFilePath(this.getFilePath());
114132
}
@@ -142,6 +160,25 @@ public String getFilePath(){
142160
return this.filePath;
143161
}
144162

163+
/**
164+
* Sets the uri of the file
165+
* @param uri a URI value representing a file's location. Currently, it only supports local files
166+
*/
167+
public void setURI(URI uri){
168+
this.uri = uri;
169+
if (shouldAutoGenerateName && uri != null) {
170+
this.fileName = generateFileNameFromUri(uri);
171+
}
172+
}
173+
174+
/**
175+
* Gets the uri of the file
176+
* @return a URI value representing a file's location. Currently, it only supports local files
177+
*/
178+
public URI getURI(){
179+
return uri;
180+
}
181+
145182
/**
146183
* Sets the byte array that represents the content of the file
147184
* @param data a byte array representing the data of the file
@@ -261,6 +298,15 @@ private String generateFileNameFromFilePath(String filePath) {
261298
return generateFileNameFromData(filePath.getBytes());
262299
}
263300

301+
/**
302+
* Generates a file name from uri by hashing the uri string and returning the last 16 chars
303+
* @param uri a URI value representing a file's location
304+
* @return a String value representing the name that will be used to store the file in the head unit
305+
*/
306+
private String generateFileNameFromUri(@NonNull URI uri) {
307+
return generateFileNameFromData(uri.toString().getBytes());
308+
}
309+
264310
/**
265311
* Used to compile hashcode for SdlFile for use to compare in equals method
266312
* @return Custom hashcode of SdlFile variables
@@ -269,11 +315,12 @@ private String generateFileNameFromFilePath(String filePath) {
269315
public int hashCode() {
270316
int result = 1;
271317
result += ((getName() == null) ? 0 : Integer.rotateLeft(getName().hashCode(), 1));
272-
result += ((getFilePath() == null) ? 0 : Integer.rotateLeft(getFilePath().hashCode(), 2));
273-
result += ((getFileData() == null) ? 0 : Integer.rotateLeft(getFileData().hashCode(), 3));
274-
result += ((getType() == null) ? 0 : Integer.rotateLeft(getType().hashCode(), 4));
275-
result += Integer.rotateLeft(Boolean.valueOf(isStaticIcon()).hashCode(), 5);
276-
result += Integer.rotateLeft(Boolean.valueOf(isPersistent()).hashCode(), 6);
318+
result += ((getURI() == null) ? 0 : Integer.rotateLeft(getURI().hashCode(), 2));
319+
result += ((getFilePath() == null) ? 0 : Integer.rotateLeft(getFilePath().hashCode(), 3));
320+
result += ((getFileData() == null) ? 0 : Integer.rotateLeft(getFileData().hashCode(), 4));
321+
result += ((getType() == null) ? 0 : Integer.rotateLeft(getType().hashCode(), 5));
322+
result += Integer.rotateLeft(Boolean.valueOf(isStaticIcon()).hashCode(), 6);
323+
result += Integer.rotateLeft(Boolean.valueOf(isPersistent()).hashCode(), 7);
277324
return result;
278325
}
279326

0 commit comments

Comments
 (0)