From 77ae4c42e496b92503d69faea88458a9e7580b4e Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 22 Jan 2023 11:06:02 +0100 Subject: [PATCH] Cloud sync: added data wipe --- .../java/eu/faircode/email/CloudSync.java | 22 ++++++++++++++----- .../faircode/email/FragmentOptionsBackup.java | 7 ++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/CloudSync.java b/app/src/main/java/eu/faircode/email/CloudSync.java index 5fca091bf6..cdc91eac47 100644 --- a/app/src/main/java/eu/faircode/email/CloudSync.java +++ b/app/src/main/java/eu/faircode/email/CloudSync.java @@ -84,7 +84,7 @@ public class CloudSync { JSONObject jrequest = new JSONObject(); - EntityLog.log(context, EntityLog.Type.Cloud, "Cloud command=" + command); + EntityLog.log(context, EntityLog.Type.Cloud, "Cloud request=" + command); if ("sync".equals(command)) { long lrevision = prefs.getLong("cloud_lrevision", 0); EntityLog.log(context, EntityLog.Type.Cloud, @@ -155,13 +155,22 @@ public class CloudSync { receiveRemoteData(context, user, password, lrevision - 1, rrevision, jstatus); } else throw new IllegalArgumentException("Expected one status item"); + + prefs.edit().putLong("cloud_last_sync", new Date().getTime()).apply(); + } else if ("wipe".equals(command)) { + jrequest.put("items", new JSONArray()); + JSONObject jresponse = call(context, user, password, "keys", jrequest); + JSONArray jitems = jresponse.getJSONArray("items"); + for (int i = 0; i < jitems.length(); i++) + jitems.getJSONObject(i).put("val", null); + jrequest.put("items", jitems); + call(context, user, password, "write", jrequest); // wipe data + call(context, user, password, "wipe", jrequest); // wipe account } else { JSONArray jitems = new JSONArray(); jrequest.put("items", jitems); call(context, user, password, command, jrequest); } - - prefs.edit().putLong("cloud_last_sync", new Date().getTime()).apply(); } private static Long updateSyncdata(Context context) throws IOException, JSONException { @@ -599,7 +608,7 @@ public class CloudSync { JSONArray jitems = jrequest.getJSONArray("items"); for (int i = 0; i < jitems.length(); i++) { JSONObject jitem = jitems.getJSONObject(i); - long revision = jitem.getLong("rev"); + long revision = jitem.optLong("rev", 0); String k = jitem.getString("key"); String v = null; @@ -668,7 +677,7 @@ public class CloudSync { JSONArray jitems = jresponse.getJSONArray("items"); for (int i = 0; i < jitems.length(); i++) { JSONObject jitem = jitems.getJSONObject(i); - long revision = jitem.getLong("rev"); + long revision = jitem.optLong("rev", 0); String k = jitem.getString("key"); String v = null; @@ -757,6 +766,7 @@ public class CloudSync { } private static List partition(JSONArray jarray) throws JSONException { + Log.i("Cloud batch size=" + jarray.length() + "/" + BATCH_SIZE); if (jarray.length() <= BATCH_SIZE) return Arrays.asList(jarray); @@ -771,6 +781,8 @@ public class CloudSync { jpartitions.add(jpartition); } + Log.i("Cloud partitions=" + jpartitions.size()); + if (count != jarray.length()) throw new IllegalArgumentException("Partition error size=" + count + "/" + jarray.length()); diff --git a/app/src/main/java/eu/faircode/email/FragmentOptionsBackup.java b/app/src/main/java/eu/faircode/email/FragmentOptionsBackup.java index 0f320cd705..0d076f0fd2 100644 --- a/app/src/main/java/eu/faircode/email/FragmentOptionsBackup.java +++ b/app/src/main/java/eu/faircode/email/FragmentOptionsBackup.java @@ -242,7 +242,7 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere Helper.linkPro(tvExportPro); cardCloud.setVisibility( BuildConfig.DEBUG && - Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && + Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !TextUtils.isEmpty(BuildConfig.CLOUD_URI) ? View.VISIBLE : View.GONE); Helper.linkPro(tvCloudPro); @@ -1511,8 +1511,6 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere } private void cloud(Bundle args) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); - new SimpleTask() { @Override protected void onPreExecute(Bundle args) { @@ -1536,7 +1534,7 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere command = "logout"; throw ex; } finally { - if ("logout".equals(command)) { + if ("logout".equals(command) || "wipe".equals(command)) { prefs.edit() .remove("cloud_revision") .remove("cloud_user") @@ -1555,7 +1553,6 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere return null; } - @Override protected void onExecuted(Bundle args, Void data) { view.post(new Runnable() {