1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-10-31 06:32:54 +01:00

Huawei: clear calendar on reconnect.

This commit is contained in:
Me7c7 2024-10-26 11:50:05 +03:00
parent bc56213eba
commit 3294e20242
2 changed files with 47 additions and 25 deletions

View File

@ -1127,6 +1127,7 @@ public class HuaweiSupportProvider {
sendUserInfo();
break;
case DeviceSettingsPreferenceConst.PREF_SYNC_CALENDAR:
case DeviceSettingsPreferenceConst.PREF_CALENDAR_LOOKAHEAD_DAYS:
HuaweiP2PCalendarService.getRegisteredInstance(huaweiP2PManager).restartSynchronization();
break;
}

View File

@ -5,6 +5,7 @@ import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.Dev
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Pair;
import com.google.gson.Gson;
@ -40,6 +41,10 @@ import nodomain.freeyourgadget.gadgetbridge.util.calendar.CalendarManager;
public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
private final Logger LOG = LoggerFactory.getLogger(HuaweiP2PCalendarService.class);
public final int OPERATION_ADD = 1;
public final int OPERATION_DELETE = 2;
public final int OPERATION_UPDATE = 2;
public static final String MODULE = "hw.unitedevice.calendarapp";
@ -191,7 +196,7 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
ret.addProperty("event_uuid", eventUUID);
ret.addProperty("has_alarm", (reminders.length() == 0) ? 0 : 1);
ret.addProperty("minutes", reminders.toString());
ret.addProperty("operation", operation); // 1 - add, 2 - delete, 3 - update
ret.addProperty("operation", operation);
ret.addProperty("rrule", valueOrEmpty(calendarEvent.getRrule(),""));
// TODO: Retrieve from CalendarContract.CalendarAlerts, field state
// TODO: see handleData function command ID 3 for details
@ -219,7 +224,7 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
ret.addProperty("event_uuid", eventUUID);
ret.addProperty("has_alarm", 0);
ret.addProperty("minutes", "");
ret.addProperty("operation", 2); // 1 - add, 2 - delete
ret.addProperty("operation", OPERATION_DELETE);
ret.addProperty("rrule", "");
ret.addProperty("state", 0);
ret.addProperty("title", "");
@ -232,7 +237,7 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
JsonArray events = new JsonArray();
for (final CalendarEvent calendarEvent : calendarEvents) {
events.add(calendarEventToJson(calendarEvent, 1));
events.add(calendarEventToJson(calendarEvent, OPERATION_ADD));
}
lastCalendarEvents = calendarEvents;
@ -269,12 +274,12 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
for (final CalendarEvent calendarEvent : updatedEvents) {
LOG.info("Update: {}", prepareEventUUID(calendarEvent.getId(), calendarEvent.getBegin()));
events.add(calendarEventToJson(calendarEvent, 3));
events.add(calendarEventToJson(calendarEvent, OPERATION_UPDATE));
}
for (final CalendarEvent calendarEvent : newEvents) {
LOG.info("New: {}", prepareEventUUID(calendarEvent.getId(), calendarEvent.getBegin()));
events.add(calendarEventToJson(calendarEvent, 1));
events.add(calendarEventToJson(calendarEvent, OPERATION_ADD));
}
for (final CalendarEvent calendarEvent : removedEvents) {
@ -305,26 +310,8 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
return sendData.array();
}
private boolean sendCalendarFile(String majorVersion, short minorVersion, short scheduleCount) {
private boolean sendCalendarFile(String majorVersion, short minorVersion, JsonArray calendarData) {
LOG.info("Send calendar file upload info");
HuaweiUploadManager huaweiUploadManager = this.manager.getSupportProvider().getUploadManager();
JsonArray calendarData;
if (majorVersion == null || majorVersion.isEmpty() || lastCalendarEvents == null || minorVersion == 0) {
calendarData = getFullCalendarData();
if(calendarData.isEmpty()) {
if(minorVersion == 0 && !(majorVersion == null || majorVersion.isEmpty())) {
return false;
}
minorVersion = 0;
} else {
minorVersion++;
}
} else {
calendarData = getUpdateCalendarData();
if (calendarData.isEmpty())
return false;
}
if (majorVersion == null || majorVersion.isEmpty()) {
majorVersion = new String(this.manager.getSupportProvider().getAndroidId(), StandardCharsets.UTF_8);
@ -362,6 +349,9 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
manager.getSupportProvider().getDevice().unsetBusyTask();
manager.getSupportProvider().getDevice().sendDeviceUpdateIntent(manager.getSupportProvider().getContext());
}
if(lastCalendarEvents == null) {
scheduleUpdate(100);
}
}
@Override
@ -371,6 +361,8 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
}
});
HuaweiUploadManager huaweiUploadManager = this.manager.getSupportProvider().getUploadManager();
huaweiUploadManager.setFileUploadInfo(fileInfo);
try {
@ -382,6 +374,35 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
return true;
}
private boolean syncCalendarEvents(String majorVersion, short minorVersion, short scheduleCount) {
LOG.info("Sync calendar file upload info");
JsonArray calendarData;
if (TextUtils.isEmpty(majorVersion) || lastCalendarEvents == null || minorVersion == 0) {
if(lastCalendarEvents == null && minorVersion != 0) {
minorVersion = 0;
calendarData = new JsonArray();
} else {
calendarData = getFullCalendarData();
if (calendarData.isEmpty()) {
if (minorVersion == 0 && !TextUtils.isEmpty(majorVersion)) {
return false;
}
minorVersion = 0;
} else {
minorVersion++;
}
}
} else {
calendarData = getUpdateCalendarData();
if (calendarData.isEmpty())
return false;
}
return sendCalendarFile(majorVersion, minorVersion, calendarData);
}
@Override
public void handleData(byte[] data) {
try {
@ -424,7 +445,7 @@ public class HuaweiP2PCalendarService extends HuaweiBaseP2PService {
//external calendar synchronization only supported on Harmony devices. I don't know how to deal with this.
if (!manager.getSupportProvider().getHuaweiCoordinator().supportsExternalCalendarService()) {
if (!sendCalendarFile(majorVersion, minorVersion, scheduleCount)) {
if (!syncCalendarEvents(majorVersion, minorVersion, scheduleCount)) {
sendCalendarCmd((byte) 0x01, (byte) 0x04, null); //No sync required
}
}