diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-09-12 13:23:29 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-09-12 13:23:29 +0200 |
commit | 455b6c717254e921c579a2aa6af8ab2ec0979786 (patch) | |
tree | f4aede446fa20c5ed0acd22be2dc58495e4e6bc2 /src/de/danoeh/antennapod/storage/DBWriter.java | |
parent | 4c1d210274f66bd4969d467233ad8e69b55b8642 (diff) | |
parent | a0b3c5f165134a6e99e4c6e4a065b3eee44ea9e9 (diff) | |
download | AntennaPod-455b6c717254e921c579a2aa6af8ab2ec0979786.zip |
Merge branch 'TomHennen-move-to-top' into develop
Diffstat (limited to 'src/de/danoeh/antennapod/storage/DBWriter.java')
-rw-r--r-- | src/de/danoeh/antennapod/storage/DBWriter.java | 117 |
1 files changed, 94 insertions, 23 deletions
diff --git a/src/de/danoeh/antennapod/storage/DBWriter.java b/src/de/danoeh/antennapod/storage/DBWriter.java index b5cab8b5a..9eb0ab643 100644 --- a/src/de/danoeh/antennapod/storage/DBWriter.java +++ b/src/de/danoeh/antennapod/storage/DBWriter.java @@ -466,7 +466,61 @@ 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 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) { + return dbExec.submit(new Runnable() { + @Override + public void run() { + List<Long> 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"); + } + }); + } + + /** + * Moves the specified item to the bottom of the queue. + * + * @param context A context that is used for opening a database connection. + * @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) { + return dbExec.submit(new Runnable() { + @Override + public void run() { + List<Long> 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"); + } + }); + } + /** * Changes the position of a FeedItem in the queue. * @@ -483,31 +537,48 @@ public class DBWriter { @Override public void run() { - final PodDBAdapter adapter = new PodDBAdapter(context); - adapter.open(); - final List<FeedItem> 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(); - } + moveQueueItemHelper(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<FeedItem> 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(); } /** |