Disable download log cleanup, handle media download before saving DownloadStatus

Might resolve problems with failed downloads being reported as successful
This commit is contained in:
daniel oeh 2013-10-04 16:14:15 +02:00
parent 7fcefdbbe7
commit 44e4924db9
5 changed files with 19 additions and 16 deletions

View File

@ -104,6 +104,7 @@
<string name="download_error_malformed_url">Malformed URL</string> <string name="download_error_malformed_url">Malformed URL</string>
<string name="download_error_io_error">IO Error</string> <string name="download_error_io_error">IO Error</string>
<string name="download_error_request_error">Request error</string> <string name="download_error_request_error">Request error</string>
<string name="download_error_db_access">Database access error</string>
<string name="downloads_left">\u0020Downloads left</string> <string name="downloads_left">\u0020Downloads left</string>
<string name="download_notification_title">Downloading podcast data</string> <string name="download_notification_title">Downloading podcast data</string>
<string name="download_report_content">%1$d downloads succeeded, %2$d failed</string> <string name="download_report_content">%1$d downloads succeeded, %2$d failed</string>

View File

@ -827,23 +827,24 @@ public class DownloadService extends Service {
} }
} }
saveDownloadStatus(status);
sendDownloadHandledIntent();
try { try {
if (chaptersRead) { if (chaptersRead) {
DBWriter.setFeedItem(DownloadService.this, media.getItem()).get(); DBWriter.setFeedItem(DownloadService.this, media.getItem()).get();
} }
DBWriter.setFeedMedia(DownloadService.this, media).get(); DBWriter.setFeedMedia(DownloadService.this, media).get();
if (!DBTasks.isInQueue(DownloadService.this, media.getItem().getId())) {
DBWriter.addQueueItem(DownloadService.this, media.getItem().getId()).get();
}
} catch (ExecutionException e) { } catch (ExecutionException e) {
e.printStackTrace(); e.printStackTrace();
status = new DownloadStatus(media, media.getEpisodeTitle(), DownloadError.ERROR_DB_ACCESS_ERROR, false, e.getMessage());
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
status = new DownloadStatus(media, media.getEpisodeTitle(), DownloadError.ERROR_DB_ACCESS_ERROR, false, e.getMessage());
} }
if (!DBTasks.isInQueue(DownloadService.this, media.getItem().getId())) { saveDownloadStatus(status);
DBWriter.addQueueItem(DownloadService.this, media.getItem().getId()); sendDownloadHandledIntent();
}
numberOfDownloads.decrementAndGet(); numberOfDownloads.decrementAndGet();
queryDownloadsAsync(); queryDownloadsAsync();

View File

@ -254,9 +254,7 @@ public class DBWriter {
PodDBAdapter adapter = new PodDBAdapter(context); PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open(); adapter.open();
adapter.setDownloadStatus(status); adapter.setDownloadStatus(status);
cleanupDownloadLog(adapter);
adapter.close(); adapter.close();
EventDistributor.getInstance().sendDownloadLogUpdateBroadcast(); EventDistributor.getInstance().sendDownloadLogUpdateBroadcast();
} }

View File

@ -558,23 +558,25 @@ public class PodDBAdapter {
db.update(TABLE_NAME_DOWNLOAD_LOG, values, KEY_ID + "=?", db.update(TABLE_NAME_DOWNLOAD_LOG, values, KEY_ID + "=?",
new String[]{String.valueOf(status.getId())}); new String[]{String.valueOf(status.getId())});
} }
return status.getId(); return status.getId();
} }
public long getDownloadLogSize() { public long getDownloadLogSize() {
Cursor result = db.rawQuery("SELECT COUNT(?) AS ? FROM ?", final String query = String.format("SELECT COUNT(%s) FROM %s", KEY_ID, TABLE_NAME_DOWNLOAD_LOG);
new String[]{KEY_ID, KEY_ID, TABLE_NAME_DOWNLOAD_LOG}); Cursor result = db.rawQuery(query, null);
long count = result.getLong(KEY_ID_INDEX); long count = 0;
if (result.moveToFirst()) {
count = result.getLong(0);
}
result.close(); result.close();
return count; return count;
} }
public void removeDownloadLogItems(long count) { public void removeDownloadLogItems(long count) {
if (count > 0) { if (count > 0) {
db.rawQuery("DELETE FROM ? ORDER BY ? ASC LIMIT ?", final String sql = String.format("DELETE FROM %s WHERE %s in (SELECT %s from %s ORDER BY %s ASC LIMIT %d)",
new String[]{TABLE_NAME_DOWNLOAD_LOG, TABLE_NAME_DOWNLOAD_LOG, KEY_ID, KEY_ID, TABLE_NAME_DOWNLOAD_LOG, KEY_COMPLETION_DATE, count);
KEY_COMPLETION_DATE, String.valueOf(count)}); db.execSQL(sql, null);
} }
} }

View File

@ -17,7 +17,8 @@ public enum DownloadError {
ERROR_HTTP_DATA_ERROR(9, R.string.download_error_http_data_error), ERROR_HTTP_DATA_ERROR(9, R.string.download_error_http_data_error),
ERROR_NOT_ENOUGH_SPACE(10, R.string.download_error_insufficient_space), ERROR_NOT_ENOUGH_SPACE(10, R.string.download_error_insufficient_space),
ERROR_UNKNOWN_HOST(11, R.string.download_error_unknown_host), ERROR_UNKNOWN_HOST(11, R.string.download_error_unknown_host),
ERROR_REQUEST_ERROR(12, R.string.download_error_request_error); ERROR_REQUEST_ERROR(12, R.string.download_error_request_error),
ERROR_DB_ACCESS_ERROR(13, R.string.download_error_db_access);
private final int code; private final int code;
private final int resId; private final int resId;