* Created by stefan on 20.09.15.
*/
public class NoteServerSyncHelper {
private NotesClient client = null;
private NoteSQLiteOpenHelper db = null;
private final View.OnClickListener goToSettingsListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity parent = (Activity) db.getContext();
Intent intent = new Intent(parent, SettingsActivity.class);
parent.startActivity(intent);
}
};
private int operationsCount = 0;
private int operationsFinished = 0;
private Handler handler = null;
private List callbacks = new ArrayList<>();
public NoteServerSyncHelper(NoteSQLiteOpenHelper db) {
this.db = db;
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
for (ICallback callback : callbacks) {
callback.onFinish();
}
callbacks.clear();
}
};
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(db.getContext().getApplicationContext());
String url = preferences.getString(SettingsActivity.SETTINGS_URL,
SettingsActivity.DEFAULT_SETTINGS);
String username = preferences.getString(SettingsActivity.SETTINGS_USERNAME,
SettingsActivity.DEFAULT_SETTINGS);
String password = preferences.getString(SettingsActivity.SETTINGS_PASSWORD,
SettingsActivity.DEFAULT_SETTINGS);
client = new NotesClient(url, username, password);
}
/**
* Adds a callback method to the NoteServerSyncHelper.
* All callbacks will be executed once all synchronize operations are done.
* After execution the callback will be deleted, so it has to be added again if it shall be
* executed the next time all synchronize operations are finished.
*
* @param callback Implementation of ICallback, contains one method that shall be executed.
*/
public void addCallback(ICallback callback) {
callbacks.add(callback);
}
/**
* Synchronizes Edited, New and Deleted Notes. After all changed content has been sent to the
* server, it downloads all changes that happened on the server.
*/
public void synchronize() {
uploadEditedNotes();
uploadNewNotes();
uploadDeletedNotes();
downloadNotes();
}
/**
* Helper method to check if all synchronize operations are done yet.
*/
private void asyncTaskFinished() {
operationsFinished++;
if (operationsFinished == operationsCount) {
handler.obtainMessage(1).sendToTarget();
}
}
public void uploadEditedNotes() {
List notes = db.getNotesByStatus(DBStatus.LOCAL_EDITED);
for (Note note : notes) {
UploadEditedNotesTask editedNotesTask = new UploadEditedNotesTask();
editedNotesTask.execute(note);
}
}
public void uploadNewNotes() {
List notes = db.getNotesByStatus(DBStatus.LOCAL_CREATED);
for (Note note : notes) {
UploadNewNoteTask newNotesTask = new UploadNewNoteTask();
newNotesTask.execute(note);
}
}
public void uploadDeletedNotes() {
List notes = db.getNotesByStatus(DBStatus.LOCAL_DELETED);
for (Note note : notes) {
UploadDeletedNoteTask deletedNotesTask = new UploadDeletedNoteTask();
deletedNotesTask.execute(note);
}
}
public void downloadNotes() {
DownloadNotesTask downloadNotesTask = new DownloadNotesTask();
downloadNotesTask.execute();
}
private class UploadNewNoteTask extends AsyncTask