Huawei: file upload: Refactoring Watchface -> Fileupload

This commit is contained in:
Vitaliy Tomin 2024-04-01 15:14:31 +08:00
parent c36aa14463
commit 2d915e7346
11 changed files with 129 additions and 132 deletions

View File

@ -40,7 +40,7 @@ import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FindPhone;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FitnessData;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.MusicControl;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Notifications;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.WatchfaceUpload;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FileUpload;
import nodomain.freeyourgadget.gadgetbridge.util.CheckSums;
public class HuaweiPacket {
@ -540,10 +540,10 @@ public class HuaweiPacket {
this.isEncrypted = this.attemptDecrypt(); // Helps with debugging
return this;
}
case WatchfaceUpload.id:
case FileUpload.id:
switch(this.commandId) {
case WatchfaceUpload.WatchfaceNextChunkParams.id:
return new WatchfaceUpload.WatchfaceNextChunkParams(paramsProvider).fromPacket(this);
case FileUpload.FileNextChunkParams.id:
return new FileUpload.FileNextChunkParams(paramsProvider).fromPacket(this);
default:
this.isEncrypted = this.attemptDecrypt(); // Helps with debugging
return this;

View File

@ -3,9 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiTLV;
public class WatchfaceUpload {
public class FileUpload {
public static final byte id = 0x28;
public static class WatchfaceStartSend {
public static class FileInfoSend {
public static final byte id = 0x02;
public static class Request extends HuaweiPacket {
@ -14,14 +14,14 @@ public class WatchfaceUpload {
String watchfaceName,
String watchfaceVersion) {
super(paramsProvider);
this.serviceId = WatchfaceUpload.id;
this.serviceId = FileUpload.id;
this.commandId = id;
String filename = watchfaceName + "_" + watchfaceVersion;
this.tlv = new HuaweiTLV()
.put(0x01, filename)
.put(0x02, fileSize)
.put(0x03, (byte) 0x1) // ???
.put(0x03, (byte) 0x1) // file type 1 - watchface, 2 - music
.put(0x05, watchfaceName)
.put(0x06, watchfaceVersion);
@ -39,7 +39,7 @@ public class WatchfaceUpload {
}
public static class WatchfaceSendHash {
public static class FileHashSend {
public static final byte id = 0x03;
public static class Request extends HuaweiPacket {
@ -47,11 +47,11 @@ public class WatchfaceUpload {
public Request(ParamsProvider paramsProvider,
byte[] hash) {
super(paramsProvider);
this.serviceId = WatchfaceUpload.id;
this.serviceId = FileUpload.id;
this.commandId = id;
this.tlv = new HuaweiTLV()
.put(0x01, (byte) 1) //???
.put(0x01, (byte) 1) // filetype 1 - watchface, 2 - music
.put(0x03, hash);
this.complete = true;
@ -68,17 +68,17 @@ public class WatchfaceUpload {
}
public static class WatchfaceSendConsultAck {
public static class FileuploadConsultAck {
public static final byte id = 0x04;
public static class Request extends HuaweiPacket {
public Request(ParamsProvider paramsProvider) {
super(paramsProvider);
this.serviceId = WatchfaceUpload.id;
this.serviceId = FileUpload.id;
this.commandId = id;
this.tlv = new HuaweiTLV()
.put(0x7f, 0x000186A0) //ok
.put(0x01, (byte) 0x01)
.put(0x09, (byte) 0x01);
.put(0x01, (byte) 0x01) // filetype 1 - watchface, 2 -music
.put(0x09, (byte) 0x01); //??? present in watchface only
this.complete = true;
}
}
@ -90,14 +90,14 @@ public class WatchfaceUpload {
}
}
public static class WatchfaceNextChunkParams extends HuaweiPacket {
public static class FileNextChunkParams extends HuaweiPacket {
public static final byte id = 0x05;
public int bytesUploaded = 0;
public int nextchunkSize = 0;
public WatchfaceNextChunkParams(ParamsProvider paramsProvider) {
public FileNextChunkParams(ParamsProvider paramsProvider) {
super(paramsProvider);
this.serviceId = WatchfaceUpload.id;
this.serviceId = FileUpload.id;
this.commandId = id;
this.complete = true;
}
@ -111,15 +111,14 @@ public class WatchfaceUpload {
}
public static class WatchfaceSendNextChunk extends HuaweiPacket {
public static class FileNextChunkSend extends HuaweiPacket {
public static final byte id = 0x06;
public WatchfaceSendNextChunk(ParamsProvider paramsProvider) {
public FileNextChunkSend(ParamsProvider paramsProvider) {
super(paramsProvider);
this.serviceId = WatchfaceUpload.id;
this.serviceId = FileUpload.id;
this.commandId = id;
this.complete = true;
}
}

View File

@ -48,14 +48,14 @@ import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FindPhone;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.GpsAndTime;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Menstrual;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.MusicControl;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.WatchfaceUpload;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FileUpload;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.Weather;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.Request;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.GetPhoneInfoRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendMenstrualModifyTimeRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWatchfaceAck;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWatchfaceChunk;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWatchfaceHash;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendFileUploadAck;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendFileUploadChunk;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendFileUploadHash;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWeatherDeviceRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SetMusicStatusRequest;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -392,10 +392,10 @@ public class AsynchronousResponse {
}
private void handleWatchfaceHash(HuaweiPacket response) {
if (response.serviceId == WatchfaceUpload.id && response.commandId == WatchfaceUpload.WatchfaceSendHash.id) {
if (response.serviceId == FileUpload.id && response.commandId == FileUpload.FileHashSend.id) {
try {
SendWatchfaceHash sendWatchfaceHash = new SendWatchfaceHash(this.support, this.support.huaweiWatchfaceManager);
sendWatchfaceHash.doPerform();
SendFileUploadHash sendFileUploadHash = new SendFileUploadHash(this.support, this.support.huaweiUploadManager);
sendFileUploadHash.doPerform();
} catch (IOException e) {
LOG.error("Could not send watchface hash request", e);
}
@ -403,10 +403,10 @@ public class AsynchronousResponse {
}
private void handleWatchfaceAck(HuaweiPacket response) {
if (response.serviceId == WatchfaceUpload.id && response.commandId == WatchfaceUpload.WatchfaceSendConsultAck.id) {
if (response.serviceId == FileUpload.id && response.commandId == FileUpload.FileuploadConsultAck.id) {
try {
SendWatchfaceAck sendWatchfaceAck = new SendWatchfaceAck(this.support);
sendWatchfaceAck.doPerform();
SendFileUploadAck sendFileUploadAck = new SendFileUploadAck(this.support);
sendFileUploadAck.doPerform();
} catch (IOException e) {
LOG.error("Could not send watchface ack request", e);
}
@ -415,16 +415,16 @@ public class AsynchronousResponse {
//FIXME: implement sliced raw serialization and add handeWatchfaceNextChunk to handleResponse list
private void handeWatchfaceNextChunk(HuaweiPacket response) throws Request.ResponseParseException {
if (response.serviceId == WatchfaceUpload.id && response.commandId == WatchfaceUpload.WatchfaceNextChunkParams.id) {
if (!(response instanceof WatchfaceUpload.WatchfaceNextChunkParams))
throw new Request.ResponseTypeMismatchException(response, WatchfaceUpload.WatchfaceNextChunkParams.class);
WatchfaceUpload.WatchfaceNextChunkParams resp = (WatchfaceUpload.WatchfaceNextChunkParams) response;
support.huaweiWatchfaceManager.setUploadChunkSize(resp.nextchunkSize);
support.huaweiWatchfaceManager.setCurrentUploadPosition(resp.bytesUploaded);
if (response.serviceId == FileUpload.id && response.commandId == FileUpload.FileNextChunkParams.id) {
if (!(response instanceof FileUpload.FileNextChunkParams))
throw new Request.ResponseTypeMismatchException(response, FileUpload.FileNextChunkParams.class);
FileUpload.FileNextChunkParams resp = (FileUpload.FileNextChunkParams) response;
support.huaweiUploadManager.setUploadChunkSize(resp.nextchunkSize);
support.huaweiUploadManager.setCurrentUploadPosition(resp.bytesUploaded);
try {
SendWatchfaceChunk sendWatchfaceChunk = new SendWatchfaceChunk(this.support, this.support.huaweiWatchfaceManager);
sendWatchfaceChunk.doPerform();
SendFileUploadChunk sendFileUploadChunk = new SendFileUploadChunk(this.support, this.support.huaweiUploadManager);
sendFileUploadChunk.doPerform();
} catch (IOException e) {
LOG.error("Could not send watchface next chunk request", e);
}

View File

@ -88,7 +88,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.GetS
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendExtendedAccountRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendGpsAndTimeToDeviceRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendGpsDataRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWatchfaceInfo;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendFileUploadInfo;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendWeatherCurrentRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendNotifyHeartRateCapabilityRequest;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests.SendNotifyRestHeartRateCapabilityRequest;
@ -180,7 +180,7 @@ public class HuaweiSupportProvider {
private final HuaweiPacket.ParamsProvider paramsProvider = new HuaweiPacket.ParamsProvider();
protected ResponseManager responseManager = new ResponseManager(this);
protected HuaweiWatchfaceManager huaweiWatchfaceManager = new HuaweiWatchfaceManager(this);
protected HuaweiUploadManager huaweiUploadManager = new HuaweiUploadManager(this);
public HuaweiCoordinatorSupplier getCoordinator() {
return ((HuaweiCoordinatorSupplier) this.gbDevice.getDeviceCoordinator());
@ -1828,13 +1828,13 @@ public class HuaweiSupportProvider {
public void onInstallApp(Uri uri) {
LOG.info("enter onAppInstall uri: "+uri);
huaweiWatchfaceManager.setWatchfaceUri(uri);
huaweiUploadManager.setWatchfaceUri(uri);
SendWatchfaceInfo sendWatchfaceInfo = new SendWatchfaceInfo(this, huaweiWatchfaceManager);
SendFileUploadInfo sendFileUploadInfo = new SendFileUploadInfo(this, huaweiUploadManager);
try {
sendWatchfaceInfo.doPerform();
sendFileUploadInfo.doPerform();
} catch (IOException e) {
GB.toast(context, "Failed to send watchface info", Toast.LENGTH_SHORT, GB.ERROR, e);
LOG.error("Failed to send watchface info", e);

View File

@ -15,8 +15,8 @@ import nodomain.freeyourgadget.gadgetbridge.util.GBZipFile;
import nodomain.freeyourgadget.gadgetbridge.util.UriHelper;
import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
public class HuaweiWatchfaceManager {
private static final Logger LOG = LoggerFactory.getLogger(HuaweiWatchfaceManager.class);
public class HuaweiUploadManager {
private static final Logger LOG = LoggerFactory.getLogger(HuaweiUploadManager.class);
private final HuaweiSupportProvider support;
byte[] watchfaceBin;
byte[] watchfaceSHA256;
@ -27,7 +27,7 @@ public class HuaweiWatchfaceManager {
String watchfaceName = "413493857"; //FIXME generate random name
String watchfaceVersion = "1.0.0"; //FIXME generate random version
public HuaweiWatchfaceManager(HuaweiSupportProvider support) {
public HuaweiUploadManager(HuaweiSupportProvider support) {
this.support=support;
}

View File

@ -3,21 +3,20 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.WatchfaceUpload;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FileUpload;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiWatchfaceManager;
public class SendWatchfaceAck extends Request {
public SendWatchfaceAck(HuaweiSupportProvider support) {
public class SendFileUploadAck extends Request {
public SendFileUploadAck(HuaweiSupportProvider support) {
super(support);
this.serviceId = WatchfaceUpload.id;
this.commandId = WatchfaceUpload.WatchfaceSendConsultAck.id;
this.serviceId = FileUpload.id;
this.commandId = FileUpload.FileuploadConsultAck.id;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
return new WatchfaceUpload.WatchfaceSendConsultAck.Request(this.paramsProvider ).serialize();
return new FileUpload.FileuploadConsultAck.Request(this.paramsProvider ).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
}

View File

@ -0,0 +1,29 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FileUpload;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiUploadManager;
public class SendFileUploadChunk extends Request {
HuaweiUploadManager huaweiUploadManager;
public SendFileUploadChunk(HuaweiSupportProvider support,
HuaweiUploadManager watchfaceManager) {
super(support);
this.huaweiUploadManager = watchfaceManager;
this.serviceId = FileUpload.id;
this.commandId = FileUpload.FileNextChunkSend.id;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
//FIXME need new package type with raw data chunks ?
return new FileUpload.FileNextChunkSend(this.paramsProvider).serializeFileChunk(
huaweiUploadManager.getCurrentChunk(),
huaweiUploadManager.getCurrentUploadPosition()
);
}
}

View File

@ -3,26 +3,26 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.WatchfaceUpload;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FileUpload;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiWatchfaceManager;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiUploadManager;
public class SendWatchfaceHash extends Request{
HuaweiWatchfaceManager huaweiWatchfaceManager;
public SendWatchfaceHash(HuaweiSupportProvider support,
HuaweiWatchfaceManager huaweiWatchfaceManager) {
public class SendFileUploadHash extends Request{
HuaweiUploadManager huaweiUploadManager;
public SendFileUploadHash(HuaweiSupportProvider support,
HuaweiUploadManager huaweiUploadManager) {
super(support);
this.huaweiWatchfaceManager = huaweiWatchfaceManager;
this.serviceId = WatchfaceUpload.id;
this.commandId = WatchfaceUpload.WatchfaceSendHash.id;
this.huaweiUploadManager = huaweiUploadManager;
this.serviceId = FileUpload.id;
this.commandId = FileUpload.FileHashSend.id;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
return new WatchfaceUpload.WatchfaceSendHash.Request(this.paramsProvider,
huaweiWatchfaceManager.getWatchfaceSHA256()
return new FileUpload.FileHashSend.Request(this.paramsProvider,
huaweiUploadManager.getWatchfaceSHA256()
).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);

View File

@ -0,0 +1,36 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.FileUpload;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiUploadManager;
public class SendFileUploadInfo extends Request{
HuaweiUploadManager huaweiUploadManager;
public SendFileUploadInfo(HuaweiSupportProvider support,
HuaweiUploadManager huaweiUploadManager) {
super(support);
this.huaweiUploadManager = huaweiUploadManager;
this.serviceId = FileUpload.id;
this.commandId = FileUpload.FileInfoSend.id;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
return new FileUpload.FileInfoSend.Request(this.paramsProvider,
huaweiUploadManager.getFileSize(),
huaweiUploadManager.getWatchfaceName(),
huaweiUploadManager.getWatchfaceVersion()
).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
}
}
}

View File

@ -1,30 +0,0 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.WatchfaceUpload;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiWatchfaceManager;
public class SendWatchfaceChunk extends Request {
HuaweiWatchfaceManager huaweiWatchfaceManager;
public SendWatchfaceChunk(HuaweiSupportProvider support,
HuaweiWatchfaceManager watchfaceManager) {
super(support);
this.huaweiWatchfaceManager = watchfaceManager;
this.serviceId = WatchfaceUpload.id;
this.commandId = WatchfaceUpload.WatchfaceSendNextChunk.id;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
//FIXME need new package type with raw data chunks ?
return new WatchfaceUpload.WatchfaceSendNextChunk(this.paramsProvider).serializeFileChunk(
huaweiWatchfaceManager.getCurrentChunk(),
huaweiWatchfaceManager.getCurrentUploadPosition()
);
}
}

View File

@ -1,36 +0,0 @@
package nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.requests;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiPacket;
import nodomain.freeyourgadget.gadgetbridge.devices.huawei.packets.WatchfaceUpload;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiSupportProvider;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiWatchfaceManager;
public class SendWatchfaceInfo extends Request{
HuaweiWatchfaceManager huaweiWatchfaceManager;
public SendWatchfaceInfo(HuaweiSupportProvider support,
HuaweiWatchfaceManager huaweiWatchfaceManager) {
super(support);
this.huaweiWatchfaceManager = huaweiWatchfaceManager;
this.serviceId = WatchfaceUpload.id;
this.commandId = WatchfaceUpload.WatchfaceStartSend.id;
}
@Override
protected List<byte[]> createRequest() throws RequestCreationException {
try {
return new WatchfaceUpload.WatchfaceStartSend.Request(this.paramsProvider,
huaweiWatchfaceManager.getFileSize(),
huaweiWatchfaceManager.getWatchfaceName(),
huaweiWatchfaceManager.getWatchfaceVersion()
).serialize();
} catch (HuaweiPacket.CryptoException e) {
throw new RequestCreationException(e);
}
}
}