diff options
author | Stefan Niedermann <info@niedermann.it> | 2021-04-12 14:13:53 +0300 |
---|---|---|
committer | Stefan Niedermann <info@niedermann.it> | 2021-04-12 14:13:53 +0300 |
commit | 20b4163ea014968f5daa95117b4217f2e286580d (patch) | |
tree | 4b0f2c4529ca99a8763a2e954cd9297f1f585421 /app/src/main/java/it | |
parent | f4f64fb5bba6054040bfa297fc56fc01880c66f1 (diff) |
#831 Fix pruneBefore
Diffstat (limited to 'app/src/main/java/it')
6 files changed, 28 insertions, 27 deletions
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java index b7a5f3a9..f5a8f189 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClient.java @@ -18,6 +18,7 @@ import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; +import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.HashMap; @@ -90,13 +91,14 @@ public abstract class NotesClient { /** * Gets the list of notes from the server. - * @param ssoAccount Account to be used + * + * @param ssoAccount Account to be used * @param lastModified Last modified time of a former response (Unix timestamp in seconds!). All notes older than this time will be skipped. - * @param lastETag ETag of a former response. If nothing changed, the response will be 304 NOT MODIFIED. + * @param lastETag ETag of a former response. If nothing changed, the response will be 304 NOT MODIFIED. * @return list of notes * @throws Exception */ - abstract NotesResponse getNotes(SingleSignOnAccount ssoAccount, long lastModified, String lastETag) throws Exception; + abstract NotesResponse getNotes(SingleSignOnAccount ssoAccount, Calendar lastModified, String lastETag) throws Exception; abstract NoteResponse createNote(SingleSignOnAccount ssoAccount, Note note) throws Exception; @@ -111,9 +113,9 @@ public abstract class NotesClient { private final String content; private final String etag; private final String supportedApiVersions; - private final long lastModified; + private final Calendar lastModified; - ResponseData(@NonNull String content, String etag, long lastModified, @Nullable String supportedApiVersions) { + ResponseData(@NonNull String content, String etag, @NonNull Calendar lastModified, @Nullable String supportedApiVersions) { this.content = content; this.etag = etag; this.lastModified = lastModified; @@ -128,7 +130,7 @@ public abstract class NotesClient { return etag; } - public long getLastModified() { + public Calendar getLastModified() { return lastModified; } @@ -188,10 +190,11 @@ public abstract class NotesClient { etag = Objects.requireNonNull(eTagHeader.getValue()).replace("\"", ""); } - long lastModified = 0; + final Calendar lastModified = Calendar.getInstance(); + lastModified.setTimeInMillis(0); final AidlNetworkRequest.PlainHeader lastModifiedHeader = response.getPlainHeader(HEADER_KEY_LAST_MODIFIED); if (lastModifiedHeader != null) - lastModified = new Date(lastModifiedHeader.getValue()).getTime() / 1_000; + lastModified.setTimeInMillis(Date.parse(lastModifiedHeader.getValue())); Log.d(TAG, "ETag: " + etag + "; Last-Modified: " + lastModified + " (" + lastModified + ")"); String supportedApiVersions = null; diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV02.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV02.java index 2ee7a550..31a4de39 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV02.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV02.java @@ -9,6 +9,7 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount; import org.json.JSONObject; +import java.util.Calendar; import java.util.HashMap; import java.util.Map; @@ -25,9 +26,9 @@ public class NotesClientV02 extends NotesClient { super(appContext); } - NotesResponse getNotes(SingleSignOnAccount ssoAccount, long lastModified, String lastETag) throws Exception { - Map<String, String> parameter = new HashMap<>(); - parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified)); + NotesResponse getNotes(SingleSignOnAccount ssoAccount, Calendar lastModified, String lastETag) throws Exception { + final Map<String, String> parameter = new HashMap<>(); + parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified == null ? 0 : lastModified.getTimeInMillis() / 1_000)); return new NotesResponse(requestServer(ssoAccount, "notes", METHOD_GET, parameter, null, lastETag)); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java index f685b057..56661543 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesClientV1.java @@ -9,6 +9,7 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount; import org.json.JSONObject; +import java.util.Calendar; import java.util.HashMap; import java.util.Map; @@ -25,9 +26,9 @@ public class NotesClientV1 extends NotesClient { super(appContext); } - NotesResponse getNotes(SingleSignOnAccount ssoAccount, long lastModified, String lastETag) throws Exception { - Map<String, String> parameter = new HashMap<>(); - parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified)); + NotesResponse getNotes(SingleSignOnAccount ssoAccount, Calendar lastModified, String lastETag) throws Exception { + final Map<String, String> parameter = new HashMap<>(); + parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified == null ? 0 : lastModified.getTimeInMillis() / 1_000)); return new NotesResponse(requestServer(ssoAccount, "notes", METHOD_GET, parameter, null, lastETag)); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java index bc38de95..40d918df 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesDatabase.java @@ -178,15 +178,16 @@ public abstract class NotesDatabase extends RoomDatabase { /** * Inserts a note directly into the Database. - * Excerpt will be generated and {@link DBStatus#LOCAL_EDITED} will be applied. - * No Synchronisation will be triggered! Use addNoteAndSync()! + * Excerpt will be generated, {@link DBStatus#LOCAL_EDITED} will be applied in case the note has + * already has a local ID, otherwise {@link DBStatus#VOID} will be applied. + * No Synchronisation will be triggered! Use {@link #addNoteAndSync(Account, Note)}! * * @param note {@link Note} to be added. */ @NonNull @WorkerThread public Note addNote(long accountId, @NonNull Note note) { - note.setStatus(DBStatus.LOCAL_EDITED); + note.setStatus(note.getId() > 0 ? DBStatus.LOCAL_EDITED : DBStatus.VOID); note.setAccountId(accountId); note.setExcerpt(generateNoteExcerpt(note.getContent(), note.getTitle())); return getNoteDao().getNoteById(getNoteDao().addNote(note)); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesServerSyncTask.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesServerSyncTask.java index 01f86475..252490e9 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesServerSyncTask.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesServerSyncTask.java @@ -9,7 +9,6 @@ import com.nextcloud.android.sso.exceptions.TokenMismatchException; import com.nextcloud.android.sso.model.SingleSignOnAccount; import java.util.ArrayList; -import java.util.Calendar; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -164,12 +163,10 @@ abstract class NotesServerSyncTask extends Thread { Log.d(TAG, "pullRemoteChanges() for account " + localAccount.getAccountName()); try { final Map<Long, Long> idMap = db.getIdMap(localAccount.getId()); - final Calendar modified = localAccount.getModified(); - final long modifiedForServer = modified == null ? 0 : modified.getTimeInMillis() / 1_000; // FIXME re-reading the localAccount is only a workaround for a not-up-to-date eTag in localAccount. - final ServerResponse.NotesResponse response = notesClient.getNotes(ssoAccount, modifiedForServer, db.getAccountDao().getAccountById(localAccount.getId()).getETag()); - List<Note> remoteNotes = response.getNotes(); - Set<Long> remoteIDs = new HashSet<>(); + final ServerResponse.NotesResponse response = notesClient.getNotes(ssoAccount, localAccount.getModified(), db.getAccountDao().getAccountById(localAccount.getId()).getETag()); + final List<Note> remoteNotes = response.getNotes(); + final Set<Long> remoteIDs = new HashSet<>(); // pull remote changes: update or create each remote note for (Note remoteNote : remoteNotes) { Log.v(TAG, " Process Remote Note: " + remoteNote); @@ -201,9 +198,7 @@ abstract class NotesServerSyncTask extends Thread { // update ETag and Last-Modified in order to reduce size of next response localAccount.setETag(response.getETag()); - Calendar calendar = Calendar.getInstance(); - calendar.setTimeInMillis(response.getLastModified()); - localAccount.setModified(calendar); + localAccount.setModified(response.getLastModified()); db.getAccountDao().updateETag(localAccount.getId(), localAccount.getETag()); db.getAccountDao().updateModified(localAccount.getId(), localAccount.getModified().getTimeInMillis()); try { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/model/ServerResponse.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/model/ServerResponse.java index 60c4ad5e..dca53fb9 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/shared/model/ServerResponse.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/model/ServerResponse.java @@ -59,7 +59,7 @@ public class ServerResponse { return response.getETag(); } - public long getLastModified() { + public Calendar getLastModified() { return response.getLastModified(); } |