Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class SettingsActivity extends AppCompatActivity {
public static final String SETTINGS_URL = "settingsUrl";
public static final String SETTINGS_USERNAME = "settingsUsername";
public static final String SETTINGS_PASSWORD = "settingsPassword";
public static final String SETTINGS_KEY_ETAG = "notes_last_etag";
public static final String SETTINGS_KEY_LAST_MODIFIED = "notes_last_modified";
public static final String DEFAULT_SETTINGS = "";
public static final int CREDENTIALS_CHANGED = 3;

Expand Down Expand Up @@ -258,6 +260,8 @@ protected void onPostExecute(LoginStatus status) {
editor.putString(SETTINGS_URL, url);
editor.putString(SETTINGS_USERNAME, username);
editor.putString(SETTINGS_PASSWORD, password);
editor.remove(SETTINGS_KEY_ETAG);
editor.remove(SETTINGS_KEY_LAST_MODIFIED);
editor.apply();

final Intent data = new Intent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ private DBNote getNoteFromCursor(Cursor cursor) {

public void debugPrintFullDB() {
List<DBNote> notes = getNotesCustom("", new String[]{}, default_order);
Log.d(getClass().getSimpleName(), "Full Database ("+notes.size()+" notes):");
Log.v(getClass().getSimpleName(), "Full Database ("+notes.size()+" notes):");
for (DBNote note : notes) {
Log.d(getClass().getSimpleName(), " "+note);
Log.v(getClass().getSimpleName(), " "+note);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import it.niedermann.owncloud.notes.util.ICallback;
import it.niedermann.owncloud.notes.util.NotesClient;
import it.niedermann.owncloud.notes.util.NotesClientUtil.LoginStatus;
import it.niedermann.owncloud.notes.util.ServerResponse;
import it.niedermann.owncloud.notes.util.SupportUtil;

/**
Expand Down Expand Up @@ -218,15 +219,15 @@ protected void onPreExecute() {
@Override
protected LoginStatus doInBackground(Void... voids) {
client = createNotesClient(); // recreate NoteClients on every sync in case the connection settings was changed
Log.d(getClass().getSimpleName(), "STARTING SYNCHRONIZATION");
Log.i(getClass().getSimpleName(), "STARTING SYNCHRONIZATION");
//dbHelper.debugPrintFullDB();
LoginStatus status = LoginStatus.OK;
pushLocalChanges();
if(!onlyLocalChanges) {
status = pullRemoteChanges();
}
//dbHelper.debugPrintFullDB();
Log.d(getClass().getSimpleName(), "SYNCHRONIZATION FINISHED");
Log.i(getClass().getSimpleName(), "SYNCHRONIZATION FINISHED");
return status;
}

Expand All @@ -242,34 +243,34 @@ private void pushLocalChanges() {
CloudNote remoteNote=null;
switch(note.getStatus()) {
case LOCAL_EDITED:
Log.d(getClass().getSimpleName(), " ...create/edit");
Log.v(getClass().getSimpleName(), " ...create/edit");
// if note is not new, try to edit it.
if (note.getRemoteId()>0) {
Log.d(getClass().getSimpleName(), " ...try to edit");
Log.v(getClass().getSimpleName(), " ...try to edit");
try {
remoteNote = client.editNote(customCertManager, note);
remoteNote = client.editNote(customCertManager, note).getNote();
} catch(FileNotFoundException e) {
// Note does not exists anymore
}
}
// However, the note may be deleted on the server meanwhile; or was never synchronized -> (re)create
// Please note, thas dbHelper.updateNote() realizes an optimistic conflict resolution, which is required for parallel changes of this Note from the UI.
if (remoteNote == null) {
Log.d(getClass().getSimpleName(), " ...Note does not exist on server -> (re)create");
remoteNote = client.createNote(customCertManager, note);
Log.v(getClass().getSimpleName(), " ...Note does not exist on server -> (re)create");
remoteNote = client.createNote(customCertManager, note).getNote();
}
dbHelper.updateNote(note.getId(), remoteNote, note);
break;
case LOCAL_DELETED:
if(note.getRemoteId()>0) {
Log.d(getClass().getSimpleName(), " ...delete (from server and local)");
Log.v(getClass().getSimpleName(), " ...delete (from server and local)");
try {
client.deleteNote(customCertManager, note.getRemoteId());
} catch (FileNotFoundException e) {
Log.d(getClass().getSimpleName(), " ...Note does not exist on server (anymore?) -> delete locally");
Log.v(getClass().getSimpleName(), " ...Note does not exist on server (anymore?) -> delete locally");
}
} else {
Log.d(getClass().getSimpleName(), " ...delete (only local, since it was not synchronized)");
Log.v(getClass().getSimpleName(), " ...delete (only local, since it was not synchronized)");
}
// Please note, thas dbHelper.deleteNote() realizes an optimistic conflict resolution, which is required for parallel changes of this Note from the UI.
dbHelper.deleteNote(note.getId(), DBStatus.LOCAL_DELETED);
Expand All @@ -289,32 +290,57 @@ private void pushLocalChanges() {
*/
private LoginStatus pullRemoteChanges() {
Log.d(getClass().getSimpleName(), "pullRemoteChanges()");
LoginStatus status = null;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(appContext);
String lastETag = preferences.getString(SettingsActivity.SETTINGS_KEY_ETAG, null);
long lastModified = preferences.getLong(SettingsActivity.SETTINGS_KEY_LAST_MODIFIED, 0);
LoginStatus status;
try {
Map<Long, Long> idMap = dbHelper.getIdMap();
List<CloudNote> remoteNotes = client.getNotes(customCertManager);
ServerResponse.NotesResponse response = client.getNotes(customCertManager, lastModified, lastETag);
List<CloudNote> remoteNotes = response.getNotes();
Set<Long> remoteIDs = new HashSet<>();
// pull remote changes: update or create each remote note
for (CloudNote remoteNote : remoteNotes) {
Log.d(getClass().getSimpleName(), " Process Remote Note: "+remoteNote);
Log.v(getClass().getSimpleName(), " Process Remote Note: "+remoteNote);
remoteIDs.add(remoteNote.getRemoteId());
if(idMap.containsKey(remoteNote.getRemoteId())) {
Log.d(getClass().getSimpleName(), " ... found -> Update");
if(remoteNote.getModified()==null) {
Log.v(getClass().getSimpleName(), " ... unchanged");
} else if(idMap.containsKey(remoteNote.getRemoteId())) {
Log.v(getClass().getSimpleName(), " ... found -> Update");
dbHelper.updateNote(idMap.get(remoteNote.getRemoteId()), remoteNote, null);
} else {
Log.d(getClass().getSimpleName(), " ... create");
Log.v(getClass().getSimpleName(), " ... create");
dbHelper.addNote(remoteNote);
}
}
Log.d(getClass().getSimpleName(), " Remove remotely deleted Notes (only those without local changes)");
// remove remotely deleted notes (only those without local changes)
for (Map.Entry<Long, Long> entry : idMap.entrySet()) {
if(!remoteIDs.contains(entry.getKey())) {
Log.d(getClass().getSimpleName(), " ... remove "+entry.getValue());
Log.v(getClass().getSimpleName(), " ... remove "+entry.getValue());
dbHelper.deleteNote(entry.getValue(), DBStatus.VOID);
}
}
status = LoginStatus.OK;

// update ETag and Last-Modified in order to reduce size of next response
SharedPreferences.Editor editor = preferences.edit();
String etag = response.getETag();
if(etag!=null && !etag.isEmpty()) {
editor.putString(SettingsActivity.SETTINGS_KEY_ETAG, etag);
} else {
editor.remove(SettingsActivity.SETTINGS_KEY_ETAG);
}
long modified = response.getLastModified();
if(modified!=0) {
editor.putLong(SettingsActivity.SETTINGS_KEY_LAST_MODIFIED, modified);
} else {
editor.remove(SettingsActivity.SETTINGS_KEY_LAST_MODIFIED);
}
editor.apply();
} catch (ServerResponse.NotModifiedException e) {
Log.d(getClass().getSimpleName(), "No changes, nothing to do.");
status = LoginStatus.OK;
} catch (IOException e) {
Log.e(getClass().getSimpleName(), "Exception", e);
exceptions.add(e);
Expand Down
Loading