From 8f9344fbe74a6922f949ddffbc1779f539fcef3f Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 20 Jun 2012 16:02:05 +0200 Subject: [PATCH] Implemented Queue for FeedItems --- res/menu/feeditemlist.xml | 2 + res/values/strings.xml | 2 + .../podfetcher/activity/ItemviewActivity.java | 46 +++++++++++++------ src/de/podfetcher/feed/FeedManager.java | 38 ++++++++++++++- .../fragment/FeedItemlistFragment.java | 15 +++++- src/de/podfetcher/storage/PodDBAdapter.java | 32 +++++++++++++ 6 files changed, 119 insertions(+), 16 deletions(-) diff --git a/res/menu/feeditemlist.xml b/res/menu/feeditemlist.xml index e4164aeb1..400ec8d6e 100644 --- a/res/menu/feeditemlist.xml +++ b/res/menu/feeditemlist.xml @@ -6,6 +6,8 @@ + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index d2c7a50db..8f031c912 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -49,4 +49,6 @@ HTTP Data Error Unknown Error Show player + Add to Queue + Remove from Queue diff --git a/src/de/podfetcher/activity/ItemviewActivity.java b/src/de/podfetcher/activity/ItemviewActivity.java index 499457122..dde014849 100644 --- a/src/de/podfetcher/activity/ItemviewActivity.java +++ b/src/de/podfetcher/activity/ItemviewActivity.java @@ -13,6 +13,9 @@ import android.widget.ImageView; import android.widget.TextView; import com.actionbarsherlock.app.SherlockActivity; +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuInflater; +import com.actionbarsherlock.view.MenuItem; import de.podfetcher.R; import de.podfetcher.asynctask.DownloadObserver; @@ -49,14 +52,13 @@ public class ItemviewActivity extends SherlockActivity { manager = FeedManager.getInstance(); extractFeeditem(); populateUI(); - getDownloadStatus(); butDownload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { requester = DownloadRequester.getInstance(); requester.downloadMedia(v.getContext(), item.getMedia()); - getDownloadStatus(); + //getDownloadStatus(); } }); @@ -72,7 +74,7 @@ public class ItemviewActivity extends SherlockActivity { @Override public void onClick(View v) { if (manager.deleteFeedMedia(v.getContext(), item.getMedia())) { - setNotDownloadedState(); + //setNotDownloadedState(); } } @@ -123,16 +125,16 @@ public class ItemviewActivity extends SherlockActivity { webvDescription.loadData(item.getDescription(), "text/html", null); } - private void getDownloadStatus() { + private void getDownloadStatus(Menu menu) { FeedMedia media = item.getMedia(); if (media.getFile_url() == null) { - setNotDownloadedState(); + setNotDownloadedState(menu); } else if (media.isDownloaded()) { - setDownloadedState(); + setDownloadedState(menu); } else { // observe - setDownloadingState(); - downloadObserver.execute(media); + setDownloadingState(menu); + //downloadObserver.execute(media); } } @@ -147,26 +149,44 @@ public class ItemviewActivity extends SherlockActivity { protected void onPostExecute(Boolean result) { boolean r = getStatusList()[0].isSuccessful(); if (r) { - setDownloadedState(); + //setDownloadedState(); } else { - setNotDownloadedState(); + //setNotDownloadedState(); } } }; + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + MenuInflater inflater = new MenuInflater(this); + inflater.inflate(R.menu.feeditemlist, menu); + getDownloadStatus(menu); + return true; + } + + - private void setDownloadingState() { + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // TODO Auto-generated method stub + return super.onOptionsItemSelected(item); + } + + + private void setDownloadingState(Menu menu) { + butDownload.setEnabled(false); butPlay.setEnabled(false); butRemove.setEnabled(false); } - private void setDownloadedState() { + private void setDownloadedState(Menu menu) { butDownload.setEnabled(false); butPlay.setEnabled(true); butRemove.setEnabled(true); } - private void setNotDownloadedState() { + private void setNotDownloadedState(Menu menu) { butPlay.setEnabled(false); butDownload.setEnabled(true); butRemove.setEnabled(false); diff --git a/src/de/podfetcher/feed/FeedManager.java b/src/de/podfetcher/feed/FeedManager.java index cdcde55c8..506b62efc 100644 --- a/src/de/podfetcher/feed/FeedManager.java +++ b/src/de/podfetcher/feed/FeedManager.java @@ -34,6 +34,9 @@ public class FeedManager { /** Contains completed Download status entries */ private ArrayList downloadLog; + + /** Contains the queue of items to be played. */ + private ArrayList queue; private DownloadRequester requester; @@ -43,6 +46,7 @@ public class FeedManager { unreadItems = new ArrayList(); requester = DownloadRequester.getInstance(); downloadLog = new ArrayList(); + queue = new ArrayList(); } public static FeedManager getInstance() { @@ -141,6 +145,24 @@ public class FeedManager { } return adapter.setDownloadStatus(status); } + + public void addQueueItem(Context context, FeedItem item) { + PodDBAdapter adapter = new PodDBAdapter(context); + queue.add(item); + adapter.setQueue(queue); + } + + public void removeQueueItem(Context context, FeedItem item) { + boolean removed = queue.remove(item); + if (removed) { + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.setQueue(queue); + } + } + + public boolean isInQueue(FeedItem item) { + return queue.contains(item); + } private void addNewFeed(Context context, Feed feed) { feeds.add(feed); @@ -300,7 +322,6 @@ public class FeedManager { /** Reads the database */ public void loadDBData(Context context) { - PodDBAdapter adapter = new PodDBAdapter(context); updateArrays(context); } @@ -309,6 +330,7 @@ public class FeedManager { categories.clear(); extractFeedlistFromCursor(context); extractDownloadLogFromCursor(context); + extractQueueFromCursor(context); } private void extractFeedlistFromCursor(Context context) { @@ -423,6 +445,20 @@ public class FeedManager { } while (logCursor.moveToNext()); } } + + private void extractQueueFromCursor(Context context) { + PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + Cursor cursor = adapter.getQueueCursor(); + if (cursor.moveToFirst()) { + do { + int index = cursor.getInt(cursor.getColumnIndex(PodDBAdapter.KEY_ID)); + Feed feed = getFeed(cursor.getColumnIndex(PodDBAdapter.KEY_FEED)); + FeedItem item = getFeedItem(cursor.getColumnIndex(PodDBAdapter.KEY_FEEDITEM), feed); + queue.add(index, item); + } while (cursor.moveToNext()); + } + } public ArrayList getFeeds() { return feeds; diff --git a/src/de/podfetcher/fragment/FeedItemlistFragment.java b/src/de/podfetcher/fragment/FeedItemlistFragment.java index 00d55905f..a11966f53 100644 --- a/src/de/podfetcher/fragment/FeedItemlistFragment.java +++ b/src/de/podfetcher/fragment/FeedItemlistFragment.java @@ -125,7 +125,13 @@ public class FeedItemlistFragment extends SherlockListFragment { } else { menu.findItem(R.id.mark_read_item).setVisible(true); } - + + if (manager.isInQueue(selectedItem)) { + menu.findItem(R.id.remove_from_queue_item).setVisible(true); + } else { + menu.findItem(R.id.add_to_queue_item).setVisible(true); + } + return true; } @@ -155,7 +161,12 @@ public class FeedItemlistFragment extends SherlockListFragment { case R.id.mark_unread_item: manager.markItemRead(getSherlockActivity(), selectedItem, false); break; - + case R.id.add_to_queue_item: + manager.addQueueItem(getSherlockActivity(), selectedItem); + break; + case R.id.remove_from_queue_item: + manager.removeQueueItem(getSherlockActivity(), selectedItem); + break; } fila.notifyDataSetChanged(); mode.finish(); diff --git a/src/de/podfetcher/storage/PodDBAdapter.java b/src/de/podfetcher/storage/PodDBAdapter.java index 527f2b0b2..49261a743 100644 --- a/src/de/podfetcher/storage/PodDBAdapter.java +++ b/src/de/podfetcher/storage/PodDBAdapter.java @@ -1,5 +1,7 @@ package de.podfetcher.storage; +import java.util.ArrayList; + import de.podfetcher.asynctask.DownloadStatus; import de.podfetcher.feed.Feed; import de.podfetcher.feed.FeedCategory; @@ -48,6 +50,8 @@ public class PodDBAdapter { 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"; + // Table names public static final String TABLE_NAME_FEEDS = "Feeds"; public static final String TABLE_NAME_FEED_ITEMS = "FeedItems"; @@ -55,6 +59,7 @@ public class PodDBAdapter { 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"; // SQL Statements for creating new tables private static final String TABLE_PRIMARY_KEY = KEY_ID @@ -92,6 +97,11 @@ public class PodDBAdapter { + " 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)"; /** * Used for storing download status entries to determine the type of the @@ -115,6 +125,7 @@ public class PodDBAdapter { try { db = helper.getWritableDatabase(); } catch (SQLException ex) { + ex.printStackTrace(); db = helper.getReadableDatabase(); } } @@ -294,6 +305,20 @@ public class PodDBAdapter { return status.getId(); } + public void setQueue(ArrayList queue) { + ContentValues values = new ContentValues(); + open(); + 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); + } + close(); + } + public void removeFeedMedia(FeedMedia media) { open(); db.delete(TABLE_NAME_FEED_MEDIA, KEY_ID + "=?", new String[] {String.valueOf(media.getId())}); @@ -411,6 +436,12 @@ public class PodDBAdapter { 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. @@ -491,6 +522,7 @@ public class PodDBAdapter { db.execSQL(CREATE_TABLE_FEED_IMAGES); db.execSQL(CREATE_TABLE_FEED_MEDIA); db.execSQL(CREATE_TABLE_DOWNLOAD_LOG); + db.execSQL(CREATE_TABLE_QUEUE); } @Override