Skip to content

Commit

Permalink
Add basic options to dump and clean offline queue
Browse files Browse the repository at this point in the history
  • Loading branch information
di72nn committed Feb 7, 2020
1 parent d480210 commit 0fa9c9f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/src/main/java/fr/gaulupeau/apps/Poche/data/StorageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import androidx.core.content.ContextCompat;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import fr.gaulupeau.apps.Poche.App;

Expand Down Expand Up @@ -114,4 +119,25 @@ public static boolean deleteFile(String path) {
return new File(path).delete();
}

public static File dumpQueueData(String data) throws IOException {
if (!isExternalStorageWritable()) {
throw new IllegalStateException("External storage is not writable!");
}

String path = getExternalStoragePath() + "/"
+ "Local_changes_"
+ new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date())
+ ".txt";
File file = new File(path);

//noinspection ResultOfMethodCallIgnored: should exist either way
file.createNewFile();

try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(data);
}

return file;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,20 @@
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import fr.gaulupeau.apps.InThePoche.R;
import fr.gaulupeau.apps.Poche.App;
import fr.gaulupeau.apps.Poche.data.DbConnection;
import fr.gaulupeau.apps.Poche.data.OperationsHelper;
import fr.gaulupeau.apps.Poche.data.QueueHelper;
import fr.gaulupeau.apps.Poche.data.Settings;
import fr.gaulupeau.apps.Poche.data.StorageHelper;
import fr.gaulupeau.apps.Poche.data.dao.entities.QueueItem;
import fr.gaulupeau.apps.Poche.events.ArticlesChangedEvent;
import fr.gaulupeau.apps.Poche.events.EventHelper;
import fr.gaulupeau.apps.Poche.events.FeedsChangedEvent;
Expand Down Expand Up @@ -121,6 +126,8 @@ public void onCreate(Bundle savedInstanceState) {
setOnClickListener(R.string.pref_key_sync_syncTypes_description);
setOnClickListener(R.string.pref_key_ui_disableTouch_keyCode);
setOnClickListener(R.string.pref_key_misc_wipeDB);
setOnClickListener(R.string.pref_key_misc_localQueue_dumpToFile);
setOnClickListener(R.string.pref_key_misc_localQueue_removeFirstItem);

ListPreference themeListPreference = (ListPreference)findPreference(
getString(R.string.pref_key_ui_theme));
Expand Down Expand Up @@ -499,11 +506,81 @@ public void onClick(DialogInterface dialogInterface, int i) {
}
return true;
}
case R.string.pref_key_misc_localQueue_dumpToFile: {
dumpOfflineQueue();
return true;
}
case R.string.pref_key_misc_localQueue_removeFirstItem: {
removeFirstOfflineQueueItem();
return true;
}
}

return false;
}

private void dumpOfflineQueue() {
Activity activity = getActivity();
if (activity == null) return;

QueueHelper queueHelper = new QueueHelper(DbConnection.getSession());
List<QueueItem> items = queueHelper.getQueueItems();
if (items.isEmpty()) {
Toast.makeText(activity, R.string.misc_localQueue_empty, Toast.LENGTH_SHORT).show();
return;
}

String string = getString(R.string.misc_localQueue_dumpToFile_header,
getString(R.string.issues_url)) + "\r\n\r\n"
+ queueItemsToString(items);

try {
File file = StorageHelper.dumpQueueData(string);
Toast.makeText(activity, getString(R.string.misc_localQueue_dumpToFile_result_dumped,
file.getAbsolutePath()), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.w(TAG, "Error during dumping offline queue", e);
Toast.makeText(activity, getString(R.string.misc_localQueue_dumpToFile_result_error,
e.toString()), Toast.LENGTH_LONG).show();
}
}

private String queueItemsToString(List<QueueItem> items) {
String nl = "\r\n";
String delim = "; ";

StringBuilder sb = new StringBuilder();

sb.append("id").append(delim)
.append("action").append(delim)
.append("articleId").append(delim)
.append("extra").append(nl);

for (QueueItem item : items) {
sb.append(item.getId()).append(delim)
.append(item.getAction()).append(delim)
.append(item.getArticleId()).append(delim)
.append(item.getExtra()).append(nl);
}

return sb.toString();
}

private void removeFirstOfflineQueueItem() {
Activity activity = getActivity();
if (activity == null) return;

QueueHelper queueHelper = new QueueHelper(DbConnection.getSession());
List<QueueItem> items = queueHelper.getQueueItems();
if (items.isEmpty()) {
Toast.makeText(activity, R.string.misc_localQueue_empty, Toast.LENGTH_SHORT).show();
return;
}

queueHelper.dequeueItems(Collections.singletonList(items.get(0)));
Toast.makeText(activity, R.string.misc_localQueue_removeFirstItem_done, Toast.LENGTH_SHORT).show();
}

private void showDisableTouchSetKeyCodeDialog() {
Activity activity = getActivity();
if(activity != null) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings-preference-keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
<string name="pref_key_misc_appendWallabagMention_enabled" translatable="false">misc.appendWallabagMention.enabled</string>
<string name="pref_key_misc_handleHttpScheme" translatable="false">misc.handleHttpScheme</string>
<string name="pref_key_misc_wipeDB" translatable="false">misc.wipeDB</string>
<string name="pref_key_misc_localQueue" translatable="false">misc.localQueue</string>
<string name="pref_key_misc_localQueue_category" translatable="false">misc.localQueue.category</string>
<string name="pref_key_misc_localQueue_notice" translatable="false">misc.localQueue.notice</string>
<string name="pref_key_misc_localQueue_dumpToFile" translatable="false">misc.localQueue.dumpToFile</string>
<string name="pref_key_misc_localQueue_removeFirstItem" translatable="false">misc.localQueue.removeFirstItem</string>

<string name="pref_key_internal_preferencesVersion" translatable="false">internal.preferencesVersion</string>
<string name="pref_key_internal_firstRun" translatable="false">internal.firstRun</string>
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,21 @@
<string name="pref_desc_misc_wipeDB">Wipes all the articles from local database, also removes all not synchronized local changes and URLs</string>
<string name="pref_name_misc_wipeDB_confirmTitle">Wipe Database?</string>
<string name="pref_name_misc_wipeDB_confirmMessage">Are you sure you want to wipe the database?</string>
<string name="pref_categoryName_misc_localQueue">Local changes</string>
<string name="pref_name_misc_localQueue_notice">WARNING</string>
<string name="pref_desc_misc_localQueue_notice">Don\'t do anything here unless you know what you\'re doing</string>
<string name="pref_name_misc_localQueue_dumpToFile">Dump to file</string>
<string name="pref_desc_misc_localQueue_dumpToFile">Dumps all local changes to a txt file (you can study it yourself or attach it to a bug report)</string>
<string name="pref_name_misc_localQueue_removeFirstItem">Remove first local queue item</string>
<string name="pref_desc_misc_localQueue_removeFirstItem">Removes the first not synchronized item. May help if you get repeating error notifications during sync. Be sure to dump all local changes to file first</string>

<string name="misc_localQueue_empty">No local changes</string>
<string name="misc_localQueue_removeFirstItem_done">Done</string>
<string name="misc_localQueue_dumpToFile_result_dumped">Dumped to %s</string>
<string name="misc_localQueue_dumpToFile_result_error">Error: %s</string>
<string name="misc_localQueue_dumpToFile_header">If you experience troubles with synchronization, please report your problem here: %s</string>

<string name="issues_url" translatable="false">https://github.com/wallabag/android-app/issues</string>

<string name="connectionWizard_misc_scanQrCode">Scan a QR code</string>
<string name="connectionWizard_misc_installQrCodeScanner">Please install a QR-code scanner</string>
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,31 @@
android:title="@string/pref_name_misc_wipeDB"
android:summary="@string/pref_desc_misc_wipeDB"
android:persistent="false"/>
<PreferenceScreen
android:key="@string/pref_key_misc_localQueue"
android:persistent="false"
android:title="@string/pref_categoryName_misc_localQueue">
<PreferenceCategory
android:key="@string/pref_key_misc_localQueue_category"
android:persistent="false"
android:title="@string/pref_categoryName_misc_localQueue">
<Preference
android:key="@string/pref_key_misc_localQueue_notice"
android:persistent="false"
android:summary="@string/pref_desc_misc_localQueue_notice"
android:title="@string/pref_name_misc_localQueue_notice" />
<Preference
android:key="@string/pref_key_misc_localQueue_dumpToFile"
android:persistent="false"
android:summary="@string/pref_desc_misc_localQueue_dumpToFile"
android:title="@string/pref_name_misc_localQueue_dumpToFile" />
<Preference
android:key="@string/pref_key_misc_localQueue_removeFirstItem"
android:persistent="false"
android:summary="@string/pref_desc_misc_localQueue_removeFirstItem"
android:title="@string/pref_name_misc_localQueue_removeFirstItem" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>

0 comments on commit 0fa9c9f

Please sign in to comment.