use of org.wikipedia.readinglist.database.ReadingListPage in project apps-android-wikipedia by wikimedia.
the class SavedPageSyncService method savePages.
private int savePages(List<ReadingListPage> queue) {
int itemsTotal = queue.size();
int itemsSaved = 0;
while (!queue.isEmpty()) {
// Pick off the DB row that we'll be working on...
ReadingListPage page = queue.remove(0);
if (savedPageSyncNotification.isSyncPaused()) {
// Remaining transactions will be picked up again when the service is resumed.
break;
} else if (savedPageSyncNotification.isSyncCanceled()) {
// Mark remaining pages as online-only!
queue.add(page);
ReadingListDbHelper.instance().markPagesForOffline(queue, false);
break;
}
savedPageSyncNotification.setNotificationProgress(getApplicationContext(), itemsTotal, itemsSaved);
boolean success = false;
long totalSize = 0;
try {
// Lengthy operation during which the db state may change...
totalSize = savePageFor(page);
success = true;
} catch (InterruptedException e) {
// fall through
} catch (Exception e) {
// This can be an IOException from the storage media, or several types
// of network exceptions from malformed URLs, timeouts, etc.
e.printStackTrace();
if (!ThrowableUtil.isOffline(e) && !ThrowableUtil.isNetworkError(e)) {
// If it's anything but a transient network error, let's log it aggressively,
// to make sure we've fixed any other errors with saving pages.
L.logRemoteError(e);
}
}
if (ReadingListSyncAdapter.inProgress()) {
// Reading list sync was started in the meantime, so bail.
break;
}
if (success) {
page.status(ReadingListPage.STATUS_SAVED);
page.sizeBytes(totalSize);
ReadingListDbHelper.instance().updatePage(page);
itemsSaved++;
sendSyncEvent();
}
}
return itemsSaved;
}
use of org.wikipedia.readinglist.database.ReadingListPage in project apps-android-wikipedia by wikimedia.
the class SavedPageSyncService method onHandleWork.
@Override
protected void onHandleWork(@NonNull Intent intent) {
if (ReadingListSyncAdapter.inProgress()) {
// Reading list sync was started in the meantime, so bail.
return;
}
List<ReadingListPage> pagesToSave = ReadingListDbHelper.instance().getAllPagesToBeSaved();
List<ReadingListPage> pagesToUnsave = ReadingListDbHelper.instance().getAllPagesToBeUnsaved();
List<ReadingListPage> pagesToDelete = ReadingListDbHelper.instance().getAllPagesToBeDeleted();
boolean shouldSendSyncEvent = false;
try {
for (ReadingListPage page : pagesToDelete) {
deletePageContents(page);
}
for (ReadingListPage page : pagesToUnsave) {
deletePageContents(page);
}
} catch (Exception e) {
L.e("Error while deleting page: " + e.getMessage());
} finally {
if (!pagesToDelete.isEmpty()) {
ReadingListDbHelper.instance().purgeDeletedPages();
shouldSendSyncEvent = true;
}
if (!pagesToUnsave.isEmpty()) {
ReadingListDbHelper.instance().resetUnsavedPageStatus();
shouldSendSyncEvent = true;
}
}
int itemsTotal = pagesToSave.size();
if (itemsTotal > 0) {
shouldSendSyncEvent = true;
}
int itemsSaved = 0;
try {
itemsSaved = savePages(pagesToSave);
} finally {
if (savedPageSyncNotification.isSyncPaused()) {
savedPageSyncNotification.setNotificationPaused(getApplicationContext(), itemsTotal, itemsSaved);
} else {
savedPageSyncNotification.cancelNotification(getApplicationContext());
savedPageSyncNotification.setSyncCanceled(false);
if (shouldSendSyncEvent) {
sendSyncEvent();
}
}
}
}
use of org.wikipedia.readinglist.database.ReadingListPage in project apps-android-wikipedia by wikimedia.
the class DeveloperSettingsPreferenceLoader method createTestReadingList.
private void createTestReadingList(String listName, int numOfLists, int numOfArticles) {
int index = 0;
List<ReadingList> lists = ReadingListDbHelper.instance().getAllListsWithoutContents();
for (int i = lists.size() - 1; i >= 0; i--) {
ReadingList lastReadingList = lists.get(i);
if (lastReadingList.title().contains(listName)) {
String trimmedListTitle = lastReadingList.title().substring(listName.length()).trim();
index = (trimmedListTitle.isEmpty()) ? index : (Integer.valueOf(trimmedListTitle) > index ? Integer.valueOf(trimmedListTitle) : index);
break;
}
}
for (int i = 0; i < numOfLists; i++) {
index += 1;
ReadingList list = ReadingListDbHelper.instance().createList(listName + " " + index, "");
List<ReadingListPage> pages = new ArrayList<>();
for (int j = 0; j < numOfArticles; j++) {
PageTitle pageTitle = new PageTitle("" + (j + 1), WikipediaApp.getInstance().getWikiSite());
pages.add(new ReadingListPage(pageTitle));
}
ReadingListDbHelper.instance().addPagesToList(list, pages, true);
}
}
Aggregations