From 469169c00d79088e33e1b07dce738ee7fc883e47 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 18 Sep 2012 14:59:17 +0400 Subject: [PATCH] fix offline preparations to actually happen in background --- .../ttrss/offline/OfflineDownloadService.java | 105 +++++++++++------- 1 file changed, 65 insertions(+), 40 deletions(-) diff --git a/src/org/fox/ttrss/offline/OfflineDownloadService.java b/src/org/fox/ttrss/offline/OfflineDownloadService.java index bace2709..7ac08e7c 100644 --- a/src/org/fox/ttrss/offline/OfflineDownloadService.java +++ b/src/org/fox/ttrss/offline/OfflineDownloadService.java @@ -198,9 +198,11 @@ public class OfflineDownloadService extends Service { ApiRequest req = new ApiRequest(getApplicationContext()) { @Override - protected void onPostExecute(JsonElement content) { + protected JsonElement doInBackground(HashMap... params) { + JsonElement content = super.doInBackground(params); + if (content != null) { - + try { Type listType = new TypeToken>() {}.getType(); List feeds = new Gson().fromJson(content, listType); @@ -215,29 +217,35 @@ public class OfflineDownloadService extends Service { stmtInsert.bindString(3, feed.feed_url); stmtInsert.bindLong(4, feed.has_icon ? 1 : 0); stmtInsert.bindLong(5, feed.cat_id); - + stmtInsert.execute(); } - + stmtInsert.close(); - + Log.d(TAG, "offline: done downloading feeds"); m_articleOffset = 0; getWritableDb().execSQL("DELETE FROM articles;"); - - if (m_canProceed) { - downloadArticles(); - } else { - downloadFailed(); - } } catch (Exception e) { e.printStackTrace(); updateNotification(R.string.offline_switch_error); downloadFailed(); } + } + return content; + } + + @Override + protected void onPostExecute(JsonElement content) { + if (content != null) { + if (m_canProceed) { + downloadArticles(); + } else { + downloadFailed(); + } } else { updateNotification(getErrorMessage()); downloadFailed(); @@ -266,10 +274,10 @@ public class OfflineDownloadService extends Service { getWritableDb().execSQL("DELETE FROM categories;"); ApiRequest req = new ApiRequest(getApplicationContext()) { - @Override - protected void onPostExecute(JsonElement content) { + protected JsonElement doInBackground(HashMap... params) { + JsonElement content = super.doInBackground(params); + if (content != null) { - try { Type listType = new TypeToken>() {}.getType(); List cats = new Gson().fromJson(content, listType); @@ -289,17 +297,23 @@ public class OfflineDownloadService extends Service { Log.d(TAG, "offline: done downloading categories"); - if (m_canProceed) { - downloadFeeds(); - } else { - downloadFailed(); - } } catch (Exception e) { e.printStackTrace(); updateNotification(R.string.offline_switch_error); downloadFailed(); } - + } + + return content; + } + @Override + protected void onPostExecute(JsonElement content) { + if (content != null) { + if (m_canProceed) { + downloadFeeds(); + } else { + downloadFailed(); + } } else { updateNotification(getErrorMessage()); downloadFailed(); @@ -335,22 +349,27 @@ public class OfflineDownloadService extends Service { } public class OfflineArticlesRequest extends ApiRequest { + List
m_articles; + public OfflineArticlesRequest(Context context) { super(context); } @Override - protected void onPostExecute(JsonElement content) { + protected JsonElement doInBackground(HashMap... params) { + JsonElement content = super.doInBackground(params); + if (content != null) { + try { Type listType = new TypeToken>() {}.getType(); - List
articles = new Gson().fromJson(content, listType); + m_articles = new Gson().fromJson(content, listType); SQLiteStatement stmtInsert = getWritableDb().compileStatement("INSERT INTO articles " + "("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); - for (Article article : articles) { + for (Article article : m_articles) { String tagsString = ""; @@ -402,25 +421,12 @@ public class OfflineDownloadService extends Service { } + m_articleOffset += m_articles.size(); + + Log.d(TAG, "offline: received " + m_articles.size() + " articles; canProc=" + m_canProceed); + stmtInsert.close(); - //m_canGetMoreArticles = articles.size() == 30; - m_articleOffset += articles.size(); - - Log.d(TAG, "offline: received " + articles.size() + " articles; canProc=" + m_canProceed); - - if (m_canProceed) { - if (articles.size() == OFFLINE_SYNC_SEQ && m_articleOffset < m_syncMax) { - downloadArticles(); - } else { - downloadComplete(); - } - } else { - downloadFailed(); - } - - return; - } catch (Exception e) { updateNotification(R.string.offline_switch_error); Log.d(TAG, "offline: failed: exception when loading articles"); @@ -428,6 +434,25 @@ public class OfflineDownloadService extends Service { downloadFailed(); } + } + + return content; + } + + @Override + protected void onPostExecute(JsonElement content) { + if (content != null) { + + if (m_canProceed && m_articles != null) { + if (m_articles.size() == OFFLINE_SYNC_SEQ && m_articleOffset < m_syncMax) { + downloadArticles(); + } else { + downloadComplete(); + } + } else { + downloadFailed(); + } + } else { Log.d(TAG, "offline: failed: " + getErrorMessage()); updateNotification(getErrorMessage());