From 07277b2fef6dcb1470ef18c55947b50ed710a7d5 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Wed, 14 Aug 2013 15:12:14 -0400 Subject: added a 'move to top' function to DBWriter that puts the provided item at the top of the queue. There may be a way to do this while re-using the existing move function. Perhaps a helper is in order. --- src/de/danoeh/antennapod/storage/DBWriter.java | 34 ++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src') diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index 5cdd6aee4..4b0dd9c2d 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -456,7 +456,41 @@ public class DBWriter { }); } + + /** + * Moves the specified item to the top of the queue. + * + * @param context A context that is used for opening a database connection. + * @param selectedItem The item to move to the top of the queue + * @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to + * false if the caller wants to avoid unexpected updates of the GUI. + * @throws IndexOutOfBoundsException if (to < 0 || to >= queue.size()) || (from < 0 || from >= queue.size()) + */ + public static Future moveQueueItemToTop(final Context context, final FeedItem selectedItem, final boolean broadcastUpdate) { + return dbExec.submit(new Runnable() { + + @Override + public void run() { + final PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + final List queue = DBReader + .getQueue(context, adapter); + if (queue != null) { + if (queue.remove(selectedItem)) { + // it was removed, put it on the front + queue.add(0, selectedItem); + } else { + Log.e(TAG, "moveQueueItemToTop: Could not move to top, no such item"); + } + } else { + Log.e(TAG, "moveQueueItemToTop: Could not move to top, no queue"); + } + adapter.close(); + } + }); + } + /** * Changes the position of a FeedItem in the queue. * -- cgit v1.2.3 From 2d83b39c27a32f51776dac14b032a72e0c3fbef8 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Fri, 16 Aug 2013 14:39:54 -0400 Subject: added move to top to individual items in the episode queue. Move to bottom coming soon. --- src/de/danoeh/antennapod/storage/DBWriter.java | 23 +++++++++++++++++----- .../util/menuhandler/FeedItemMenuHandler.java | 3 +++ 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index 9946783e9..c34f134ff 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -480,11 +480,24 @@ public class DBWriter { .getQueue(context, adapter); if (queue != null) { - if (queue.remove(selectedItem)) { - // it was removed, put it on the front - queue.add(0, selectedItem); - } else { - Log.e(TAG, "moveQueueItemToTop: Could not move to top, no such item"); + // it seems like there should be a better way to get + // the item we need, but iterating seems to be the + // only way + for (FeedItem item : queue) { + if (item.getId() == selectedItem.getId()) { + if (queue.remove(item)) { + queue.add(0, item); + Log.d(TAG, "moveQueueItemToTop: moved"); + adapter.setQueue(queue); + if (broadcastUpdate) { + EventDistributor.getInstance() + .sendQueueUpdateBroadcast(); + } + } else { + Log.e(TAG, "moveQueueItemToTop: Could not move to top, no such item"); + } + break; + } } } else { Log.e(TAG, "moveQueueItemToTop: Could not move to top, no queue"); diff --git a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java index aa029f672..4b99b47fb 100644 --- a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java +++ b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java @@ -150,6 +150,9 @@ public class FeedItemMenuHandler { DBTasks.playMedia(context, selectedItem.getMedia(), true, true, true); break; + case R.id.move_to_top_item: + DBWriter.moveQueueItemToTop(context, selectedItem, true); + break; case R.id.visit_website_item: Uri uri = Uri.parse(selectedItem.getLink()); context.startActivity(new Intent(Intent.ACTION_VIEW, uri)); -- cgit v1.2.3 From b886c38308a4e7ff9ab733e52b3703962d789fbf Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Fri, 16 Aug 2013 15:19:56 -0400 Subject: * updated code to reuse existing function for moving items in the queue * added 'move to bottom' --- src/de/danoeh/antennapod/storage/DBWriter.java | 67 ++++++++++------------ .../util/menuhandler/FeedItemMenuHandler.java | 5 +- 2 files changed, 35 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index c34f134ff..d879f3c83 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -467,44 +467,39 @@ public class DBWriter { * @param selectedItem The item to move to the top of the queue * @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to * false if the caller wants to avoid unexpected updates of the GUI. - * @throws IndexOutOfBoundsException if (to < 0 || to >= queue.size()) || (from < 0 || from >= queue.size()) */ - public static Future moveQueueItemToTop(final Context context, final FeedItem selectedItem, final boolean broadcastUpdate) { - return dbExec.submit(new Runnable() { - - @Override - public void run() { - final PodDBAdapter adapter = new PodDBAdapter(context); - adapter.open(); - final List queue = DBReader - .getQueue(context, adapter); - - if (queue != null) { - // it seems like there should be a better way to get - // the item we need, but iterating seems to be the - // only way - for (FeedItem item : queue) { - if (item.getId() == selectedItem.getId()) { - if (queue.remove(item)) { - queue.add(0, item); - Log.d(TAG, "moveQueueItemToTop: moved"); - adapter.setQueue(queue); - if (broadcastUpdate) { - EventDistributor.getInstance() - .sendQueueUpdateBroadcast(); - } - } else { - Log.e(TAG, "moveQueueItemToTop: Could not move to top, no such item"); - } - break; - } - } - } else { - Log.e(TAG, "moveQueueItemToTop: Could not move to top, no queue"); - } - adapter.close(); + public static Future moveQueueItemToTop(final Context context, final long itemId, final boolean broadcastUpdate) { + List queueIdList = DBReader.getQueueIDList(context); + int currentLocation = 0; + for (long id : queueIdList) { + if (id == itemId) { + return moveQueueItem(context, currentLocation, 0, true); } - }); + currentLocation++; + } + Log.e(TAG, "moveQueueItemToTop: item not found"); + return null; + } + + /** + * Moves the specified item to the bottom of the queue. + * + * @param context A context that is used for opening a database connection. + * @param selectedItem The item to move to the bottom of the queue + * @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to + * false if the caller wants to avoid unexpected updates of the GUI. + */ + public static Future moveQueueItemToBottom(final Context context, final long itemId, final boolean broadcastUpdate) { + List queueIdList = DBReader.getQueueIDList(context); + int currentLocation = 0; + for (long id : queueIdList) { + if (id == itemId) { + return moveQueueItem(context, currentLocation, queueIdList.size() - 1, true); + } + currentLocation++; + } + Log.e(TAG, "moveQueueItemToBottom: item not found"); + return null; } /** diff --git a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java index 4b99b47fb..0116dbf21 100644 --- a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java +++ b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java @@ -151,7 +151,10 @@ public class FeedItemMenuHandler { true); break; case R.id.move_to_top_item: - DBWriter.moveQueueItemToTop(context, selectedItem, true); + DBWriter.moveQueueItemToTop(context, selectedItem.getId(), true); + break; + case R.id.move_to_bottom_item: + DBWriter.moveQueueItemToBottom(context, selectedItem.getId(), true); break; case R.id.visit_website_item: Uri uri = Uri.parse(selectedItem.getLink()); -- cgit v1.2.3 From 4e845b83c650b0f103b564da221326b997e8d032 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Mon, 2 Sep 2013 16:45:57 -0400 Subject: Updated moveQueue functions to all use dbExec to start tasks * created a helper function (moveQueueItemHelper) that does all the moving * Updated moveQueueItemToTop and moveQueueItemToBottom to use the helper function while using the ExecutorService * Updated moveQueueItem to use the helper function as well. --- src/de/danoeh/antennapod/storage/DBWriter.java | 115 ++++++++++++++++--------- 1 file changed, 72 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index d879f3c83..cef3a3cc2 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -464,42 +464,54 @@ public class DBWriter { * Moves the specified item to the top of the queue. * * @param context A context that is used for opening a database connection. - * @param selectedItem The item to move to the top of the queue + * @param itemId The item to move to the top of the queue * @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to * false if the caller wants to avoid unexpected updates of the GUI. */ public static Future moveQueueItemToTop(final Context context, final long itemId, final boolean broadcastUpdate) { - List queueIdList = DBReader.getQueueIDList(context); - int currentLocation = 0; - for (long id : queueIdList) { - if (id == itemId) { - return moveQueueItem(context, currentLocation, 0, true); + return dbExec.submit(new Runnable() { + @Override + public void run() { + List queueIdList = DBReader.getQueueIDList(context); + int currentLocation = 0; + for (long id : queueIdList) { + if (id == itemId) { + moveQueueItemHelper(context, currentLocation, 0, broadcastUpdate); + return; + } + currentLocation++; + } + Log.e(TAG, "moveQueueItemToTop: item not found"); } - currentLocation++; - } - Log.e(TAG, "moveQueueItemToTop: item not found"); - return null; + }); } /** * Moves the specified item to the bottom of the queue. * * @param context A context that is used for opening a database connection. - * @param selectedItem The item to move to the bottom of the queue + * @param itemId The item to move to the bottom of the queue * @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to * false if the caller wants to avoid unexpected updates of the GUI. */ - public static Future moveQueueItemToBottom(final Context context, final long itemId, final boolean broadcastUpdate) { - List queueIdList = DBReader.getQueueIDList(context); - int currentLocation = 0; - for (long id : queueIdList) { - if (id == itemId) { - return moveQueueItem(context, currentLocation, queueIdList.size() - 1, true); + public static Future moveQueueItemToBottom(final Context context, final long itemId, + final boolean broadcastUpdate) { + return dbExec.submit(new Runnable() { + @Override + public void run() { + List queueIdList = DBReader.getQueueIDList(context); + int currentLocation = 0; + for (long id : queueIdList) { + if (id == itemId) { + moveQueueItemHelper(context, currentLocation, queueIdList.size() - 1, + broadcastUpdate); + return; + } + currentLocation++; + } + Log.e(TAG, "moveQueueItemToBottom: item not found"); } - currentLocation++; - } - Log.e(TAG, "moveQueueItemToBottom: item not found"); - return null; + }); } /** @@ -518,31 +530,48 @@ public class DBWriter { @Override public void run() { - final PodDBAdapter adapter = new PodDBAdapter(context); - adapter.open(); - final List queue = DBReader - .getQueue(context, adapter); - - if (queue != null) { - if (from >= 0 && from < queue.size() && to >= 0 - && to < queue.size()) { - - final FeedItem item = queue.remove(from); - queue.add(to, item); - - adapter.setQueue(queue); - if (broadcastUpdate) { - EventDistributor.getInstance() - .sendQueueUpdateBroadcast(); - } + moveQueueItem(context, from, to, broadcastUpdate); + } + }); + } - } - } else { - Log.e(TAG, "moveQueueItem: Could not load queue"); + /** + * Changes the position of a FeedItem in the queue. + * + * This function must be run using the ExecutorService (dbExec). + * + * @param context A context that is used for opening a database connection. + * @param from Source index. Must be in range 0..queue.size()-1. + * @param to Destination index. Must be in range 0..queue.size()-1. + * @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to + * false if the caller wants to avoid unexpected updates of the GUI. + * @throws IndexOutOfBoundsException if (to < 0 || to >= queue.size()) || (from < 0 || from >= queue.size()) + */ + private static void moveQueueItemHelper(final Context context, final int from, + final int to, final boolean broadcastUpdate) { + final PodDBAdapter adapter = new PodDBAdapter(context); + adapter.open(); + final List queue = DBReader + .getQueue(context, adapter); + + if (queue != null) { + if (from >= 0 && from < queue.size() && to >= 0 + && to < queue.size()) { + + final FeedItem item = queue.remove(from); + queue.add(to, item); + + adapter.setQueue(queue); + if (broadcastUpdate) { + EventDistributor.getInstance() + .sendQueueUpdateBroadcast(); } - adapter.close(); + } - }); + } else { + Log.e(TAG, "moveQueueItemHelper: Could not load queue"); + } + adapter.close(); } /** -- cgit v1.2.3 From e1fc800d088a4b150deb3aff072cda7af8da8ca7 Mon Sep 17 00:00:00 2001 From: Tom Hennen Date: Mon, 9 Sep 2013 21:29:53 -0400 Subject: Moved move to top/bottom menu options to EpisodesFragement from FeedItemMenuHandler so that we can ensure the user was actually in the 'Queue' dropdown when they opened the menu. Also, move to top now only shows up if the item isn't already at the top, similarly, for move to bottom, it only shows up if the item isn't already at the bottom. --- .../antennapod/fragment/EpisodesFragment.java | 38 +++++++++++++++++++++- .../util/menuhandler/FeedItemMenuHandler.java | 6 ---- 2 files changed, 37 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/de/danoeh/antennapod/fragment/EpisodesFragment.java b/src/de/danoeh/antennapod/fragment/EpisodesFragment.java index a99056c9a..6b228662f 100644 --- a/src/de/danoeh/antennapod/fragment/EpisodesFragment.java +++ b/src/de/danoeh/antennapod/fragment/EpisodesFragment.java @@ -222,6 +222,12 @@ public class EpisodesFragment extends Fragment { } }, selectedItem, false, QueueAccess.ItemListAccess(queue)); + // check to see if the item is in the queue, if so add queue menu items + int itemIndex = queue.indexOf(selectedItem); + if (itemIndex != -1) { + addQueueOnlyMenus(menu, itemIndex); + } + } else if (selectedGroupId == ExternalEpisodesListAdapter.GROUP_POS_QUEUE) { menu.add(Menu.NONE, R.id.organize_queue_item, Menu.NONE, R.string.organize_queue_label); @@ -237,6 +243,24 @@ public class EpisodesFragment extends Fragment { } } + /** + * Adds submenus to the ContextMenu if the item selected is in the queue. + * @param menu the ContextMenu to add the submenus to + * @param itemIndex the index of the selected item within the queue. + */ + private void addQueueOnlyMenus(ContextMenu menu, int itemIndex) { + if (itemIndex != 0) { + // don't add move to top if this item is already on the top + menu.add(Menu.NONE, R.id.move_to_top_item, Menu.NONE, getActivity() + .getString(R.string.move_to_top_label)); + } + if (itemIndex != queue.size() - 1) { + // don't add move to bottom if this item is already on the bottom + menu.add(Menu.NONE, R.id.move_to_bottom_item, Menu.NONE, getActivity() + .getString(R.string.move_to_bottom_label)); + } + } + @Override public boolean onContextItemSelected(android.view.MenuItem item) { boolean handled = false; @@ -249,7 +273,19 @@ public class EpisodesFragment extends Fragment { DownloadRequestErrorDialogCreator.newRequestErrorDialog( getActivity(), e.getMessage()); } - + if (!handled) { + // if it wasn't handled by the FeedItemMenuHandler it might be one of ours + switch (item.getItemId()) { + case R.id.move_to_top_item: + DBWriter.moveQueueItemToTop(getActivity(), selectedItem.getId(), true); + handled = true; + break; + case R.id.move_to_bottom_item: + DBWriter.moveQueueItemToBottom(getActivity(), selectedItem.getId(), true); + handled = true; + break; + } + } } else if (selectedGroupId == ExternalEpisodesListAdapter.GROUP_POS_QUEUE) { handled = true; switch (item.getItemId()) { diff --git a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java index aad240fc7..e99a733dc 100644 --- a/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java +++ b/src/de/danoeh/antennapod/util/menuhandler/FeedItemMenuHandler.java @@ -153,12 +153,6 @@ public class FeedItemMenuHandler { DBTasks.playMedia(context, selectedItem.getMedia(), true, true, true); break; - case R.id.move_to_top_item: - DBWriter.moveQueueItemToTop(context, selectedItem.getId(), true); - break; - case R.id.move_to_bottom_item: - DBWriter.moveQueueItemToBottom(context, selectedItem.getId(), true); - break; case R.id.visit_website_item: Uri uri = Uri.parse(selectedItem.getLink()); context.startActivity(new Intent(Intent.ACTION_VIEW, uri)); -- cgit v1.2.3 From a0b3c5f165134a6e99e4c6e4a065b3eee44ea9e9 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Thu, 12 Sep 2013 13:23:03 +0200 Subject: Call moveQueueItemHelper instead of moveQueueItem --- src/de/danoeh/antennapod/storage/DBWriter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index f4ba8c237..90983774a 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -537,7 +537,7 @@ public class DBWriter { @Override public void run() { - moveQueueItem(context, from, to, broadcastUpdate); + moveQueueItemHelper(context, from, to, broadcastUpdate); } }); } -- cgit v1.2.3