summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2012-09-18 14:59:17 +0400
committerAndrew Dolgov <[email protected]>2012-09-18 14:59:17 +0400
commit469169c00d79088e33e1b07dce738ee7fc883e47 (patch)
treebf4fcd7f6503fba6af986cf712e5c0fbfc7e5b81
parentcc1b9affb429a03a89263f5f26931fe4c3172ea9 (diff)
fix offline preparations to actually happen in background
-rw-r--r--src/org/fox/ttrss/offline/OfflineDownloadService.java105
1 files 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<String, String>... params) {
+ JsonElement content = super.doInBackground(params);
+
if (content != null) {
-
+
try {
Type listType = new TypeToken<List<Feed>>() {}.getType();
List<Feed> 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<String, String>... params) {
+ JsonElement content = super.doInBackground(params);
+
if (content != null) {
-
try {
Type listType = new TypeToken<List<FeedCategory>>() {}.getType();
List<FeedCategory> 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<Article> m_articles;
+
public OfflineArticlesRequest(Context context) {
super(context);
}
@Override
- protected void onPostExecute(JsonElement content) {
+ protected JsonElement doInBackground(HashMap<String, String>... params) {
+ JsonElement content = super.doInBackground(params);
+
if (content != null) {
+
try {
Type listType = new TypeToken<List<Article>>() {}.getType();
- List<Article> 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());