fix offline preparations to actually happen in background

This commit is contained in:
Andrew Dolgov 2012-09-18 14:59:17 +04:00
parent cc1b9affb4
commit 469169c00d

View File

@ -198,7 +198,9 @@ 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 {
@ -226,18 +228,24 @@ public class OfflineDownloadService extends Service {
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) {
if (content != null) {
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());