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()) { ApiRequest req = new ApiRequest(getApplicationContext()) {
@Override @Override
protected void onPostExecute(JsonElement content) { protected JsonElement doInBackground(HashMap<String, String>... params) {
JsonElement content = super.doInBackground(params);
if (content != null) { if (content != null) {
try { try {
@ -226,18 +228,24 @@ public class OfflineDownloadService extends Service {
m_articleOffset = 0; m_articleOffset = 0;
getWritableDb().execSQL("DELETE FROM articles;"); getWritableDb().execSQL("DELETE FROM articles;");
if (m_canProceed) {
downloadArticles();
} else {
downloadFailed();
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
updateNotification(R.string.offline_switch_error); updateNotification(R.string.offline_switch_error);
downloadFailed(); downloadFailed();
} }
}
return content;
}
@Override
protected void onPostExecute(JsonElement content) {
if (content != null) {
if (m_canProceed) {
downloadArticles();
} else {
downloadFailed();
}
} else { } else {
updateNotification(getErrorMessage()); updateNotification(getErrorMessage());
downloadFailed(); downloadFailed();
@ -266,10 +274,10 @@ public class OfflineDownloadService extends Service {
getWritableDb().execSQL("DELETE FROM categories;"); getWritableDb().execSQL("DELETE FROM categories;");
ApiRequest req = new ApiRequest(getApplicationContext()) { ApiRequest req = new ApiRequest(getApplicationContext()) {
@Override protected JsonElement doInBackground(HashMap<String, String>... params) {
protected void onPostExecute(JsonElement content) { JsonElement content = super.doInBackground(params);
if (content != null) {
if (content != null) {
try { try {
Type listType = new TypeToken<List<FeedCategory>>() {}.getType(); Type listType = new TypeToken<List<FeedCategory>>() {}.getType();
List<FeedCategory> cats = new Gson().fromJson(content, listType); List<FeedCategory> cats = new Gson().fromJson(content, listType);
@ -289,17 +297,23 @@ public class OfflineDownloadService extends Service {
Log.d(TAG, "offline: done downloading categories"); Log.d(TAG, "offline: done downloading categories");
if (m_canProceed) {
downloadFeeds();
} else {
downloadFailed();
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
updateNotification(R.string.offline_switch_error); updateNotification(R.string.offline_switch_error);
downloadFailed(); downloadFailed();
} }
}
return content;
}
@Override
protected void onPostExecute(JsonElement content) {
if (content != null) {
if (m_canProceed) {
downloadFeeds();
} else {
downloadFailed();
}
} else { } else {
updateNotification(getErrorMessage()); updateNotification(getErrorMessage());
downloadFailed(); downloadFailed();
@ -335,22 +349,27 @@ public class OfflineDownloadService extends Service {
} }
public class OfflineArticlesRequest extends ApiRequest { public class OfflineArticlesRequest extends ApiRequest {
List<Article> m_articles;
public OfflineArticlesRequest(Context context) { public OfflineArticlesRequest(Context context) {
super(context); super(context);
} }
@Override @Override
protected void onPostExecute(JsonElement content) { protected JsonElement doInBackground(HashMap<String, String>... params) {
JsonElement content = super.doInBackground(params);
if (content != null) { if (content != null) {
try { try {
Type listType = new TypeToken<List<Article>>() {}.getType(); 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 " + SQLiteStatement stmtInsert = getWritableDb().compileStatement("INSERT INTO articles " +
"("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content) " + "("+BaseColumns._ID+", unread, marked, published, updated, is_updated, title, link, feed_id, tags, content) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
for (Article article : articles) { for (Article article : m_articles) {
String tagsString = ""; 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(); 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) { } catch (Exception e) {
updateNotification(R.string.offline_switch_error); updateNotification(R.string.offline_switch_error);
Log.d(TAG, "offline: failed: exception when loading articles"); Log.d(TAG, "offline: failed: exception when loading articles");
@ -428,6 +434,25 @@ public class OfflineDownloadService extends Service {
downloadFailed(); 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 { } else {
Log.d(TAG, "offline: failed: " + getErrorMessage()); Log.d(TAG, "offline: failed: " + getErrorMessage());
updateNotification(getErrorMessage()); updateNotification(getErrorMessage());