huawei: fileupload: Refactoring to use file in UploadManager
* generate random file name for watchfaces
This commit is contained in:
parent
34e9b5ceb5
commit
f027d95a33
|
@ -26,18 +26,21 @@ public class FileUpload {
|
|||
|
||||
public Request(ParamsProvider paramsProvider,
|
||||
int fileSize,
|
||||
String watchfaceName,
|
||||
String watchfaceVersion) {
|
||||
String fileName,
|
||||
byte fileType) {
|
||||
super(paramsProvider);
|
||||
this.serviceId = FileUpload.id;
|
||||
this.commandId = id;
|
||||
String filename = watchfaceName + "_" + watchfaceVersion;
|
||||
String watchfaceName = fileName.split("_")[0];
|
||||
String watchfaceVersion = fileName.split("_")[1];
|
||||
|
||||
this.tlv = new HuaweiTLV()
|
||||
.put(0x01, filename)
|
||||
.put(0x01, fileName)
|
||||
.put(0x02, fileSize)
|
||||
.put(0x03, (byte) 0x1) // file type 1 - watchface, 2 - music
|
||||
.put(0x05, watchfaceName)
|
||||
.put(0x03, (byte) fileType);
|
||||
|
||||
if (fileType == 1)
|
||||
this.tlv.put(0x05, watchfaceName)
|
||||
.put(0x06, watchfaceVersion);
|
||||
|
||||
this.complete = true;
|
||||
|
@ -101,7 +104,7 @@ public class FileUpload {
|
|||
|
||||
public static class Response extends HuaweiPacket {
|
||||
|
||||
public FileUploadParams fileUploadParams;
|
||||
public FileUploadParams fileUploadParams = new FileUploadParams();
|
||||
public Response (ParamsProvider paramsProvider) {
|
||||
super(paramsProvider);
|
||||
}
|
||||
|
|
|
@ -1834,8 +1834,10 @@ public class HuaweiSupportProvider {
|
|||
|
||||
public void onInstallApp(Uri uri) {
|
||||
LOG.info("enter onAppInstall uri: "+uri);
|
||||
huaweiUploadManager.setFileName(getHuaweiCoordinator().getHuaweiWatchfaceManager().getRandomName());
|
||||
huaweiUploadManager.setWatchfaceUri(uri);
|
||||
|
||||
|
||||
SendFileUploadInfo sendFileUploadInfo = new SendFileUploadInfo(this, huaweiUploadManager);
|
||||
|
||||
|
||||
|
|
|
@ -19,15 +19,15 @@ import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
|
|||
public class HuaweiUploadManager {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(HuaweiUploadManager.class);
|
||||
private final HuaweiSupportProvider support;
|
||||
byte[] watchfaceBin;
|
||||
byte[] watchfaceSHA256;
|
||||
byte[] fileBin;
|
||||
byte[] fileSHA256;
|
||||
byte fileType = 1; // 1 - watchface, 2 - music
|
||||
int fileSize = 0;
|
||||
|
||||
int currentUploadPosition = 0;
|
||||
int uploadChunkSize =0;
|
||||
|
||||
String watchfaceName = "413493857"; //FIXME generate random name
|
||||
String watchfaceVersion = "1.0.0"; //FIXME generate random version
|
||||
String fileName = ""; //FIXME generate random name
|
||||
|
||||
//ack values set from 28 4 response
|
||||
FileUploadParams fileUploadParams;
|
||||
|
@ -45,9 +45,8 @@ public class HuaweiUploadManager {
|
|||
uriHelper = UriHelper.get(uri, support.getContext());
|
||||
|
||||
GBZipFile watchfacePackage = new GBZipFile(uriHelper.openInputStream());
|
||||
String watchfaceDescription = new String(watchfacePackage.getFileFromZip("description.xml"));
|
||||
watchfaceBin = watchfacePackage.getFileFromZip("com.huawei.watchface");
|
||||
fileSize = watchfaceBin.length;
|
||||
fileBin = watchfacePackage.getFileFromZip("com.huawei.watchface");
|
||||
fileSize = fileBin.length;
|
||||
|
||||
|
||||
} catch (ZipFileException e) {
|
||||
|
@ -67,8 +66,8 @@ public class HuaweiUploadManager {
|
|||
|
||||
try {
|
||||
MessageDigest m = MessageDigest.getInstance("SHA256");
|
||||
m.update(watchfaceBin, 0, watchfaceBin.length);
|
||||
watchfaceSHA256 = m.digest();
|
||||
m.update(fileBin, 0, fileBin.length);
|
||||
fileSHA256 = m.digest();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
LOG.error("Digest alghoritm not found.", e);
|
||||
return;
|
||||
|
@ -77,7 +76,7 @@ public class HuaweiUploadManager {
|
|||
currentUploadPosition = 0;
|
||||
uploadChunkSize = 0;
|
||||
//TODO: generate random watchfaceName and watchfaceVersion
|
||||
LOG.info("watchface loaded, SHA256: "+ GB.hexdump(watchfaceSHA256) + " watchfaceName: " + watchfaceName + " watchfaceVersion: "+watchfaceVersion);
|
||||
LOG.info("watchface loaded, SHA256: "+ GB.hexdump(fileSHA256) + " fileName: " + fileName);
|
||||
|
||||
}
|
||||
|
||||
|
@ -85,16 +84,20 @@ public class HuaweiUploadManager {
|
|||
return fileSize;
|
||||
}
|
||||
|
||||
public String getWatchfaceName() {
|
||||
return watchfaceName;
|
||||
public String getFileName() {
|
||||
return this.fileName;
|
||||
}
|
||||
|
||||
public String getWatchfaceVersion() {
|
||||
return watchfaceVersion;
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
public byte[] getWatchfaceSHA256() {
|
||||
return watchfaceSHA256;
|
||||
public byte getFileType() {
|
||||
return this.fileType;
|
||||
}
|
||||
|
||||
public byte[] getFileSHA256() {
|
||||
return fileSHA256;
|
||||
}
|
||||
|
||||
public void setUploadChunkSize(int chunkSize) {
|
||||
|
@ -111,7 +114,7 @@ public class HuaweiUploadManager {
|
|||
|
||||
public byte[] getCurrentChunk() {
|
||||
byte[] ret = new byte[uploadChunkSize];
|
||||
System.arraycopy(watchfaceBin, currentUploadPosition, ret, 0, uploadChunkSize);
|
||||
System.arraycopy(fileBin, currentUploadPosition, ret, 0, uploadChunkSize);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ import org.xml.sax.InputSource;
|
|||
import java.io.StringReader;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
|
@ -100,5 +101,18 @@ public class HuaweiWatchfaceManager
|
|||
return params.height;
|
||||
}
|
||||
|
||||
public String getRandomName() {
|
||||
Random random = new Random();
|
||||
|
||||
String res="";
|
||||
for (int i = 0; i < 9; i++) {
|
||||
int ran = random.nextInt(9);
|
||||
res += String.valueOf(ran);
|
||||
}
|
||||
|
||||
res += "_1.0.0";
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ public class SendFileUploadHash extends Request{
|
|||
protected List<byte[]> createRequest() throws RequestCreationException {
|
||||
try {
|
||||
return new FileUpload.FileHashSend.Request(this.paramsProvider,
|
||||
huaweiUploadManager.getWatchfaceSHA256()
|
||||
huaweiUploadManager.getFileSHA256()
|
||||
).serialize();
|
||||
} catch (HuaweiPacket.CryptoException e) {
|
||||
throw new RequestCreationException(e);
|
||||
|
|
|
@ -25,8 +25,8 @@ public class SendFileUploadInfo extends Request{
|
|||
try {
|
||||
return new FileUpload.FileInfoSend.Request(this.paramsProvider,
|
||||
huaweiUploadManager.getFileSize(),
|
||||
huaweiUploadManager.getWatchfaceName(),
|
||||
huaweiUploadManager.getWatchfaceVersion()
|
||||
huaweiUploadManager.getFileName(),
|
||||
huaweiUploadManager.getFileType()
|
||||
).serialize();
|
||||
} catch (HuaweiPacket.CryptoException e) {
|
||||
throw new RequestCreationException(e);
|
||||
|
|
Loading…
Reference in New Issue