diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-07-13 12:23:47 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-07-13 12:23:47 +0200 |
commit | ba2d2afbbc6cbb79fc75493703425b5d6d040530 (patch) | |
tree | e731a1209160e8224679cb238c0a964c3e757590 /src/de/danoeh/antennapod/storage | |
parent | 1ae00a0f2531fdb05a44877dda88ee2300e3ffec (diff) | |
download | AntennaPod-ba2d2afbbc6cbb79fc75493703425b5d6d040530.zip |
Renamed package and application
Diffstat (limited to 'src/de/danoeh/antennapod/storage')
-rw-r--r-- | src/de/danoeh/antennapod/storage/DownloadRequester.java | 247 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/storage/PodDBAdapter.java | 591 |
2 files changed, 838 insertions, 0 deletions
diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java new file mode 100644 index 000000000..6aaabafaa --- /dev/null +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -0,0 +1,247 @@ +package de.danoeh.antennapod.storage; + +import java.util.ArrayList; +import java.io.File; +import java.util.concurrent.Callable; + +import de.danoeh.antennapod.feed.*; +import de.danoeh.antennapod.service.DownloadService; +import de.danoeh.antennapod.util.NumberGenerator; +import de.danoeh.antennapod.R; + +import android.util.Log; +import android.database.Cursor; +import android.annotation.SuppressLint; +import android.app.DownloadManager; +import android.content.Context; +import android.net.Uri; +import android.os.Messenger; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.content.ComponentName; +import android.os.Message; +import android.os.RemoteException; +import android.content.Intent; +import android.webkit.URLUtil; + +public class DownloadRequester { + private static final String TAG = "DownloadRequester"; + private static final int currentApi = android.os.Build.VERSION.SDK_INT; + + public static String EXTRA_DOWNLOAD_ID = "extra.de.danoeh.antennapod.storage.download_id"; + public static String EXTRA_ITEM_ID = "extra.de.danoeh.antennapod.storage.item_id"; + + public static String ACTION_DOWNLOAD_QUEUED = "action.de.danoeh.antennapod.storage.downloadQueued"; + + + private static boolean STORE_ON_SD = true; + public static String IMAGE_DOWNLOADPATH = "images/"; + public static String FEED_DOWNLOADPATH = "cache/"; + public static String MEDIA_DOWNLOADPATH = "media/"; + + private static DownloadRequester downloader; + private DownloadManager manager; + + public ArrayList<FeedFile> downloads; + + private DownloadRequester() { + downloads = new ArrayList<FeedFile>(); + } + + public static DownloadRequester getInstance() { + if (downloader == null) { + downloader = new DownloadRequester(); + } + return downloader; + } + + @SuppressLint("NewApi") + private long download(Context context, FeedFile item, File dest) { + if (dest.exists()) { + Log.d(TAG, "File already exists. Deleting !"); + dest.delete(); + } + Log.d(TAG, "Requesting download of url " + item.getDownload_url()); + downloads.add(item); + DownloadManager.Request request = new DownloadManager.Request( + Uri.parse(item.getDownload_url())).setDestinationUri(Uri + .fromFile(dest)); + Log.d(TAG, "Version is " + currentApi); + if (currentApi >= 11) { + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); + } else { + request.setVisibleInDownloadsUi(false); + request.setShowRunningNotification(false); + } + + // TODO Set Allowed Network Types + DownloadManager manager = (DownloadManager) context + .getSystemService(Context.DOWNLOAD_SERVICE); + + long downloadId = manager.enqueue(request); + item.setDownloadId(downloadId); + item.setFile_url(dest.toString()); + context.startService(new Intent(context, DownloadService.class)); + context.sendBroadcast(new Intent(ACTION_DOWNLOAD_QUEUED)); + return downloadId; + } + + public long downloadFeed(Context context, Feed feed) { + return download(context, feed, new File(getFeedfilePath(context), + getFeedfileName(feed))); + } + + public long downloadImage(Context context, FeedImage image) { + return download(context, image, new File(getImagefilePath(context), + getImagefileName(image))); + } + + public long downloadMedia(Context context, FeedMedia feedmedia) { + return download(context, feedmedia, + new File(getMediafilePath(context, feedmedia), + getMediafilename(feedmedia))); + } + + /** + * Cancels a running download. + * + * @param context + * A context needed to get the DownloadManager service + * @param id + * ID of the download to cancel + * */ + public void cancelDownload(final Context context, final long id) { + Log.d(TAG, "Cancelling download with id " + id); + DownloadManager dm = (DownloadManager) context + .getSystemService(Context.DOWNLOAD_SERVICE); + int removed = dm.remove(id); + if (removed > 0) { + FeedFile f = getFeedFile(id); + if (f != null) { + downloads.remove(f); + f.setFile_url(null); + f.setDownloadId(0); + } + notifyDownloadService(context); + } + } + + /** Cancels all running downloads */ + public void cancelAllDownloads(Context context) { + Log.d(TAG, "Cancelling all running downloads"); + DownloadManager dm = (DownloadManager) context + .getSystemService(Context.DOWNLOAD_SERVICE); + for (FeedFile f : downloads) { + dm.remove(f.getDownloadId()); + f.setFile_url(null); + f.setDownloadId(0); + } + downloads.clear(); + notifyDownloadService(context); + } + + /** Get a feedfile by its download id */ + public FeedFile getFeedFile(long id) { + for (FeedFile f : downloads) { + if (f.getDownloadId() == id) { + return f; + } + } + return null; + } + + /** Returns true if there is at least one Feed in the downloads queue. */ + public boolean isDownloadingFeeds() { + for (FeedFile f : downloads) { + if (f.getClass() == Feed.class) { + return true; + } + } + return false; + } + + /** Checks if feedfile is in the downloads list */ + public boolean isDownloadingFile(FeedFile item) { + for (FeedFile f : downloads) { + if (f.getDownload_url().equals(item.getDownload_url())) { + return true; + } + } + return false; + } + + /** Remove an object from the downloads-list of the requester. */ + public void removeDownload(FeedFile f) { + downloads.remove(f); + } + + public ArrayList<FeedFile> getDownloads() { + return downloads; + } + + /** Get the number of uncompleted Downloads */ + public int getNumberOfDownloads() { + return downloads.size(); + } + + public String getFeedfilePath(Context context) { + return context.getExternalFilesDir(FEED_DOWNLOADPATH).toString() + "/"; + } + + public String getFeedfileName(Feed feed) { + return "feed-" + NumberGenerator.generateLong(feed.getDownload_url()); + } + + public String getImagefilePath(Context context) { + return context.getExternalFilesDir(IMAGE_DOWNLOADPATH).toString() + "/"; + } + + public String getImagefileName(FeedImage image) { + return "image-" + NumberGenerator.generateLong(image.getDownload_url()); + } + + public String getMediafilePath(Context context, FeedMedia media) { + return context + .getExternalFilesDir( + MEDIA_DOWNLOADPATH + + media.getItem().getFeed().getTitle() + "/") + .toString(); + } + + public String getMediafilename(FeedMedia media) { + return URLUtil.guessFileName(media.getDownload_url(), null, + media.getMime_type()); + } + + /* + * ------------ Methods for communicating with the DownloadService + * ------------- + */ + private DownloadService mService = null; + private Context mContext = null; + boolean mIsBound; + + private ServiceConnection mConnection = new ServiceConnection() { + public void onServiceConnected(ComponentName className, IBinder service) { + mService = ((DownloadService.LocalBinder) service).getService(); + Log.d(TAG, "Connection to service established"); + mService.queryDownloads(); + mContext.unbindService(mConnection); + } + + public void onServiceDisconnected(ComponentName className) { + mService = null; + mIsBound = false; + mContext = null; + Log.i(TAG, "Closed connection with DownloadService."); + } + }; + + /** Notifies the DownloadService to check if there are any Downloads left */ + public void notifyDownloadService(Context context) { + context.bindService(new Intent(context, DownloadService.class), + mConnection, Context.BIND_AUTO_CREATE); + mContext = context; + mIsBound = true; + } +} diff --git a/src/de/danoeh/antennapod/storage/PodDBAdapter.java b/src/de/danoeh/antennapod/storage/PodDBAdapter.java new file mode 100644 index 000000000..c9a68717d --- /dev/null +++ b/src/de/danoeh/antennapod/storage/PodDBAdapter.java @@ -0,0 +1,591 @@ +package de.danoeh.antennapod.storage; + +import java.util.ArrayList; + +import de.danoeh.antennapod.asynctask.DownloadStatus; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedCategory; +import de.danoeh.antennapod.feed.FeedImage; +import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.feed.SimpleChapter; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.SQLException; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteDatabase.CursorFactory; +import android.database.sqlite.SQLiteOpenHelper; +import android.util.Log; + +/** + * Implements methods for accessing the database + * */ +public class PodDBAdapter { + private static final String TAG = "PodDBAdapter"; + private static final int DATABASE_VERSION = 1; + private static final String DATABASE_NAME = "Antennapod.db"; + + // Key-constants + public static final String KEY_ID = "id"; + public static final String KEY_TITLE = "title"; + public static final String KEY_NAME = "name"; + public static final String KEY_LINK = "link"; + public static final String KEY_DESCRIPTION = "description"; + public static final String KEY_FILE_URL = "file_url"; + public static final String KEY_DOWNLOAD_URL = "download_url"; + public static final String KEY_PUBDATE = "pubDate"; + public static final String KEY_READ = "read"; + public static final String KEY_DURATION = "duration"; + public static final String KEY_POSITION = "position"; + public static final String KEY_SIZE = "filesize"; + public static final String KEY_MIME_TYPE = "mime_type"; + public static final String KEY_IMAGE = "image"; + public static final String KEY_CATEGORY = "category"; + public static final String KEY_FEED = "feed"; + 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"; + public static final String KEY_FEEDITEM = "feeditem"; + public static final String KEY_CONTENT_ENCODED = "content_encoded"; + public static final String KEY_PAYMENT_LINK = "payment_link"; + public static final String KEY_START = "start"; + public static final String KEY_LANGUAGE = "language"; + public static final String KEY_AUTHOR = "author"; + + // 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"; + public static final String TABLE_NAME_QUEUE = "Queue"; + public static final String TABLE_NAME_SIMPLECHAPTERS = "SimpleChapters"; + + // SQL Statements for creating new tables + private static final String TABLE_PRIMARY_KEY = KEY_ID + + " INTEGER PRIMARY KEY AUTOINCREMENT ,"; + private static final String CREATE_TABLE_FEEDS = "CREATE TABLE " + + TABLE_NAME_FEEDS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE + + " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION + " TEXT," + + KEY_IMAGE + " INTEGER," + KEY_CATEGORY + " INTEGER," + + KEY_FILE_URL + " TEXT," + KEY_DOWNLOAD_URL + " TEXT," + + KEY_DOWNLOADED + " INTEGER," + KEY_LASTUPDATE + " TEXT," + + KEY_PAYMENT_LINK + " TEXT," + KEY_LANGUAGE + " TEXT," + + KEY_AUTHOR + " TEXT)"; + + private static final String CREATE_TABLE_FEED_ITEMS = "CREATE TABLE " + + TABLE_NAME_FEED_ITEMS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE + + " TEXT," + KEY_LINK + " TEXT," + KEY_DESCRIPTION + " TEXT," + + KEY_CONTENT_ENCODED + " TEXT," + KEY_PUBDATE + " INTEGER," + + KEY_MEDIA + " INTEGER," + KEY_FEED + " INTEGER," + KEY_READ + + " INTEGER," + KEY_PAYMENT_LINK + " TEXT)"; + + private static final String CREATE_TABLE_FEED_CATEGORIES = "CREATE TABLE " + + TABLE_NAME_FEED_CATEGORIES + " (" + TABLE_PRIMARY_KEY + KEY_NAME + + " TEXT)"; + + private static final String CREATE_TABLE_FEED_IMAGES = "CREATE TABLE " + + TABLE_NAME_FEED_IMAGES + " (" + TABLE_PRIMARY_KEY + KEY_TITLE + + " TEXT," + KEY_FILE_URL + " TEXT," + KEY_DOWNLOAD_URL + " TEXT," + + KEY_DOWNLOADED + " INTEGER)"; + + private static final String CREATE_TABLE_FEED_MEDIA = "CREATE TABLE " + + TABLE_NAME_FEED_MEDIA + " (" + TABLE_PRIMARY_KEY + KEY_DURATION + + " INTEGER," + KEY_POSITION + " INTEGER," + KEY_SIZE + " INTEGER," + + 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)"; + + private static final String CREATE_TABLE_QUEUE = "CREATE TABLE " + + TABLE_NAME_QUEUE + "(" + KEY_ID + " INTEGER PRIMARY KEY," + + KEY_FEEDITEM + " INTEGER," + KEY_FEED + " INTEGER)"; + + private static final String CREATE_TABLE_SIMPLECHAPTERS = "CREATE TABLE " + + TABLE_NAME_SIMPLECHAPTERS + " (" + TABLE_PRIMARY_KEY + KEY_TITLE + + " TEXT," + KEY_START + " INTEGER," + KEY_FEEDITEM + " 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; + + public PodDBAdapter(Context c) { + this.context = c; + helper = new PodDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION); + } + + public PodDBAdapter open() { + if (db == null || !db.isOpen() || db.isReadOnly()) { + Log.d(TAG, "Opening DB"); + try { + db = helper.getWritableDatabase(); + } catch (SQLException ex) { + ex.printStackTrace(); + db = helper.getReadableDatabase(); + } + } + return this; + } + + public void close() { + Log.d(TAG, "Closing DB"); + db.close(); + } + + /** + * Inserts or updates a feed entry + * + * @return the id of the entry + * */ + public long setFeed(Feed feed) { + ContentValues values = new ContentValues(); + values.put(KEY_TITLE, feed.getTitle()); + values.put(KEY_LINK, feed.getLink()); + values.put(KEY_DESCRIPTION, feed.getDescription()); + values.put(KEY_PAYMENT_LINK, feed.getPaymentLink()); + values.put(KEY_AUTHOR, feed.getAuthor()); + values.put(KEY_LANGUAGE, feed.getLanguage()); + if (feed.getImage() != null) { + if (feed.getImage().getId() == 0) { + setImage(feed.getImage()); + } + values.put(KEY_IMAGE, feed.getImage().getId()); + } + if (feed.getCategory() != null) { + if (feed.getCategory().getId() == 0) { + setCategory(feed.getCategory()); + } + values.put(KEY_CATEGORY, feed.getCategory().getId()); + } + values.put(KEY_FILE_URL, feed.getFile_url()); + values.put(KEY_DOWNLOAD_URL, feed.getDownload_url()); + values.put(KEY_DOWNLOADED, feed.isDownloaded()); + values.put(KEY_LASTUPDATE, feed.getLastUpdate().getTime()); + if (feed.getId() == 0) { + // Create new entry + Log.d(this.toString(), "Inserting new Feed into db"); + feed.setId(db.insert(TABLE_NAME_FEEDS, null, values)); + } else { + Log.d(this.toString(), "Updating existing Feed in db"); + db.update(TABLE_NAME_FEEDS, values, KEY_ID + "=?", + new String[] { Long.toString(feed.getId()) }); + } + return feed.getId(); + } + + /** + * Inserts or updates a category entry + * + * @return the id of the entry + * */ + public long setCategory(FeedCategory category) { + ContentValues values = new ContentValues(); + values.put(KEY_NAME, category.getName()); + if (category.getId() == 0) { + category.setId(db.insert(TABLE_NAME_FEED_CATEGORIES, null, values)); + } else { + db.update(TABLE_NAME_FEED_CATEGORIES, values, KEY_ID + "=?", + new String[] { String.valueOf(category.getId()) }); + + } + return category.getId(); + } + + /** + * Inserts or updates an image entry + * + * @return the id of the entry + * */ + public long setImage(FeedImage image) { + ContentValues values = new ContentValues(); + values.put(KEY_TITLE, image.getTitle()); + values.put(KEY_DOWNLOAD_URL, image.getDownload_url()); + values.put(KEY_DOWNLOADED, image.isDownloaded()); + values.put(KEY_FILE_URL, image.getFile_url()); + if (image.getId() == 0) { + image.setId(db.insert(TABLE_NAME_FEED_IMAGES, null, values)); + } else { + db.update(TABLE_NAME_FEED_IMAGES, values, KEY_ID + "=?", + new String[] { String.valueOf(image.getId()) }); + } + return image.getId(); + } + + /** + * Inserts or updates an image entry + * + * @return the id of the entry + */ + public long setMedia(FeedMedia media) { + ContentValues values = new ContentValues(); + values.put(KEY_DURATION, media.getDuration()); + values.put(KEY_POSITION, media.getPosition()); + values.put(KEY_SIZE, media.getSize()); + values.put(KEY_MIME_TYPE, media.getMime_type()); + values.put(KEY_DOWNLOAD_URL, media.getDownload_url()); + values.put(KEY_DOWNLOADED, media.isDownloaded()); + values.put(KEY_FILE_URL, media.getFile_url()); + if (media.getId() == 0) { + media.setId(db.insert(TABLE_NAME_FEED_MEDIA, null, values)); + } else { + db.update(TABLE_NAME_FEED_MEDIA, values, KEY_ID + "=?", + new String[] { String.valueOf(media.getId()) }); + } + return media.getId(); + } + + /** Insert all FeedItems of a feed and the feed object itself in a single transaction */ + public void setCompleteFeed(Feed feed) { + db.beginTransaction(); + setFeed(feed); + for (FeedItem item : feed.getItems()) { + setFeedItem(item); + } + db.setTransactionSuccessful(); + db.endTransaction(); + } + + public long setSingleFeedItem(FeedItem item) { + db.beginTransaction(); + long result = setFeedItem(item); + db.setTransactionSuccessful(); + db.endTransaction(); + return result; + } + + /** + * Inserts or updates a feeditem entry + * + * @return the id of the entry + */ + private long setFeedItem(FeedItem item) { + ContentValues values = new ContentValues(); + values.put(KEY_TITLE, item.getTitle()); + values.put(KEY_LINK, item.getLink()); + values.put(KEY_DESCRIPTION, item.getDescription()); + values.put(KEY_CONTENT_ENCODED, item.getContentEncoded()); + values.put(KEY_PUBDATE, item.getPubDate().getTime()); + values.put(KEY_PAYMENT_LINK, item.getPaymentLink()); + if (item.getMedia() != null) { + if (item.getMedia().getId() == 0) { + setMedia(item.getMedia()); + } + values.put(KEY_MEDIA, item.getMedia().getId()); + } + if (item.getFeed().getId() == 0) { + setFeed(item.getFeed()); + } + values.put(KEY_FEED, item.getFeed().getId()); + values.put(KEY_READ, item.isRead()); + + if (item.getId() == 0) { + Log.d(TAG, "inserting new feeditem into db"); + item.setId(db.insert(TABLE_NAME_FEED_ITEMS, null, values)); + } else { + Log.d(TAG, "updating existing feeditem in db"); + db.update(TABLE_NAME_FEED_ITEMS, values, KEY_ID + "=?", + new String[] { String.valueOf(item.getId()) }); + } + if (item.getSimpleChapters() != null) { + setSimpleChapters(item); + } + return item.getId(); + } + + public void setSimpleChapters(FeedItem item) { + ContentValues values = new ContentValues(); + for (SimpleChapter chapter : item.getSimpleChapters()) { + values.put(KEY_TITLE, chapter.getTitle()); + values.put(KEY_START, chapter.getStart()); + values.put(KEY_FEEDITEM, item.getId()); + if (chapter.getId() == 0) { + chapter.setId(db + .insert(TABLE_NAME_SIMPLECHAPTERS, null, values)); + } else { + db.update(TABLE_NAME_SIMPLECHAPTERS, values, KEY_ID + "=?", + new String[] { String.valueOf(chapter.getId()) }); + } + } + } + + /** + * Inserts or updates a download status. + * */ + public long setDownloadStatus(DownloadStatus status) { + // Don't save failed downloads + if (status.getFeedFile() != null) { + 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()); + 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()) }); + } + } + return status.getId(); + } + + public void setQueue(ArrayList<FeedItem> queue) { + ContentValues values = new ContentValues(); + db.delete(TABLE_NAME_QUEUE, null, null); + for (int i = 0; i < queue.size(); i++) { + FeedItem item = queue.get(i); + values.put(KEY_ID, i); + values.put(KEY_FEEDITEM, item.getId()); + values.put(KEY_FEED, item.getFeed().getId()); + db.insertWithOnConflict(TABLE_NAME_QUEUE, null, values, + SQLiteDatabase.CONFLICT_REPLACE); + } + } + + public void removeFeedMedia(FeedMedia media) { + db.delete(TABLE_NAME_FEED_MEDIA, KEY_ID + "=?", + new String[] { String.valueOf(media.getId()) }); + } + + public void removeFeedImage(FeedImage image) { + db.delete(TABLE_NAME_FEED_IMAGES, KEY_ID + "=?", + new String[] { String.valueOf(image.getId()) }); + } + + /** Remove a FeedItem and its FeedMedia entry. */ + public void removeFeedItem(FeedItem item) { + if (item.getMedia() != null) { + removeFeedMedia(item.getMedia()); + } + db.delete(TABLE_NAME_FEED_ITEMS, KEY_ID + "=?", + new String[] { String.valueOf(item.getId()) }); + } + + /** Remove a feed with all its FeedItems and Media entries. */ + public void removeFeed(Feed feed) { + if (feed.getImage() != null) { + removeFeedImage(feed.getImage()); + } + for (FeedItem item : feed.getItems()) { + removeFeedItem(item); + } + db.delete(TABLE_NAME_FEEDS, KEY_ID + "=?", + new String[] { String.valueOf(feed.getId()) }); + } + + public void removeDownloadStatus(DownloadStatus remove) { + db.delete(TABLE_NAME_DOWNLOAD_LOG, KEY_ID + "=?", + new String[] { String.valueOf(remove.getId()) }); + } + + /** + * Get all Categories from the Categories Table. + * + * @return The cursor of the query + * */ + public final Cursor getAllCategoriesCursor() { + open(); + Cursor c = db.query(TABLE_NAME_FEED_CATEGORIES, null, null, null, null, + null, null); + return c; + } + + /** + * Get all Feeds from the Feed Table. + * + * @return The cursor of the query + * */ + public final Cursor getAllFeedsCursor() { + open(); + Cursor c = db.query(TABLE_NAME_FEEDS, null, null, null, null, null, + null); + return c; + } + + /** + * Returns a cursor with all FeedItems of a Feed. + * + * @param feed + * The feed you want to get the FeedItems from. + * @return The cursor of the query + * */ + public final Cursor getAllItemsOfFeedCursor(final Feed feed) { + open(); + Cursor c = db + .query(TABLE_NAME_FEED_ITEMS, null, KEY_FEED + "=?", + new String[] { String.valueOf(feed.getId()) }, null, + null, null); + return c; + } + + /** + * Returns a cursor for a DB query in the FeedMedia table for a given ID. + * + * @param item + * The item you want to get the FeedMedia from + * @return The cursor of the query + * */ + public final Cursor getFeedMediaOfItemCursor(final FeedItem item) { + open(); + Cursor c = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?", + new String[] { String.valueOf(item.getMedia().getId()) }, null, + null, null); + return c; + } + + /** + * Returns a cursor for a DB query in the FeedImages table for a given ID. + * + * @param id + * ID of the FeedImage + * @return The cursor of the query + * */ + public final Cursor getImageOfFeedCursor(final long id) { + open(); + Cursor c = db.query(TABLE_NAME_FEED_IMAGES, null, KEY_ID + "=?", + new String[] { String.valueOf(id) }, null, null, null); + return c; + } + + public final Cursor getSimpleChaptersOfFeedItemCursor(final FeedItem item) { + open(); + Cursor c = db + .query(TABLE_NAME_SIMPLECHAPTERS, null, KEY_ID + "=?", + new String[] { String.valueOf(item.getId()) }, null, + null, null); + return c; + } + + public final Cursor getDownloadLogCursor() { + open(); + Cursor c = db.query(TABLE_NAME_DOWNLOAD_LOG, null, null, null, null, + null, null); + return c; + } + + public final Cursor getQueueCursor() { + open(); + Cursor c = db.query(TABLE_NAME_QUEUE, null, null, null, null, null, + null); + return c; + } + + /** + * Get a FeedMedia object from the Database. + * + * @param rowIndex + * DB Index of Media object + * @param owner + * FeedItem the Media object belongs to + * @return A newly created FeedMedia object + * */ + public final FeedMedia getFeedMedia(final long rowIndex, + final FeedItem owner) throws SQLException { + Cursor cursor = db.query(TABLE_NAME_FEED_MEDIA, null, KEY_ID + "=?", + new String[] { String.valueOf(rowIndex) }, null, null, null); + if ((cursor.getCount() == 0) || !cursor.moveToFirst()) { + throw new SQLException("No FeedMedia found at index: " + rowIndex); + } + FeedMedia media = new FeedMedia(rowIndex, owner, cursor.getInt(cursor + .getColumnIndex(KEY_DURATION)), cursor.getInt(cursor + .getColumnIndex(KEY_POSITION)), cursor.getLong(cursor + .getColumnIndex(KEY_SIZE)), cursor.getString(cursor + .getColumnIndex(KEY_MIME_TYPE)), cursor.getString(cursor + .getColumnIndex(KEY_FILE_URL)), cursor.getString(cursor + .getColumnIndex(KEY_DOWNLOAD_URL)), cursor.getInt(cursor + .getColumnIndex(KEY_DOWNLOADED)) > 0); + cursor.close(); + return media; + } + + /** + * Searches the DB for a FeedImage of the given id. + * + * @param id + * The id of the object + * @return The found object + * */ + public final FeedImage getFeedImage(final long id) throws SQLException { + Cursor cursor = this.getImageOfFeedCursor(id); + if ((cursor.getCount() == 0) || !cursor.moveToFirst()) { + throw new SQLException("No FeedImage found at index: " + id); + } + FeedImage image = new FeedImage(id, cursor.getString(cursor + .getColumnIndex(KEY_TITLE)), cursor.getString(cursor + .getColumnIndex(KEY_FILE_URL)), cursor.getString(cursor + .getColumnIndex(KEY_DOWNLOAD_URL)), cursor.getInt(cursor + .getColumnIndex(KEY_DOWNLOADED)) > 0); + cursor.close(); + return image; + } + + /** Helper class for opening the Antennapod database. */ + private static class PodDBHelper extends SQLiteOpenHelper { + + /** + * Constructor. + * + * @param context + * Context to use + * @param name + * Name of the database + * @param factory + * to use for creating cursor objects + * @param version + * number of the database + * */ + public PodDBHelper(final Context context, final String name, + final CursorFactory factory, final int version) { + super(context, name, factory, version); + } + + @Override + public void onCreate(final SQLiteDatabase db) { + db.execSQL(CREATE_TABLE_FEEDS); + db.execSQL(CREATE_TABLE_FEED_ITEMS); + db.execSQL(CREATE_TABLE_FEED_CATEGORIES); + db.execSQL(CREATE_TABLE_FEED_IMAGES); + db.execSQL(CREATE_TABLE_FEED_MEDIA); + db.execSQL(CREATE_TABLE_DOWNLOAD_LOG); + db.execSQL(CREATE_TABLE_QUEUE); + db.execSQL(CREATE_TABLE_SIMPLECHAPTERS); + } + + @Override + public void onUpgrade(final SQLiteDatabase db, final int oldVersion, + final int newVersion) { + Log.w("DBAdapter", "Upgrading from version " + oldVersion + " to " + + newVersion + "."); + // TODO delete Database + } + } + +} |