#679 Lots of empty notes are created if the server is full

Signed-off-by: stefan-niedermann <info@niedermann.it>
This commit is contained in:
stefan-niedermann 2020-02-04 17:57:19 +01:00
parent eff00d25ac
commit dddf6abb71
4 changed files with 47 additions and 27 deletions

View file

@ -10,7 +10,7 @@ public enum LoginStatus {
JSON_FAILED(R.string.error_json),
PROBLEM_WITH_FILES_APP(R.string.error_files_app),
FILES_APP_VERSION_TOO_OLD(R.string.error_files_app_version_too_old),
INSUFFICIENT_STORAGE(R.string.insufficient_storage),
INSUFFICIENT_STORAGE(R.string.error_insufficient_storage),
UNKNOWN_PROBLEM(R.string.error_unknown);
@StringRes

View file

@ -0,0 +1,6 @@
package it.niedermann.owncloud.notes.model;
public class SyncResultStatus {
public LoginStatus pullStatus = LoginStatus.OK;
public LoginStatus pushStatus = LoginStatus.OK;
}

View file

@ -7,12 +7,14 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
@ -47,6 +49,7 @@ import it.niedermann.owncloud.notes.model.DBStatus;
import it.niedermann.owncloud.notes.model.ISyncCallback;
import it.niedermann.owncloud.notes.model.LocalAccount;
import it.niedermann.owncloud.notes.model.LoginStatus;
import it.niedermann.owncloud.notes.model.SyncResultStatus;
import it.niedermann.owncloud.notes.util.ExceptionUtil;
import it.niedermann.owncloud.notes.util.SSOUtil;
import it.niedermann.owncloud.notes.util.ServerResponse;
@ -143,9 +146,9 @@ public class NoteServerSyncHelper {
/**
* Synchronization is only possible, if there is an active network connection.
*
* <p>
* This method respects the user preference "Sync on Wi-Fi only".
*
* <p>
* NoteServerSyncHelper observes changes in the network connection.
* The current state can be retrieved with this method.
*
@ -276,7 +279,7 @@ public class NoteServerSyncHelper {
* SyncTask is an AsyncTask which performs the synchronization in a background thread.
* Synchronization consists of two parts: pushLocalChanges and pullRemoteChanges.
*/
private class SyncTask extends AsyncTask<Void, Void, LoginStatus> {
private class SyncTask extends AsyncTask<Void, Void, SyncResultStatus> {
private final LocalAccount localAccount;
private final SingleSignOnAccount ssoAccount;
private final boolean onlyLocalChanges;
@ -306,13 +309,13 @@ public class NoteServerSyncHelper {
}
@Override
protected LoginStatus doInBackground(Void... voids) {
protected SyncResultStatus doInBackground(Void... voids) {
Log.i(TAG, "STARTING SYNCHRONIZATION");
//db.debugPrintFullDB();
LoginStatus status = LoginStatus.OK;
pushLocalChanges();
SyncResultStatus status = new SyncResultStatus();
status.pushStatus = pushLocalChanges();
if (!onlyLocalChanges) {
status = pullRemoteChanges();
status.pullStatus = pullRemoteChanges();
}
//db.debugPrintFullDB();
Log.i(TAG, "SYNCHRONIZATION FINISHED");
@ -322,9 +325,9 @@ public class NoteServerSyncHelper {
/**
* Push local changes: for each locally created/edited/deleted Note, use NotesClient in order to push the changed to the server.
*/
private void pushLocalChanges() {
private LoginStatus pushLocalChanges() {
if (localAccount == null) {
return;
return LoginStatus.NO_NETWORK;
}
Log.d(TAG, "pushLocalChanges()");
List<DBNote> notes = db.getLocalModifiedNotes(localAccount.getId());
@ -355,7 +358,7 @@ public class NoteServerSyncHelper {
} else {
Log.v(TAG, " ...delete (only local, since it was not synchronized)");
}
// Please note, thas db.deleteNote() realizes an optimistic conflict resolution, which is required for parallel changes of this Note from the UI.
// Please note, that db.deleteNote() realizes an optimistic conflict resolution, which is required for parallel changes of this Note from the UI.
db.deleteNote(note.getId(), DBStatus.LOCAL_DELETED);
break;
default:
@ -364,15 +367,21 @@ public class NoteServerSyncHelper {
} catch (NextcloudHttpRequestFailedException e) {
if (e.getStatusCode() == 304) {
Log.d(TAG, "Server returned HTTP Status Code 304 - Not Modified");
} else if(e.getStatusCode() == 507) {
return LoginStatus.OK;
} else if (e.getStatusCode() == 507) {
exceptions.add(e);
Log.d(TAG, "Server returned HTTP Status Code 507 - Insufficient Storage");
return LoginStatus.INSUFFICIENT_STORAGE;
} else {
exceptions.add(e);
return LoginStatus.JSON_FAILED;
}
} catch (Exception e) {
exceptions.add(e);
return LoginStatus.UNKNOWN_PROBLEM;
}
}
return LoginStatus.OK;
}
/**
@ -383,7 +392,6 @@ public class NoteServerSyncHelper {
return LoginStatus.NO_NETWORK;
}
Log.d(TAG, "pullRemoteChanges() for account " + localAccount.getAccountName());
LoginStatus status;
try {
Map<Long, Long> idMap = db.getIdMap(localAccount.getId());
ServerResponse.NotesResponse response = notesClient.getNotes(ssoAccount, localAccount.getModified(), localAccount.getEtag());
@ -420,12 +428,12 @@ public class NoteServerSyncHelper {
return LoginStatus.OK;
} catch (JSONException | NullPointerException e) {
exceptions.add(e);
status = LoginStatus.JSON_FAILED;
return LoginStatus.JSON_FAILED;
} catch (NextcloudHttpRequestFailedException e) {
Log.d(TAG, "Server returned HTTP Status Code " + e.getStatusCode() + " - " + e.getMessage());
if (e.getStatusCode() == 304) {
return LoginStatus.OK;
} else if(e.getStatusCode() == 507) {
} else if (e.getStatusCode() == 507) {
exceptions.add(e);
return LoginStatus.INSUFFICIENT_STORAGE;
} else {
@ -445,22 +453,27 @@ public class NoteServerSyncHelper {
exceptions.add(e);
return LoginStatus.UNKNOWN_PROBLEM;
}
return status;
}
@Override
protected void onPostExecute(LoginStatus status) {
protected void onPostExecute(SyncResultStatus status) {
super.onPostExecute(status);
if (status != LoginStatus.OK) {
if (status.pullStatus != LoginStatus.OK || status.pushStatus != LoginStatus.OK) {
for (Throwable e : exceptions) {
Log.e(TAG, e.getMessage(), e);
}
String statusMessage = context.getApplicationContext().getString(R.string.error_sync, context.getApplicationContext().getString(status.str));
String statusMessage = context.getApplicationContext().getString(R.string.error_sync, context.getApplicationContext().getString(
// Since we can only display one snackbar at a time, let's first fix the pullStatus errors.
status.pushStatus == LoginStatus.OK
? status.pullStatus.str
: status.pushStatus.str
)
);
if (context instanceof ViewProvider && context instanceof AppCompatActivity) {
Snackbar.make(((ViewProvider) context).getView(), statusMessage, Snackbar.LENGTH_LONG)
.setAction(R.string.simple_more, v -> {
String debugInfos = ExceptionUtil.getDebugInfos((AppCompatActivity) context, exceptions);
new AlertDialog.Builder(context)
AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle(statusMessage)
.setMessage(debugInfos)
.setPositiveButton(android.R.string.copy, (a, b) -> {
@ -471,8 +484,9 @@ public class NoteServerSyncHelper {
a.dismiss();
})
.setNegativeButton(R.string.simple_close, null)
.create()
.show();
.create();
dialog.show();
((TextView) dialog.findViewById(android.R.id.message)).setTypeface(Typeface.MONOSPACE);
})
.show();
} else {

View file

@ -51,10 +51,11 @@
<!-- Error -->
<string name="error_sync">Synchronization failed: %1$s</string>
<string name="error_json">is the Notes app activated on the server?</string>
<string name="error_no_network">no network connection</string>
<string name="error_files_app">do you have installed the files app?</string>
<string name="error_unknown">An unknown error has occurred</string>
<string name="error_json">Is the Notes app activated on the server?</string>
<string name="error_no_network">No network connection</string>
<string name="error_files_app">Do you have installed the files app?</string>
<string name="error_unknown">An unknown error has occurred.</string>
<string name="error_insufficient_storage">Your server storage is full.</string>
<!-- Snackbar Actions -->
@ -154,7 +155,6 @@
<string name="category_readonly">Read only</string>
<string name="no_category">No category</string>
<string name="add_category">Add %1$s</string>
<string name="insufficient_storage">Your server is out of memory. Please delete some files to sync successfully.</string>
<!-- Array: note modes -->
<string-array name="noteMode_entries">