FeedManager now maintains a list of DownloadStatus entries
This commit is contained in:
parent
c41a61752f
commit
2f5e0dd023
|
@ -5,6 +5,7 @@ import java.util.ArrayList;
|
|||
import java.util.Date;
|
||||
|
||||
import de.podfetcher.activity.MediaplayerActivity;
|
||||
import de.podfetcher.service.DownloadStatus;
|
||||
import de.podfetcher.service.PlaybackService;
|
||||
import de.podfetcher.storage.*;
|
||||
import android.content.Context;
|
||||
|
@ -20,6 +21,9 @@ import android.util.Log;
|
|||
public class FeedManager {
|
||||
private static final String TAG = "FeedManager";
|
||||
|
||||
/** Number of completed Download status entries to store. */
|
||||
private static final int DOWNLOAD_LOG_SIZE = 25;
|
||||
|
||||
private static FeedManager singleton;
|
||||
|
||||
private ArrayList<Feed> feeds;
|
||||
|
@ -27,6 +31,10 @@ public class FeedManager {
|
|||
|
||||
/** Contains all items where 'read' is false */
|
||||
private ArrayList<FeedItem> unreadItems;
|
||||
|
||||
/** Contains completed Download status entries */
|
||||
private ArrayList<DownloadStatus> downloadLog;
|
||||
|
||||
private DownloadRequester requester;
|
||||
|
||||
private FeedManager() {
|
||||
|
@ -34,7 +42,7 @@ public class FeedManager {
|
|||
categories = new ArrayList<FeedCategory>();
|
||||
unreadItems = new ArrayList<FeedItem>();
|
||||
requester = DownloadRequester.getInstance();
|
||||
|
||||
downloadLog = new ArrayList<DownloadStatus>();
|
||||
}
|
||||
|
||||
public static FeedManager getInstance() {
|
||||
|
@ -99,6 +107,15 @@ public class FeedManager {
|
|||
}
|
||||
}
|
||||
|
||||
public long addDownloadStatus(Context context, DownloadStatus status) {
|
||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||
downloadLog.add(status);
|
||||
if (downloadLog.size() > DOWNLOAD_LOG_SIZE) {
|
||||
adapter.removeDownloadStatus(downloadLog.remove(0));
|
||||
}
|
||||
return adapter.setDownloadStatus(status);
|
||||
}
|
||||
|
||||
private void addNewFeed(Context context, Feed feed) {
|
||||
feeds.add(feed);
|
||||
feed.setId(setFeed(context, feed));
|
||||
|
@ -106,25 +123,18 @@ public class FeedManager {
|
|||
setFeedItem(context, item);
|
||||
}
|
||||
}
|
||||
/* TODO Decide if still useful
|
||||
|
||||
public void addFeedItem(Context context, FeedItem item) {
|
||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||
// Search list for feeditem
|
||||
Feed feed = item.getFeed();
|
||||
FeedItem foundItem = searchFeedItemByLink(feed, item.getLink());
|
||||
if (foundItem != null) {
|
||||
// Update Information
|
||||
item.id = foundItem.id;
|
||||
foundItem = item;
|
||||
item.setRead(foundItem.isRead());
|
||||
adapter.setFeedItem(item);
|
||||
} else {
|
||||
feed.getItems().add(item);
|
||||
item.id = adapter.setFeedItem(item);
|
||||
}
|
||||
}
|
||||
*/
|
||||
/*
|
||||
* TODO Decide if still useful
|
||||
*
|
||||
* public void addFeedItem(Context context, FeedItem item) { PodDBAdapter
|
||||
* adapter = new PodDBAdapter(context); // Search list for feeditem Feed
|
||||
* feed = item.getFeed(); FeedItem foundItem = searchFeedItemByLink(feed,
|
||||
* item.getLink()); if (foundItem != null) { // Update Information item.id =
|
||||
* foundItem.id; foundItem = item; item.setRead(foundItem.isRead());
|
||||
* adapter.setFeedItem(item); } else { feed.getItems().add(item); item.id =
|
||||
* adapter.setFeedItem(item); } }
|
||||
*/
|
||||
public void updateFeed(Context context, Feed newFeed) {
|
||||
// Look up feed in the feedslist
|
||||
Feed savedFeed = searchFeedByLink(newFeed.getLink());
|
||||
|
@ -240,6 +250,19 @@ public class FeedManager {
|
|||
return null;
|
||||
}
|
||||
|
||||
/** Get a FeedMedia object by the id of the Media object. */
|
||||
public FeedMedia getFeedMedia(long id) {
|
||||
for (Feed feed : feeds) {
|
||||
for (FeedItem item : feed.getItems()) {
|
||||
if (item.getMedia().getId() == id) {
|
||||
return item.getMedia();
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.w(TAG, "Couldn't find FeedMedia with id " + id);
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Reads the database */
|
||||
public void loadDBData(Context context) {
|
||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||
|
@ -327,6 +350,40 @@ public class FeedManager {
|
|||
return items;
|
||||
}
|
||||
|
||||
private void extractDownloadLogFromCursor(Context context) {
|
||||
PodDBAdapter adapter = new PodDBAdapter(context);
|
||||
adapter.open();
|
||||
Cursor logCursor = adapter.getDownloadLogCursor();
|
||||
if (logCursor.moveToFirst()) {
|
||||
do {
|
||||
long id = logCursor.getLong(logCursor
|
||||
.getColumnIndex(PodDBAdapter.KEY_ID));
|
||||
long feedfileId = logCursor.getLong(logCursor
|
||||
.getColumnIndex(PodDBAdapter.KEY_FEEDFILE));
|
||||
int feedfileType = logCursor.getInt(logCursor
|
||||
.getColumnIndex(PodDBAdapter.KEY_FEEDFILETYPE));
|
||||
FeedFile feedfile = null;
|
||||
switch (feedfileType) {
|
||||
case PodDBAdapter.FEEDFILETYPE_FEED:
|
||||
feedfile = getFeed(feedfileId);
|
||||
break;
|
||||
case PodDBAdapter.FEEDFILETYPE_FEEDIMAGE:
|
||||
feedfile = getFeedImage(feedfileId);
|
||||
break;
|
||||
case PodDBAdapter.FEEDFILETYPE_FEEDMEDIA:
|
||||
feedfile = getFeedMedia(feedfileId);
|
||||
}
|
||||
if (feedfile != null) { // otherwise ignore status
|
||||
boolean successful = logCursor.getInt(logCursor.getColumnIndex(PodDBAdapter.KEY_SUCCESSFUL)) > 0;
|
||||
int reason = logCursor.getInt(logCursor.getColumnIndex(PodDBAdapter.KEY_REASON));
|
||||
Date completionDate = new Date(logCursor.getLong(logCursor.getColumnIndex(PodDBAdapter.KEY_COMPLETION_DATE)));
|
||||
downloadLog.add(new DownloadStatus(id, feedfile, successful, reason, completionDate));
|
||||
}
|
||||
|
||||
} while (logCursor.moveToNext());
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<Feed> getFeeds() {
|
||||
return feeds;
|
||||
}
|
||||
|
@ -335,6 +392,4 @@ public class FeedManager {
|
|||
return unreadItems;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -63,8 +63,6 @@ public class DownloadService extends Service {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
queryDownloads();
|
||||
|
@ -148,7 +146,8 @@ public class DownloadService extends Service {
|
|||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
int status = -1;
|
||||
|
||||
boolean successful = false;
|
||||
int reason = 0;
|
||||
Log.d(TAG, "Received 'Download Complete' - message.");
|
||||
long downloadId = intent.getLongExtra(
|
||||
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
|
||||
|
@ -160,10 +159,10 @@ public class DownloadService extends Service {
|
|||
status = c.getInt(c
|
||||
.getColumnIndex(DownloadManager.COLUMN_STATUS));
|
||||
}
|
||||
FeedFile download = requester.getFeedFile(downloadId);
|
||||
if (download != null) {
|
||||
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
||||
|
||||
if (status == DownloadManager.STATUS_SUCCESSFUL) {
|
||||
FeedFile download = requester.getFeedFile(downloadId);
|
||||
if (download != null) {
|
||||
if (download.getClass() == Feed.class) {
|
||||
handleCompletedFeedDownload(context, (Feed) download);
|
||||
} else if (download.getClass() == FeedImage.class) {
|
||||
|
@ -173,16 +172,18 @@ public class DownloadService extends Service {
|
|||
handleCompletedFeedMediaDownload(context,
|
||||
(FeedMedia) download);
|
||||
}
|
||||
successful = true;
|
||||
queryDownloads();
|
||||
} else if (status == DownloadManager.STATUS_FAILED) {
|
||||
reason = c.getInt(c
|
||||
.getColumnIndex(DownloadManager.COLUMN_REASON));
|
||||
Log.d(TAG, "reason code is " + reason);
|
||||
successful = false;
|
||||
}
|
||||
queryDownloads();
|
||||
} else if (status == DownloadManager.STATUS_FAILED) {
|
||||
int reason = c.getInt(c
|
||||
.getColumnIndex(DownloadManager.COLUMN_REASON));
|
||||
Log.d(TAG, "reason code is " + reason);
|
||||
|
||||
manager.addDownloadStatus(context, new DownloadStatus(download,
|
||||
reason, successful));
|
||||
c.close();
|
||||
}
|
||||
|
||||
c.close();
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
@ -1,48 +1,90 @@
|
|||
package de.podfetcher.service;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import de.podfetcher.feed.FeedFile;
|
||||
|
||||
/** Contains status attributes for one download*/
|
||||
/** Contains status attributes for one download */
|
||||
public class DownloadStatus {
|
||||
|
||||
protected FeedFile feedfile;
|
||||
protected int progressPercent;
|
||||
protected long soFar;
|
||||
protected long size;
|
||||
protected int statusMsg;
|
||||
protected int reason;
|
||||
protected boolean successful;
|
||||
protected boolean done;
|
||||
public Date getCompletionDate() {
|
||||
return completionDate;
|
||||
}
|
||||
|
||||
public DownloadStatus(FeedFile feedfile) {
|
||||
this.feedfile = feedfile;
|
||||
}
|
||||
/** Unique id for storing the object in database. */
|
||||
protected long id;
|
||||
|
||||
public FeedFile getFeedFile() {
|
||||
return feedfile;
|
||||
}
|
||||
protected FeedFile feedfile;
|
||||
protected int progressPercent;
|
||||
protected long soFar;
|
||||
protected long size;
|
||||
protected int statusMsg;
|
||||
protected int reason;
|
||||
protected boolean successful;
|
||||
protected boolean done;
|
||||
protected Date completionDate;
|
||||
|
||||
public int getProgressPercent() {
|
||||
return progressPercent;
|
||||
}
|
||||
public DownloadStatus(FeedFile feedfile) {
|
||||
this.feedfile = feedfile;
|
||||
}
|
||||
|
||||
public long getSoFar() {
|
||||
return soFar;
|
||||
}
|
||||
/** Constructor for restoring Download status entries from DB. */
|
||||
public DownloadStatus(long id, FeedFile feedfile, boolean successful, int reason,
|
||||
Date completionDate) {
|
||||
this.id = id;
|
||||
this.feedfile = feedfile;
|
||||
progressPercent = 100;
|
||||
soFar = 0;
|
||||
size = 0;
|
||||
this.reason = reason;
|
||||
this.successful = successful;
|
||||
this.done = true;
|
||||
this.completionDate = completionDate;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public int getStatusMsg() {
|
||||
return statusMsg;
|
||||
}
|
||||
/** Constructor for creating new completed downloads. */
|
||||
public DownloadStatus(FeedFile feedfile, int reason,
|
||||
boolean successful) {
|
||||
this(0, feedfile, successful, reason, new Date());
|
||||
}
|
||||
|
||||
public FeedFile getFeedFile() {
|
||||
return feedfile;
|
||||
}
|
||||
|
||||
public int getProgressPercent() {
|
||||
return progressPercent;
|
||||
}
|
||||
|
||||
public long getSoFar() {
|
||||
return soFar;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public int getStatusMsg() {
|
||||
return statusMsg;
|
||||
}
|
||||
|
||||
public int getReason() {
|
||||
return reason;
|
||||
}
|
||||
|
||||
public boolean isSuccessful() {
|
||||
return successful;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public int getReason() {
|
||||
return reason;
|
||||
}
|
||||
|
||||
public boolean isSuccessful() {
|
||||
return successful;
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ import de.podfetcher.feed.FeedCategory;
|
|||
import de.podfetcher.feed.FeedImage;
|
||||
import de.podfetcher.feed.FeedItem;
|
||||
import de.podfetcher.feed.FeedMedia;
|
||||
import de.podfetcher.service.DownloadStatus;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
|
@ -42,13 +43,18 @@ public class PodDBAdapter {
|
|||
public static final String KEY_MEDIA = "media";
|
||||
public static final String KEY_DOWNLOADED = "downloaded";
|
||||
public static final String KEY_LASTUPDATE = "last_update";
|
||||
|
||||
public static final String KEY_FEEDFILE = "feedfile";
|
||||
public static final String KEY_REASON = "reason";
|
||||
public static final String KEY_SUCCESSFUL = "successful";
|
||||
public static final String KEY_FEEDFILETYPE = "feedfile_type";
|
||||
public static final String KEY_COMPLETION_DATE = "completion_date";
|
||||
// Table names
|
||||
public static final String TABLE_NAME_FEEDS = "Feeds";
|
||||
public static final String TABLE_NAME_FEED_ITEMS = "FeedItems";
|
||||
public static final String TABLE_NAME_FEED_CATEGORIES = "FeedCategories";
|
||||
public static final String TABLE_NAME_FEED_IMAGES = "FeedImages";
|
||||
public static final String TABLE_NAME_FEED_MEDIA = "FeedMedia";
|
||||
public static final String TABLE_NAME_DOWNLOAD_LOG = "DownloadLog";
|
||||
|
||||
// SQL Statements for creating new tables
|
||||
private static final String TABLE_PRIMARY_KEY = KEY_ID
|
||||
|
@ -81,6 +87,20 @@ public class PodDBAdapter {
|
|||
+ KEY_MIME_TYPE + " TEXT," + KEY_FILE_URL + " TEXT,"
|
||||
+ KEY_DOWNLOAD_URL + " TEXT," + KEY_DOWNLOADED + " INTEGER)";
|
||||
|
||||
private static final String CREATE_TABLE_DOWNLOAD_LOG = "CREATE TABLE "
|
||||
+ TABLE_NAME_DOWNLOAD_LOG + " (" + TABLE_PRIMARY_KEY + KEY_FEEDFILE
|
||||
+ " INTEGER," + KEY_FEEDFILETYPE + " INTEGER," + KEY_REASON
|
||||
+ " INTEGER," + KEY_SUCCESSFUL + " INTEGER," + KEY_COMPLETION_DATE
|
||||
+ " INTEGER)";
|
||||
|
||||
/**
|
||||
* Used for storing download status entries to determine the type of the
|
||||
* Feedfile.
|
||||
*/
|
||||
public static final int FEEDFILETYPE_FEED = 0;
|
||||
public static final int FEEDFILETYPE_FEEDIMAGE = 1;
|
||||
public static final int FEEDFILETYPE_FEEDMEDIA = 2;
|
||||
|
||||
private SQLiteDatabase db;
|
||||
private final Context context;
|
||||
private PodDBHelper helper;
|
||||
|
@ -246,6 +266,41 @@ public class PodDBAdapter {
|
|||
return item.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts or updates a download status.
|
||||
* */
|
||||
public long setDownloadStatus(DownloadStatus status) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(KEY_FEEDFILE, status.getFeedFile().getId());
|
||||
if (status.getFeedFile().getClass() == Feed.class) {
|
||||
values.put(KEY_FEEDFILETYPE, FEEDFILETYPE_FEED);
|
||||
} else if (status.getFeedFile().getClass() == FeedImage.class) {
|
||||
values.put(KEY_FEEDFILETYPE, FEEDFILETYPE_FEEDIMAGE);
|
||||
} else if (status.getFeedFile().getClass() == FeedMedia.class) {
|
||||
values.put(KEY_FEEDFILETYPE, FEEDFILETYPE_FEEDMEDIA);
|
||||
}
|
||||
|
||||
values.put(KEY_REASON, status.getReason());
|
||||
values.put(KEY_SUCCESSFUL, status.isSuccessful());
|
||||
values.put(KEY_COMPLETION_DATE, status.getCompletionDate().getTime());
|
||||
open();
|
||||
if (status.getId() == 0) {
|
||||
status.setId(db.insert(TABLE_NAME_DOWNLOAD_LOG, null, values));
|
||||
} else {
|
||||
db.update(TABLE_NAME_DOWNLOAD_LOG, values, KEY_ID + "=?",
|
||||
new String[] { String.valueOf(status.getId()) });
|
||||
}
|
||||
close();
|
||||
return status.getId();
|
||||
}
|
||||
|
||||
public void removeDownloadStatus(DownloadStatus remove) {
|
||||
open();
|
||||
db.delete(TABLE_NAME_DOWNLOAD_LOG, KEY_ID + "=?",
|
||||
new String[] { String.valueOf(remove.getId()) });
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all Categories from the Categories Table.
|
||||
*
|
||||
|
@ -315,6 +370,13 @@ public class PodDBAdapter {
|
|||
return c;
|
||||
}
|
||||
|
||||
public final Cursor getDownloadLogCursor() {
|
||||
open();
|
||||
Cursor c = db.query(TABLE_NAME_DOWNLOAD_LOG, null, null, null, null,
|
||||
null, null);
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a FeedMedia object from the Database.
|
||||
*
|
||||
|
@ -393,6 +455,7 @@ public class PodDBAdapter {
|
|||
db.execSQL(CREATE_TABLE_FEED_CATEGORIES);
|
||||
db.execSQL(CREATE_TABLE_FEED_IMAGES);
|
||||
db.execSQL(CREATE_TABLE_FEED_MEDIA);
|
||||
db.execSQL(CREATE_TABLE_DOWNLOAD_LOG);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -403,4 +466,5 @@ public class PodDBAdapter {
|
|||
// TODO delete Database
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue