1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-03 02:06:21 +02:00

Xiaomi: run data upload service finalization on respective queue

This commit is contained in:
MrYoranimo 2024-01-05 16:49:55 +01:00 committed by José Rebelo
parent d217a0b15f
commit 53a7cc5b30
7 changed files with 80 additions and 39 deletions

View File

@ -36,6 +36,7 @@ import nodomain.freeyourgadget.gadgetbridge.proto.xiaomi.XiaomiProto;
import nodomain.freeyourgadget.gadgetbridge.service.btle.AbstractBTLEDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btle.BtLEQueue;
import nodomain.freeyourgadget.gadgetbridge.service.btle.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.PlainAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.SetProgressAction;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@ -241,12 +242,12 @@ public class XiaomiBleSupport extends XiaomiConnectionSupport {
}
@Override
public void onUploadProgress(int textRsrc, int progressPercent) {
public void onUploadProgress(int textRsrc, int progressPercent, boolean ongoing) {
try {
final TransactionBuilder builder = commsSupport.createTransactionBuilder("send data upload progress");
builder.add(new SetProgressAction(
commsSupport.getContext().getString(textRsrc),
true,
ongoing,
progressPercent,
commsSupport.getContext()
));
@ -261,6 +262,19 @@ public class XiaomiBleSupport extends XiaomiConnectionSupport {
return commsSupport.connect();
}
@Override
public void runOnQueue(String taskName, Runnable runnable) {
final TransactionBuilder b = commsSupport.createTransactionBuilder("run task " + taskName + " on queue");
b.add(new PlainAction() {
@Override
public boolean run(BluetoothGatt gatt) {
runnable.run();
return true;
}
});
b.queue(commsSupport.getQueue());
}
@Override
public void dispose() {
commsSupport.dispose();

View File

@ -27,7 +27,8 @@ import nodomain.freeyourgadget.gadgetbridge.proto.xiaomi.XiaomiProto;
public abstract class XiaomiConnectionSupport {
public abstract boolean connect();
public abstract void onAuthSuccess();
public abstract void onUploadProgress(int textRsrc, int progressPercent);
public abstract void onUploadProgress(int textRsrc, int progressPercent, boolean ongoing);
public abstract void runOnQueue(String taskName, Runnable run);
public abstract void dispose();
public abstract void setContext(final GBDevice device, final BluetoothAdapter adapter, final Context context);
public abstract void disconnect();

View File

@ -45,6 +45,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.proto.xiaomi.XiaomiProto;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.AbstractBTBRDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.TransactionBuilder;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.PlainAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.SetDeviceStateAction;
import nodomain.freeyourgadget.gadgetbridge.service.btbr.actions.SetProgressAction;
@ -110,12 +111,12 @@ public class XiaomiSppSupport extends XiaomiConnectionSupport {
}
@Override
public void onUploadProgress(final int textRsrc, final int progressPercent) {
public void onUploadProgress(final int textRsrc, final int progressPercent, final boolean ongoing) {
try {
final TransactionBuilder builder = commsSupport.createTransactionBuilder("send data upload progress");
builder.add(new SetProgressAction(
commsSupport.getContext().getString(textRsrc),
true,
ongoing,
progressPercent,
commsSupport.getContext()
));
@ -125,6 +126,24 @@ public class XiaomiSppSupport extends XiaomiConnectionSupport {
}
}
@Override
public void runOnQueue(String taskName, Runnable runnable) {
if (commsSupport == null) {
LOG.error("commsSupport is null, unable to queue task");
return;
}
final TransactionBuilder b = commsSupport.createTransactionBuilder("run task " + taskName + " on queue");
b.add(new PlainAction() {
@Override
public boolean run(BluetoothSocket socket) {
runnable.run();
return true;
}
});
b.queue(commsSupport.getQueue());
}
@Override
public void setContext(GBDevice device, BluetoothAdapter adapter, Context context) {
this.commsSupport.setContext(device, adapter, context);

View File

@ -168,15 +168,6 @@ public class XiaomiSupport extends AbstractDeviceSupport {
return false;
}
public void onUploadProgress(int textRsrc, int progressPercent) {
if (getConnectionSpecificSupport() == null) {
LOG.error("onUploadProgress called but connection specific unavailable");
return;
}
getConnectionSpecificSupport().onUploadProgress(textRsrc, progressPercent);
}
@Override
public void dispose() {
if (getConnectionSpecificSupport() != null) {

View File

@ -545,8 +545,10 @@ public class XiaomiNotificationService extends AbstractXiaomiService implements
@Override
public void onUploadFinish(final boolean success) {
LOG.debug("Notification icon upload finished: {}", success);
getSupport().getDataUploadService().setCallback(null);
getSupport().getConnectionSpecificSupport().runOnQueue(
"notification icon upload finish",
() -> getSupport().getDataUploadService().setCallback(null)
);
}
@Override

View File

@ -959,21 +959,28 @@ public class XiaomiSystemService extends AbstractXiaomiService implements Xiaomi
public void onUploadFinish(final boolean success) {
LOG.debug("Firmware upload finished: {}", success);
getSupport().getDataUploadService().setCallback(null);
if (getSupport().getConnectionSpecificSupport() != null) {
getSupport().getConnectionSpecificSupport().runOnQueue("firmware upload finish", () -> {
getSupport().getDataUploadService().setCallback(null);
final String notificationMessage = success ?
getSupport().getContext().getString(R.string.updatefirmwareoperation_update_complete) :
getSupport().getContext().getString(R.string.updatefirmwareoperation_write_failed);
final int notificationMessage = success ?
R.string.updatefirmwareoperation_update_complete :
R.string.updatefirmwareoperation_write_failed;
GB.updateInstallNotification(notificationMessage, false, 100, getSupport().getContext());
onUploadProgress(notificationMessage, 100, false);
unsetDeviceBusy();
unsetDeviceBusy();
fwHelper = null;
fwHelper = null;
});
}
}
@Override
public void onUploadProgress(final int progressPercent) {
getSupport().onUploadProgress(R.string.updatefirmwareoperation_update_in_progress, progressPercent);
onUploadProgress(R.string.updatefirmwareoperation_update_in_progress, progressPercent, true);
}
public void onUploadProgress(final int stringResource, final int progressPercent, final boolean ongoing) {
getSupport().getConnectionSpecificSupport().onUploadProgress(stringResource, progressPercent, ongoing);
}
}

View File

@ -228,29 +228,36 @@ public class XiaomiWatchfaceService extends AbstractXiaomiService implements Xia
@Override
public void onUploadFinish(final boolean success) {
LOG.debug("Watchface upload finished: {}", success);
final int notificationMessage = success ?
R.string.uploadwatchfaceoperation_complete :
R.string.uploadwatchfaceoperation_failed;
getSupport().getDataUploadService().setCallback(null);
onUploadProgress(notificationMessage, 100, false);
final String notificationMessage = success ?
getSupport().getContext().getString(R.string.uploadwatchfaceoperation_complete) :
getSupport().getContext().getString(R.string.uploadwatchfaceoperation_failed);
if (getSupport().getConnectionSpecificSupport() != null) {
getSupport().getConnectionSpecificSupport().runOnQueue("watchface upload finish", () -> {
LOG.debug("Watchface upload finished: {}", success);
getSupport().getDataUploadService().setCallback(null);
unsetDeviceBusy();
GB.updateInstallNotification(notificationMessage, false, 100, getSupport().getContext());
if (success) {
setWatchface(fwHelper.getId());
requestWatchfaceList();
}
unsetDeviceBusy();
if (success) {
setWatchface(fwHelper.getId());
requestWatchfaceList();
fwHelper = null;
});
}
fwHelper = null;
}
@Override
public void onUploadProgress(final int progressPercent) {
getSupport().onUploadProgress(R.string.uploadwatchfaceoperation_in_progress, progressPercent);
onUploadProgress(R.string.uploadwatchfaceoperation_in_progress, progressPercent, true);
}
private void onUploadProgress(final int stringResource, final int progressPercent, final boolean ongoing) {
if (getSupport().getConnectionSpecificSupport() != null)
getSupport().getConnectionSpecificSupport().onUploadProgress(stringResource, progressPercent, ongoing);
}
private void setDeviceBusy() {