1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-02 19:36:14 +02:00

huawei: fileupload: Refactoring to use file in UploadManager

* generate random file name for watchfaces
This commit is contained in:
Vitaliy Tomin 2024-04-03 17:35:22 +08:00
parent 34e9b5ceb5
commit f027d95a33
6 changed files with 49 additions and 27 deletions

View File

@ -26,18 +26,21 @@ public class FileUpload {
public Request(ParamsProvider paramsProvider, public Request(ParamsProvider paramsProvider,
int fileSize, int fileSize,
String watchfaceName, String fileName,
String watchfaceVersion) { byte fileType) {
super(paramsProvider); super(paramsProvider);
this.serviceId = FileUpload.id; this.serviceId = FileUpload.id;
this.commandId = id; this.commandId = id;
String filename = watchfaceName + "_" + watchfaceVersion; String watchfaceName = fileName.split("_")[0];
String watchfaceVersion = fileName.split("_")[1];
this.tlv = new HuaweiTLV() this.tlv = new HuaweiTLV()
.put(0x01, filename) .put(0x01, fileName)
.put(0x02, fileSize) .put(0x02, fileSize)
.put(0x03, (byte) 0x1) // file type 1 - watchface, 2 - music .put(0x03, (byte) fileType);
.put(0x05, watchfaceName)
if (fileType == 1)
this.tlv.put(0x05, watchfaceName)
.put(0x06, watchfaceVersion); .put(0x06, watchfaceVersion);
this.complete = true; this.complete = true;
@ -101,7 +104,7 @@ public class FileUpload {
public static class Response extends HuaweiPacket { public static class Response extends HuaweiPacket {
public FileUploadParams fileUploadParams; public FileUploadParams fileUploadParams = new FileUploadParams();
public Response (ParamsProvider paramsProvider) { public Response (ParamsProvider paramsProvider) {
super(paramsProvider); super(paramsProvider);
} }

View File

@ -1834,8 +1834,10 @@ public class HuaweiSupportProvider {
public void onInstallApp(Uri uri) { public void onInstallApp(Uri uri) {
LOG.info("enter onAppInstall uri: "+uri); LOG.info("enter onAppInstall uri: "+uri);
huaweiUploadManager.setFileName(getHuaweiCoordinator().getHuaweiWatchfaceManager().getRandomName());
huaweiUploadManager.setWatchfaceUri(uri); huaweiUploadManager.setWatchfaceUri(uri);
SendFileUploadInfo sendFileUploadInfo = new SendFileUploadInfo(this, huaweiUploadManager); SendFileUploadInfo sendFileUploadInfo = new SendFileUploadInfo(this, huaweiUploadManager);

View File

@ -19,15 +19,15 @@ import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
public class HuaweiUploadManager { public class HuaweiUploadManager {
private static final Logger LOG = LoggerFactory.getLogger(HuaweiUploadManager.class); private static final Logger LOG = LoggerFactory.getLogger(HuaweiUploadManager.class);
private final HuaweiSupportProvider support; private final HuaweiSupportProvider support;
byte[] watchfaceBin; byte[] fileBin;
byte[] watchfaceSHA256; byte[] fileSHA256;
byte fileType = 1; // 1 - watchface, 2 - music
int fileSize = 0; int fileSize = 0;
int currentUploadPosition = 0; int currentUploadPosition = 0;
int uploadChunkSize =0; int uploadChunkSize =0;
String watchfaceName = "413493857"; //FIXME generate random name String fileName = ""; //FIXME generate random name
String watchfaceVersion = "1.0.0"; //FIXME generate random version
//ack values set from 28 4 response //ack values set from 28 4 response
FileUploadParams fileUploadParams; FileUploadParams fileUploadParams;
@ -45,9 +45,8 @@ public class HuaweiUploadManager {
uriHelper = UriHelper.get(uri, support.getContext()); uriHelper = UriHelper.get(uri, support.getContext());
GBZipFile watchfacePackage = new GBZipFile(uriHelper.openInputStream()); GBZipFile watchfacePackage = new GBZipFile(uriHelper.openInputStream());
String watchfaceDescription = new String(watchfacePackage.getFileFromZip("description.xml")); fileBin = watchfacePackage.getFileFromZip("com.huawei.watchface");
watchfaceBin = watchfacePackage.getFileFromZip("com.huawei.watchface"); fileSize = fileBin.length;
fileSize = watchfaceBin.length;
} catch (ZipFileException e) { } catch (ZipFileException e) {
@ -67,8 +66,8 @@ public class HuaweiUploadManager {
try { try {
MessageDigest m = MessageDigest.getInstance("SHA256"); MessageDigest m = MessageDigest.getInstance("SHA256");
m.update(watchfaceBin, 0, watchfaceBin.length); m.update(fileBin, 0, fileBin.length);
watchfaceSHA256 = m.digest(); fileSHA256 = m.digest();
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
LOG.error("Digest alghoritm not found.", e); LOG.error("Digest alghoritm not found.", e);
return; return;
@ -77,7 +76,7 @@ public class HuaweiUploadManager {
currentUploadPosition = 0; currentUploadPosition = 0;
uploadChunkSize = 0; uploadChunkSize = 0;
//TODO: generate random watchfaceName and watchfaceVersion //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; return fileSize;
} }
public String getWatchfaceName() { public String getFileName() {
return watchfaceName; return this.fileName;
} }
public String getWatchfaceVersion() { public void setFileName(String fileName) {
return watchfaceVersion; this.fileName = fileName;
} }
public byte[] getWatchfaceSHA256() { public byte getFileType() {
return watchfaceSHA256; return this.fileType;
}
public byte[] getFileSHA256() {
return fileSHA256;
} }
public void setUploadChunkSize(int chunkSize) { public void setUploadChunkSize(int chunkSize) {
@ -111,7 +114,7 @@ public class HuaweiUploadManager {
public byte[] getCurrentChunk() { public byte[] getCurrentChunk() {
byte[] ret = new byte[uploadChunkSize]; byte[] ret = new byte[uploadChunkSize];
System.arraycopy(watchfaceBin, currentUploadPosition, ret, 0, uploadChunkSize); System.arraycopy(fileBin, currentUploadPosition, ret, 0, uploadChunkSize);
return ret; return ret;
} }

View File

@ -7,6 +7,7 @@ import org.xml.sax.InputSource;
import java.io.StringReader; import java.io.StringReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
@ -100,5 +101,18 @@ public class HuaweiWatchfaceManager
return params.height; 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;
}
} }

View File

@ -22,7 +22,7 @@ public class SendFileUploadHash extends Request{
protected List<byte[]> createRequest() throws RequestCreationException { protected List<byte[]> createRequest() throws RequestCreationException {
try { try {
return new FileUpload.FileHashSend.Request(this.paramsProvider, return new FileUpload.FileHashSend.Request(this.paramsProvider,
huaweiUploadManager.getWatchfaceSHA256() huaweiUploadManager.getFileSHA256()
).serialize(); ).serialize();
} catch (HuaweiPacket.CryptoException e) { } catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e); throw new RequestCreationException(e);

View File

@ -25,8 +25,8 @@ public class SendFileUploadInfo extends Request{
try { try {
return new FileUpload.FileInfoSend.Request(this.paramsProvider, return new FileUpload.FileInfoSend.Request(this.paramsProvider,
huaweiUploadManager.getFileSize(), huaweiUploadManager.getFileSize(),
huaweiUploadManager.getWatchfaceName(), huaweiUploadManager.getFileName(),
huaweiUploadManager.getWatchfaceVersion() huaweiUploadManager.getFileType()
).serialize(); ).serialize();
} catch (HuaweiPacket.CryptoException e) { } catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e); throw new RequestCreationException(e);